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.
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:
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.
c := DBConnection new open.
DBConnection defines a number of methods for executing SQL statements.
s := c exec: 'delete from employee where empnum=123'
r := c query: 'select * from employee' r first "=> a DBRow(60432 'GORDON' 'G.L.' nil nil nil 'PROF' '2267' )" r first at: #Empname "=> 'GORDON'"
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.
DBConnection provides transaction control with the following methods.
c beginRWTxn. "begin a transaction" c exec: 'delete from employee where empnum=123'. c releaseTxn. "transaction is rolled back"
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.
[ 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"
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.
c := (DBConnection new) dsn: 'watcom32'; uid: 'dba'; pwd: 'sql'; connect. c := nil.
DBConnections and DBResultSets which exist when an image is saved will be re-instated when the image is loaded.
System catalog information (such as tables, columns, indices etc) can be accessed through instances of DBConnection.
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)... "