fix a couple of minor bugs before major overhaul

db4
Doug Coleman 2008-02-21 15:57:18 -06:00
parent 8f208cbaef
commit d1e5fddbed
7 changed files with 60 additions and 48 deletions

View File

@ -23,12 +23,12 @@ HOOK: db-close db ( handle -- )
db-handle db-close db-handle db-close
] with-variable ; ] with-variable ;
TUPLE: statement handle sql slot-names bound? in-params out-params ; TUPLE: statement handle sql bound? in-params out-params ;
TUPLE: simple-statement ; TUPLE: simple-statement ;
TUPLE: prepared-statement ; TUPLE: prepared-statement ;
HOOK: <simple-statement> db ( str -- statement ) HOOK: <simple-statement> db ( str -- statement )
HOOK: <prepared-statement> db ( str slot-names -- statement ) HOOK: <prepared-statement> db ( str -- statement )
GENERIC: prepare-statement ( statement -- ) GENERIC: prepare-statement ( statement -- )
GENERIC: bind-statement* ( obj statement -- ) GENERIC: bind-statement* ( obj statement -- )
GENERIC: reset-statement ( statement -- ) GENERIC: reset-statement ( statement -- )

View File

@ -4,7 +4,7 @@ USING: arrays assocs alien alien.syntax continuations io
kernel math math.parser namespaces prettyprint quotations kernel math math.parser namespaces prettyprint quotations
sequences debugger db db.postgresql.lib db.postgresql.ffi sequences debugger db db.postgresql.lib db.postgresql.ffi
db.tuples db.types tools.annotations math.ranges db.tuples db.types tools.annotations math.ranges
combinators sequences.lib classes locals words ; combinators sequences.lib classes locals words tools.walker ;
IN: db.postgresql IN: db.postgresql
TUPLE: postgresql-db host port pgopts pgtty db user pass ; TUPLE: postgresql-db host port pgopts pgtty db user pass ;
@ -65,6 +65,7 @@ M: postgresql-result-set sql-type>factor-type ( obj type -- newobj )
} case ; } case ;
M: postgresql-statement insert-statement ( statement -- id ) M: postgresql-statement insert-statement ( statement -- id )
break
query-results [ 0 row-column ] with-disposal string>number ; query-results [ 0 row-column ] with-disposal string>number ;
M: postgresql-statement query-results ( query -- result-set ) M: postgresql-statement query-results ( query -- result-set )
@ -104,10 +105,13 @@ M: postgresql-db <simple-statement> ( sql -- statement )
{ set-statement-sql } statement construct { set-statement-sql } statement construct
<postgresql-statement> ; <postgresql-statement> ;
M: postgresql-db <prepared-statement> ( pair -- statement ) M: postgresql-db <prepared-statement> ( triple -- statement )
?first2 ?first3
{ set-statement-sql set-statement-slot-names } {
statement construct <postgresql-statement> ; set-statement-sql
set-statement-in-params
set-statement-out-params
} statement construct <postgresql-statement> ;
M: postgresql-db begin-transaction ( -- ) M: postgresql-db begin-transaction ( -- )
"BEGIN" sql-command ; "BEGIN" sql-command ;
@ -166,6 +170,7 @@ SYMBOL: postgresql-counter
: drop-function-sql ( specs table -- sql ) : drop-function-sql ( specs table -- sql )
[ [
break
"drop function add_" % % "drop function add_" % %
"(" % "(" %
remove-id remove-id
@ -215,8 +220,8 @@ M: postgresql-db drop-sql ( specs table -- seq )
] postgresql-make ; ] postgresql-make ;
M: postgresql-db insert-sql* ( specs table -- sql in-specs out-specs ) M: postgresql-db insert-sql* ( specs table -- sql in-specs out-specs )
over find-primary-key native-id? dup class db-columns find-primary-key native-id?
[ insert-function-sql ] [ insert-table-sql ] if ; [ insert-function-sql ] [ insert-table-sql ] if 3array ;
M: postgresql-db update-sql* ( specs table -- sql in-specs out-specs ) M: postgresql-db update-sql* ( specs table -- sql in-specs out-specs )
[ [
@ -228,7 +233,7 @@ M: postgresql-db update-sql* ( specs table -- sql in-specs out-specs )
" where " 0% " where " 0%
find-primary-key find-primary-key
dup sql-spec-column-name 0% " = " 0% bind% dup sql-spec-column-name 0% " = " 0% bind%
] postgresql-make ; ] postgresql-make 3array ;
M: postgresql-db delete-sql* ( specs table -- sql in-specs out-specs ) M: postgresql-db delete-sql* ( specs table -- sql in-specs out-specs )
[ [
@ -236,7 +241,7 @@ M: postgresql-db delete-sql* ( specs table -- sql in-specs out-specs )
" where " 0% " where " 0%
find-primary-key find-primary-key
dup sql-spec-column-name 0% " = " 0% bind% dup sql-spec-column-name 0% " = " 0% bind%
] postgresql-make ; ] postgresql-make 3array ;
: select-by-slots-sql ( tuple -- sql in-specs out-specs ) : select-by-slots-sql ( tuple -- sql in-specs out-specs )
[ [
@ -251,7 +256,7 @@ M: postgresql-db delete-sql* ( specs table -- sql in-specs out-specs )
[ ", " 0% ] [ ", " 0% ]
[ dup sql-spec-column-name 0% " = " 0% bind% ] interleave [ dup sql-spec-column-name 0% " = " 0% bind% ] interleave
";" 0% ";" 0%
] postgresql-make ; ] postgresql-make 3array ;
! : select-with-relations ( tuple -- sql in-specs out-specs ) ! : select-with-relations ( tuple -- sql in-specs out-specs )
@ -259,7 +264,7 @@ M: postgresql-db select-sql ( tuple -- sql in-specs out-specs )
select-by-slots-sql ; select-by-slots-sql ;
M: postgresql-db tuple>params ( specs tuple -- obj ) M: postgresql-db tuple>params ( specs tuple -- obj )
[ >r dup third swap first r> get-slot-named swap ] [ >r dup sql-spec-type swap sql-spec-slot-name r> get-slot-named swap ]
curry { } map>assoc ; curry { } map>assoc ;
M: postgresql-db type-table ( -- hash ) M: postgresql-db type-table ( -- hash )
@ -268,6 +273,7 @@ M: postgresql-db type-table ( -- hash )
{ TEXT "text" } { TEXT "text" }
{ VARCHAR "varchar" } { VARCHAR "varchar" }
{ INTEGER "integer" } { INTEGER "integer" }
{ DOUBLE "real" }
{ TIMESTAMP "timestamp" } { TIMESTAMP "timestamp" }
} ; } ;
@ -278,12 +284,13 @@ M: postgresql-db create-type-table ( -- hash )
: postgresql-compound ( str n -- newstr ) : postgresql-compound ( str n -- newstr )
over { over {
{ "varchar" [ first number>string join-space ] } { "default" [ first number>string join-space ] }
{ "references" { "varchar" [ first number>string paren append ] }
[ { "references" [
first2 >r [ unparse join-space ] keep db-columns r> first2 >r [ unparse join-space ] keep db-columns r>
swap [ sql-spec-slot-name = ] with find nip sql-spec-column-name paren append swap [ sql-spec-slot-name = ] with find nip
] } sql-spec-column-name paren append
] }
[ "no compound found" 3array throw ] [ "no compound found" 3array throw ]
} case ; } case ;

