...
To configure DDBAPI, you need to define the database connection in the BL_Config.xml file using the FIXEdge\DatabaseConnection element:
|
language | xml |
---|
|
|
|
|
|
|
The configuration for DDBAPI is found in the FIXEdge\DatabaseConnection element of your BL_Config.xml file. Here's a breakdown of the configuration options:
Name: This case-sensitive identifier is used to reference the connection throughout your FIXEdge configuration.
StorageType: This case-insensitive option specifies the type of database connection you are using. Currently, only ODBC is supported.
ConnectionString: This string contains the information needed to connect to your database. The format will vary depending on your specific DBMS.
MinPoolSize: This defines the minimum number of connections that will be available in the pool at FIXEdge system startup. Default: 1
MaxPoolSize: This sets the maximum number of connections that FIXEdge can create in the pool. Default: 16
...
JavaScript Binding: executeSQL
...
The executeSQL function is a convenient tool within FIXEdge with a JS binding feature that allows users to transfer execution control and parameters from Javascript to transfers control from the JS environment to the C++ runtime . It works as a bridge between the two environments, ensuring seamless and efficient operation.
Here's how you can use executeSQL:
The executeSQL function should be called as followsto execute SQL queries.
Example:
|
...
...
|
...
|
Parameters:
connection: This is a required string parameter where you specify the connection name that matches the 'DatabaseConnection.Name' in BL_Config.xml.
query: This is a required string parameter where you write the SQL statement that you want to be executed on the server-side.
params: This is a required array of integers, doubles, or strings that represent the query parameters to be sent to the server.
result: This is a required JS Object that you have to initialize as follows:
...
language | js |
---|
...
(string, required): Name of the database connection, must match DatabaseConnection.Name.
query (string, required): SQL statement to execute.
params (array, required): Query parameters to pass to the server (int, double, or string).
extraParams (object, optional): Additional settings to control the query's result and behavior.
result (JS object): Returned object with query results.
Query Result Objects
Compatibility Query Results
For single row set results with multiple data modification results, DDBAPI generates query result objects in compatibility mode. Here are some examples:
Example 1: Selecting a single value
|
...
|
...
|
affectedRowCount: Number of affected rows (used for modification queries).
data: Array of result rows; each row is an array of the selected data.
Example 2: Inserting multiple rows
|
...
|
...
The affectedRowCount field contains the number of rows affected if the query modifies any data. The data field consists of the rows of selected data (each row is an array of the selected columns). If no data is selected, it returns an empty array.
...
|
Example 3: Deleting rows and selecting a value
|
MARS Query Result Mode
For queries that return multiple row sets with data modifications (Multiple Active Result Sets - MARS), the result is structured as an array of result objects:
Example 1: Multiple select statements
|
Example 2: Mixed DML and select statements
|
Extra Query Parameters
The extraParams parameter accepts an object with optional properties to customize the query behavior and result format:
associativeResults (boolean)
If set to true, this parameter includes column names in the result objects. If a column name is unavailable, the column index is used instead.
Example:
|
When multiple columns are queried:
|
includeResultsMetadata (boolean)
Setting this parameter to true includes type information for each column in the result object.
Example:
|
When both associativeResults and includeResultsMetadata are enabled, the metadata includes column names as well:
|
timing (boolean)
Setting this parameter to true adds a "time" property to the result object. The value represents the query execution time in the format "$(number_of_hours)h.$(number_of_minutes)m.$(number_of_second)s.$(second_fraction)".
Example:
|
The measured time includes parameter preparation, transfer, server-side processing, receiving, and parsing of the results.
useStrictDecimal (boolean)
When set to true, this parameter enforces strict type checking for fixed-point numbers (decimal, numeric, money). Any attempt to use a JS double variable to set a fixed-point value or read a fixed-point value into a JS double variable will result in an error.
resultTemplate (array /of arrays/ of strings)
The resultTemplate parameter in the executeSQL function allows precise control over how the results of a query are cast into JavaScript types. This parameter allows you to define a template for type conversion of the query results.
The resultTemplate allows multiple configurations:
Single Array of Strings: Applies conversion directly to the query result in sequence.
Array of Arrays of Strings: Useful for Multi-Active Result Sets (MARS). If the MARS count is less than the number of arrays, the last array in resultTemplate is applied to remaining results.
Example:
|
|
resultTemplate supports the following type literals, case insensitive, as provided by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values :
resultTemplate Value | Target JS Type |
---|---|
BigInt | BigInt with int64_t backend |
Number | Number with double backend |
String | String with std::string backend |
Null, Undefined | Read as is, no additional conversion applied |
Basic Use Cases
To use DDBAPI, your BL_Config.xml file should define a JavaScript execution action in one of its rules, like so:
|
|
|
|
|
Receiving Data from the Database
To fetch data from the database, you prepare a query and execute it. Here's an example of how to execute a DBMS query and print the result:
|
|
|
|
|
Query result:
|
|
|
|
|
This code snippet demonstrates the following:
...
DDBAPI supports multiple active result sets, allowing you to efficiently read complex query results.
Sending Data to the Database
To store data in the database, you can employ the DBMS query language as follows:
|
|
|
|
|
Query result:
|
This code snippet demonstrates the following:
The query string now includes placeholders (?) for the values you want to insert.
The third argument to executeSQL is an array containing the values to be inserted.
The result variable will contain information about the execution, including the number of rows affected.
DDBAPI automatically binds the values you provide to the corresponding placeholders in the prepared query.
Calling Stored Procedure in the Database
To invoke stored procedure in the database, you can employ the DBMS query language as follows:
Code Block | ||
---|---|---|
| ||
var result = executeSQL(
"Odbc1",
"EXEC sp_MyProcedure @pParam1=?, @pParam2=?, @pParam3=?;",
[1, 2.2, '3']
);
print(JSON.stringify(result)); |
This code snippet demonstrates the following:
The query string uses ODBC
EXEC
syntax to invoke stored procedures with SQL@namedParams
bound to placeholders (?) for the values you want to insert.The third argument to executeSQL is an array containing the values to be inserted.
The result variable will contain information about the execution, including the number of rows affected.
...
When sending data from JavaScript to SQL and vice versa, the following type mappings are applied:
SQL type | JS → SQL (parameter) | SQL → JS (result) | Comment |
---|---|---|---|
tinyint, smallint, int, bigint | String: deserialize, Int: 1-to-1, Double: convert |
Int: 1-to-1 Others - convert as is | ||
bit | String: "true", "false" are converted, all others - failure Int: 0, 1 are converted, all others - failure Double: 0 - false, >0 - true, all other - failure | Always converted to: Others: convert to |
"0"/"1", 0.0/1.0 | |||
decimal, numeric, money, smallmoney | String: ODBC-side conversion. Double: fail if extraParams.useStrictDecimal | String: "123.456" | |
float,real | String: deserialize, Int: convert, Double: 1-to-1. | Double: 1-to-1. | |
date, time, datetime2, datetimeoffset, datetime smalldatetime | String: |
Int: convert,
Double: 1-to-1.try converting from FIX date, if succeeds fill the ODBC:DATE_TIME struct; if fails pass by to ODBC as string. Int: Try to interpret as JSDateEntity, if failed treat as unix time. Pass to ODBC:DATE_TIME struct in both cases. Double: Convert to unix time and pass to ODBC:DATE_TIME struct. | If no conversion requested, returns and int, representing a JSDateEntity. String: Convert to FIX date format Int: Convert to Unix timestamp. Double: Convert to Unix timestamp. | Several useful JS bindings for manipulating JSDateEntity objects are: dateToString, getDateField, createDate, getCurrentDate, (get | set | change)(Year | Month | ... | Sec|MSec|NSec) | |
char, varchar, nchar, nvarchar | String: 1-to-1, Int: serialize double: serialize | String: 1-to-1 Others - convert as is | |
text, ntext | Not supported as these types are obsolete/deprecated. |
Int: deserialize
double: deserializebinary, varbinary, image | String: passed as is, Int: passed as binary uint64_t* buffer, Double: passed as binary double* buffer. | String: example: 0xdeadbeef -> "DEADBEEF" Others - convert as is | |
cursor, geography, geometry, hierarchyid, json, rowversion, sql_variant, table, uniqueidentifier, xml | All: passed as is | All: read as is | |
Arrays of values | Not implemented |