sqlite: some helper functions
parent
ba4143e43c
commit
150b4c9272
|
@ -7,8 +7,8 @@
|
||||||
! executing SQL calls and obtaining results.
|
! executing SQL calls and obtaining results.
|
||||||
!
|
!
|
||||||
IN: sqlite
|
IN: sqlite
|
||||||
USING: alien compiler kernel namespaces sequences strings sqlite.lib
|
USING: alien compiler io.files.tmp kernel math namespaces sequences strings
|
||||||
alien.c-types continuations ;
|
sqlite.lib alien.c-types continuations ;
|
||||||
|
|
||||||
TUPLE: sqlite-error n message ;
|
TUPLE: sqlite-error n message ;
|
||||||
SYMBOL: db
|
SYMBOL: db
|
||||||
|
@ -50,12 +50,34 @@ SYMBOL: db
|
||||||
#! Bind the text to the parameterized value in the statement.
|
#! Bind the text to the parameterized value in the statement.
|
||||||
dup length SQLITE_TRANSIENT sqlite3_bind_text sqlite-check-result ;
|
dup length SQLITE_TRANSIENT sqlite3_bind_text sqlite-check-result ;
|
||||||
|
|
||||||
|
: sqlite-bind-int ( statement index int -- )
|
||||||
|
sqlite3_bind_int sqlite-check-result ;
|
||||||
|
|
||||||
|
GENERIC: sqlite-bind ( statement index obj -- )
|
||||||
|
|
||||||
|
M: object sqlite-bind ( statement index obj -- )
|
||||||
|
sqlite-bind-text ;
|
||||||
|
|
||||||
|
M: integer sqlite-bind ( statement index int -- )
|
||||||
|
sqlite-bind-int ;
|
||||||
|
|
||||||
: sqlite-bind-parameter-index ( statement name -- index )
|
: sqlite-bind-parameter-index ( statement name -- index )
|
||||||
sqlite3_bind_parameter_index ;
|
sqlite3_bind_parameter_index ;
|
||||||
|
|
||||||
: sqlite-bind-text-by-name ( statement name text -- )
|
: sqlite-bind-text-by-name ( statement name text -- )
|
||||||
>r dupd sqlite-bind-parameter-index r> sqlite-bind-text ;
|
>r dupd sqlite-bind-parameter-index r> sqlite-bind-text ;
|
||||||
|
|
||||||
|
: sqlite-bind-by-name ( statement name obj -- )
|
||||||
|
>r dupd sqlite-bind-parameter-index r> sqlite-bind ;
|
||||||
|
|
||||||
|
GENERIC# sqlite-bind-by-name-or-index 1 ( statement key val -- )
|
||||||
|
|
||||||
|
M: object sqlite-bind-by-name-or-index ( statement object val -- )
|
||||||
|
sqlite-bind-by-name ;
|
||||||
|
|
||||||
|
M: integer sqlite-bind-by-name-or-index ( statement integer val -- )
|
||||||
|
sqlite-bind ;
|
||||||
|
|
||||||
: sqlite-finalize ( statement -- )
|
: sqlite-finalize ( statement -- )
|
||||||
#! Clean up all resources related to a statement. Once called
|
#! Clean up all resources related to a statement. Once called
|
||||||
#! the statement cannot be used. All statements must be finalized
|
#! the statement cannot be used. All statements must be finalized
|
||||||
|
@ -77,6 +99,9 @@ SYMBOL: db
|
||||||
#! from zero, as a string.
|
#! from zero, as a string.
|
||||||
sqlite3_column_text ;
|
sqlite3_column_text ;
|
||||||
|
|
||||||
|
: column-int ( statement index -- int )
|
||||||
|
sqlite3_column_int ;
|
||||||
|
|
||||||
: step-complete? ( step-result -- bool )
|
: step-complete? ( step-result -- bool )
|
||||||
#! Return true if the result of a sqlite3_step is
|
#! Return true if the result of a sqlite3_step is
|
||||||
#! such that the iteration has completed (ie. it is
|
#! such that the iteration has completed (ie. it is
|
||||||
|
@ -125,3 +150,7 @@ DEFER: (sqlite-map)
|
||||||
[ db get sqlite-close ] [ ] cleanup
|
[ db get sqlite-close ] [ ] cleanup
|
||||||
] with-scope ;
|
] with-scope ;
|
||||||
|
|
||||||
|
: with-tmp-db ( quot -- )
|
||||||
|
".db" [
|
||||||
|
swap with-sqlite
|
||||||
|
] with-tmpfile ;
|
||||||
|
|
Loading…
Reference in New Issue