]> gitweb.factorcode.org Git - factor.git/blob - basis/db/db.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / db / db.factor
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 ;
6 IN: db
7
8 <PRIVATE
9
10 TUPLE: db-connection
11     handle
12     insert-statements
13     update-statements
14     delete-statements ;
15
16 : new-db-connection ( class -- obj )
17     new
18         H{ } clone >>insert-statements
19         H{ } clone >>update-statements
20         H{ } clone >>delete-statements ; inline
21
22 PRIVATE>
23
24 GENERIC: db-open ( db -- db-connection )
25 HOOK: db-close db-connection ( handle -- )
26
27 : dispose-statements ( assoc -- ) values dispose-each ;
28
29 M: db-connection dispose ( db-connection -- ) 
30     dup db-connection [
31         [ dispose-statements H{ } clone ] change-insert-statements
32         [ dispose-statements H{ } clone ] change-update-statements
33         [ dispose-statements H{ } clone ] change-delete-statements
34         [ db-close f ] change-handle
35         drop
36     ] with-variable ;
37
38 TUPLE: result-set sql in-params out-params handle n max ;
39
40 GENERIC: query-results ( query -- result-set )
41 GENERIC: #rows ( result-set -- n )
42 GENERIC: #columns ( result-set -- n )
43 GENERIC# row-column 1 ( result-set column -- obj )
44 GENERIC# row-column-typed 1 ( result-set column -- sql )
45 GENERIC: advance-row ( result-set -- )
46 GENERIC: more-rows? ( result-set -- ? )
47
48 : init-result-set ( result-set -- )
49     dup #rows >>max
50     0 >>n drop ;
51
52 : new-result-set ( query handle class -- result-set )
53     new
54         swap >>handle
55         [ [ sql>> ] [ in-params>> ] [ out-params>> ] tri ] dip
56         swap >>out-params
57         swap >>in-params
58         swap >>sql ;
59
60 TUPLE: statement handle sql in-params out-params bind-params bound? type retries ;
61 TUPLE: simple-statement < statement ;
62 TUPLE: prepared-statement < statement ;
63
64 : new-statement ( sql in out class -- statement )
65     new
66         swap >>out-params
67         swap >>in-params
68         swap >>sql ;
69
70 HOOK: <simple-statement> db-connection ( string in out -- statement )
71 HOOK: <prepared-statement> db-connection ( string in out -- statement )
72 GENERIC: prepare-statement ( statement -- )
73 GENERIC: bind-statement* ( statement -- )
74 GENERIC: low-level-bind ( statement -- )
75 GENERIC: bind-tuple ( tuple statement -- )
76
77 GENERIC: execute-statement* ( statement type -- )
78
79 M: object execute-statement* ( statement type -- )
80     drop query-results dispose ;
81
82 : execute-one-statement ( statement -- )
83     dup type>> execute-statement* ;
84
85 : execute-statement ( statement -- )
86     dup sequence? [
87         [ execute-one-statement ] each
88     ] [
89         execute-one-statement
90     ] if ;
91
92 : bind-statement ( obj statement -- )
93     swap >>bind-params
94     [ bind-statement* ] keep
95     t >>bound? drop ;
96
97 : sql-row ( result-set -- seq )
98     dup #columns [ row-column ] with map ;
99
100 : sql-row-typed ( result-set -- seq )
101     dup #columns [ row-column-typed ] with map ;
102
103 : query-each ( statement quot: ( statement -- ) -- )
104     over more-rows? [
105         [ call ] 2keep over advance-row query-each
106     ] [
107         2drop
108     ] if ; inline recursive
109
110 : query-map ( statement quot -- seq )
111     accumulator [ query-each ] dip { } like ; inline
112
113 : with-db ( db quot -- )
114     [ db-open db-connection ] dip
115     '[ db-connection get [ drop @ ] with-disposal ] with-variable ; inline
116
117 ! Words for working with raw SQL statements
118 : default-query ( query -- result-set )
119     query-results [ [ sql-row ] query-map ] with-disposal ;
120
121 : sql-query ( sql -- rows )
122     f f <simple-statement> [ default-query ] with-disposal ;
123
124 : (sql-command) ( string -- )
125     f f <simple-statement> [ execute-statement ] with-disposal ;
126
127 : sql-command ( sql -- )
128     dup string? [ (sql-command) ] [ [ (sql-command) ] each ] if ;
129
130 ! Transactions
131 SYMBOL: in-transaction
132
133 HOOK: begin-transaction db-connection ( -- )
134 HOOK: commit-transaction db-connection ( -- )
135 HOOK: rollback-transaction db-connection ( -- )
136
137 M: db-connection begin-transaction ( -- ) "BEGIN" sql-command ;
138 M: db-connection commit-transaction ( -- ) "COMMIT" sql-command ;
139 M: db-connection rollback-transaction ( -- ) "ROLLBACK" sql-command ;
140
141 : in-transaction? ( -- ? ) in-transaction get ;
142
143 : with-transaction ( quot -- )
144     t in-transaction [
145         begin-transaction
146         [ ] [ rollback-transaction ] cleanup commit-transaction
147     ] with-variable ;