1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs classes continuations destructors kernel math
4 namespaces sequences classes.tuple words strings
5 tools.walker accessors combinators fry db.errors ;
16 : new-db-connection ( class -- obj )
18 H{ } clone >>insert-statements
19 H{ } clone >>update-statements
20 H{ } clone >>delete-statements ; inline
24 GENERIC: db-open ( db -- db-connection )
25 HOOK: db-close db-connection ( handle -- )
26 HOOK: parse-db-error db-connection ( error -- error' )
28 : dispose-statements ( assoc -- ) values dispose-each ;
30 M: db-connection dispose ( db-connection -- )
32 [ dispose-statements H{ } clone ] change-insert-statements
33 [ dispose-statements H{ } clone ] change-update-statements
34 [ dispose-statements H{ } clone ] change-delete-statements
35 [ db-close f ] change-handle
39 TUPLE: result-set sql in-params out-params handle n max ;
41 GENERIC: query-results ( query -- result-set )
42 GENERIC: #rows ( result-set -- n )
43 GENERIC: #columns ( result-set -- n )
44 GENERIC# row-column 1 ( result-set column -- obj )
45 GENERIC# row-column-typed 1 ( result-set column -- sql )
46 GENERIC: advance-row ( result-set -- )
47 GENERIC: more-rows? ( result-set -- ? )
49 : init-result-set ( result-set -- )
53 : new-result-set ( query handle class -- result-set )
56 [ [ sql>> ] [ in-params>> ] [ out-params>> ] tri ] dip
61 TUPLE: statement handle sql in-params out-params bind-params bound? type retries ;
62 TUPLE: simple-statement < statement ;
63 TUPLE: prepared-statement < statement ;
65 : new-statement ( sql in out class -- statement )
71 HOOK: <simple-statement> db-connection ( string in out -- statement )
72 HOOK: <prepared-statement> db-connection ( string in out -- statement )
73 GENERIC: prepare-statement ( statement -- )
74 GENERIC: bind-statement* ( statement -- )
75 GENERIC: low-level-bind ( statement -- )
76 GENERIC: bind-tuple ( tuple statement -- )
78 GENERIC: execute-statement* ( statement type -- )
80 M: object execute-statement* ( statement type -- )
82 _ _ drop query-results dispose
84 parse-db-error rethrow
87 : execute-one-statement ( statement -- )
88 dup type>> execute-statement* ;
90 : execute-statement ( statement -- )
92 [ execute-one-statement ] each
97 : bind-statement ( obj statement -- )
99 [ bind-statement* ] keep
102 : sql-row ( result-set -- seq )
103 dup #columns [ row-column ] with map ;
105 : sql-row-typed ( result-set -- seq )
106 dup #columns [ row-column-typed ] with map ;
108 : query-each ( statement quot: ( statement -- ) -- )
110 [ call ] 2keep over advance-row query-each
113 ] if ; inline recursive
115 : query-map ( statement quot -- seq )
116 accumulator [ query-each ] dip { } like ; inline
118 : with-db ( db quot -- )
119 [ db-open db-connection ] dip
120 '[ db-connection get [ drop @ ] with-disposal ] with-variable ; inline
122 ! Words for working with raw SQL statements
123 : default-query ( query -- result-set )
124 query-results [ [ sql-row ] query-map ] with-disposal ;
126 : sql-query ( sql -- rows )
127 f f <simple-statement> [ default-query ] with-disposal ;
129 : (sql-command) ( string -- )
130 f f <simple-statement> [ execute-statement ] with-disposal ;
132 : sql-command ( sql -- )
133 dup string? [ (sql-command) ] [ [ (sql-command) ] each ] if ;
136 SYMBOL: in-transaction
138 HOOK: begin-transaction db-connection ( -- )
139 HOOK: commit-transaction db-connection ( -- )
140 HOOK: rollback-transaction db-connection ( -- )
142 M: db-connection begin-transaction ( -- ) "BEGIN" sql-command ;
143 M: db-connection commit-transaction ( -- ) "COMMIT" sql-command ;
144 M: db-connection rollback-transaction ( -- ) "ROLLBACK" sql-command ;
146 : in-transaction? ( -- ? ) in-transaction get ;
148 : with-transaction ( quot -- )
151 [ ] [ rollback-transaction ] cleanup commit-transaction