From ee30ab92cd14bfd7e134c25afbbd120ea6f57bb8 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 11 Apr 2009 13:02:47 -0500 Subject: [PATCH] redoing db framework. it'll live in extra until it can replace db in basis --- extra/db2/authors.txt | 1 + extra/db2/connections/authors.txt | 1 + extra/db2/connections/connections.factor | 17 +++++++++ extra/db2/db2.factor | 20 +++++++++++ extra/db2/result-sets/authors.txt | 1 + extra/db2/result-sets/result-sets.factor | 29 ++++++++++++++++ extra/db2/statements/authors.txt | 1 + extra/db2/statements/statements.factor | 40 ++++++++++++++++++++++ extra/db2/transactions/authors.txt | 1 + extra/db2/transactions/transactions.factor | 23 +++++++++++++ 10 files changed, 134 insertions(+) create mode 100644 extra/db2/authors.txt create mode 100644 extra/db2/connections/authors.txt create mode 100644 extra/db2/connections/connections.factor create mode 100644 extra/db2/db2.factor create mode 100644 extra/db2/result-sets/authors.txt create mode 100644 extra/db2/result-sets/result-sets.factor create mode 100644 extra/db2/statements/authors.txt create mode 100644 extra/db2/statements/statements.factor create mode 100644 extra/db2/transactions/authors.txt create mode 100644 extra/db2/transactions/transactions.factor diff --git a/extra/db2/authors.txt b/extra/db2/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/connections/authors.txt b/extra/db2/connections/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/connections/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/connections/connections.factor b/extra/db2/connections/connections.factor new file mode 100644 index 0000000000..c622f9a4b4 --- /dev/null +++ b/extra/db2/connections/connections.factor @@ -0,0 +1,17 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors destructors fry kernel namespaces ; +IN: db2.connections + +TUPLE: db-connection handle ; + +GENERIC: db-open ( db -- db-connection ) +HOOK: db-close db-connection ( handle -- ) +HOOK: parse-db-error db-connection ( error -- error' ) + +M: db-connection dispose ( db-connection -- ) + [ db-close f ] change-handle drop ; + +: with-db ( db quot -- ) + [ db-open db-connection dup ] dip + '[ _ [ drop @ ] with-disposal ] with-variable ; inline diff --git a/extra/db2/db2.factor b/extra/db2/db2.factor new file mode 100644 index 0000000000..16afbd2782 --- /dev/null +++ b/extra/db2/db2.factor @@ -0,0 +1,20 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors continuations destructors fry kernel +namespaces sequences strings db2.statements ; +IN: db2 + + [ execute-statement ] with-disposal ; + +PRIVATE> + +: sql-command ( sql -- ) + dup string? + [ execute-sql-string ] + [ [ execute-sql-string ] each ] if ; + +: sql-query ( sql -- sequence ) + f f [ statement>result-sequence ] with-disposal ; diff --git a/extra/db2/result-sets/authors.txt b/extra/db2/result-sets/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/result-sets/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/result-sets/result-sets.factor b/extra/db2/result-sets/result-sets.factor new file mode 100644 index 0000000000..0f242da473 --- /dev/null +++ b/extra/db2/result-sets/result-sets.factor @@ -0,0 +1,29 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors kernel sequences ; +IN: db2.result-sets + +TUPLE: result-set sql in out handle n max ; + +GENERIC: #rows ( result-set -- n ) +GENERIC: #columns ( result-set -- n ) +GENERIC: advance-row ( result-set -- ) +GENERIC: more-rows? ( result-set -- ? ) +GENERIC# column 1 ( result-set column -- obj ) +GENERIC# column-typed 1 ( result-set column -- sql ) + +: init-result-set ( result-set -- result-set ) + dup #rows >>max + 0 >>n ; + +: new-result-set ( query handle class -- result-set ) + new + swap >>handle + swap [ sql>> >>sql ] [ in>> >>in ] [ out>> >>out ] tri ; + +: sql-row ( result-set -- seq ) + dup #columns [ column ] with map ; + +: sql-row-typed ( result-set -- seq ) + dup #columns [ column-typed ] with map ; + diff --git a/extra/db2/statements/authors.txt b/extra/db2/statements/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/statements/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/statements/statements.factor b/extra/db2/statements/statements.factor new file mode 100644 index 0000000000..282fb7d5bf --- /dev/null +++ b/extra/db2/statements/statements.factor @@ -0,0 +1,40 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors continuations destructors fry kernel +sequences db2.result-sets db2.connections ; +IN: db2.statements + +TUPLE: statement handle sql in out ; + +: new-statement ( sql in out class -- statement ) + new + swap >>out + swap >>in + swap >>sql ; + +HOOK: db-connection ( sql in out -- statement ) +GENERIC: execute-statement* ( statement type -- ) +GENERIC: statement>result-set ( statement -- result-set ) + +M: object execute-statement* ( statement type -- ) + drop '[ _ statement>result-set dispose ] + [ parse-db-error rethrow ] recover ; + +: execute-one-statement ( statement -- ) + dup type>> execute-statement* ; + +: execute-statement ( statement -- ) + dup sequence? + [ [ execute-one-statement ] each ] + [ execute-one-statement ] if ; + +: statement-each ( statement quot: ( statement -- ) -- ) + over more-rows? + [ [ call ] 2keep over advance-row statement-each ] + [ 2drop ] if ; inline recursive + +: statement-map ( statement quot -- sequence ) + accumulator [ statement-each ] dip { } like ; inline + +: statement>result-sequence ( statement -- sequence ) + statement>result-set [ [ sql-row ] statement-map ] with-disposal ; diff --git a/extra/db2/transactions/authors.txt b/extra/db2/transactions/authors.txt new file mode 100644 index 0000000000..b4bd0e7b35 --- /dev/null +++ b/extra/db2/transactions/authors.txt @@ -0,0 +1 @@ +Doug Coleman \ No newline at end of file diff --git a/extra/db2/transactions/transactions.factor b/extra/db2/transactions/transactions.factor new file mode 100644 index 0000000000..081c8dbe57 --- /dev/null +++ b/extra/db2/transactions/transactions.factor @@ -0,0 +1,23 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: db2 ; +IN: db2.transactions + +! Transactions +SYMBOL: in-transaction + +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 ; +M: db-connection rollback-transaction ( -- ) "ROLLBACK" sql-command ; + +: in-transaction? ( -- ? ) in-transaction get ; + +: with-transaction ( quot -- ) + t in-transaction [ + begin-transaction + [ ] [ rollback-transaction ] cleanup commit-transaction + ] with-variable ; inline