diff --git a/extra/sqlite/authors.txt b/extra/sqlite/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/lib/authors.txt b/extra/sqlite/lib/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/lib/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/lib/lib.factor b/extra/sqlite/lib/lib.factor deleted file mode 100644 index 438f22a80f..0000000000 --- a/extra/sqlite/lib/lib.factor +++ /dev/null @@ -1,120 +0,0 @@ -! Copyright (C) 2005 Chris Double, Doug Coleman. -! See http://factorcode.org/license.txt for BSD license. -! -! An interface to the sqlite database. Tested against sqlite v3.1.3. -! Remeber to pass the following to factor: -! -libraries:sqlite=libsqlite3.so -! -! Not all functions have been wrapped yet. Only those directly involving -! executing SQL calls and obtaining results. -! -IN: sqlite.lib -USING: alien compiler kernel math namespaces sequences strings alien.syntax - system combinators ; - -<< -"sqlite" { - { [ win32? ] [ "sqlite3.dll" ] } - { [ macosx? ] [ "/usr/lib/libsqlite3.dylib" ] } - { [ unix? ] [ "libsqlite3.so" ] } -} cond "cdecl" add-library ->> - -! Return values from sqlite functions -: SQLITE_OK 0 ; inline ! Successful result -: SQLITE_ERROR 1 ; inline ! SQL error or missing database -: SQLITE_INTERNAL 2 ; inline ! An internal logic error in SQLite -: SQLITE_PERM 3 ; inline ! Access permission denied -: SQLITE_ABORT 4 ; inline ! Callback routine requested an abort -: SQLITE_BUSY 5 ; inline ! The database file is locked -: SQLITE_LOCKED 6 ; inline ! A table in the database is locked -: SQLITE_NOMEM 7 ; inline ! A malloc() failed -: SQLITE_READONLY 8 ; inline ! Attempt to write a readonly database -: SQLITE_INTERRUPT 9 ; inline ! Operation terminated by sqlite_interrupt() -: SQLITE_IOERR 10 ; inline ! Some kind of disk I/O error occurred -: SQLITE_CORRUPT 11 ; inline ! The database disk image is malformed -: SQLITE_NOTFOUND 12 ; inline ! (Internal Only) Table or record not found -: SQLITE_FULL 13 ; inline ! Insertion failed because database is full -: SQLITE_CANTOPEN 14 ; inline ! Unable to open the database file -: SQLITE_PROTOCOL 15 ; inline ! Database lock protocol error -: SQLITE_EMPTY 16 ; inline ! (Internal Only) Database table is empty -: SQLITE_SCHEMA 17 ; inline ! The database schema changed -: SQLITE_TOOBIG 18 ; inline ! Too much data for one row of a table -: SQLITE_CONSTRAINT 19 ; inline ! Abort due to contraint violation -: SQLITE_MISMATCH 20 ; inline ! Data type mismatch -: SQLITE_MISUSE 21 ; inline ! Library used incorrectly -: SQLITE_NOLFS 22 ; inline ! Uses OS features not supported on host -: SQLITE_AUTH 23 ; inline ! Authorization denied -: SQLITE_FORMAT 24 ; inline ! Auxiliary database format error -: SQLITE_RANGE 25 ; inline ! 2nd parameter to sqlite3_bind out of range -: SQLITE_NOTADB 26 ; inline ! File opened that is not a database file - -: sqlite-error-messages ( -- seq ) { - "Successful result" - "SQL error or missing database" - "An internal logic error in SQLite" - "Access permission denied" - "Callback routine requested an abort" - "The database file is locked" - "A table in the database is locked" - "A malloc() failed" - "Attempt to write a readonly database" - "Operation terminated by sqlite_interrupt()" - "Some kind of disk I/O error occurred" - "The database disk image is malformed" - "(Internal Only) Table or record not found" - "Insertion failed because database is full" - "Unable to open the database file" - "Database lock protocol error" - "(Internal Only) Database table is empty" - "The database schema changed" - "Too much data for one row of a table" - "Abort due to contraint violation" - "Data type mismatch" - "Library used incorrectly" - "Uses OS features not supported on host" - "Authorization denied" - "Auxiliary database format error" - "2nd parameter to sqlite3_bind out of range" - "File opened that is not a database file" -} ; - -: SQLITE_ROW 100 ; inline ! sqlite_step() has another row ready -: SQLITE_DONE 101 ; inline ! sqlite_step() has finished executing - -! Return values from the sqlite3_column_type function -: SQLITE_INTEGER 1 ; inline -: SQLITE_FLOAT 2 ; inline -: SQLITE_TEXT 3 ; inline -: SQLITE_BLOB 4 ; inline -: SQLITE_NULL 5 ; inline - -! Values for the 'destructor' parameter of the 'bind' routines. -: SQLITE_STATIC 0 ; inline -: SQLITE_TRANSIENT -1 ; inline - -TYPEDEF: void sqlite3 -TYPEDEF: void sqlite3_stmt - -LIBRARY: sqlite -FUNCTION: int sqlite3_open ( char* filename, void* ppDb ) ; -FUNCTION: int sqlite3_close ( sqlite3* pDb ) ; -FUNCTION: int sqlite3_prepare ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ; -FUNCTION: int sqlite3_finalize ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_reset ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_step ( sqlite3_stmt* pStmt ) ; -FUNCTION: int sqlite3_last_insert_rowid ( sqlite3* pStmt ) ; -FUNCTION: int sqlite3_bind_blob ( sqlite3_stmt* pStmt, int index, void* ptr, int len, int destructor ) ; -FUNCTION: int sqlite3_bind_int ( sqlite3_stmt* pStmt, int index, int n ) ; -FUNCTION: int sqlite3_bind_null ( sqlite3_stmt* pStmt, int n ) ; -FUNCTION: int sqlite3_bind_text ( sqlite3_stmt* pStmt, int index, char* text, int len, int destructor ) ; -FUNCTION: int sqlite3_bind_parameter_index ( sqlite3_stmt* pStmt, char* name ) ; -FUNCTION: int sqlite3_column_count ( sqlite3_stmt* pStmt ) ; -FUNCTION: void* sqlite3_column_blob ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_bytes ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: char* sqlite3_column_decltype ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_int ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_name ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: char* sqlite3_column_text ( sqlite3_stmt* pStmt, int col ) ; -FUNCTION: int sqlite3_column_type ( sqlite3_stmt* pStmt, int col ) ; - diff --git a/extra/sqlite/sqlite-docs.factor b/extra/sqlite/sqlite-docs.factor deleted file mode 100644 index d58b553f11..0000000000 --- a/extra/sqlite/sqlite-docs.factor +++ /dev/null @@ -1,87 +0,0 @@ -! Copyright (C) 2006 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -USING: help help.syntax help.markup ; -IN: sqlite - -HELP: sqlite-open -{ $values { "filename" "path to sqlite database" } - { "db" "the database object" } -} -{ $description "Opens the sqlite3 database." } -{ $see-also sqlite-close sqlite-last-insert-rowid } ; - -HELP: sqlite-close -{ $values { "db" "the database object" } -} -{ $description "Closes the sqlite3 database." } -{ $see-also sqlite-open sqlite-last-insert-rowid } ; - -HELP: sqlite-last-insert-rowid -{ $values { "db" "the database object" } - { "rowid" "the row number of the last insert" } -} -{ $description "Returns the number of the row of the last statement inserted into the database." } -{ $see-also sqlite-open sqlite-close } ; - -HELP: sqlite-prepare -{ $values { "db" "the database object" } - { "sql" "the SQL statement as a string" } - { "statement" "the prepared SQL statement" } -} -{ $description "Internally compiles the SQL statement ready to be run by sqlite. The statement is executed and the results iterated over using " { $link sqlite-each } " and " { $link sqlite-map } ". The SQL statement can use named parameters which are later bound to values using " { $link sqlite-bind-text } " and " { $link sqlite-bind-text-by-name } "." } -{ $see-also sqlite-open sqlite-close } ; - -HELP: sqlite-bind-text -{ $values { "statement" "a prepared SQL statement" } - { "index" "the index of the bound parameter in the SQL statement" } - { "text" "the string value to bind to that column" } - -} -{ $description "Binds the text to a parameter in the SQL statement. The parameter to be bound is identified by the index given and the indexes start from one." } -{ $examples { $code "\"people.db\" sqlite-open\n\"select * from people where name=?\" sqlite-prepare\n1 \"chris\" sqlite-bind-text" } } -{ $see-also sqlite-bind-text-by-name } ; - -HELP: sqlite-bind-text-by-name -{ $values { "statement" "a prepared SQL statement" } - { "name" "the name of the bound parameter in the SQL statement" } - { "text" "the string value to bind to that column" } - -} -{ $description "Binds the text to a parameter in the SQL statement. The parameter to be bound is identified by the given name." } -{ $examples { $code "\"people.db\" sqlite-open\n\"select * from people where name=:name\" sqlite-prepare\n\"name\" \"chris\" sqlite-bind-text" } } -{ $see-also sqlite-bind-text } ; - -HELP: sqlite-finalize -{ $values { "statement" "a prepared SQL statement" } -} -{ $description "Clean up all resources related to a statement. Once called the statement cannot be used again. All statements must be finalized before closing the database." } -{ $see-also sqlite-close sqlite-prepare } ; - -HELP: sqlite-reset -{ $values { "statement" "a prepared SQL statement" } -} -{ $description "Reset a statement so it can be called again, possibly with different bound parameters." } -{ $see-also sqlite-bind-text sqlite-bind-text-by-name } ; - -HELP: column-count -{ $values { "statement" "a prepared SQL statement" } { "int" "the number of columns" } } -{ $description "Return the number of columns in each row of the result set of the given statement." } -{ $see-also column-text sqlite-each sqlite-map } ; - -HELP: column-text -{ $values { "statement" "a prepared SQL statement" } { "index" "column number indexed from zero" } { "string" "column value" } -} -{ $description "Return the value of the given column, indexed from zero, as a string." } -{ $see-also column-count sqlite-each sqlite-map } ; - -HELP: sqlite-each -{ $values { "statement" "a prepared SQL statement" } { "quot" "A quotation with stack effect ( statement -- )" } -} -{ $description "Executes the SQL statement and for each returned row calls the qutotation passing the statement on the stack. The quotation can use " { $link column-text } " to get result values for that row." } -{ $see-also column-count column-text sqlite-map } ; - -HELP: sqlite-map -{ $values { "statement" "a prepared SQL statement" } { "quot" "A quotation with stack effect ( statement -- value )" } { "seq" "a new sequence" } -} -{ $description "Executes the SQL statement and for each returned row calls the qutotation passing the statement on the stack. The quotation can use " { $link column-text } " to get result values for that row. The quotation should leave a value on the stack which gets collected and returned in the resulting sequence." } -{ $see-also column-count column-text sqlite-each } ; diff --git a/extra/sqlite/sqlite-tests.factor b/extra/sqlite/sqlite-tests.factor deleted file mode 100644 index 5eecbec369..0000000000 --- a/extra/sqlite/sqlite-tests.factor +++ /dev/null @@ -1,69 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. Redistributions in binary form must reproduce the above copyright notice, -! this list of conditions and the following disclaimer in the documentation -! and/or other materials provided with the distribution. -! -! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -! -! Test the sqlite interface -! -! Create a test database like follows: -! -! sqlite3 test.db < test.txt -! -! Then run this file. -USE: sqlite -USE: kernel -USE: io -USE: io.files -USE: prettyprint - -: test.db "libs/sqlite/test.db" resource-path ; - -: show-people ( statement -- ) - dup 0 column-text write " from " write 1 column-text . ; - -: run-test ( -- ) - test.db sqlite-open - dup "select * from test" sqlite-prepare - dup [ show-people ] sqlite-each - sqlite-finalize - sqlite-close ; - -: find-person ( name -- ) - test.db sqlite-open ! name db - dup "select * from test where name=?" sqlite-prepare ! name db stmt - [ rot 1 swap sqlite-bind-text ] keep ! db stmt - [ [ 1 column-text . ] sqlite-each ] keep - sqlite-finalize - sqlite-close ; - -: find-all ( -- ) - test.db sqlite-open ! db - dup "select * from test" sqlite-prepare ! db stmt - [ [ [ 0 column-text ] keep 1 column-text curry ] sqlite-map ] keep - sqlite-finalize - swap sqlite-close ; - -: run-test2 ( -- ) - test.db sqlite-open - dup "select * from test" sqlite-prepare - dup [ show-people ] ; - -run-test diff --git a/extra/sqlite/sqlite.factor b/extra/sqlite/sqlite.factor deleted file mode 100644 index d651ad916c..0000000000 --- a/extra/sqlite/sqlite.factor +++ /dev/null @@ -1,127 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -! -! An interface to the sqlite database. Tested against sqlite v3.0.8. -! -! Not all functions have been wrapped yet. Only those directly involving -! executing SQL calls and obtaining results. -! -IN: sqlite -USING: alien compiler kernel namespaces sequences strings sqlite.lib - alien.c-types continuations ; - -TUPLE: sqlite-error n message ; -SYMBOL: db - -! High level sqlite routines -: sqlite-check-result ( result -- ) - #! Check the result from a sqlite call is ok. If it is - #! return, otherwise throw an error. - dup SQLITE_OK = [ - drop - ] [ - dup sqlite-error-messages nth - \ sqlite-error construct-boa throw - ] if ; - -: sqlite-open ( filename -- db ) - #! Open the database referenced by the filename and return - #! a handle to that database. An error is thrown if the database - #! failed to open. - "void*" [ sqlite3_open sqlite-check-result ] keep *void* ; - -: sqlite-close ( db -- ) - #! Close the given database - sqlite3_close sqlite-check-result ; - -: sqlite-last-insert-rowid ( db -- rowid ) - #! Return the rowid of the last insert - sqlite3_last_insert_rowid ; - -: sqlite-prepare ( db sql -- statement ) - #! Prepare a SQL statement. Returns the statement which - #! can have values bound to parameters or simply executed. - #! TODO: Support multiple statements in the SQL string. - dup length "void*" "void*" - [ sqlite3_prepare sqlite-check-result ] 2keep - drop *void* ; - -: sqlite-bind-text ( statement index text -- ) - #! Bind the text to the parameterized value in the statement. - dup length SQLITE_TRANSIENT sqlite3_bind_text sqlite-check-result ; - -: 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-finalize ( statement -- ) - #! Clean up all resources related to a statement. Once called - #! the statement cannot be used. All statements must be finalized - #! before closing the database. - sqlite3_finalize sqlite-check-result ; - -: sqlite-reset ( statement -- ) - #! Reset a statement so it can be called again, possibly with - #! different parameters. - sqlite3_reset sqlite-check-result ; - -: column-count ( statement -- int ) - #! Given a prepared statement, return the number of - #! columns in each row of the result set of that statement. - sqlite3_column_count ; - -: column-text ( statement index -- string ) - #! Return the value of the given column, indexed - #! from zero, as a string. - sqlite3_column_text ; - -: step-complete? ( step-result -- bool ) - #! Return true if the result of a sqlite3_step is - #! such that the iteration has completed (ie. it is - #! SQLITE_DONE). Throw an error if an error occurs. - dup SQLITE_ROW = [ - drop f - ] [ - dup SQLITE_DONE = [ - drop t - ] [ - sqlite-check-result t - ] if - ] if ; - -: sqlite-each ( statement quot -- ) - #! Execute the SQL statement, and call the quotation for - #! each row returned from executing the statement with the - #! statement on the top of the stack. - over sqlite3_step step-complete? [ - 2drop - ] [ - [ call ] 2keep sqlite-each - ] if ; inline - -! For comparison, here is the linrec implementation of sqlite-each -! [ drop sqlite3_step step-complete? ] -! [ 2drop ] -! [ 2dup 2slip ] -! [ ] linrec ; - -DEFER: (sqlite-map) - -: (sqlite-map) ( statement quot seq -- ) - pick sqlite3_step step-complete? [ - 2nip - ] [ - >r 2dup call r> swap add (sqlite-map) - ] if ; - -: sqlite-map ( statement quot -- seq ) - { } (sqlite-map) ; - -: with-sqlite ( path quot -- ) - [ - >r sqlite-open db set r> - [ db get sqlite-close ] [ ] cleanup - ] with-scope ; - diff --git a/extra/sqlite/test.txt b/extra/sqlite/test.txt deleted file mode 100644 index 5c7ae2b52a..0000000000 --- a/extra/sqlite/test.txt +++ /dev/null @@ -1,3 +0,0 @@ -create table test (name varchar(30), address varchar(30)); -insert into test values('John', 'America'); -insert into test values('Jane', 'New Zealand'); diff --git a/extra/sqlite/tuple-db/authors.txt b/extra/sqlite/tuple-db/authors.txt deleted file mode 100755 index 44b06f94bc..0000000000 --- a/extra/sqlite/tuple-db/authors.txt +++ /dev/null @@ -1 +0,0 @@ -Chris Double diff --git a/extra/sqlite/tuple-db/tuple-db-docs.factor b/extra/sqlite/tuple-db/tuple-db-docs.factor deleted file mode 100644 index 3c6df0eaa6..0000000000 --- a/extra/sqlite/tuple-db/tuple-db-docs.factor +++ /dev/null @@ -1,131 +0,0 @@ -! Copyright (C) 2006 Chris Double. -! See http://factorcode.org/license.txt for BSD license. -USING: help sqlite help.syntax help.markup ; -IN: sqlite.tuple-db - -ARTICLE: { "sqlite" "tuple-db-loading" } "Loading" -"The quickest way to get up and running with this library is to use the vocabulary:" -{ $code "USING: sqlite sqlite.tuple-db ;\n" } -"Some simple tests can be run to check that everything is working ok:" -{ $code "\"libs/sqlite\" test-module" } ; - -ARTICLE: { "sqlite" "tuple-db-usage" } "Basic Usage" -"This library can be used for storing simple Factor tuples in a sqlite database. In its current form the tuples must not contain references to other tuples and should not have a delegate set." -$nl -"This document will use the following tuple for demonstration purposes:" -{ $code "TUPLE: person name surname phone ;" } -"The sqlite database to store tuples must be created, or an existing one opened. This is done using the " { $link sqlite-open } " word. If the database does not exist then it is created. The examples in this document store the database pointer in a variable called 'db':" -{ $code "SYMBOL: db\n\"example.db\" sqlite-open db set-global" } ; - -ARTICLE: { "sqlite" "tuple-db-mappings" } "Tuple Mappings" -"Each tuple has a 'mapping' tuple associated with it. The 'mapping' stores information about what table the tuple will be stored in, the datatypes of the tuple slots, etc. A mapping must be created before a tuple can be stored in a database. A default mapping is easily created using " { $link default-mapping } ". Given the tuple class, this will use reflection to get the slots of it, assume that all slots are of database type 'text', and store the tuple objects in a table with the same name as the tuple." -$nl -"The following shows how to create the default mapping for the 'person' tuple, and how to register that mapping so the 'tuple-db' system can know how to handle 'person' instances:" -{ $code "person default-mapping set-mapping" } ; - -ARTICLE: { "sqlite" "tuple-db-create" } "Creating the table" -"The table used to store tuple instances may need to be created. This can be done manually using the external sqlite program or via " { $link create-tuple-table } ":" -{ $code "db get person create-tuple-table" } -"The SQL used to create the table is produced internally by " { $link create-sql } ". This is a generic word dispatched on the mapping object, and could be specialised if needed. If you wish to see the SQL used to create the table, use the following code:" -{ $code "person get-mapping create-sql .\n => \"create table person (name text,surname text,phone text);\"" } ; - -ARTICLE: { "sqlite" "tuple-db-insert" } "Inserting instances" -"The " { $link insert-tuple } " word will store instances of a tuple into the database table defined by its mapping object:" -{ $code "db get \"John\" \"Smith\" \"123-456-789\" insert-tuple" } -{ $link insert-tuple } " internally uses the " { $link insert-sql } " word to produce the SQL used to store the tuple. Like " { $link create-sql } ", it is a generic word specialized on the mapping object. You can call it directly to see what SQL is generated:" -{ $code "person get-mapping insert-sql .\n => \"insert into person values(:name,:surname,:phone);\"" } -"Notice that the SQL uses named parameters. These parameters are bound to the values stored in the tuple object when the SQL is compiled. This helps prevent SQL injection techniques." -$nl -"When " { $link insert-sql } " is run, it adds a delegate to the tuple being stored. The delegate is of type 'persistent' and holds the row id of the tuple in its 'key' slot. This way the exact record can be updated or retrieved later. The following demonstates this fact:" -{ $code "\"Mandy\" \"Jones\" \"987-654-321\" dup .\n => T{ person f \"Mandy\" \"Jones\" \"987-654-321\" }\ndb get over insert-tuple .\n => T{ person T{ persistent ... 2 } \"Mandy\" \"Jones\" \"987-654-321\" }" } -"The '2' in the above example is the row id of the record inserted. We can go into the 'sqlite' command and view this record:" -{ $code " $ sqlite3 example.db\n SQLite version 3.0.8\n Enter \".help\" for instructions\n sqlite> select ROWID,* from person;\n 1|John|Smith|123-456-789\n 2|Mandy|Jones|987-654-321\n sqlite>" } ; - -ARTICLE: { "sqlite" "tuple-db-finding" } "Finding instances" -"The " { $link find-tuples } " word is used to return tuples populated with data already existing in the database. As well as the database objcet, it takes a tuple that should be populated only with the fields that should be matched in the database. All fields you do not wish to match against should be set to 'f':" -{ $code "db get f \"Smith\" f find-tuples .\n => { T{ person # \"John\" \"Smith\" \"123-456-789\" } }\ndb get \"Mandy\" f f find-tuples .\n => { T{ person # \"Mandy\" \"Jones\" \"987-654-321\" } }\ndb get \"Joe\" f f find-tuples .\n => { }" } -"Notice that if no matching tuples are found then an empty sequence is returned. The returned tuples also have their delegate set to 'persistent' with the correct row id set as the key. This can be used to later update the tuples with new information and store them in the database." ; - -ARTICLE: { "sqlite" "tuple-db-updating" } "Updating instances" -"Given a tuple that has the 'persistent' delegate with the row id set as the key, you can update this specific record using " { $link update-tuple } ":" -{ $code "db get f \"Smith\" f find-tuples dup .\n => { T{ person # \"John\" \"Smith\" \"123-456-789\" } }\nfirst { \"999-999-999\" swap set-person-phone ] keep dup .\n => T{ person T{ persistent f # \"1\" } \"John\" \"Smith\" \"999-999-999\" ...\n db get swap update-tuple" } -"Using the 'sqlite' command from the system shell you can see the record was updated:" -{ $code " $ sqlite3 example.db\n SQLite version 3.0.8\n Enter \".help\" for instructions\n sqlite> select ROWID,* from person;\n 1|John|Smith|999-999-999\n 2|Mandy|Jones|987-654-321\n sqlite>" } ; - -ARTICLE: { "sqlite" "tuple-db-inserting-or-updating" } "Inserting or Updating instances" -"The " { $link save-tuple } " word can be used to insert a tuple if it has not already been stored in the database, or update it if it already exists. Whether to insert or update is decided by the existance of the 'persistent' delegate:" -{ $code "\"Mary\" \"Smith\" \"111-111-111\" dup .\n => T{ person f \"Mary\" \"Smith\" \"111-111-111\" }\n! This will insert the tuple\ndb get over save-tuple dup .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"111-111-111\" ...\n[ \"222-222-222\" swap set-person-phone ] keep dup .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"222-222-222\" ...\n! This will update the tuple\ndb get over save-tuple .\n => T{ person T{ persistent f # \"3\" } \"Mary\" \"Smith\" \"222-222-222\" ..." } ; - -ARTICLE: { "sqlite" "tuple-db-deleting" } "Deleting instances" -"Given a tuple with the delegate set to 'persistent' (ie. One already stored in the database) you can delete it from the database with " { $link delete-tuple } ":" -{ $code "db get f \"Smith\" f find-tuples [ db get swap delete-tuple ] each" } ; - -ARTICLE: { "sqlite" "tuple-db-closing" } "Closing the database" -"It's important to close the sqlite database when you've finished using it. The word for this is " { $link sqlite-close } ":" -{ $code "db get sqlite-close" } ; - -ARTICLE: { "sqlite" "tuple-db" } "Tuple Database Library" -"The version of sqlite required by this library is version 3 or greater. This library allows storing Factor tuples in a sqlite database. It provides words to create, read update and delete these entries as well as simple searching." -$nl -"The library is in a very early state and is likely to change quite a bit in the near future. Its most notable omission is it cannot currently handle relationships between tuples." -{ $subsection { "sqlite" "tuple-db-loading" } } -{ $subsection { "sqlite" "tuple-db-usage" } } -{ $subsection { "sqlite" "tuple-db-mappings" } } -{ $subsection { "sqlite" "tuple-db-create" } } -{ $subsection { "sqlite" "tuple-db-insert" } } -{ $subsection { "sqlite" "tuple-db-finding" } } -{ $subsection { "sqlite" "tuple-db-updating" } } -{ $subsection { "sqlite" "tuple-db-inserting-or-updating" } } -{ $subsection { "sqlite" "tuple-db-deleting" } } -{ $subsection { "sqlite" "tuple-db-closing" } } -; - -HELP: default-mapping -{ $values { "class" "symbol for the tuple class" } - { "mapping" "a mapping object" } -} -{ $description "Given a tuple class, create a default mappings object. This is used to associate field names in the tuple with SQL statement field names, etc." } -{ $see-also { "sqlite" "tuple-db" } set-mapping } ; - -HELP: set-mapping -{ $values { "mapping" "a mapping object" } -} -{ $description "Store a database mapping so that the tuple-db system knows how to store instances of the tuple in the database." } -{ $see-also { "sqlite" "tuple-db" } default-mapping } ; - -HELP: create-tuple-table -{ $values { "db" "a database object" } { "class" "symbol for the tuple class" } -} -{ $description "Create the database table to store intances of the given tuple." } -{ $see-also { "sqlite" "tuple-db" } default-mapping get-mapping } ; - -HELP: insert-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Insert the tuple instance into the database. It is assumed that this tuple does not currently exist in the database." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: find-tuples -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } { "seq" "a sequence of tuples" } } -{ $description "Return a sequence of all tuples in the database that match the tuple provided as a template. All fields in the tuple must match the entries in the database, except for those set to 'f'." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: update-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Update the database record for this tuple instance. The tuple must have previously been obtained from the database, or inserted into it. It must have a delegate of 'persistent' with the key field set (which is done by the find and insert operations)." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: save-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Insert or Update the tuple instance depending on whether it has a persistent delegate." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -HELP: delete-tuple -{ $values { "db" "a database object" } { "tuple" "an instance of a tuple" } -} -{ $description "Delete this tuple instance from the database. The tuple must have previously been obtained from the database, or inserted into it. It must have a delegate of 'persistent' with the key field set (which is done by the find and insert operations)." } -{ $see-also { "sqlite" "tuple-db" } insert-tuple update-tuple find-tuples delete-tuple save-tuple } ; - -ABOUT: { "sqlite" "tuple-db" } \ No newline at end of file diff --git a/extra/sqlite/tuple-db/tuple-db-tests.factor b/extra/sqlite/tuple-db/tuple-db-tests.factor deleted file mode 100644 index 8ed2631b45..0000000000 --- a/extra/sqlite/tuple-db/tuple-db-tests.factor +++ /dev/null @@ -1,39 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! See http://factorcode.org/license.txt for BSD license. - -IN: temporary -USING: io io.files kernel sequences namespaces -hashtables sqlite sqlite.tuple-db math words tools.test ; - -TUPLE: testdata one two ; - -C: testdata - -testdata default-mapping set-mapping - -"libs/sqlite/test.db" resource-path [ - - db get testdata create-tuple-table - - [ "two" { } ] [ - db get "one" "two" insert-tuple - db get "one" f find-tuples - first [ testdata-two ] keep - db get swap delete-tuple - db get "one" f find-tuples - ] unit-test - - [ "junk" ] [ - db get "one" "two" insert-tuple - db get "one" f find-tuples - first - "junk" over set-testdata-two - db get swap update-tuple - db get "one" f find-tuples - first [ testdata-two ] keep - db get swap delete-tuple - ] unit-test - - db get testdata drop-tuple-table -] with-sqlite - diff --git a/extra/sqlite/tuple-db/tuple-db.factor b/extra/sqlite/tuple-db/tuple-db.factor deleted file mode 100644 index c37a49d2b6..0000000000 --- a/extra/sqlite/tuple-db/tuple-db.factor +++ /dev/null @@ -1,270 +0,0 @@ -! Copyright (C) 2005 Chris Double. -! -! A tuple that is persistent has its delegate set as 'persistent'. -! 'persistent' holds the numeric rowid for that tuple in its table. -IN: sqlite.tuple-db -USING: io kernel sequences namespaces slots classes slots.private -assocs math words generic sqlite math.parser ; - -! Each slot in a tuple that is storable in the database has -! an instance of a db-field object the gives the name of the -! database table and slot number in the tuple object of that field. -TUPLE: db-field name bind-name slot type ; - -C: db-field - -! The mapping tuple holds information on how the slots of -! a tuple are mapped to the fields of a sqlite database. -TUPLE: mapping tuple table fields one-to-one one-to-many ; - -C: mapping - -: sanitize ( string -- string ) - #! Convert a string so it can be used as a table or field name. - clone - H{ { CHAR: - CHAR: _ } { CHAR: ? CHAR: p } } - over substitute ; - -: tuple-fields ( class -- seq ) - #! Given a tuple class return a list of the fields - #! within that tuple. Ignores the delegate field. - "slots" word-prop 1 tail [ - [ slot-spec-name sanitize dup ":" swap append ] keep - slot-spec-offset - "text" - - ] map ; - -: default-mapping ( class -- mapping ) - #! Given a tuple class, create a default mappings object. It assumes - #! there are no one-to-one or one-to-many relationships. - dup [ word-name sanitize ] keep tuple-fields f f ; - -! The mappings variable holds a hashtable mapping the tuple symbol -! to the mapping object, describing how that tuple is stored -! in the database. -SYMBOL: mappings - -: init-mappings ( -- ) - H{ } mappings set-global ; - -: get-mappings ( -- hashtable ) - mappings get-global ; - -: set-mapping ( mapping -- ) - #! Store a database mapping so that the persistence system - #! knows how to store instances of the relevant tuple in the database. - dup mapping-tuple get-mappings set-at ; - -: get-mapping ( class -- mapping ) - #! Return the database mapping for the given tuple class. - get-mappings at ; - -! The 'persistent' tuple will be set to the delegate of any tuple -! instance stored in the database. It contains the database key -! of the row in the database table for the instance or 'f' if it has -! not yet been stored in the database. It also contains the 'mapping' -! object used to translate the fields of the tuple to the database fields. -TUPLE: persistent mapping key ; -: ( tuple -- persistent ) - persistent construct-empty - >r class get-mapping r> - [ set-persistent-mapping ] keep ; - -: make-persistent ( tuple -- tuple ) - #! Convert the tuple into something that can be stored - #! into a database by setting its delegate to 'persistent'. - [ ] keep - [ set-delegate ] keep ; - - -: comma-fields ( mapping quot -- string ) - #! Given a mapping, call quot on each field in - #! the mapping. The contents of quot should call ',' or '%' - #! to generate output. The output of each quot call - #! seperated by commas is returned as a string. 'quot' should be - #! stack effect ( field -- ). - >r mapping-fields r> [ "" make ] curry map "," join ; inline - -GENERIC: create-sql ( mapping -- string ) -M: mapping create-sql ( mapping -- string ) - #! Return the SQL used to create a table for storing this type of tuple. - [ - "create table " % dup mapping-table % - " (" % - [ dup db-field-name % " " % db-field-type % ] comma-fields % - ");" % - ] "" make ; - -GENERIC: drop-sql ( mapping -- string ) -M: mapping drop-sql ( mapping -- string ) - #! Return the SQL used to drop the table for storing this type of tuple. - [ - "drop table " % mapping-table % ";" % - ] "" make ; - -GENERIC: insert-sql ( mapping -- string ) -M: mapping insert-sql ( mapping -- string ) - #! Return the SQL used to insert a tuple into a table - [ - "insert into " % dup mapping-table % - " values(" % - [ db-field-bind-name % ] comma-fields % - ");" % - ] "" make ; - -GENERIC: delete-sql ( mapping -- string ) -M: mapping delete-sql ( mapping -- string ) - #! Return the SQL used to delete a tuple from a table - [ - "delete from " % mapping-table % - " where ROWID=:rowid;" % - ] "" make ; - -GENERIC: update-sql ( mapping -- string ) -M: mapping update-sql ( mapping -- string ) - #! Return the SQL used to update the tuple - [ - "update " % dup mapping-table % - " set " % - [ dup db-field-name % "=" % db-field-bind-name % ] comma-fields % - " where ROWID=:rowid;" % - ] "" make ; - -GENERIC: select-sql ( tuple mapping -- select ) -M: mapping select-sql ( tuple mapping -- select ) - #! Return the SQL used to select a series of tuples from the database. It - #! will select based on only the filled in fields of the tuple (ie. all non-f). - [ - "select ROWID,* from " % dup mapping-table % - mapping-fields [ ! tuple field - swap over db-field-slot slot ! field value - [ - [ dup db-field-name % "=" % db-field-bind-name % ] "" make - ] [ - drop f - ] if - ] with map [ ] subset dup length 0 > [ - " where " % - " and " join % - ] [ - drop - ] if - ";" % - ] "" make ; - -: execute-update-sql ( db string -- ) - #! Execute the SQL, which should contain a database update - #! statement (update, insert, create, etc). Ignore the result. - sqlite-prepare dup [ drop ] sqlite-each sqlite-finalize ; - -: create-tuple-table ( db class -- ) - #! Create the table for the tuple class. - get-mapping create-sql execute-update-sql ; - -: drop-tuple-table ( db class -- ) - #! Create the table for the tuple class. - get-mapping drop-sql execute-update-sql ; - -: bind-for-insert ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared insert statement. - dup class get-mapping mapping-fields [ ! statement tuple field - [ db-field-slot slot ] keep ! statement value field - db-field-bind-name swap ! statement name value - >r dupd r> sqlite-bind-text-by-name - ] with each drop ; - -: bind-for-select ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared select statement. - dup class get-mapping mapping-fields [ ! statement tuple field - [ db-field-slot slot ] keep ! statement value field - over [ - db-field-bind-name swap ! statement name value - >r dupd r> sqlite-bind-text-by-name - ] [ - 2drop - ] if - ] with each drop ; - -: bind-for-update ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared update statement. - 2dup bind-for-insert - >r ":rowid" r> persistent-key sqlite-bind-text-by-name ; - -: bind-for-delete ( statement tuple -- ) - #! Bind the fields in the tuple to the fields in the - #! prepared delete statement. - >r ":rowid" r> persistent-key sqlite-bind-text-by-name ; - -: (insert-tuple) ( db tuple -- ) - #! Insert this tuple instance into the database. Note that - #! it inserts only this instance, and not any one-to-one or - #! one-to-many fields. - dup class get-mapping insert-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-insert ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: insert-tuple ( db tuple -- ) - #! Insert this tuple instance into the database and - #! update the rowid of the insert in the tuple. - [ (insert-tuple) ] 2keep - >r sqlite-last-insert-rowid number>string r> make-persistent set-persistent-key ; - -: update-tuple ( db tuple -- ) - #! Update this tuple instance in the database. The tuple should have - #! a delegate of 'persistent' with the key field set. - dup class get-mapping update-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-update ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: save-tuple ( db tuple -- ) - #! Insert or Update the tuple instance depending on whether it - #! has a persistent delegate. - dup delegate [ update-tuple ] [ insert-tuple ] if ; - -: delete-tuple ( db tuple -- ) - #! Delete this tuple instance from the database. The tuple should have - #! a delegate of 'persistent' with the key field set. - dup class get-mapping delete-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - dupd bind-for-delete ! statement - dup [ drop ] sqlite-each - sqlite-finalize ; - -: restore-tuple ( statement tuple -- tuple ) - #! Using 'tuple' as a template, clone it and - #! return the clone with fields set to the values from the - #! database. - clone dup class get-mapping mapping-fields 1 swap - [ ! statement tuple index field ) - over 1+ >r ! statement tuple index field r: index+1 - db-field-slot >r ! statement tuple index r: index+1 slot - pick swap column-text ! statement tuple value r: index+1 slot - over r> set-slot r> ! statement tuple index+1 - ] each ! statement tuple index - drop make-persistent swap 0 column-text swap [ set-persistent-key ] keep ; - -: find-tuples ( db tuple -- seq ) - #! Return a sequence of all tuples in the database that - #! match the tuple provided as a template. All fields in the - #! tuple must match the entries in the database, except for - #! those set to 'f'. - dup class get-mapping dupd select-sql ! db tuple sql - swapd sqlite-prepare swap ! statement tuple - 2dup bind-for-select ! statement tuple - [ - over [ ! tuple statement - over restore-tuple , - ] sqlite-each - ] { } make nip ! statement tuple accum - swap sqlite-finalize ; - - -get-mappings [ init-mappings ] unless