NAME
odbc - Utilize SQL databases from Tcl
SYNOPSIS
::dmh::odbc connect cmdname database user password
::dmh::odbc help
::dmh::odbc sources
cmdname SQL_statement
cmdname process_statement
cmdname auto_commit ?{1|0}?
cmdname catalog tables
cmdname disconnect
cmdname get_info infotype ?datatype?
cmdname help
cmdname null_number_map ?value?
cmdname null_string_map ?value?
cmdname schema tablename
::dmh::odbc_tabwin ?window? ?cmdname? ?title?
::dmh::odbc_table_win cmdname tablename
DESCRIPTION
This command allows you to utilize SQL databases from within your Tcl application.
ODBC is an acronym for Open Database Connectivity, a standard programming
interface defined by Microsoft. Although this standard was created for
Windows, a compatible version of this command has been implemented by Hume
Integration for Oracle on POSIX (Unix) platforms. The odbc Tcl command
is built into the usual Windows dmh_wish.exe
program and is always available. The POSIX Oracle odbc Tcl command
is available in the separate executable dmh83_sql_tcl
after executing "package require ora_odbc".
Hume Integration has also developed a Tcl application script,
dmh_SQLsrv
which uses the odbc command to provide a Graphical User Interface
to a persistent database. The dmh_SQLsrv application also can be
used to process SQL command messages received in a DMH
mailbox as part of a distributed application. It is desirable to use the
DMH message system to queue SQL statements that are intended for a persistent
database, when the persistent database interface would be a performance
bottleneck if used directly in a high performance application such as realtime
data collection. A typical design is to collect data into an in-memory
Datahub
table, and to update a persistent database using a table subscription that
feeds SQL messages to a separate
dmh_SQLsrv process.
COMMAND SUMMARY
In order to use the odbc command on Windows NT, you must install
the Win32 32 bit ODBC DLL drivers provided by your database vendor. You
should also have 32 bit ODBC administration support installed on your system.
If this has been done, you will have an ODBC administration icon on your
Control Panel window, and/or an ODBC administration icon in a different
program group. The ODBC administrator is used to configure which databases
are available on your system when using ODBC drivers.
odbc sources
The subcommand odbc sources is only supported on Windows. Use this
command to display which databases are available through ODBC. The command
returns a list of pairs where each pair is the name and description of
an ODBC data source. This list is created and maintained using the ODBC
administrator application.
odbc connect cmdname database user password
This command is used to establish a connection to a specified database.
The cmdname argument is a name that you assign to the connection,
and it becomes a command name in the Tcl interpreter. The database
argument is the "Data Source" name that was specified to the ODBC administrator.
The user and password arguments are the security credentials
that are passed to the database in order to establish the connection. These
arguments are configured using the database vendor's program. For example,
to connect to the Watcom sample database, you could enter
odbc connect sql {WSQL 4.0 Sample} admin school
In this example, the cmdname is sql. After connecting, the
sql
command is used to interact with the database.
With the POSIX Oracle version, the database can be specified as an empty
string if it is implicit from the ORACLE_SID environment variable, or specified
in the user item such as "scott@cim5". If you have problems connecting,
you should verify that the database and user environment are properly setup
by using Oracle's sqlplus utility application to connect to the database.
The user typically needs to set environment variables such as ORACLE_HOME
and add $ORACLE_HOME/bin to his PATH. Also, for the POSIX Oracle version,
the API does not support connecting to more
than one database simultaneously
from a single process. If you need to work with multiple databases, use
multiple processes, and integrate them using the DMH messaging system.
Also, the POSIX Oracle version adds the Tcl command "sql" to the interpreter
as part of its odbc implementation. Therefore you cannot use the name "sql"
as the cmdname argument to the odbc connect command.
cmdname SQL_statement
Use this subcommand to execute any SQL statement supported by the vendor's
ODBC interface. The most common statement types are insert, update, delete,
and select. There is limited support for selecting when you have very large
database fields in a table. The interface will not ask for more than 64kb
per field in a table row. The select reply may be considerably larger than
this as it combines multiple fields in a row, and multiple rows. The format
of a select reply is explained below. For other
types of SQL statements, if the SQL statement is executed successfully,
an empty string is returned. A command such as a conditional update or
a conditional delete is considered to have executed successfully even if
no rows are changed. If the command is faulty or there is an error condition
with the database connection, a diagnostic message is returned as an error
result. You can use the Tcl catch command to trap error results.
cmdname process_statement
This subcommand is used to process the data of a select query using specified
Tcl code. The Tcl code is executed for each row of data, as the data is
fetched. Only one row of data is in memory at a time so it is feasible
to work with large tables. If the Tcl code returns an error, return, or
break result code (see the return command) the
command returns without processing more data rows. For example, you can
process only the first row of an ordered query by returning the break result
code. Here is the syntax of the process_statement and examples:
process select_clause with tclcode
process * from my_table where a>0 with puts
process [join $cols ,] from big_table with [list my_proc $cols]
The select_clause is any select statement supported by your database
with the leading
select keyword removed. You can use order by
and other standard SQL select options to control the result.
The current implementation of the process statement will not work properly
if the with keyword can be found in the select_clause. You
can execute any of the usual SQL statements in your tclcode, but
you cannot execute another process statement. You can however use another
connection to achieve the same result.
The data values of a row are formatted as a list and appended as a single
argument to the tclcode for evaluation. If the evaluation returns
the usual ok code, the next data row is fetched, and the process is repeated.
If there has not been an error, the reply will show the number of rows
that have been processed in a format similar to the Select Reply Format
described below.
The process statement is not a standard SQL statement type; it has been
developed by Hume Integration to leverage the benefits of the Tcl language
environment without requiring the end user to deal with the complexity
of SQL cursors.
cmdname auto_commit ?{1|0}?
If auto_commit is set true, the database commits each SQL statement
as it is executed. This is the default behavior. If auto_commit
is set false, you are able to use the SQL rollback statement to undo statements
that have been executed since the last commit. Use this subcommand without
the 1 or 0 value to query the current value of the auto_commit
property. Use the subcommand with a 1 or 0 value to set the
property true or false.
cmdname catalog tables
This subcommand returns information on the tables that exist in the database.
The data is formatted as a select reply; see the explanation below.
The returned columns are TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, TABLE_TYPE,
and REMARKS. A table will have a TABLE_TYPE value of "TABLE".
cmdname disconnect
This subcommand is used to close the database connection. It eliminates
the Tcl command cmdname as well.
cmdname get_info infotype ?datatype?
This command invokes the SQLGetInfo( ) call to obtain information.
The information call is poorly supported by most drivers.
The infotype argument is an integer from the set defined in the
header file SQL.h. The datatype argument is a string which contains
one of the sequences INT, SMALL, or STRING to indicate
one of the API choices SQLUINTEGER, SQLUSMALLINT, or a null terminated
character string. Most of the information numeric items are obtainable as INT
and this is the default value. When an item is not supported, the typical
result is the value 0 or an empty string. Here is a short list of potentially
useful queries:
infotype | name | datatype |
2 | DATA_SOURCE_NAME | STRING |
13 | SERVER_NAME | STRING |
17 | DBMS_NAME | STRING |
18 | DBMS_VERSION | STRING |
30 | MAX_COLUMN_NAME_LEN | SMALL |
100 | MAX_COLUMNS_IN_SELECT | SMALL |
101 | MAX_COLUMNS_IN_TABLE | SMALL |
104 | MAX_ROW_SIZE | INT |
105 | MAX_STATEMENT_LEN | INT |