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 ;
10 : sqlite-compile-options ( -- seq )
11 [ sqlite3_compileoption_get ] zero-loop>array ;
13 ERROR: sqlite-error < db-error n string ;
15 : sqlite-other-error ( n -- * )
16 dup sqlite-error-messages nth sqlite-error ;
18 : sqlite-statement-error ( -- * )
19 db-connection get handle>> sqlite3_errmsg
20 parse-sqlite-sql-error throw ;
22 : sqlite-check-result ( n -- )
25 { SQLITE_ERROR [ sqlite-statement-error ] }
26 [ sqlite-other-error ]
29 : sqlite-open ( path -- db )
31 { void* } [ sqlite3_open sqlite-check-result ]
34 : sqlite-close ( db -- )
35 sqlite3_close sqlite-check-result ;
37 : sqlite-prepare ( db sql -- handle )
38 utf8 encode dup length
40 [ sqlite3_prepare_v2 sqlite-check-result ]
41 with-out-parameters drop ;
43 : sqlite-bind-parameter-index ( handle name -- index )
44 sqlite3_bind_parameter_index ;
46 : parameter-index ( handle name text -- handle name text )
47 [ dupd sqlite-bind-parameter-index ] dip ;
49 : sqlite-bind-text ( handle index text -- )
50 utf8 encode dup length SQLITE_TRANSIENT
51 sqlite3_bind_text sqlite-check-result ;
53 : sqlite-bind-int ( handle i n -- )
54 sqlite3_bind_int sqlite-check-result ;
56 : sqlite-bind-int64 ( handle i n -- )
57 sqlite3_bind_int64 sqlite-check-result ;
59 : sqlite-bind-uint64 ( handle i n -- )
60 sqlite3-bind-uint64 sqlite-check-result ;
62 : sqlite-bind-double ( handle i x -- )
63 sqlite3_bind_double sqlite-check-result ;
65 : sqlite-bind-null ( handle i -- )
66 sqlite3_bind_null sqlite-check-result ;
68 : sqlite-bind-blob ( handle i byte-array -- )
69 dup length SQLITE_TRANSIENT
70 sqlite3_bind_blob sqlite-check-result ;
72 : sqlite-bind-text-by-name ( handle name text -- )
73 parameter-index sqlite-bind-text ;
75 : sqlite-bind-int-by-name ( handle name int -- )
76 parameter-index sqlite-bind-int ;
78 : sqlite-bind-int64-by-name ( handle name int64 -- )
79 parameter-index sqlite-bind-int64 ;
81 : sqlite-bind-uint64-by-name ( handle name int64 -- )
82 parameter-index sqlite-bind-uint64 ;
84 : sqlite-bind-boolean-by-name ( handle name obj -- )
85 >boolean 1 0 ? parameter-index sqlite-bind-int ;
87 : sqlite-bind-double-by-name ( handle name double -- )
88 parameter-index sqlite-bind-double ;
90 : sqlite-bind-blob-by-name ( handle name blob -- )
91 parameter-index sqlite-bind-blob ;
93 : sqlite-bind-null-by-name ( handle name obj -- )
94 parameter-index drop sqlite-bind-null ;
96 : (sqlite-bind-type) ( handle key value type -- )
97 dup array? [ first ] when
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 ] }
120 : sqlite-bind-type ( handle key value type -- )
121 ! null and empty values need to be set by sqlite-bind-null-by-name
123 NULL = [ 2drop NULL NULL ] when
126 ] if* (sqlite-bind-type) ;
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 ;
138 : sqlite3-column-null ( sqlite n obj -- obj/f )
139 [ sqlite3_column_type SQLITE_NULL = f ] dip ? ; inline
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 ;
146 : sqlite3-column-int64 ( handle index -- int/f )
147 2dup sqlite3_column_int64 dup 0 = [ sqlite3-column-null ] [ 2nip ] if ;
149 : sqlite3-column-uint64 ( handle index -- int/f )
150 2dup sqlite3_column_uint64 dup 0 = [ sqlite3-column-null ] [ 2nip ] if ;
152 : sqlite3-column-double ( handle index -- int/f )
153 2dup sqlite3_column_double dup 0.0 = [ sqlite3-column-null ] [ 2nip ] if ;
155 : sqlite-column-blob ( handle index -- byte-array/f )
156 [ sqlite3_column_bytes ] 2keep
160 sqlite3_column_blob swap memory>byte-array
163 : sqlite-column-typed ( handle index type -- obj )
164 dup array? [ first ] when
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 ] }
186 : sqlite-row ( handle -- seq )
187 dup sqlite-#columns [ sqlite-column ] with { } map-integers ;
189 : sqlite-step-has-more-rows? ( prepared -- ? )
192 { SQLITE_DONE [ f ] }
193 [ sqlite-check-result f ]
196 : sqlite-next ( prepared -- ? )
197 sqlite3_step sqlite-step-has-more-rows? ;