You should also read the documentation for DBI as this document qualifies what is stated there. Note that this document was last updated for the DBI 0.85 specification, but the code requires features from the DBI 0.89 release.

You should also read the Sqlflex.License file which is distributed with DBD::Sqlflex for information about Infoflex Inc. software.

Under normal circumstances, you should then connect to your database using the notation in the section "CONNECTING TO A DATABASE" which calls DBI->connect(). Note that some of the DBD::Sqlflex test code does not operate under normal circumstances, and therefore uses the non-preferred techniques in the section "Driver Attributes and Methods".

If you have a burning desire to do so, you can explicitly install the Sqlflex driver independently of connecting to any database using:

$drh = DBI->install_driver('Sqlflex');

This gives you a reference to the driver, aka the driver handle. If the load fails, your program stops immediately (unless, perhaps, you eval the statement).

Once you have the driver handle, you can interrogate the driver for some basic information:

print "Driver Information\n";
# Type is always 'dr'.
print " Type: $drh->{Type}\n";
# Name is always 'Sqlflex'.
print " Name: $drh->{Name}\n";
# Version is the version of DBD::Sqlflex (eg 0.50).
print " Version: $drh->{Version}\n";
# The Attribution identifies the culprits who provided you
# with this software.
print " Attribution: $drh->{Attribution}\n";
# ProductName is the version of ESQL/C; it corresponds to
# the first line of the output from "esql -V".
print " Product: $drh->{ix_ProductName}\n";
# ProductVersion is an integer version number such as 721
# for ESQL/C version 7.21.UC1.
print " Product Version: $drh->{ix_ProductVersion}\n";
# MultipleConnections indicates whether the driver
# supports multiple connections (1) or not (0).
print " Multiple Connections: $drh->{ix_MultipleConnections}\n";
# ActiveConnections identifies the number of open connections.
print " Active Connections: $drh->{ix_ActiveConnections}\n";
# CurrentConnection identifies the current connection.
print " Current Connections: $drh->{ix_CurrentConnection}\n";

Once you have the driver loaded, you can connect to a database, or you can sever all connections to databases with disconnect_all.

$drh->disconnect_all;

You can find out which databases are available using the function:

@dbnames = DBI->data_sources('Sqlflex');

Note that you may be able to connect to still other databases using other notations (eg, you can probably connect to "dbase@server" if "server" appears in the sqlhosts file and the database "dbase" exists on the server and the server is up and you have permission to use the server and the database on the server and so on). Also, you may not be able to connect to every one of the databases listed if you have not been given connect permission on the database. However, the list provided by the DBI->data_sources method certainly exist and it is legitimate to try connecting to them.

NB: this section is more subject to change than most. Read the DBI specification for information about "dbi:Sqlflex:" as the initial portion of the connect string.

