From 150b4c927299223c94eb1becbd1dae5d298b9148 Mon Sep 17 00:00:00 2001 From: Alex Chapman Date: Fri, 15 Feb 2008 15:39:31 +1100 Subject: [PATCH] sqlite: some helper functions --- extra/sqlite/sqlite.factor | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/extra/sqlite/sqlite.factor b/extra/sqlite/sqlite.factor index d651ad916c..63d9d64237 100644 --- a/extra/sqlite/sqlite.factor +++ b/extra/sqlite/sqlite.factor @@ -7,8 +7,8 @@ ! executing SQL calls and obtaining results. ! IN: sqlite -USING: alien compiler kernel namespaces sequences strings sqlite.lib - alien.c-types continuations ; +USING: alien compiler io.files.tmp kernel math namespaces sequences strings + sqlite.lib alien.c-types continuations ; TUPLE: sqlite-error n message ; SYMBOL: db @@ -50,12 +50,34 @@ SYMBOL: db #! Bind the text to the parameterized value in the statement. 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 ) sqlite3_bind_parameter_index ; : sqlite-bind-text-by-name ( statement name 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 -- ) #! Clean up all resources related to a statement. Once called #! the statement cannot be used. All statements must be finalized @@ -77,6 +99,9 @@ SYMBOL: db #! from zero, as a string. sqlite3_column_text ; +: column-int ( statement index -- int ) + sqlite3_column_int ; + : step-complete? ( step-result -- bool ) #! Return true if the result of a sqlite3_step is #! such that the iteration has completed (ie. it is @@ -125,3 +150,7 @@ DEFER: (sqlite-map) [ db get sqlite-close ] [ ] cleanup ] with-scope ; +: with-tmp-db ( quot -- ) + ".db" [ + swap with-sqlite + ] with-tmpfile ;