• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • Synchronization
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
      • Overview
      • Execution Environments
      • Security, Resource Management, and Formatting Options
      • Base Actions
      • Application Namespace
      • Clipboard Namespace
      • Data Namespace
        • Data Action Invoker Namespace
        • Data.close Action
        • Data.contains Action
        • Data.countCases Action
        • Data.deleteCase Action
        • Data.getCurrentCase Action
        • Data.open Action
        • Data.query
        • Data.queryCases Action
        • Data.queryKeys Action
        • Data.readCase Action
        • Data.writeCase Action
      • Dictionary Namespace
      • File Namespace
      • Hash Namespace
      • Localhost Namespace
      • Logic Namespace
      • Message Namespace
      • Network Namespace
      • Path Namespace
      • Settings Namespace
      • Sqlite Namespace
      • Sync Namespace
      • System Namespace
      • UI Namespace
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Data.open Action

Format
d = CS.Data.open(connection := ... ‖ name := ...
             
ʃ, dictionary := ...ʅ
             
ʃ, openFlags := ...ʅ)
ArgumentDescriptionTypes / Required
connectionThe data connection string specifying the data source.string, object
not required
nameThe name of a dictionary associated with an application.string
not required
dictionaryThe dictionary that describes the data source's cases.string, object
not required
openFlagsFlags to control how the data source is opened.
The default value is "read".
string
not required
Description
The Data.open action opens or creates a data source and returns a resource ID that can be used for future operations. The connection to the data source remains active until it is closed using Data.close.
Either connection or name must be specified, with connection used for data sources that are not already connected to an application.
This action is similar to the CSPro logic function setfile. When using actions, multiple data sources associated with the same dictionary can be opened, whereas using CSPro logic, there is only one dictionary-based data source per call to setfile.
Application Data Sources
If specifying name, the action connects to a data source that is connected to a dictionary that is part of an application. The access permissions and dictionary used by the application to open the data source are inherited, so the openFlags and dictionary arguments are ignored. For example, this code would create a connection to the data source associated with the dictionary named CORRUPTION_DICT:
CS.Data.open(name := "CORRUPTION_DICT");
The data source associated with the dictionary is evaluated every time the data resource ID is used, so if the application's data source changes, the resource ID will always refer to the current data source identified by the dictionary name.
If working with application dictionaries, you can "open" them as detailed, or you can simply use the dictionary name as the resource ID for other actions. For example, these two approaches are equivalent:
// check if a case exists by first opening the data source
numeric dataId = tonumber(CS.Data.open(name := "RENARD_DICT"));

if tonumber(CS.Data.contains(dataId := dataId, key := "260519")) then

// check if a case exists without explicitly opening the data source
if tonumber(CS.Data.contains(dataId := "RENARD_DICT", key := "260519")) then
Non-Application Data Sources
If specifying connection, the action will open a data source that is not already open as part of an application's dictionaries. The argument connection is a data connection string, generally a path identifying a data file. Connection strings can be specified as a single string or as an object (with the rules for each described here). Details about properties specific to each data source are available on the documentation describing each of the data sources.
The openFlags argument controls how the data source is opened and can be one of the following:
  • "read": The data source is opened in read-only mode, with an error occurring if the data source does not exist. Actions such as Data.writeCase will fail in this mode.
  • "readWrite": The data source is opened in read and write mode, with an error occurring if the data source does not exist.
  • "readWriteCreate": The data source is opened in read and write mode, with a new data source created if it does not already exist.
  • "readWriteTruncate": The data source is opened in read and write mode, with all cases deleted from the data source if it already exists. If the data source does not exist, it is created. Deleting cases cannot be undone so be careful using this mode.
When opening an existing data source that has an embedded dictionary (e.g., a CSPro DB data source), it is not necessary to specify the dictionary that describes the data source's cases, as the embedded dictionary will be used when processing cases. However, if creating a new data source, or opening a data source without an embedded dictionary (e.g., a Text data source), you must specify the dictionary. The argument dictionary is determined as follows:
  1. If the connection string contains a "dictionaryPath" property, the dictionary will be read from the disk, evaluated relative to the path of the data source.
  2. If dictionary is an object, it is parsed as the JSON representation of a dictionary.
  3. If dictionary is a string that matches the name of an application dictionary, that dictionary is used.
  4. Otherwise, dictionary is parsed as a string with a file path identifying a dictionary on the disk.
Return Value
The action returns a numeric resource ID that is used to identify this data source in calls to future Data actions.
Exceptions
The action throws an exception if any of its arguments are not specified in a valid form, or if:
  • The data source does not exist when opening in a mode that requires an existing data source.
  • The data source does not have an embedded dictionary and a dictionary is not specified.
  • There is an error opening, creating, or otherwise connecting to the data source.
Example (JavaScript): Data Actions
let dataId;

try {
   
// open the Popstan Census example data
    dataId = CS.Data.open({
        connection: 
"Popstan Census.csdb"
    });

   
// display the number of cases
    const numCases = CS.Data.countCases({
        dataId: dataId
    });

    print(`Popstan Census contains ${numCases} cases.`);
}
catch (error) {
    print(
"Error interacting with data source: " + error);
}
finally {
   
// close the data source
    if( dataId != undefined ) {
        CS.Data.close({
            dataId: dataId
        });
    }
}
Example (CSPro Logic): Specifying Dictionaries
In an application with a dictionary CORRUPTION_DICT, statements like this might result in a similar result:
// access the data source currently associated with the application's CORRUPTION_DICT dictionary
CS.Data.open(name := "CORRUPTION_DICT");

// open a text data source using the application's CORRUPTION_DICT dictionary
CS.Data.open(connection := "data/justice.dat", dictionary := "CORRUPTION_DICT");

// open a text data source specifying the file path of the dictionary
CS.Data.open(connection := "data/justice.dat", dictionary := "dicts/corruption.dcf");

// open a text data source specifying the file path of the dictionary in the connection string
CS.Data.open(connection := "data/justice.dat|dictionaryPath=../dicts/corruption.dcf");

// open a text data source specifying the dictionary contents in JSON format
string dcfJson = CS.File.readText(path := "dicts/corruption.dcf");
CS.Data.open(connection := "data/justice.csdb", dictionary := @object dcfJson);
See also: Data Action Invoker Namespace, Data.close Action, SetFile Function