The builtin-in functions in the SQL package perform connection and transaction operations, cursor allocation and management, and SQL statement operations with generic SQL databases.
The major functionalities offered by the SQL Package are the following:
Since much of the functionality of the SQL package is interdependent, a single code sample showing correct usage and sequence of fundamental package calls is provided below. In the example below, and arbitrary error handling routine from the DBWizAPI OSpace is being used. Error handling will, of course, vary depending on implementation and usage.
Example:
// Retrieve values from DTree using a "fetching" cursor, and insert values into DTree // using a second "inserting" cursor. Integer status List colData string ok = undefined SqlCursor fetchCursor = undefined SqlCursor insertCursor = undefined Boolean done = FALSE SqlConnection sqlCnct = SQL.AllocFromConnect( $Connect ) // Start a transaction status = SQL.StartTransaction( sqlCnct ) //Declare the fetching cursor fetchCursor = SQL.Declare(\ sqlCnct,\ "Select OwnerID,DataID,DComment from " +\ "DTree " +\ "where " +\ "( DataType = 0 ) and ( SubType in (130,134) )" ) ok = $DBWizAPI.ConnectPkg.Check( fetchCursor, "Error Declaring FetchCursor] ) if IsDefined( ok ) fetchCursor = undefined end if IsUndefined( ok ) // Open the fetching cursor status = SQL.Open( fetchCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Opening Fetch Cursor" ) end if IsUndefined( ok ) // Prepare the fetching cursor status = SQL.Prepare( fetchCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Preparing Fetch Cursor" ) end if IsUndefined( ok ) // Execute the fetching cursor status = SQL.Execute( fetchCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Executing Fetch Cursor" ) end if IsUndefined( ok ) // Declare the insert cursor (note the value to be bound at a future point) insertCursor = SQL.Declare( sqlCnct, "Insert into DiscussionText values ( :A1, :A2, :A3 )" ) ok = $DBWizAPI.ConnectPkg.Check( insertCursor, "Error Declaring Insert Cursor" ) if IsDefined( ok ) insertCursor = undefined end end if IsUndefined( ok ) // Open the insert cursor status = SQL.Open( insertCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Opening Insert Cursor" ) end if IsUndefined( ok ) // Prepare the insert cursor status = SQL.Prepare( insertCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Preparing Insert Cursor" ) end while ok && !done // Fetch results using the fetch cursor status = SQL.Fetch( fetchCursor ) if IsError( status ) done = TRUE else // Retrieve column data from the fetching cursor colData = SQL.Cols( fetchCursor ) if IsDefined( coldata[ 3 ] ) // Bind the values retrieved by the fetching cursor into the // inserting cursor status = SQL.Bind( insertCursor, colData ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Binding Values"] ) if IsUndefined( ok ) // Execute the inserting cursor (whose statement has been bound with values from the // fetching cursor status = SQL.Execute( insertCursor ) ok = $DBWizAPI.ConnectPkg.Check( status, "Error Executing Insert Cursor" ) end end end end // Close the cursors if IsDefined( fetchCursor ) SQL.Close( fetchCursor ) end if IsDefined( insertCursor ) SQL.Close( insertCursor ) end // End the transaction status = SQL.EndTransaction( sqlCnct, !IsDefined( ok ) )
Integer AUTOCOMMIT
A commit flag for ODBC.
CACHEInteger CACHE
This attribute directs SQL.Declare() to cache the cursor, allowing for rows to be fetched from the database more efficiently.
CACHEDYNAMICInteger CACHEDYNAMIC
Not used.
COMMITInteger COMMIT
A constant passed to Sql.EndTransaction() indicating a transaction should be committed.
MULTIEXECInteger MULTIEXEC
Not used.
NOCACHEInteger NOCACHE
Directs SQL.Declare() to not cache the cursor.
NOLOGInteger NOLOG
Directs SQL.Declare() to turn off statement logging within the cursor.
NOTRANSWARNInteger NOTRANSWARN
Directs SQL.Declare() to turn off warnings if the cursor is being used outside the context of a transaction.
ROLLBACKError ROLLBACK
An error passed to Sql.EndTransaction() indicating a transaction should be rolled back. SQL.Fetch() also returns SQL.ROLLBACK to indicated no further data can be fetched.
SLOWMODEInteger SLOWMODE
Not used.
AllocSqlConnection Alloc( Integer conType, CAPILOG logObj, CAPIERR errObj, Integer num )
Allocates an SqlConnection object. This data structure is used to hold information about the database connection.
conType | - |
An Integer specifying the connection type. CAPI.CT_ORACLE indicates Oracle and CAPI.CT_MSDBLIB indicates MSSQL Server. |
logObj | - | A CAPILog object for logging transactions. |
errObj | - | A CAPIErr object for logging errors. |
num | - | This parameter must be zero. |
Remember that Alloc() merely allocates the connection, and that Connect() must be called afterwards to actually connect to the database.
SqlConnection AllocFromConnect( CAPICONNECT connect )
Allocates an SqlConnection object from a CAPICONNECT. This SqlConnection is used to hold information about the database connection and is required for most SQL functions in this package.
connect | - | A CAPIConnect, used to allocate an SqlConnection. |
Integer Bind( SqlCursor cursor, List values )
Binds a list of Strings, Integers, and/or Dates to parameters in the SQL statement contained in the cursor. This function must be called before the execution and fetching of the cursor, but after SQL.Prepare() is called. If there are no parameters in the SQL statement, pass an empty list.
cursor | - | The SqlCursor to be bound. |
values | - | A list of values to bind to the parameters in the SQL statement within the cursor, or an empty list. These values can be Strings, Integers, or Dates. |
Integer Close( SqlCursor cursor )
Closes the specified SqlCursor after processing is complete.
cursor | - | The SqlCursor to be closed. |
List ColNames( SqlCursor cursor )
Returns a List of column names referenced by the SQL statement within the cursor.
cursor | - | The SqlCursor from which column information will be extracted. |
List Cols( SqlCursor cursor )
Returns a List of the data from the last cursor fetch.
cursor | - | An SqlCursor. |
Integer Connect( SqlConnection connect, String srvName, String dbName, String usrName, String pword, String applStr, String hostStr )
Connects an allocated SqlConnection to the database.
connect | - | The SqlConnection to be connected. |
srvName | - | The String name of the server to which the connection is made. |
dbName | - | The String name of the database to which the connection is made. For an Oracle databases, a NULL string must be used. |
usrName | - | The String name of the person user being connected. |
pword | - | The password of the user being connected. |
SqlCursor Declare( SqlConnection connect, String stmt, [Integer flags] )
Declares an SqlCursor for later use. The statement may contain parameters for binding in the form of ":A1", ":A2", etc. Repeated values must be expressed as separate parameters, so that ":A1" cannot be reused later in the SQL statement even if the bound value is identical in both instances.
connect | - | An SqlConnection. |
stmt | - | An SQL statement. |
flags | - | Flag(s) to indicate how the statement and cursor will be handled. SQL.CACHE, SQL.NOCACHE, SQL.NOLOG, and SQL.NOTRANSWARN are all valid flags (See Attributes section above for more information on these flags) . |
Integer Disconnect( SqlConnection connect )
Drops or disconnects a database connection.
connect | - | An SqlConnection to be dropped. |
Integer EndTransaction( SqlConnection connect, Dynamic value )
Ends a database transaction, allowing commitment or rollback of the transaction. See also SQL.EndTransaction().
RecArray Exec( SqlConnection connect, String stmt, [Dynamic sub<n>], [Dynamic sub<n>] )
Executes an SQL statement.
connect | - | An SqlConnection. |
stmt | - | An SQL statement. Parameterized values can be included in this statement, with the parameter syntax consisting of a colon followed by alphanumeric characters (":A1", ":A2", etc.). Up to 30 parameters can be passed. |
sub<n> | - | the values of the optional parameters
included in the SQL statement. Either all the parameters can be
passed in a List for this argument, or they may be passed
as arguments, since this is a variable-argument function (like
Str.Format()). Each sub<n> parameter specifies a value to be substituted in the SQL statement for the respective parameter. Values are substituted from left to right, regardless of variable name or parameter characters. |
If a parameterized statement is executed multiple times for various data, ExecN() should be used instead since it is optimized for such cases.
RecArray ExecN( SqlConnection connect, String stmt, RecArray params )
Executes a parameterized SQL statement multiple times; once for each row in parms, a RecArray of parameter values. ExecN() may be optimized in some databases to perform better than calling Exec() for each iteration.
connect | - | An SqlConnection. |
stmt | - | An SQL statement. Parameterized values can be included in this statement, with the parameter syntax consisting of a colon followed by alphanumeric characters (":A1", ":A2", etc.). Up to 30 parameters can be passed. |
params | - | A RecArray of parameters. The parameterized SQL statement is executed using parameters from each row in the RecArray, in left-to-right column order in a given row. |
Integer Execute( SqlCursor cursor )
Executes an SqlCursor. This function differs from SQL.Fetch() in that it is performed once, after the declaration, preparation, and binding of the cursor. All the data that will be obtained from the database by the SQL statement contained in the cursor is retrieved during SQL.Execute(). See also SQL.Prepare(), SQL.Bind(), and SQL.Fetch().
cursor | - | An SqlCursor to execute. |
Integer Fetch( SqlCursor cursor )
Fetches a row of data based on the specified SqlCursor. After an SQL.Execute() has been performed on a cursor, SQL.Fetch() is used to retrieve the data incrementally. In the case of a cached cursor, rows of data are retrieved in blocks for greater efficiency. See also SQL.Prepare(), SQL.Bind(), and SQL.Execute().
cursor | - | An SqlCursor upon which the fetch is to be performed. |
Integer GetBlob( SqlConnection arg1, String arg2, Integer arg3, Integer arg4 )
Internal use only.
connect |
- | An SqlConnection. |
arg2 | - | Internal use only |
arg3 | - | Internal use only |
arg4 | - | Internal use only |
Integer IsSelect( SqlCursor cursor )
Indicates whether the SQL statement within an SqlCursor is a SELECT statement. The SQL statement must have been parsed with SQL.Prepare() before this method is called.
cursor | - | The SqlCursor in question. |
Integer NCols( SqlCursor cursor )
Determines the number of columns that the SQL statement in the cursor will return.
cursor | - | The SqlCursor in question. |
Date Now( SqlConnection connect )
Returns the current date and time as defined by the database server.
connect | - | An SqlConnection. |
Integer Open( SqlCursor cursor )
Opens the SqlCursor after it has been declared.
cursor | - | The SqlCursor to be opened. |
Integer Prepare( SqlCursor cursor )
Triggers the server to parse the SQL statement within the cursor. The SqlCursor must be parsed before most other methods can be performed with it.
cursor | - | The SqlCursor to be parsed/prepared. |
Dynamic PutBlob( SqlConnection connect, String arg2, Integer arg3, Integer arg4 )
Internal use only.
connect | - | The SqlConnection. |
arg2 | - | Internal use only. |
arg3 | - | Internal use only. |
arg4 | - | Internal use only. |
Integer RowCount( SqlCursor cursor )
Determines the number of rows affected by the execution of an SQL statement defined within an SqlCursor. SQL.Prepare() must have been called before this method can be used.
cursor | - | The SqlCursor whose affected rows are to be counted. |
Integer StartTransaction( SqlConnection connect )
Starts a database transaction, allowing multiple SQL statements to be grouped together. Should an error occur during the execution of any statement after the beginning of the transaction and before the SQL.EndTransaction(), all of the intervening statements can be rolled back or canceled.
connect | - | An SqlConnection. |