View File

@ -79,6 +79,7 @@ IN: db.sqlite.lib
{ VARCHAR [ sqlite-bind-text-by-name ] } { VARCHAR [ sqlite-bind-text-by-name ] }
{ DOUBLE [ sqlite-bind-double-by-name ] } { DOUBLE [ sqlite-bind-double-by-name ] }
{ TIMESTAMP [ sqlite-bind-double-by-name ] } { TIMESTAMP [ sqlite-bind-double-by-name ] }
{ +native-id+ [ sqlite-bind-int-by-name ] }
! { NULL [ sqlite-bind-null-by-name ] } ! { NULL [ sqlite-bind-null-by-name ] }
[ no-sql-type ] [ no-sql-type ]
} case ; } case ;

View File

@ -4,7 +4,7 @@ USING: alien arrays assocs classes compiler db
hashtables io.files kernel math math.parser namespaces hashtables io.files kernel math math.parser namespaces
prettyprint sequences strings tuples alien.c-types prettyprint sequences strings tuples alien.c-types
continuations db.sqlite.lib db.sqlite.ffi db.tuples continuations db.sqlite.lib db.sqlite.ffi db.tuples
words combinators.lib db.types combinators ; words combinators.lib db.types combinators tools.walker ;
IN: db.sqlite IN: db.sqlite
TUPLE: sqlite-db path ; TUPLE: sqlite-db path ;
@ -99,7 +99,7 @@ M: sqlite-db create-sql ( specs table -- sql )
M: sqlite-db drop-sql ( specs table -- sql ) M: sqlite-db drop-sql ( specs table -- sql )
[ [
"drop table " % % ";" % "drop table " % % ";" % drop
] "" make ; ] "" make ;
M: sqlite-db insert-sql* ( specs table -- sql ) M: sqlite-db insert-sql* ( specs table -- sql )
@ -161,9 +161,9 @@ M: sqlite-db select-sql ( tuple -- sql )
M: sqlite-db tuple>params ( specs tuple -- obj ) M: sqlite-db tuple>params ( specs tuple -- obj )
[ [
>r [ second ":" swap append ] keep r> >r [ sql-spec-column-name ":" swap append ] keep r>
dupd >r first r> get-slot-named swap dupd >r sql-spec-slot-name r> get-slot-named swap
third 3array sql-spec-type 3array
] curry map ; ] curry map ;
M: sqlite-db modifier-table ( -- hashtable ) M: sqlite-db modifier-table ( -- hashtable )

