ODBC Database Connection


Introduction

The ODBC Database Connection package for Dolphin Smalltalk offers an interface for accessing databases through Microsoft's ODBC driver layer.

You must agree to the license conditions when you install the software. Once installed, use the Package Browser to load Database Connection.pac into your system.


Connecting to a database

The DBConnection class defines the functionality of a database connection. One instance of DBConnection should be created for every database connection.

To connect to a datasource:

  1. create a new instance of DBConnection
  2. set the datasource name with #dsn:
  3. set the username with #uid:
  4. set the password with #pwd:
  5. finally, use #connect to make the connection

Example

c := (DBConnection new)
	dsn: 'watcom32'; uid: 'dba'; pwd: 'sql';
	connect.

Alternatively, the connection details can be entered by the user at runtime using the #open message. This allows the user to select from a list of available datasources.

Example

c := DBConnection new open.

Running SQL statements

DBConnection defines a number of methods for executing SQL statements.

exec: aSqlString

Example

s := c exec: 'delete from employee where empnum=123'

query: aSqlString

Example

r := c query: 'select * from employee'
r first
	"=> a DBRow(60432 'GORDON' 'G.L.' nil nil nil 'PROF' '2267' )"
r first at: #Empname
	"=> 'GORDON'"

prepare: aSqlString

Example

p := c prepare: 'insert into employee values (?, ?, ?, ?, ?, ?, ?, ?)'.
p paramCols: r describeCols.
p values: #(11111 'TOM' 'N' nil nil nil 'PROF' '2267' ).
p exec.
p values: #(22222 'JERRY' 'N' nil nil nil 'PROF' '2267' ).
p exec.

Transactions

DBConnection provides transaction control with the following methods.

Example

c beginRWTxn. "begin a transaction"
c exec: 'delete from employee where empnum=123'.
c releaseTxn. "transaction is rolled back"

Error handling

Error handling is achieved through the exception mechanism. The default outcome of an error is a walkback indicating an unhandled exception. Exceptions can be handled by enclosing the code in a block and using the on:do: method.

Example

[ r := c query: 'select xxx from employee' ]
        on: DBError do: [ :e | Transcript show: e details errors first msg; cr ]
	"=> [WATCOM][ODBC Driver]: column 'xxx' not found"

Finalization

Unreferenced connections and statements are automatically cleaned up during garbage collection. For example, evaluating the following expressions will (in the case of a Watcom database) cause the Watcom engine to appear and then disappear a few seconds later.

Example

c := (DBConnection new)
        dsn: 'watcom32'; uid: 'dba'; pwd: 'sql';
        connect.
c := nil.

Re-instatement

DBConnections and DBResultSets which exist when an image is saved will be re-instated when the image is loaded.


Catalog functions

System catalog information (such as tables, columns, indices etc) can be accessed through instances of DBConnection.

Example

C tables.
	"=> an OrderedCollection('Assignment' 'Class' 'Course' 'Employee' ... "
C columns: 'Employee'.
	"=> an OrderedCollection(
	a DBColAttr(1,Empnum,SQLInteger,4)
	a DBColAttr(2,Empname,SQLVarChar,20)... "