From 2c678e64dcb31f651674155fccaf2c21a3e3ac74 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 17 Dec 2008 21:04:17 -0600 Subject: [PATCH] rename db symbol -> db-connection --- basis/db/db-docs.factor | 22 +++++++-------- basis/db/db.factor | 24 ++++++++--------- basis/db/pools/pools.factor | 4 +-- basis/db/postgresql/lib/lib.factor | 8 +++--- basis/db/postgresql/postgresql-tests.factor | 6 +---- basis/db/postgresql/postgresql.factor | 5 ++-- basis/db/sqlite/lib/lib.factor | 5 ++-- basis/db/sqlite/sqlite.factor | 4 +-- basis/db/tuples/tuples-tests.factor | 6 ++--- basis/db/tuples/tuples.factor | 30 +++++++++++---------- basis/db/types/types.factor | 10 +++---- 11 files changed, 60 insertions(+), 64 deletions(-) diff --git a/basis/db/db-docs.factor b/basis/db/db-docs.factor index 8173ff6a5b..ae7451cb48 100644 --- a/basis/db/db-docs.factor +++ b/basis/db/db-docs.factor @@ -1,20 +1,20 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: classes kernel help.markup help.syntax sequences -alien assocs strings math multiline quotations ; +alien assocs strings math multiline quotations db.private ; IN: db -HELP: db -{ $description "The " { $snippet "db" } " class is the superclass of all other database classes. It stores a " { $snippet "handle" } " to the database as well as insert, update, and delete queries." } ; +HELP: db-connection +{ $description "The " { $snippet "db-connection" } " class is the superclass of all other database classes. It stores a " { $snippet "handle" } " to the database as well as insert, update, and delete queries. Stores the current database object as a dynamic variable." } ; -HELP: new-db -{ $values { "class" class } { "obj" object } } +HELP: new-db-connection +{ $values { "class" class } { "obj" db-connection } } { $description "Creates a new database object from a given class with caches for prepared statements. Does not actually connect to the database until " { $link db-open } " or " { $link with-db } " is called." } { $notes "User-defined databases must call this constructor word instead of " { $link new } "." } ; HELP: db-open -{ $values { "db" db } { "db" db } } -{ $description "Opens a database using the configuration data stored in a " { $link db } " tuple. The database object now references a database handle that must be cleaned up. Therefore, it is better to use the " { $link with-db } " combinator than calling this word directly." } ; +{ $values { "db" "a database configuration object" } { "db-connection" db-connection } } +{ $description "Opens a database using the configuration data stored in a " { $snippet "database configuration object" } "tuple. The database object now references a database handle that must be cleaned up. Therefore, it is better to use the " { $link with-db } " combinator than calling this word directly." } ; HELP: db-close { $values { "handle" alien } } @@ -141,13 +141,13 @@ HELP: rollback-transaction HELP: sql-command { $values { "sql" string } } -{ $description "Executes a SQL string using the databse in the " { $link db } " symbol." } ; +{ $description "Executes a SQL string using the databse in the " { $link db-connection } " symbol." } ; HELP: sql-query { $values { "sql" string } { "rows" "an array of arrays of strings" } } -{ $description "Runs a SQL query of raw text in the database in the " { $link db } " symbol. Each row is returned as an array of strings; no type-conversions are done on the resulting data." } ; +{ $description "Runs a SQL query of raw text in the database in the " { $link db-connection } " symbol. Each row is returned as an array of strings; no type-conversions are done on the resulting data." } ; { sql-command sql-query } related-words @@ -167,8 +167,8 @@ HELP: sql-row-typed HELP: with-db { $values - { "db" db } { "quot" quotation } } -{ $description "Calls the quotation with a database bound to the " { $link db } " symbol. See " { $link "db-custom-database-combinators" } " for help setting up database access." } ; + { "db" "a database configuration object" } { "quot" quotation } } +{ $description "Calls the quotation with a database bound to the " { $link db-connection } " symbol. See " { $link "db-custom-database-combinators" } " for help setting up database access." } ; HELP: with-transaction { $values diff --git a/basis/db/db.factor b/basis/db/db.factor index d4e91cf720..0b18044f2b 100644 --- a/basis/db/db.factor +++ b/basis/db/db.factor @@ -5,8 +5,6 @@ namespaces sequences classes.tuple words strings tools.walker accessors combinators fry ; IN: db -SYMBOL: db - -GENERIC: db-open ( db -- db ) -HOOK: db-close db ( handle -- ) +GENERIC: db-open ( db -- db-connection ) +HOOK: db-close db-connection ( handle -- ) : dispose-statements ( assoc -- ) values dispose-each ; -M: db-connection dispose ( db -- ) - dup db [ +M: db-connection dispose ( db-connection -- ) + dup db-connection [ [ dispose-statements H{ } clone ] change-insert-statements [ dispose-statements H{ } clone ] change-update-statements [ dispose-statements H{ } clone ] change-delete-statements @@ -69,8 +67,8 @@ TUPLE: prepared-statement < statement ; swap >>in-params swap >>sql ; -HOOK: db ( string in out -- statement ) -HOOK: db ( string in out -- statement ) +HOOK: db-connection ( string in out -- statement ) +HOOK: db-connection ( string in out -- statement ) GENERIC: prepare-statement ( statement -- ) GENERIC: bind-statement* ( statement -- ) GENERIC: low-level-bind ( statement -- ) @@ -113,8 +111,8 @@ M: object execute-statement* ( statement type -- ) accumulator [ query-each ] dip { } like ; inline : with-db ( db quot -- ) - [ db-open db ] dip - '[ db get [ drop @ ] with-disposal ] with-variable ; inline + [ db-open db-connection ] dip + '[ db-connection get [ drop @ ] with-disposal ] with-variable ; inline ! Words for working with raw SQL statements : default-query ( query -- result-set ) @@ -132,9 +130,9 @@ M: object execute-statement* ( statement type -- ) ! Transactions SYMBOL: in-transaction -HOOK: begin-transaction db ( -- ) -HOOK: commit-transaction db ( -- ) -HOOK: rollback-transaction db ( -- ) +HOOK: begin-transaction db-connection ( -- ) +HOOK: commit-transaction db-connection ( -- ) +HOOK: rollback-transaction db-connection ( -- ) M: db-connection begin-transaction ( -- ) "BEGIN" sql-command ; M: db-connection commit-transaction ( -- ) "COMMIT" sql-command ; diff --git a/basis/db/pools/pools.factor b/basis/db/pools/pools.factor index 8bc5e87f0e..55ff3a383b 100644 --- a/basis/db/pools/pools.factor +++ b/basis/db/pools/pools.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors kernel arrays namespaces sequences continuations -io.pools db fry ; +io.pools db fry db.private ; IN: db.pools TUPLE: db-pool < pool db ; @@ -17,4 +17,4 @@ M: db-pool make-connection ( pool -- ) db>> db-open ; : with-pooled-db ( pool quot -- ) - '[ db _ with-variable ] with-pooled-connection ; inline + '[ db-connection _ with-variable ] with-pooled-connection ; inline diff --git a/basis/db/postgresql/lib/lib.factor b/basis/db/postgresql/lib/lib.factor index 5149d14f3d..19cf5c5002 100644 --- a/basis/db/postgresql/lib/lib.factor +++ b/basis/db/postgresql/lib/lib.factor @@ -6,7 +6,7 @@ db.types tools.walker ascii splitting math.parser combinators libc shuffle calendar.format byte-arrays destructors prettyprint accessors strings serialize io.encodings.binary io.encodings.utf8 alien.strings io.streams.byte-array summary present urls -specialized-arrays.uint specialized-arrays.alien ; +specialized-arrays.uint specialized-arrays.alien db.private ; IN: db.postgresql.lib : postgresql-result-error-message ( res -- str/f ) @@ -24,7 +24,7 @@ IN: db.postgresql.lib "\n" split [ [ blank? ] trim ] map "\n" join ; : postgresql-error-message ( -- str ) - db get handle>> (postgresql-error-message) ; + db-connection get handle>> (postgresql-error-message) ; : postgresql-error ( res -- res ) dup [ postgresql-error-message throw ] unless ; @@ -44,7 +44,7 @@ M: postgresql-result-null summary ( obj -- str ) dup PQstatus zero? [ (postgresql-error-message) throw ] unless ; : do-postgresql-statement ( statement -- res ) - db get handle>> swap sql>> PQexec dup postgresql-result-ok? [ + db-connection get handle>> swap sql>> PQexec dup postgresql-result-ok? [ [ postgresql-result-error-message ] [ PQclear ] bi throw ] unless ; @@ -99,7 +99,7 @@ M: postgresql-result-null summary ( obj -- str ) : do-postgresql-bound-statement ( statement -- res ) [ - [ db get handle>> ] dip + [ db-connection get handle>> ] dip { [ sql>> ] [ bind-params>> length ] diff --git a/basis/db/postgresql/postgresql-tests.factor b/basis/db/postgresql/postgresql-tests.factor index bc5ec2f0c5..cf6dc903f1 100644 --- a/basis/db/postgresql/postgresql-tests.factor +++ b/basis/db/postgresql/postgresql-tests.factor @@ -1,5 +1,5 @@ USING: kernel db.postgresql alien continuations io classes -prettyprint sequences namespaces tools.test db +prettyprint sequences namespaces tools.test db db.private db.tuples db.types unicode.case accessors system ; IN: db.postgresql.tests @@ -92,7 +92,3 @@ os windows? cpu x86.64? and [ ] with-db ] unit-test ] unless - - -: with-dummy-db ( quot -- ) - [ T{ postgresql-db } db ] dip with-variable ; diff --git a/basis/db/postgresql/postgresql.factor b/basis/db/postgresql/postgresql.factor index 0041a70985..a094fbc542 100644 --- a/basis/db/postgresql/postgresql.factor +++ b/basis/db/postgresql/postgresql.factor @@ -38,8 +38,7 @@ M: postgresql-db db-open ( db -- db-connection ) [ password>> ] } cleave connect-postgres ; -M: postgresql-db-connection db-close ( handle -- ) - PQfinish ; +M: postgresql-db-connection db-close ( handle -- ) PQfinish ; M: postgresql-statement bind-statement* ( statement -- ) drop ; @@ -106,7 +105,7 @@ M: postgresql-result-set dispose ( result-set -- ) M: postgresql-statement prepare-statement ( statement -- ) dup - [ db get handle>> f ] dip + [ db-connection get handle>> f ] dip [ sql>> ] [ in-params>> ] bi length f PQprepare postgresql-error >>handle drop ; diff --git a/basis/db/sqlite/lib/lib.factor b/basis/db/sqlite/lib/lib.factor index 6741296806..d19af808a0 100644 --- a/basis/db/sqlite/lib/lib.factor +++ b/basis/db/sqlite/lib/lib.factor @@ -5,7 +5,8 @@ namespaces sequences db.sqlite.ffi db combinators continuations db.types calendar.format serialize io.streams.byte-array byte-arrays io.encodings.binary io.backend db.errors present urls io.encodings.utf8 -io.encodings.string accessors shuffle io prettyprint ; +io.encodings.string accessors shuffle io prettyprint +db.private ; IN: db.sqlite.lib ERROR: sqlite-error < db-error n string ; @@ -16,7 +17,7 @@ ERROR: sqlite-sql-error < sql-error n string ; : sqlite-statement-error ( -- * ) SQLITE_ERROR - db get handle>> sqlite3_errmsg sqlite-sql-error ; + db-connection get handle>> sqlite3_errmsg sqlite-sql-error ; : sqlite-check-result ( n -- ) { diff --git a/basis/db/sqlite/sqlite.factor b/basis/db/sqlite/sqlite.factor index 42a2cb56ee..0f545030a3 100644 --- a/basis/db/sqlite/sqlite.factor +++ b/basis/db/sqlite/sqlite.factor @@ -42,7 +42,7 @@ M: sqlite-db-connection ( str in out -- obj ) : sqlite-maybe-prepare ( statement -- statement ) dup handle>> [ - db get handle>> over sql>> sqlite-prepare + db-connection get handle>> over sql>> sqlite-prepare >>handle ] unless ; @@ -99,7 +99,7 @@ M: sqlite-statement bind-tuple ( tuple statement -- ) ERROR: sqlite-last-id-fail ; : last-insert-id ( -- id ) - db get handle>> sqlite3_last_insert_rowid + db-connection get handle>> sqlite3_last_insert_rowid dup zero? [ sqlite-last-id-fail ] when ; M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- ) diff --git a/basis/db/tuples/tuples-tests.factor b/basis/db/tuples/tuples-tests.factor index b834c2c990..246946c715 100644 --- a/basis/db/tuples/tuples-tests.factor +++ b/basis/db/tuples/tuples-tests.factor @@ -4,7 +4,7 @@ USING: io.files io.files.temp kernel tools.test db db.tuples classes db.types continuations namespaces math math.ranges prettyprint calendar sequences db.sqlite math.intervals db.postgresql accessors random math.bitwise system -math.ranges strings urls fry db.tuples.private ; +math.ranges strings urls fry db.tuples.private db.private ; IN: db.tuples.tests : sqlite-db ( -- sqlite-db ) @@ -33,10 +33,10 @@ IN: db.tuples.tests ! These words leak resources, but are useful for interactivel testing : sqlite-test-db ( -- ) - sqlite-db db-open db set ; + sqlite-db db-open db-connection set ; : postgresql-test-db ( -- ) - postgresql-db db-open db set ; + postgresql-db db-open db-connection set ; TUPLE: person the-id the-name the-number the-real ts date time blob factor-blob url ; diff --git a/basis/db/tuples/tuples.factor b/basis/db/tuples/tuples.factor index 7a5c9e41e6..d2116058d8 100644 --- a/basis/db/tuples/tuples.factor +++ b/basis/db/tuples/tuples.factor @@ -3,20 +3,20 @@ USING: arrays assocs classes db kernel namespaces classes.tuple words sequences slots math accessors math.parser io prettyprint db.types continuations -destructors mirrors sets db.types ; +destructors mirrors sets db.types db.private ; IN: db.tuples -HOOK: create-sql-statement db ( class -- object ) -HOOK: drop-sql-statement db ( class -- object ) +HOOK: create-sql-statement db-connection ( class -- object ) +HOOK: drop-sql-statement db-connection ( class -- object ) -HOOK: db ( class -- object ) -HOOK: db ( class -- object ) -HOOK: db ( class -- object ) -HOOK: db ( tuple class -- object ) -HOOK: db ( tuple class -- tuple ) -HOOK: db ( query -- statement ) -HOOK: query>statement db ( query -- statement ) -HOOK: insert-tuple-set-key db ( tuple statement -- ) +HOOK: db-connection ( class -- object ) +HOOK: db-connection ( class -- object ) +HOOK: db-connection ( class -- object ) +HOOK: db-connection ( tuple class -- object ) +HOOK: db-connection ( tuple class -- tuple ) +HOOK: db-connection ( query -- statement ) +HOOK: query>statement db-connection ( query -- statement ) +HOOK: insert-tuple-set-key db-connection ( tuple statement -- ) > [ ] cache + db-connection get insert-statements>> + [ ] cache [ bind-tuple ] 2keep insert-tuple-set-key ; : insert-user-assigned-statement ( tuple -- ) dup class - db get insert-statements>> [ ] cache + db-connection get insert-statements>> + [ ] cache [ bind-tuple ] keep execute-statement ; : do-select ( exemplar-tuple statement -- tuples ) @@ -117,7 +119,7 @@ M: tuple >query swap >>tuple ; : update-tuple ( tuple -- ) dup class - db get update-statements>> [ ] cache + db-connection get update-statements>> [ ] cache [ bind-tuple ] keep execute-statement ; : delete-tuples ( tuple -- ) diff --git a/basis/db/types/types.factor b/basis/db/types/types.factor index da9fe39b80..bb3c6c26c1 100644 --- a/basis/db/types/types.factor +++ b/basis/db/types/types.factor @@ -4,11 +4,11 @@ USING: arrays assocs db kernel math math.parser sequences continuations sequences.deep prettyprint words namespaces slots slots.private classes mirrors classes.tuple combinators calendar.format symbols -classes.singleton accessors quotations random ; +classes.singleton accessors quotations random db.private ; IN: db.types -HOOK: persistent-table db ( -- hash ) -HOOK: compound db ( string obj -- hash ) +HOOK: persistent-table db-connection ( -- hash ) +HOOK: compound db-connection ( string obj -- hash ) TUPLE: sql-spec class slot-name column-name type primary-key modifiers ; @@ -158,8 +158,8 @@ ERROR: no-sql-type type ; modifiers>> [ lookup-modifier ] map " " join [ "" ] [ " " prepend ] if-empty ; -HOOK: bind% db ( spec -- ) -HOOK: bind# db ( spec obj -- ) +HOOK: bind% db-connection ( spec -- ) +HOOK: bind# db-connection ( spec obj -- ) ERROR: no-column column ;