View File

@ -2,19 +2,18 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: io.files kernel tools.test db db.sqlite db.tuples USING: io.files kernel tools.test db db.sqlite db.tuples
db.types continuations namespaces db.postgresql math db.types continuations namespaces db.postgresql math
prettyprint ; prettyprint tools.walker ;
! tools.time ;
IN: temporary IN: temporary
TUPLE: person the-id the-name the-number real ; TUPLE: person the-id the-name the-number the-real ;
: <person> ( name age real -- person ) : <person> ( name age real -- person )
{ {
set-person-the-name set-person-the-name
set-person-the-number set-person-the-number
set-person-real set-person-the-real
} person construct ; } person construct ;
: <assigned-person> ( id name number real -- obj ) : <assigned-person> ( id name number the-real -- obj )
<person> [ set-person-the-id ] keep ; <person> [ set-person-the-id ] keep ;
SYMBOL: the-person SYMBOL: the-person
@ -31,8 +30,10 @@ SYMBOL: the-person
[ ] [ the-person get update-tuple ] unit-test [ ] [ the-person get update-tuple ] unit-test
! T{ person f f f 200 f } select-tuples
[ ] [ the-person get delete-tuple ] unit-test [ ] [ the-person get delete-tuple ] unit-test
; ! 1 [ ] [ person drop-table ] unit-test ; [ ] [ person drop-table ] unit-test ;
: test-sqlite ( -- ) : test-sqlite ( -- )
"tuples-test.db" resource-path <sqlite-db> [ "tuples-test.db" resource-path <sqlite-db> [
@ -49,20 +50,20 @@ person "PERSON"
{ "the-id" "ID" +native-id+ } { "the-id" "ID" +native-id+ }
{ "the-name" "NAME" { VARCHAR 256 } +not-null+ } { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
{ "the-number" "AGE" INTEGER { +default+ 0 } } { "the-number" "AGE" INTEGER { +default+ 0 } }
{ "real" "REAL" DOUBLE { +default+ 0.3 } } { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
} define-persistent } define-persistent
"billy" 10 3.14 <person> the-person set "billy" 10 3.14 <person> the-person set
test-sqlite ! test-sqlite
! test-postgresql test-postgresql
! person "PERSON" ! person "PERSON"
! { ! {
! { "the-id" "ID" INTEGER +assigned-id+ } ! { "the-id" "ID" INTEGER +assigned-id+ }
! { "the-name" "NAME" { VARCHAR 256 } +not-null+ } ! { "the-name" "NAME" { VARCHAR 256 } +not-null+ }
! { "the-number" "AGE" INTEGER { +default+ 0 } } ! { "the-number" "AGE" INTEGER { +default+ 0 } }
! { "real" "REAL" DOUBLE { +default+ 0.3 } } ! { "the-real" "REAL" DOUBLE { +default+ 0.3 } }
! } define-persistent ! } define-persistent
! 1 "billy" 20 6.28 <assigned-person> the-person set ! 1 "billy" 20 6.28 <assigned-person> the-person set
@ -95,11 +96,11 @@ annotation "ANNOTATION"
{ "contents" "CONTENTS" TEXT } { "contents" "CONTENTS" TEXT }
} define-persistent } define-persistent
"localhost" "postgres" "" "factor-test" <postgresql-db> [ ! "localhost" "postgres" "" "factor-test" <postgresql-db> [
[ paste drop-table ] [ drop ] recover ! [ paste drop-table ] [ drop ] recover
[ annotation drop-table ] [ drop ] recover ! [ annotation drop-table ] [ drop ] recover
[ paste drop-table ] [ drop ] recover ! [ paste drop-table ] [ drop ] recover
[ annotation drop-table ] [ drop ] recover ! [ annotation drop-table ] [ drop ] recover
paste create-table ! [ ] [ paste create-table ] unit-test
annotation create-table ! [ ] [ annotation create-table ] unit-test
] with-db ! ] with-db