To connect to a database, you can use the connect function, which yields a valid reference or database handle if it is successful. If the driver itself cannot be loaded (by the DBI->install_driver() method mentioned above), DBI aborts the script (and DBD::Sqlflex can do nothing about it because it wasn't loaded successfully).

The 'new style' connections are distinguished by the presence of the 'dbi:Sqlflex:' portion at the front of the first argument. The string following the second colon is all that is passed to the DBD::Sqlflex code. With this format, you do not have to specify the username or password. Note that if you specify the username but not the password, the username will be ignored. You can also specify certain attributes in the connect call. These include:

AutoCommit
PrintError
RaiseError

Note that you cannot specify ChopBlanks in this list. For the time being, the PrintError attribute is synonymous with the ix_AutoErrorReport (see below), except that ix_AutoErrorReport is not recognised in the connect call. However, ix_AutoErrorReport is now deprecated and you should upgrade any code which uses it to use PrintError instead (because PrintError is a DBI standard name and ix_AutoErrorReport is not). Using the new style connect, you could therefore specify that the database is not to operate in AutoCommit mode but errors should be reported automatically by specifying:

Using this style of connection, the default value for AutoCommit is On (or 1); this is a contrast to the old style where the default is Off (or 0). Also note that starting with the DBD::Sqlflex 0.50 release, the behaviour is not affected by the type of Sqlflex database to which you are connecting, except that you may get a warning if you try to set AutoCommit Off when you connect to an UnLogged database. See also the extensive notes in the TRANSACTION MANAGEMENT section later in this document.

Note that there are problems with the new style connection syntax is the connection fails; DBI does not give the correct error indications. This means that the tests do not, in general, exploit the new syntax.

The older style of connection does not use the string "dbi:Sqlflex:" at the start of the first argument (or uses a string as the fourth argument).

$dbh = DBI->connect($database, $username, $password, 'Sqlflex');

Note that if you omit the fourth argument ('Sqlflex'), then DBI will load the driver specified by $ENV{DBI_DRIVER}. If you omit the fourth argument, you can also omit the $password and $username arguments if desired. If you specify the fourth argument, you can leave the $password and $username arguments empty and they will be ignored.

The 5.0x versions ignore the username and password data, and the statement is equivalent to "EXEC SQL DATABASE :database;". The 6.0x versions only use the username and password if both are supplied, but it is then equivalent to:

The database name is not supplied implicitly by DBD::Sqlflex, but the DBI driver will supply the value in $ENV{DBI_DBNAME} if the environment variable is set and no database name is supplied in the connect call. If DBD::Sqlflex sees an empty string, then it makes no connection to any database with ESQL/C 5.0x, and it makes a default connection with ESQL/C 6.00 and later. There is an additional string, ".DEFAULT.", which can be specified explicitly as the database name and which will be interpreted as a request for a default connection. Note that this is not a valid Sqlflex database name, so there can be no confusion.

Once you have a database handle, you can interrogate it for some basic information about the database, etc.

print "Database Information\n";
# Type is always 'db'
print " Type: $dbh->{Type}\n";
# Name is the name of the database specified at connect
print " Database Name: $dbh->{Name}\n";
# AutoCommit is 1 (true) if the database commits each statement.
print " AutoCommit: $dbh->{AutoCommit}\n";
# ix_LoggedDatabase is 1 (true) if the database has
# transactions.
print " Logged Database: $dbh->{ix_LoggedDatabase}\n";
# ix_ModeAnsiDatabase is 1 (true) if the database is MODE ANSI.
print " Mode ANSI Database: $dbh->{ix_ModeAnsiDatabase}\n";
# ix_AutoErrorReport is 1 (true) if errors are reported as they
# are detected. This is now deprecated -- use $dbh->{PrintError}.
print " AutoErrorReport: $dbh->{PrintError}\n";
# ix_InTransaction is 1 (true) if the database is in a transaction
print " Transaction Active: $dbh->{ix_InTransaction}\n";

If $dbh->{PrintError} is true, then DBI will report each error automatically on STDERR when it is detected. The error is also available via the package variables $DBI::errstr and $DBI::err. Note that $DBI::errstr includes the SQL error number and the ISAM error number if there is one, and ends with a newline. The message may or may not extend over several lines, and is generally formatted so that it will display neatly within 80 columns. The last character of the message is a newline.

If $dbh->{PrintError} is false, then DBI does not report any errors when it detects them; it is up to the user to note that errors have occurred and to report them.

If you connect using the DBI->connect() method, or if you have forgotten the driver, you can discover it again using:

$drh = $dbh->{Driver};

This allows you to access the driver methods and attributes described previously.

BUG: The name of the database should be tracked more carefully via the DATABASE, CLOSE DATABASE, CREATE DATABASE, ROLLFORWARD DATABASE and START DATABASE statements. Note that you cannot prepare CONNECT statements, so they do not have to be tracked.

The lists of tables are all qualified as 'owner'.tablename, and may be used in SQL statements without fear that the table is not present in the database (unless someone deletes it behind your back). The leading arguments qualify the list of names returned. Private synonyms are only reported for the current user.

The lists are each references to an array of values corresponding to the owner name, table name, the column number, the column name, the basic data type (ix_ColType value - see below) and data length (ix_ColLength - see below). If no tables are listed, then all columns in the database are listed. This can be quite slow because handling synonyms properly requires a UNION operation. Further, although the '_tables' method report the names of remote synonyms, the '_columns' method does not expand them (mainly because it is very hard t do it properly). See the examples in t/metadata.t for how these can be used. Exercise for the reader: extend '_columns' so that it reports on the columns in remote synonyms, including relocated remote synonyms where the original referenced site now forwards the name to a third site!

This will rollback any uncommitted work. Note that this does not destroy the database handle. You need to do an explicit 'undef $dbh' to destroy the handle. Any statements prepared using this handle are finished (see below) and cannot be used again. All space associated with the statements is released.

If you are using an Sqlflex driver for which $drh->{ProductVersion} >= 600, then you can have multiple concurrent connections. This means that multiple calls to $drh->connect will give you independent connections to one or more databases.

If you are using an Sqlflex driver for which $drh->{ProductVersion} < 600, then you cannot have multiple concurrent connections. If you make multiple calls to $drh->connect, you will achieve the same effect as executing several database statements in a row. This will generally switch databases successfully, but may invalidate any statements previously prepared. It may fail if the current database is not local, or if there is an active transaction, etc.

Given a database connection, you can execute a variety of simple statements using a variety of different calls:

$dbh->commit;
$dbh->rollback;

These two operations commit or rollback the current transaction. If the database is unlogged, they do nothing. If AutoCommit is set to 1, then they do nothing useful. If AutoCommit is set to 0, then a new transaction is started (implicitly for a database which is MODE ANSI, explicitly for a database which is not MODE ANSI).

You can execute most preparable parameterless statements using:

$dbh->do($stmt);

The statement should not be either SELECT (other than SELECT...INTO TEMP) or EXECUTE PROCEDURE where the procedure returns data.

The 'undef' represents an undefined reference to a hash of attributes (\%attr) which is documented in the DBI specification. The 0.56 edition of this documentation omitted this argument, which caused confusion. Again, the statement must not be a SELECT or EXECUTE PROCEDURE which returns data. The values in @parameters (or the separate values) are bound to the question marks in the statement string.

$sth = $dbh->prepare($stmt);
$sth->execute(@parameters);

The code in DBD::Sqlflex versions 0.26 through 0.55 used handled the 'do' operation exclusively with its own code, and used the EXECUTE IMMEDIATE statement when possible. Releases prior to 0.26 and releases from 0.56 use the code from the DBI package and do not use EXECUTE IMMEDIATE.

You can embed an arbitrary string inside a statement with any quote marks correctly handled by invoking:

$dbh->quote($string);

This method is provided by the DBI package implementation and is inherited by the DBD::Sqlflex package. The string is enclosed in single quotes, and any embedded single quotes are doubled up, which conforms to the SQL-92 standard. This would typically be used in a context such as:

Doing this ensures that the data in $values will be interpreted correctly, regardless of what quotes appear in $value (unless it contains newline characters). Note that the alternative assignment below does not work!

You can also prepare a statement for multiple uses, and you can do this for SELECT and EXECUTE PROCEDURE statements which return data (cursory statements) as well as non-cursory statements which return no data. You create a statement handle (another reference) using:

$sth = $dbh->prepare($stmt);

If the statement is a SELECT which returns data (not SELECT...INTO TEMP) or an EXECUTE PROCEDURE for a procedure which returns values, then a cursor is declared for the prepared statement.

According to the DBI specification, the prepare call accepts an optional attributes parameter which is a reference to a hash. At the moment, no parameters are recognized. It would be reasonable to add, for example, {ix_CursorWithHold => 1} to specify that the cursor should be declared WITH HOLD. Similarly, you could add {ix_BlobLocation => 'InFile'} to support per-statement blob location, and {ix_ScrollCursor => 1} to support scroll cursors.

Note: in versions of DBD::Sqlflex prior to 0.25, preparing a statement also executed non-cursory statements and opened the cursor for cursory statements. This no longer occurs.

More typically, you need to do error checking, and this is achieved by using:

BUG: There is no way to tell whether the statement is just executable or whether it is a cursory (fetchable) statement. You are assumed to know. An attribute such as {ix_IsCursory} could be added to povide this key piece of information, and it shouldn't really be Sqlflex-specific.

Once the statement is prepared, you can execute it:

$sth->execute;

For a non-cursory statement, this simply executes the statement. If the statement is executed successfully, then the number of rows affected will be returned. If an error occurs, the returned value will be undef. If the statement does not affect any rows, the string returned is "0E0" which evaluates to true but also to zero.

For a cursory statement, it opens the cursor. If the cursor is opened successfully, then it returns the value "0E0" which evaluates to true but also to zero. If an error occurs, the returned value will be undef.

Although the DBI 0.85 spec is silent on the issue, you can also specify the input parameters for a statement using:

$sth->execute(@parameters);

The first parameter will be supplied as the value for the first place-holder question mark in the statement, the second parameter for the second place-holder, etc.

Issue: At the moment, there is no checking by DBD::Sqlflex on how many input parameters are supplied and how many are needed. Note that the Sqlflex engines give no support for determining the number of input parameters except in the VALUES clause of an INSERT statement. This needs to be resolved.

Issue: The Sqlflex engines give no support for determining the types of input parameters except in the VALUES clause of an INSERT statement. This means that DBD::Sqlflex cannot handle blobs in the SET clause of an UPDATE statement. The only known way to deal with this is to use a SELECT to retrieve the old data, a DELETE to remove it, and an INSERT to replace it with the modified data. Not nice, but it works.

Warning: later versions of DBI will specify methods to bind input parameters for statements to Perl variables. This is another area subject to change, therefore.

For cursory statements, you can discover what the returned column names, types, nullability, etc are. You do this with:

Note: Sqlflex uses '(expression)' in the array $sth->{NAME} for any non-aliassed computed value in a SELECT list, and to describe the return values from stored procedures, and so on. This could be usefully improved. There is also no guarantee that the names returned are unique. For example, in "SELECT A.Column, B.Column FROM Table1 A, Table1 B WHERE ...", both the return columns are described as 'column'.

If the statement is a cursory statement, you can retrieve the values in any of a number of ways, as described in the DBI specification.

The returned data includes blobs mapped into strings. Note that byte blobs might contain ASCII NUL '\0' characters. Perl knows how long the strings are and does preserve NUL in the middle of a byte blob. However, you may need to be careful deciding how to handle this string.

There is provision to specify how you want blobs handled. You can set the attribute:

The InFile mode returns the name of a file in the fetched array, and that file can be accessed by Perl using normal file access methods. The DummyValue mode returns "<<TEXT VALUE>>" for text blobs or "<<BYTE VALUE>>" for byte (binary) blobs. The NullValue mode returns undefined (meaning that Perl's "defined" operator would return false) values. Note that these two options do not necessarily prevent the Server from returning the data to the application, but the user does not get to see the data -- this depends on the internal implementation of the ESQL/C FETCH operation in conjunction with SQL descriptors.

You can also set the BlobLocation attribute on the database, overriding it at the statement level.

BUG: BlobLocation is not honoured.

When you have fetched as many rows as required, you close the cursor using:

$sth->finish;

This simply closes the cursor; it does not free the cursor or the statement. That is done when you destroy (undef) the statement handle:

undef $sth;

You can also implicitly rebind a statement handle to a new statement by simply using the same variable again. This does not cause any memory leaks.

With DBD::Sqlflex v0.50 and later, you can use the attribute $sth->{CursorName} to retrieve the name of a cursor. If the statement for $sth is actually a SELECT, and the cursor is in a MODE ANSI database or is declared with the 'FOR UPDATE [OF col,...' tag, then you can use the cursor name in a 'DELETE...WHERE CURRENT OF' or 'UPDATE...WHERE CURRENT OF' statement.

Note that the warning information is treated as an array (as in Sqlflex-4GL) rather than as a bunch of separate fields (as in Sqlflex-ESQL/C). Inspect the code in the print_sqlca() function in SqlflexTest.pm for more ideas on the use of these. You cannot set the sqlca record.

Transaction management changed in the DBD::Sqlflex 0.50 release, in part because the DBI specification has changed. You should read this section carefully. If you find a deviation between what is documented and what actually occurs, please report it. The problem may be in the documentation or in the code (or both).

Previously, the type of Sqlflex database had an affect on the default AutoCommit attribute. Now the AutoCommit attribute (which can be set in the DBI->connect() call) controls the AutoCommit behaviour exclusively.

There are 3 types of Sqlflex database to consider: MODE ANSI, Logged, UnLogged. Although MODE ANSI databases also have a transaction log, the category of Logged databases specifically excludes MODE ANSI databases. In OnLine, this refers to databases created WITH LOG or WITH BUFFERED LOG; in SE, to databases created WITH LOG IN "/some/file/name".

The Group 1 statements establish connections to databases. The type of database to which you are connected has no effect on the AutoCommit mode. Group 1A is the primary means of connecting to a database; the Group 1B statements can change the current database. The Group 1B statements cannot be executed except on the ".DEFAULT." connection if you are using ESQL/C 6.00 or later.

For all types of database, the default AutoCommit mode is On. With a MODE ANSI or a Logged database, the value of AutoCommit can be set to Off, which automatically starts a transaction (explicitly if the database is Logged, implicitly if the database is MODE ANSI). For an UnLogged database, the AutoCommit mode cannot be changed. Any attempt to change AutoCommit mode to Off with an UnLogged database generates a non-fatal warning.

The Group 2 statements sever the connection to a database. The Group 2A statement renders the database handle unusable; no further operations are possible except 'undef' or re-assigning with a new connection. The Group 2B statement means that no operations other than those in Group 1B or 'DROP DATABASE' are permitted on the handle. As with the Group 1B statements, the Group 2B statement can only be used on a ".DEFAULT." connection. The value of AutoCommit is irrelevant after the database is closed, but is not altered by DBD::Sqlflex.

The Group 3 & 4 statements interact in many complicated ways, but the new style of operation simplifies the interactions considerably. One side-effect of the changes is that BEGIN WORK is completely marginalized, and will generally cause an error. Although UPDATE is cited in Group 4B, it represents any statement which is not a SELECT statement. Note that 'SELECT...INTO TEMP' is a Group 4B statement because it returns no data to the program. An 'EXECUTE PROCEDURE' statement is in Group 4A if it returns data, and in Group 4B if it does not, and you cannot tell which of the two groups applies until after the statement is prepared.

Previously, MODE ANSI databases were regarded as being in a transaction at all times, but this is not the only way to view the way these databases work. However, it is more satisfactory to regard the state immediately after a database is opened, or immediately after a COMMIT WORK or ROLLBACK WORK operation as being in the No-TX state. Any statement other than a disconnection statement (Group 2) or a commit or rollback (Groups 3A or 3C) takes the databases into the In-TX state.

In a MODE ANSI database, you can execute BEGIN WORK successfully. However, if AutoCommit is On, the transaction is immediately committed, so it does you no good.

If the user elects to switch to AutoCommit On, things get trickier. All cursors need to be declared WITH HOLD so that Group 4B statements being committed do not close the active cursors. Whenever a Group 4B statement is executed, the statement needs to be committed. With OnLine (and theoretically with SE), if the statement fails there is no need to do a rollback -- the statement failing did the rollback anyway. As before, the code does ROLLBACK WORK before disconnecting, though it should not actually be necessary.

The transaction state is No-TX and AutoCommit is On, and this cannot be changed. Any attempt to set AutoCommit to Off generates a non-fatal warning but the program will continue; setting it to On generates neither a warning nor an error. Both $dbh->commit and $dbh->rollback succeed but do nothing. Executing any Group 3B or 3C statement will generate an error.

Ideally, if you attempt to connect to an UnLogged database with AutoCommit Off, you would get a connect failure. There are problems implementing this because of the way DBI 0.85 behaves when failures occur, so this is not actually implemented.

In previous releases, some of the Sqlflex-specific attributes had names which did not start 'ix_'. Starting with release 0.57, the old-style attribute names are no longer recognised and an error message is generated (by DBI).

Note that {ix_AutoErrorReport} will become {PrintError}; the two names will be synonymous for a few versions, and then {ix_AutoErrorReport} will be deprecated. Note that some other Sqlflex-specific attributes may be adopted (probably with a name change) by the DBI specification. These too will initially be treated as synonyms for the official DBI names before being deprecated. For those who have been following the attribute renaming saga, the new deprecation cycle is likely to be shorter than 5 releases.

Unrecognized attributes starting with 'ix_' will generate a warning message.

A crude form of the mapping between DBD::Sqlflex functions and ESQL/C equivalents follows -- there are a number of ways in which it isn't quite precise (eg the influence of AutoCommit), but it is accurate enough for most purposes.

If you use a 6.00 (or, maybe, 7.20) or later version of Sqlflex ESQL/C and do not have both the environment variables CLIENT_LOCALE and DB_LOCALE set, then ESQL/C may set one or both of them during the connect operation. When it does so, it makes Perl emit a "Bad free()" error if you subsequently modify the %ENV hash in the Perl script. This is nasty, but not readily resolvable. If you need to establish what values you should set, modify the code in dbdimp.ec so that the function dbd_ix_printenv() is called in dbd_ix_db_login() and the function itself is compiled (it is protected by #ifdef DBD_IX_DEBUG_ENVIRONMENT, unlike the calls which are pure comments).