rename db symbol -> db-connection

db4
Doug Coleman 2008-12-17 21:04:17 -06:00
parent 4329578b2f
commit 2c678e64dc
11 changed files with 60 additions and 64 deletions

View File

@ -1,20 +1,20 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: classes kernel help.markup help.syntax sequences USING: classes kernel help.markup help.syntax sequences
alien assocs strings math multiline quotations ; alien assocs strings math multiline quotations db.private ;
IN: db IN: db
HELP: db HELP: db-connection
{ $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." } ; { $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 HELP: new-db-connection
{ $values { "class" class } { "obj" object } } { $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." } { $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 } "." } ; { $notes "User-defined databases must call this constructor word instead of " { $link new } "." } ;
HELP: db-open HELP: db-open
{ $values { "db" db } { "db" db } } { $values { "db" "a database configuration object" } { "db-connection" db-connection } }
{ $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." } ; { $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 HELP: db-close
{ $values { "handle" alien } } { $values { "handle" alien } }
@ -141,13 +141,13 @@ HELP: rollback-transaction
HELP: sql-command HELP: sql-command
{ $values { $values
{ "sql" string } } { "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 HELP: sql-query
{ $values { $values
{ "sql" string } { "sql" string }
{ "rows" "an array of arrays of strings" } } { "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 { sql-command sql-query } related-words
@ -167,8 +167,8 @@ HELP: sql-row-typed
HELP: with-db HELP: with-db
{ $values { $values
{ "db" db } { "quot" quotation } } { "db" "a database configuration object" } { "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." } ; { $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 HELP: with-transaction
{ $values { $values

View File

@ -5,8 +5,6 @@ namespaces sequences classes.tuple words strings
tools.walker accessors combinators fry ; tools.walker accessors combinators fry ;
IN: db IN: db
SYMBOL: db
<PRIVATE <PRIVATE
TUPLE: db-connection TUPLE: db-connection
@ -23,13 +21,13 @@ TUPLE: db-connection
PRIVATE> PRIVATE>
GENERIC: db-open ( db -- db ) GENERIC: db-open ( db -- db-connection )
HOOK: db-close db ( handle -- ) HOOK: db-close db-connection ( handle -- )
: dispose-statements ( assoc -- ) values dispose-each ; : dispose-statements ( assoc -- ) values dispose-each ;
M: db-connection dispose ( db -- ) M: db-connection dispose ( db-connection -- )
dup db [ dup db-connection [
[ dispose-statements H{ } clone ] change-insert-statements [ dispose-statements H{ } clone ] change-insert-statements
[ dispose-statements H{ } clone ] change-update-statements [ dispose-statements H{ } clone ] change-update-statements
[ dispose-statements H{ } clone ] change-delete-statements [ dispose-statements H{ } clone ] change-delete-statements
@ -69,8 +67,8 @@ TUPLE: prepared-statement < statement ;
swap >>in-params swap >>in-params
swap >>sql ; swap >>sql ;
HOOK: <simple-statement> db ( string in out -- statement ) HOOK: <simple-statement> db-connection ( string in out -- statement )
HOOK: <prepared-statement> db ( string in out -- statement ) HOOK: <prepared-statement> db-connection ( string in out -- statement )
GENERIC: prepare-statement ( statement -- ) GENERIC: prepare-statement ( statement -- )
GENERIC: bind-statement* ( statement -- ) GENERIC: bind-statement* ( statement -- )
GENERIC: low-level-bind ( statement -- ) GENERIC: low-level-bind ( statement -- )
@ -113,8 +111,8 @@ M: object execute-statement* ( statement type -- )
accumulator [ query-each ] dip { } like ; inline accumulator [ query-each ] dip { } like ; inline
: with-db ( db quot -- ) : with-db ( db quot -- )
[ db-open db ] dip [ db-open db-connection ] dip
'[ db get [ drop @ ] with-disposal ] with-variable ; inline '[ db-connection get [ drop @ ] with-disposal ] with-variable ; inline
! Words for working with raw SQL statements ! Words for working with raw SQL statements
: default-query ( query -- result-set ) : default-query ( query -- result-set )
@ -132,9 +130,9 @@ M: object execute-statement* ( statement type -- )
! Transactions ! Transactions
SYMBOL: in-transaction SYMBOL: in-transaction
HOOK: begin-transaction db ( -- ) HOOK: begin-transaction db-connection ( -- )
HOOK: commit-transaction db ( -- ) HOOK: commit-transaction db-connection ( -- )
HOOK: rollback-transaction db ( -- ) HOOK: rollback-transaction db-connection ( -- )
M: db-connection begin-transaction ( -- ) "BEGIN" sql-command ; M: db-connection begin-transaction ( -- ) "BEGIN" sql-command ;
M: db-connection commit-transaction ( -- ) "COMMIT" sql-command ; M: db-connection commit-transaction ( -- ) "COMMIT" sql-command ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel arrays namespaces sequences continuations USING: accessors kernel arrays namespaces sequences continuations
io.pools db fry ; io.pools db fry db.private ;
IN: db.pools IN: db.pools
TUPLE: db-pool < pool db ; TUPLE: db-pool < pool db ;
@ -17,4 +17,4 @@ M: db-pool make-connection ( pool -- )
db>> db-open ; db>> db-open ;
: with-pooled-db ( pool quot -- ) : with-pooled-db ( pool quot -- )
'[ db _ with-variable ] with-pooled-connection ; inline '[ db-connection _ with-variable ] with-pooled-connection ; inline

View File

@ -6,7 +6,7 @@ db.types tools.walker ascii splitting math.parser combinators
libc shuffle calendar.format byte-arrays destructors prettyprint libc shuffle calendar.format byte-arrays destructors prettyprint
accessors strings serialize io.encodings.binary io.encodings.utf8 accessors strings serialize io.encodings.binary io.encodings.utf8
alien.strings io.streams.byte-array summary present urls 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 IN: db.postgresql.lib
: postgresql-result-error-message ( res -- str/f ) : postgresql-result-error-message ( res -- str/f )
@ -24,7 +24,7 @@ IN: db.postgresql.lib
"\n" split [ [ blank? ] trim ] map "\n" join ; "\n" split [ [ blank? ] trim ] map "\n" join ;
: postgresql-error-message ( -- str ) : postgresql-error-message ( -- str )
db get handle>> (postgresql-error-message) ; db-connection get handle>> (postgresql-error-message) ;
: postgresql-error ( res -- res ) : postgresql-error ( res -- res )
dup [ postgresql-error-message throw ] unless ; 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 ; dup PQstatus zero? [ (postgresql-error-message) throw ] unless ;
: do-postgresql-statement ( statement -- res ) : 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 [ postgresql-result-error-message ] [ PQclear ] bi throw
] unless ; ] unless ;
@ -99,7 +99,7 @@ M: postgresql-result-null summary ( obj -- str )
: do-postgresql-bound-statement ( statement -- res ) : do-postgresql-bound-statement ( statement -- res )
[ [
[ db get handle>> ] dip [ db-connection get handle>> ] dip
{ {
[ sql>> ] [ sql>> ]
[ bind-params>> length ] [ bind-params>> length ]

View File

@ -1,5 +1,5 @@
USING: kernel db.postgresql alien continuations io classes 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 ; db.tuples db.types unicode.case accessors system ;
IN: db.postgresql.tests IN: db.postgresql.tests
@ -92,7 +92,3 @@ os windows? cpu x86.64? and [
] with-db ] with-db
] unit-test ] unit-test
] unless ] unless
: with-dummy-db ( quot -- )
[ T{ postgresql-db } db ] dip with-variable ;

View File

@ -38,8 +38,7 @@ M: postgresql-db db-open ( db -- db-connection )
[ password>> ] [ password>> ]
} cleave connect-postgres <postgresql-db-connection> ; } cleave connect-postgres <postgresql-db-connection> ;
M: postgresql-db-connection db-close ( handle -- ) M: postgresql-db-connection db-close ( handle -- ) PQfinish ;
PQfinish ;
M: postgresql-statement bind-statement* ( statement -- ) drop ; M: postgresql-statement bind-statement* ( statement -- ) drop ;
@ -106,7 +105,7 @@ M: postgresql-result-set dispose ( result-set -- )
M: postgresql-statement prepare-statement ( statement -- ) M: postgresql-statement prepare-statement ( statement -- )
dup dup
[ db get handle>> f ] dip [ db-connection get handle>> f ] dip
[ sql>> ] [ in-params>> ] bi [ sql>> ] [ in-params>> ] bi
length f PQprepare postgresql-error length f PQprepare postgresql-error
>>handle drop ; >>handle drop ;

View File

@ -5,7 +5,8 @@ namespaces sequences db.sqlite.ffi db combinators
continuations db.types calendar.format serialize continuations db.types calendar.format serialize
io.streams.byte-array byte-arrays io.encodings.binary io.streams.byte-array byte-arrays io.encodings.binary
io.backend db.errors present urls io.encodings.utf8 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 IN: db.sqlite.lib
ERROR: sqlite-error < db-error n string ; ERROR: sqlite-error < db-error n string ;
@ -16,7 +17,7 @@ ERROR: sqlite-sql-error < sql-error n string ;
: sqlite-statement-error ( -- * ) : sqlite-statement-error ( -- * )
SQLITE_ERROR SQLITE_ERROR
db get handle>> sqlite3_errmsg sqlite-sql-error ; db-connection get handle>> sqlite3_errmsg sqlite-sql-error ;
: sqlite-check-result ( n -- ) : sqlite-check-result ( n -- )
{ {

View File

@ -42,7 +42,7 @@ M: sqlite-db-connection <prepared-statement> ( str in out -- obj )
: sqlite-maybe-prepare ( statement -- statement ) : sqlite-maybe-prepare ( statement -- statement )
dup handle>> [ dup handle>> [
db get handle>> over sql>> sqlite-prepare db-connection get handle>> over sql>> sqlite-prepare
>>handle >>handle
] unless ; ] unless ;
@ -99,7 +99,7 @@ M: sqlite-statement bind-tuple ( tuple statement -- )
ERROR: sqlite-last-id-fail ; ERROR: sqlite-last-id-fail ;
: last-insert-id ( -- id ) : 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 ; dup zero? [ sqlite-last-id-fail ] when ;
M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- ) M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- )

View File

@ -4,7 +4,7 @@ USING: io.files io.files.temp kernel tools.test db db.tuples classes
db.types continuations namespaces math math.ranges db.types continuations namespaces math math.ranges
prettyprint calendar sequences db.sqlite math.intervals prettyprint calendar sequences db.sqlite math.intervals
db.postgresql accessors random math.bitwise system 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 IN: db.tuples.tests
: sqlite-db ( -- sqlite-db ) : sqlite-db ( -- sqlite-db )
@ -33,10 +33,10 @@ IN: db.tuples.tests
! These words leak resources, but are useful for interactivel testing ! These words leak resources, but are useful for interactivel testing
: sqlite-test-db ( -- ) : sqlite-test-db ( -- )
sqlite-db db-open db set ; sqlite-db db-open db-connection set ;
: postgresql-test-db ( -- ) : 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 TUPLE: person the-id the-name the-number the-real
ts date time blob factor-blob url ; ts date time blob factor-blob url ;

View File

@ -3,20 +3,20 @@
USING: arrays assocs classes db kernel namespaces USING: arrays assocs classes db kernel namespaces
classes.tuple words sequences slots math accessors classes.tuple words sequences slots math accessors
math.parser io prettyprint db.types continuations math.parser io prettyprint db.types continuations
destructors mirrors sets db.types ; destructors mirrors sets db.types db.private ;
IN: db.tuples IN: db.tuples
HOOK: create-sql-statement db ( class -- object ) HOOK: create-sql-statement db-connection ( class -- object )
HOOK: drop-sql-statement db ( class -- object ) HOOK: drop-sql-statement db-connection ( class -- object )
HOOK: <insert-db-assigned-statement> db ( class -- object ) HOOK: <insert-db-assigned-statement> db-connection ( class -- object )
HOOK: <insert-user-assigned-statement> db ( class -- object ) HOOK: <insert-user-assigned-statement> db-connection ( class -- object )
HOOK: <update-tuple-statement> db ( class -- object ) HOOK: <update-tuple-statement> db-connection ( class -- object )
HOOK: <delete-tuples-statement> db ( tuple class -- object ) HOOK: <delete-tuples-statement> db-connection ( tuple class -- object )
HOOK: <select-by-slots-statement> db ( tuple class -- tuple ) HOOK: <select-by-slots-statement> db-connection ( tuple class -- tuple )
HOOK: <count-statement> db ( query -- statement ) HOOK: <count-statement> db-connection ( query -- statement )
HOOK: query>statement db ( query -- statement ) HOOK: query>statement db-connection ( query -- statement )
HOOK: insert-tuple-set-key db ( tuple statement -- ) HOOK: insert-tuple-set-key db-connection ( tuple statement -- )
<PRIVATE <PRIVATE
@ -52,12 +52,14 @@ GENERIC: eval-generator ( singleton -- object )
: insert-db-assigned-statement ( tuple -- ) : insert-db-assigned-statement ( tuple -- )
dup class dup class
db get insert-statements>> [ <insert-db-assigned-statement> ] cache db-connection get insert-statements>>
[ <insert-db-assigned-statement> ] cache
[ bind-tuple ] 2keep insert-tuple-set-key ; [ bind-tuple ] 2keep insert-tuple-set-key ;
: insert-user-assigned-statement ( tuple -- ) : insert-user-assigned-statement ( tuple -- )
dup class dup class
db get insert-statements>> [ <insert-user-assigned-statement> ] cache db-connection get insert-statements>>
[ <insert-user-assigned-statement> ] cache
[ bind-tuple ] keep execute-statement ; [ bind-tuple ] keep execute-statement ;
: do-select ( exemplar-tuple statement -- tuples ) : do-select ( exemplar-tuple statement -- tuples )
@ -117,7 +119,7 @@ M: tuple >query <query> swap >>tuple ;
: update-tuple ( tuple -- ) : update-tuple ( tuple -- )
dup class dup class
db get update-statements>> [ <update-tuple-statement> ] cache db-connection get update-statements>> [ <update-tuple-statement> ] cache
[ bind-tuple ] keep execute-statement ; [ bind-tuple ] keep execute-statement ;
: delete-tuples ( tuple -- ) : delete-tuples ( tuple -- )

View File

@ -4,11 +4,11 @@ USING: arrays assocs db kernel math math.parser
sequences continuations sequences.deep prettyprint sequences continuations sequences.deep prettyprint
words namespaces slots slots.private classes mirrors words namespaces slots slots.private classes mirrors
classes.tuple combinators calendar.format symbols classes.tuple combinators calendar.format symbols
classes.singleton accessors quotations random ; classes.singleton accessors quotations random db.private ;
IN: db.types IN: db.types
HOOK: persistent-table db ( -- hash ) HOOK: persistent-table db-connection ( -- hash )
HOOK: compound db ( string obj -- hash ) HOOK: compound db-connection ( string obj -- hash )
TUPLE: sql-spec class slot-name column-name type primary-key modifiers ; 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 modifiers>> [ lookup-modifier ] map " " join
[ "" ] [ " " prepend ] if-empty ; [ "" ] [ " " prepend ] if-empty ;
HOOK: bind% db ( spec -- ) HOOK: bind% db-connection ( spec -- )
HOOK: bind# db ( spec obj -- ) HOOK: bind# db-connection ( spec obj -- )
ERROR: no-column column ; ERROR: no-column column ;