View File

@ -3,7 +3,7 @@
USING: arrays assocs classes db kernel namespaces USING: arrays assocs classes db kernel namespaces
tuples words sequences slots slots.private math tuples words sequences slots slots.private math
math.parser io prettyprint db.types continuations math.parser io prettyprint db.types continuations
mirrors sequences.lib ; mirrors sequences.lib tools.walker ;
IN: db.tuples IN: db.tuples
: db-table ( class -- obj ) "db-table" word-prop ; : db-table ( class -- obj ) "db-table" word-prop ;
@ -33,7 +33,7 @@ TUPLE: no-slot-named ;
dup class primary-key-spec get-slot-named ; dup class primary-key-spec get-slot-named ;
: set-primary-key ( obj tuple -- ) : set-primary-key ( obj tuple -- )
[ class primary-key-spec first ] keep [ class primary-key-spec sql-spec-slot-name ] keep
set-slot-named ; set-slot-named ;
: cache-statement ( columns class assoc quot -- statement ) : cache-statement ( columns class assoc quot -- statement )
@ -92,7 +92,7 @@ HOOK: tuple>params db ( columns tuple -- obj )
: delete-tuple ( tuple -- ) : delete-tuple ( tuple -- )
[ [ primary-key? ] subset ] [ delete-sql ] do-tuple-statement ; [ [ primary-key? ] subset ] [ delete-sql ] do-tuple-statement ;
: select-tuple ( tuple -- ) : select-tuples ( tuple -- )
[ select-sql ] keep do-query ; [ select-sql ] keep do-query ;
: persist ( tuple -- ) : persist ( tuple -- )

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs db kernel math math.parser USING: arrays assocs db kernel math math.parser
sequences continuations sequences.deep sequences.lib sequences continuations sequences.deep sequences.lib
words namespaces ; words namespaces tools.walker ;
IN: db.types IN: db.types
TUPLE: sql-spec slot-name column-name type modifiers primary-key ; TUPLE: sql-spec slot-name column-name type modifiers primary-key ;
@ -12,15 +12,18 @@ SYMBOL: +native-id+
! +assigned-id+ can only be a modifier ! +assigned-id+ can only be a modifier
SYMBOL: +assigned-id+ SYMBOL: +assigned-id+
: primary-key? ( obj -- ? ) : (primary-key?) ( obj -- ? )
{ +native-id+ +assigned-id+ } member? ; { +native-id+ +assigned-id+ } member? ;
: primary-key? ( spec -- ? )
sql-spec-primary-key (primary-key?) ;
: normalize-spec ( spec -- ) : normalize-spec ( spec -- )
dup sql-spec-type dup primary-key? [ dup sql-spec-type dup (primary-key?) [
swap set-sql-spec-primary-key swap set-sql-spec-primary-key
] [ ] [
drop dup sql-spec-modifiers [ drop dup sql-spec-modifiers [
primary-key? (primary-key?)
] deep-find ] deep-find
[ swap set-sql-spec-primary-key ] [ drop ] if* [ swap set-sql-spec-primary-key ] [ drop ] if*
] if ; ] if ;