[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[sqlite] accessing

"open the test database"
| dbName db |
dbName := 'test.sqlite3'.
db := S8Ref alloc: (LibSqlite3 sqlite3Types  at: #sqlite3PtrPtr).
LibSqlite3 current open: dbName sqlite: db.
db := db deref.
self basicAt: #db put: db. "save the database reference"


"execute a couple SQL queries to ensure empty table foo exists"
| db |
db := self basicAt: #db ifAbsent: [ ^self error: 'Missing database' ].
LibSqlite3 current
	exec: db string: 'CREATE TABLE foo (bar VARCHAR);';
	exec: db string: 'DELETE FROM foo;';
	yourself


"insert a few records in table foo"
| db |
db := self basicAt: #db ifAbsent: [ ^self error: 'Missing database' ].
5 timesRepeat: [:i|
	LibSqlite3 current exec: db string: 'INSERT INTO foo VALUES(\''baz''' ,i ,'\'
].


"ToDo - Async access."
| db rowCount callback stringPtr buffer |
db := self basicAt: #db ifAbsent: [ ^self error: 'Missing database' ].
rowCount := 0.
stringPtr := S8Ref refType: #string.
callback := S8FFI current
	callback: #int argTypes: (Array
		with: 'void *' with: int
		with: stringPtr with: stringPtr)
	abi: nil json function: [:tmp :cols :argv :colv| | row |
		row := PoolDictionary new.
		1 to: cols do: [:i| row at: colv deref put: argv deref ].
		rowCount := rowCount + 1.
		return 0
	].
buffer := NodeBuffer string: test.
LibSqlite3 current
	execAsync: db string: 'SELECT ☆ FROM foo;'
	callback: callback voidPtr: buffer
	stringPtr: nil
	callback: [:err :ret|
		err notNil ifTrue: [ self error: err ].
		self print: 'total rowCount=',rowCount.
		self print: '	changes=',(LibSqlite3 current changes: db).
		LibSqlite3 current close: db
	] 


Done

 self basicAt: #db put: nil