]> gitweb.factorcode.org Git - factor.git/blob - basis/db/sqlite/lib/lib.factor
3c54990096b8fa7522b0ec0928be84f2e307f859
[factor.git] / basis / db / sqlite / lib / lib.factor
1 ! Copyright (C) 2008 Chris Double, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays calendar.format
4 calendar.parser combinators db db.errors db.sqlite.errors
5 db.sqlite.ffi db.types io.backend io.encodings.string
6 io.encodings.utf8 kernel math namespaces present sequences
7 sequences.extras serialize urls ;
8 IN: db.sqlite.lib
9
10 : sqlite-compile-options ( -- seq )
11     [ sqlite3_compileoption_get ] zero-loop>array ;
12
13 ERROR: sqlite-error < db-error n string ;
14
15 : sqlite-other-error ( n -- * )
16     dup sqlite-error-messages nth sqlite-error ;
17
18 : sqlite-statement-error ( -- * )
19     db-connection get handle>> sqlite3_errmsg
20     parse-sqlite-sql-error throw ;
21
22 : sqlite-check-result ( n -- )
23     {
24         { SQLITE_OK [ ] }
25         { SQLITE_ERROR [ sqlite-statement-error ] }
26         [ sqlite-other-error ]
27     } case ;
28
29 : sqlite-open ( path -- db )
30     normalize-path
31     { void* } [ sqlite3_open sqlite-check-result ]
32     with-out-parameters ;
33
34 : sqlite-close ( db -- )
35     sqlite3_close sqlite-check-result ;
36
37 : sqlite-prepare ( db sql -- handle )
38     utf8 encode dup length
39     { void* void* }
40     [ sqlite3_prepare_v2 sqlite-check-result ]
41     with-out-parameters drop ;
42
43 : sqlite-bind-parameter-index ( handle name -- index )
44     sqlite3_bind_parameter_index ;
45
46 : parameter-index ( handle name text -- handle name text )
47     [ dupd sqlite-bind-parameter-index ] dip ;
48
49 : sqlite-bind-text ( handle index text -- )
50     utf8 encode dup length SQLITE_TRANSIENT
51     sqlite3_bind_text sqlite-check-result ;
52
53 : sqlite-bind-int ( handle i n -- )
54     sqlite3_bind_int sqlite-check-result ;
55
56 : sqlite-bind-int64 ( handle i n -- )
57     sqlite3_bind_int64 sqlite-check-result ;
58
59 : sqlite-bind-uint64 ( handle i n -- )
60     sqlite3-bind-uint64 sqlite-check-result ;
61
62 : sqlite-bind-double ( handle i x -- )
63     sqlite3_bind_double sqlite-check-result ;
64
65 : sqlite-bind-null ( handle i -- )
66     sqlite3_bind_null sqlite-check-result ;
67
68 : sqlite-bind-blob ( handle i byte-array -- )
69     dup length SQLITE_TRANSIENT
70     sqlite3_bind_blob sqlite-check-result ;
71
72 : sqlite-bind-text-by-name ( handle name text -- )
73     parameter-index sqlite-bind-text ;
74
75 : sqlite-bind-int-by-name ( handle name int -- )
76     parameter-index sqlite-bind-int ;
77
78 : sqlite-bind-int64-by-name ( handle name int64 -- )
79     parameter-index sqlite-bind-int64 ;
80
81 : sqlite-bind-uint64-by-name ( handle name int64 -- )
82     parameter-index sqlite-bind-uint64 ;
83
84 : sqlite-bind-boolean-by-name ( handle name obj -- )
85     >boolean 1 0 ? parameter-index sqlite-bind-int ;
86
87 : sqlite-bind-double-by-name ( handle name double -- )
88     parameter-index sqlite-bind-double ;
89
90 : sqlite-bind-blob-by-name ( handle name blob -- )
91     parameter-index sqlite-bind-blob ;
92
93 : sqlite-bind-null-by-name ( handle name obj -- )
94     parameter-index drop sqlite-bind-null ;
95
96 : (sqlite-bind-type) ( handle key value type -- )
97     dup array? [ first ] when
98     {
99         { INTEGER [ sqlite-bind-int-by-name ] }
100         { BIG-INTEGER [ sqlite-bind-int64-by-name ] }
101         { SIGNED-BIG-INTEGER [ sqlite-bind-int64-by-name ] }
102         { UNSIGNED-BIG-INTEGER [ sqlite-bind-uint64-by-name ] }
103         { BOOLEAN [ sqlite-bind-boolean-by-name ] }
104         { TEXT [ sqlite-bind-text-by-name ] }
105         { VARCHAR [ sqlite-bind-text-by-name ] }
106         { DOUBLE [ sqlite-bind-double-by-name ] }
107         { DATE [ timestamp>ymd sqlite-bind-text-by-name ] }
108         { TIME [ duration>hms sqlite-bind-text-by-name ] }
109         { DATETIME [ timestamp>ymdhms sqlite-bind-text-by-name ] }
110         { TIMESTAMP [ timestamp>ymdhms sqlite-bind-text-by-name ] }
111         { BLOB [ sqlite-bind-blob-by-name ] }
112         { FACTOR-BLOB [ object>bytes sqlite-bind-blob-by-name ] }
113         { URL [ present sqlite-bind-text-by-name ] }
114         { +db-assigned-id+ [ sqlite-bind-int-by-name ] }
115         { +random-id+ [ sqlite-bind-int64-by-name ] }
116         { NULL [ sqlite-bind-null-by-name ] }
117         [ no-sql-type ]
118     } case ;
119
120 : sqlite-bind-type ( handle key value type -- )
121     ! null and empty values need to be set by sqlite-bind-null-by-name
122     over [
123         NULL = [ 2drop NULL NULL ] when
124     ] [
125         drop NULL
126     ] if* (sqlite-bind-type) ;
127
128 : sqlite-finalize ( handle -- ) sqlite3_finalize sqlite-check-result ;
129 : sqlite-reset ( handle -- ) sqlite3_reset sqlite-check-result ;
130 : sqlite-clear-bindings ( handle -- )
131     sqlite3_clear_bindings sqlite-check-result ;
132 : sqlite-#columns ( query -- int ) sqlite3_column_count ;
133 : sqlite-column ( handle index -- string ) sqlite3_column_text ;
134 : sqlite-column-name ( handle index -- string ) sqlite3_column_name ;
135 : sqlite-column-type ( handle index -- string ) sqlite3_column_type ;
136
137
138 : sqlite3-column-null ( sqlite n obj -- obj/f )
139     [ sqlite3_column_type SQLITE_NULL = f ] dip ? ; inline
140
141 ! sqlite_column_int returns 0 for both a ``0`` and for ``NULL``
142 ! so call sqlite3_column_type if it's 0
143 : sqlite3-column-int ( handle index -- int/f )
144     2dup sqlite3_column_int dup 0 = [ sqlite3-column-null ] [ 2nip ] if ;
145
146 : sqlite3-column-int64 ( handle index -- int/f )
147     2dup sqlite3_column_int64 dup 0 = [ sqlite3-column-null ] [ 2nip ] if ;
148
149 : sqlite3-column-uint64 ( handle index -- int/f )
150     2dup sqlite3_column_uint64 dup 0 = [ sqlite3-column-null ] [ 2nip ] if ;
151
152 : sqlite3-column-double ( handle index -- int/f )
153     2dup sqlite3_column_double dup 0.0 = [ sqlite3-column-null ] [ 2nip ] if ;
154
155 : sqlite-column-blob ( handle index -- byte-array/f )
156     [ sqlite3_column_bytes ] 2keep
157     pick zero? [
158         3drop f
159     ] [
160         sqlite3_column_blob swap memory>byte-array
161     ] if ;
162
163 : sqlite-column-typed ( handle index type -- obj )
164     dup array? [ first ] when
165     {
166         { +db-assigned-id+ [ sqlite3_column_int64  ] }
167         { +random-id+ [ sqlite3-column-uint64 ] }
168         { INTEGER [ sqlite3-column-int ] }
169         { BIG-INTEGER [ sqlite3-column-int64 ] }
170         { SIGNED-BIG-INTEGER [ sqlite3-column-int64 ] }
171         { UNSIGNED-BIG-INTEGER [ sqlite3-column-uint64 ] }
172         { BOOLEAN [ sqlite3-column-int 1 = ] }
173         { DOUBLE [ sqlite3-column-double ] }
174         { TEXT [ sqlite3_column_text ] }
175         { VARCHAR [ sqlite3_column_text ] }
176         { DATE [ sqlite3_column_text dup [ ymd>timestamp ] when ] }
177         { TIME [ sqlite3_column_text dup [ hms>duration ] when ] }
178         { TIMESTAMP [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] }
179         { DATETIME [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] }
180         { BLOB [ sqlite-column-blob ] }
181         { URL [ sqlite3_column_text dup [ >url ] when ] }
182         { FACTOR-BLOB [ sqlite-column-blob dup [ bytes>object ] when ] }
183         [ no-sql-type ]
184     } case ;
185
186 : sqlite-row ( handle -- seq )
187     dup sqlite-#columns [ sqlite-column ] with { } map-integers ;
188
189 : sqlite-step-has-more-rows? ( prepared -- ? )
190     {
191         { SQLITE_ROW [ t ] }
192         { SQLITE_DONE [ f ] }
193         [ sqlite-check-result f ]
194     } case ;
195
196 : sqlite-next ( prepared -- ? )
197     sqlite3_step sqlite-step-has-more-rows? ;