« Previous | Next »

NBSQLite3 - Callback from C to Pharo Smalltalk

02 Aug 2014

NBSQLite3 now does callback from C to Smalltalk. As a start, the tracing API, which "is invoked at various times when an SQL statement is being run".

| db cb tk s |
db := NBSQLite3Connection openOn: ':memory:'.
[ cb := NBSQLite3TraceCallback on: [ :appdata :sqlText |
    Transcript show: 'Trace #', appdata readString, ': '.
    Transcript show: sqlText; cr ].
  tk := NBExternalAddress fromString: '42'.
  db traceUsing: cb with: tk.
  db basicExecute: 'create table x (xk integer, xv integer, tv text)'.
  s := db prepare: 'insert into x values (NULL, ?, ?)'.
  1 to: 10 do: [ :x |
      s at: 1 putInteger: x * x.
      s at: 2 putString: x asString, ' * ', x asString.
      s step. s clearBindings. s reset. ].		
  s finalize.
  db basicExecute: 'drop table x'.
  tk finalize 
] ensure: [ db close ]

The Transcript output looks thusly:

Trace #42: create table x (xk integer, xv integer, tv text)
Trace #42: insert into x values (NULL, 1, '1 * 1')
Trace #42: insert into x values (NULL, 4, '2 * 2')
Trace #42: insert into x values (NULL, 9, '3 * 3')
Trace #42: insert into x values (NULL, 16, '4 * 4')
Trace #42: insert into x values (NULL, 25, '5 * 5')
Trace #42: insert into x values (NULL, 36, '6 * 6')
Trace #42: insert into x values (NULL, 49, '7 * 7')
Trace #42: insert into x values (NULL, 64, '8 * 8')
Trace #42: insert into x values (NULL, 81, '9 * 9')
Trace #42: insert into x values (NULL, 100, '10 * 10')
Trace #42: drop table x

Test incorporated and passes on OSX Mountain Lion and Linux Mint 17 for Pharo 3. Get the source on SS3.

Tags: SQLite