diff --git a/extra/db/db.factor b/extra/db/db.factor index 170d9a60f1..309847209f 100755 --- a/extra/db/db.factor +++ b/extra/db/db.factor @@ -20,8 +20,7 @@ GENERIC: db-open ( db -- ) HOOK: db-close db ( handle -- ) : make-db ( seq class -- db ) construct-empty make-db* ; -: dispose-statements ( seq -- ) - [ dispose drop ] assoc-each ; +: dispose-statements ( seq -- ) [ dispose drop ] assoc-each ; : dispose-db ( db -- ) dup db [ @@ -46,8 +45,8 @@ GENERIC: bind-tuple ( tuple statement -- ) GENERIC: query-results ( query -- result-set ) GENERIC: #rows ( result-set -- n ) GENERIC: #columns ( result-set -- n ) -GENERIC# row-column 1 ( result-set n -- obj ) -GENERIC# row-column-typed 1 ( result-set n -- sql ) +GENERIC# row-column 1 ( result-set column -- obj ) +GENERIC# row-column-typed 1 ( result-set column -- sql ) GENERIC: advance-row ( result-set -- ) GENERIC: more-rows? ( result-set -- ? ) diff --git a/extra/db/postgresql/lib/lib.factor b/extra/db/postgresql/lib/lib.factor index 25b3a6d2cf..d584632609 100755 --- a/extra/db/postgresql/lib/lib.factor +++ b/extra/db/postgresql/lib/lib.factor @@ -2,7 +2,8 @@ ! See http://factorcode.org/license.txt for BSD license. USING: arrays continuations db io kernel math namespaces quotations sequences db.postgresql.ffi alien alien.c-types -db.types tools.walker ascii splitting ; +db.types tools.walker ascii splitting math.parser +combinators combinators.cleave ; IN: db.postgresql.lib : postgresql-result-error-message ( res -- str/f ) @@ -48,3 +49,22 @@ IN: db.postgresql.lib dup postgresql-result-ok? [ dup postgresql-result-error-message swap PQclear throw ] unless ; + +: postgresql-column-typed ( handle row column type -- obj ) + dup array? [ first ] when + { + { +native-id+ [ ] } + { INTEGER [ PQgetvalue string>number ] } + { BIG-INTEGER [ PQgetvalue string>number ] } + { DOUBLE [ PQgetvalue string>number ] } + { TEXT [ PQgetvalue ] } + { VARCHAR [ PQgetvalue ] } + { DATE [ PQgetvalue ] } + { TIME [ PQgetvalue ] } + { TIMESTAMP [ PQgetvalue ] } + { DATETIME [ PQgetvalue ] } + { BLOB [ [ PQgetvalue ] 3keep PQgetlength ] } + { FACTOR-BLOB [ [ PQgetvalue ] 3keep PQgetlength ] } + [ no-sql-type ] + } case ; + ! PQgetlength PQgetisnull \ No newline at end of file diff --git a/extra/db/postgresql/postgresql.factor b/extra/db/postgresql/postgresql.factor index 9383a9290c..2c234ec419 100755 --- a/extra/db/postgresql/postgresql.factor +++ b/extra/db/postgresql/postgresql.factor @@ -4,7 +4,8 @@ USING: arrays assocs alien alien.syntax continuations io kernel math math.parser namespaces prettyprint quotations sequences debugger db db.postgresql.lib db.postgresql.ffi db.tuples db.types tools.annotations math.ranges -combinators sequences.lib classes locals words tools.walker ; +combinators sequences.lib classes locals words tools.walker +combinators.cleave namespaces.lib ; IN: db.postgresql TUPLE: postgresql-db host port pgopts pgtty db user pass ; @@ -53,11 +54,12 @@ M: postgresql-result-set #rows ( result-set -- n ) M: postgresql-result-set #columns ( result-set -- n ) result-set-handle PQnfields ; -M: postgresql-result-set row-column ( result-set n -- obj ) +M: postgresql-result-set row-column ( result-set column -- obj ) >r dup result-set-handle swap result-set-n r> PQgetvalue ; -M: postgresql-result-set row-column-typed ( result-set n type -- obj ) - >r row-column r> sql-type>factor-type ; +M: postgresql-result-set row-column-typed ( result-set column -- obj ) + dup pick result-set-out-params nth sql-spec-type + >r >r [ result-set-handle ] [ result-set-n ] bi r> r> postgresql-column-typed ; M: postgresql-statement query-results ( query -- result-set ) dup statement-bind-params [ diff --git a/extra/db/sqlite/lib/lib.factor b/extra/db/sqlite/lib/lib.factor index 9bf9ede895..d62bd43483 100755 --- a/extra/db/sqlite/lib/lib.factor +++ b/extra/db/sqlite/lib/lib.factor @@ -127,9 +127,9 @@ IN: db.sqlite.lib { +native-id+ [ sqlite3_column_int64 ] } { INTEGER [ sqlite3_column_int ] } { BIG-INTEGER [ sqlite3_column_int64 ] } + { DOUBLE [ sqlite3_column_double ] } { TEXT [ sqlite3_column_text ] } { VARCHAR [ sqlite3_column_text ] } - { DOUBLE [ sqlite3_column_double ] } { DATE [ sqlite3_column_text dup [ ymd>timestamp ] when ] } { TIME [ sqlite3_column_text dup [ hms>timestamp ] when ] } { TIMESTAMP [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] }