1 ! Copyright (C) 2005, 2008 Chris Double, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays assocs classes compiler db hashtables
4 io.files kernel math math.parser namespaces prettyprint
5 sequences strings classes.tuple alien.c-types continuations
6 db.sqlite.lib db.sqlite.ffi db.tuples words db.types combinators
7 math.intervals io nmake accessors vectors math.ranges random
8 math.bitwise db.queries destructors db.tuples.private interpolate
9 io.streams.string multiline make db.private ;
12 TUPLE: sqlite-db path ;
14 : <sqlite-db> ( path -- sqlite-db )
20 TUPLE: sqlite-db-connection < db-connection ;
22 : <sqlite-db-connection> ( handle -- db-connection )
23 sqlite-db-connection new-db-connection
28 M: sqlite-db db-open ( db -- db-connection )
29 path>> sqlite-open <sqlite-db-connection> ;
31 M: sqlite-db-connection db-close ( handle -- ) sqlite-close ;
33 TUPLE: sqlite-statement < statement ;
35 TUPLE: sqlite-result-set < result-set has-more? ;
37 M: sqlite-db-connection <simple-statement> ( str in out -- obj )
38 <prepared-statement> ;
40 M: sqlite-db-connection <prepared-statement> ( str in out -- obj )
41 sqlite-statement new-statement ;
43 : sqlite-maybe-prepare ( statement -- statement )
45 db-connection get handle>> over sql>> sqlite-prepare
49 M: sqlite-statement dispose ( statement -- )
51 [ [ sqlite3_reset drop ] keep sqlite-finalize ] when* ;
53 M: sqlite-result-set dispose ( result-set -- )
56 : reset-bindings ( statement -- )
58 handle>> [ sqlite3_reset drop ] [ sqlite3_clear_bindings drop ] bi ;
60 M: sqlite-statement low-level-bind ( statement -- )
61 [ handle>> ] [ bind-params>> ] bi
62 [ [ key>> ] [ value>> ] [ type>> ] tri sqlite-bind-type ] with each ;
64 M: sqlite-statement bind-statement* ( statement -- )
66 dup bound?>> [ dup reset-bindings ] when
69 GENERIC: sqlite-bind-conversion ( tuple obj -- array )
71 TUPLE: sqlite-low-level-binding < low-level-binding key type ;
72 : <sqlite-low-level-binding> ( key value type -- obj )
73 sqlite-low-level-binding new
78 M: sql-spec sqlite-bind-conversion ( tuple spec -- array )
79 [ column-name>> ":" prepend ]
80 [ slot-name>> rot get-slot-named ]
81 [ type>> ] tri <sqlite-low-level-binding> ;
83 M: literal-bind sqlite-bind-conversion ( tuple literal-bind -- array )
84 nip [ key>> ] [ value>> ] [ type>> ] tri
85 <sqlite-low-level-binding> ;
87 M: generator-bind sqlite-bind-conversion ( tuple generate-bind -- array )
89 [ generator-singleton>> eval-generator tuck ] [ slot-name>> ] bi
91 [ [ key>> ] [ type>> ] bi ] dip
92 swap <sqlite-low-level-binding> ;
94 M: sqlite-statement bind-tuple ( tuple statement -- )
96 in-params>> [ sqlite-bind-conversion ] with map
97 ] keep bind-statement ;
99 ERROR: sqlite-last-id-fail ;
101 : last-insert-id ( -- id )
102 db-connection get handle>> sqlite3_last_insert_rowid
103 dup zero? [ sqlite-last-id-fail ] when ;
105 M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- )
106 execute-statement last-insert-id swap set-primary-key ;
108 M: sqlite-result-set #columns ( result-set -- n )
109 handle>> sqlite-#columns ;
111 M: sqlite-result-set row-column ( result-set n -- obj )
112 [ handle>> ] [ sqlite-column ] bi* ;
114 M: sqlite-result-set row-column-typed ( result-set n -- obj )
115 dup pick out-params>> nth type>>
116 [ handle>> ] 2dip sqlite-column-typed ;
118 M: sqlite-result-set advance-row ( result-set -- )
119 dup handle>> sqlite-next >>has-more? drop ;
121 M: sqlite-result-set more-rows? ( result-set -- ? )
124 M: sqlite-statement query-results ( query -- result-set )
126 dup handle>> sqlite-result-set new-result-set
129 M: sqlite-db-connection create-sql-statement ( class -- statement )
132 "create table " 0% 0%
135 dup column-name>> [ "table-id" set ] [ 0% ] bi
137 dup type>> lookup-create-type 0%
144 [ "," 0% ] [ column-name>> 0% ] interleave
148 M: sqlite-db-connection drop-sql-statement ( class -- statement )
149 [ "drop table " 0% 0% ";" 0% drop ] query-make ;
151 M: sqlite-db-connection <insert-db-assigned-statement> ( tuple -- statement )
155 remove-db-assigned-id
156 dup [ ", " 0% ] [ column-name>> 0% ] interleave
159 dup type>> +random-id+ = [
162 column-name>> ":" prepend dup 0%
164 ] [ type>> ] tri <generator-bind> 1,
172 M: sqlite-db-connection <insert-user-assigned-statement> ( tuple -- statement )
173 <insert-db-assigned-statement> ;
175 M: sqlite-db-connection bind# ( spec obj -- )
177 [ column-name>> ":" next-sql-counter surround dup 0% ]
179 ] dip <literal-bind> 1, ;
181 M: sqlite-db-connection bind% ( spec -- )
182 dup 1, column-name>> ":" prepend 0% ;
184 M: sqlite-db-connection persistent-table ( -- assoc )
186 { +db-assigned-id+ { "integer" "integer" f } }
187 { +user-assigned-id+ { f f f } }
188 { +random-id+ { "integer" "integer" f } }
189 { +foreign-id+ { "integer" "integer" "references" } }
191 { +on-update+ { f f "on update" } }
192 { +on-delete+ { f f "on delete" } }
193 { +restrict+ { f f "restrict" } }
194 { +cascade+ { f f "cascade" } }
195 { +set-null+ { f f "set null" } }
196 { +set-default+ { f f "set default" } }
198 { BOOLEAN { "boolean" "boolean" f } }
199 { INTEGER { "integer" "integer" f } }
200 { BIG-INTEGER { "bigint" "bigint" f } }
201 { SIGNED-BIG-INTEGER { "bigint" "bigint" f } }
202 { UNSIGNED-BIG-INTEGER { "bigint" "bigint" f } }
203 { TEXT { "text" "text" f } }
204 { VARCHAR { "text" "text" f } }
205 { DATE { "date" "date" f } }
206 { TIME { "time" "time" f } }
207 { DATETIME { "datetime" "datetime" f } }
208 { TIMESTAMP { "timestamp" "timestamp" f } }
209 { DOUBLE { "real" "real" f } }
210 { BLOB { "blob" "blob" f } }
211 { FACTOR-BLOB { "blob" "blob" f } }
212 { URL { "text" "text" f } }
213 { +autoincrement+ { f f "autoincrement" } }
214 { +unique+ { f f "unique" } }
215 { +default+ { f f "default" } }
216 { +null+ { f f "null" } }
217 { +not-null+ { f f "not null" } }
218 { system-random-generator { f f f } }
219 { secure-random-generator { f f f } }
220 { random-generator { f f f } }
223 : insert-trigger ( -- string )
226 CREATE TRIGGER fki_${table}_${foreign-table}_id
227 BEFORE INSERT ON ${table}
229 SELECT RAISE(ROLLBACK, 'insert on table "${table}" violates foreign key constraint "fk_${foreign-table}_id"')
230 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
233 ] with-string-writer ;
235 : insert-trigger-not-null ( -- string )
238 CREATE TRIGGER fki_${table}_${foreign-table}_id
239 BEFORE INSERT ON ${table}
241 SELECT RAISE(ROLLBACK, 'insert on table "${table}" violates foreign key constraint "fk_${foreign-table}_id"')
242 WHERE NEW.${foreign-table-id} IS NOT NULL
243 AND (SELECT ${foreign-table-id} FROM ${foreign-table} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
246 ] with-string-writer ;
248 : update-trigger ( -- string )
251 CREATE TRIGGER fku_${table}_${foreign-table}_id
252 BEFORE UPDATE ON ${table}
254 SELECT RAISE(ROLLBACK, 'update on table "${table}" violates foreign key constraint "fk_${foreign-table}_id"')
255 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
258 ] with-string-writer ;
260 : update-trigger-not-null ( -- string )
263 CREATE TRIGGER fku_${table}_${foreign-table}_id
264 BEFORE UPDATE ON ${table}
266 SELECT RAISE(ROLLBACK, 'update on table "${table}" violates foreign key constraint "fk_${foreign-table}_id"')
267 WHERE NEW.${foreign-table-id} IS NOT NULL
268 AND (SELECT ${foreign-table-id} FROM ${foreign-table} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
271 ] with-string-writer ;
273 : delete-trigger-restrict ( -- string )
276 CREATE TRIGGER fkd_${table}_${foreign-table}_id
277 BEFORE DELETE ON ${foreign-table}
279 SELECT RAISE(ROLLBACK, 'delete on table "${foreign-table}" violates foreign key constraint "fk_${foreign-table}_id"')
280 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table} WHERE ${foreign-table-id} = OLD.${foreign-table-id}) IS NOT NULL;
283 ] with-string-writer ;
285 : delete-trigger-cascade ( -- string )
288 CREATE TRIGGER fkd_${table}_${foreign-table}_id
289 BEFORE DELETE ON ${foreign-table}
291 DELETE from ${table} WHERE ${table-id} = OLD.${foreign-table-id};
294 ] with-string-writer ;
296 : can-be-null? ( -- ? )
297 "sql-spec" get modifiers>> [ +not-null+ = ] contains? not ;
299 : delete-cascade? ( -- ? )
300 "sql-spec" get modifiers>> { +on-delete+ +cascade+ } swap subseq? ;
302 : sqlite-trigger, ( string -- )
303 { } { } <simple-statement> 3, ;
305 : create-sqlite-triggers ( -- )
307 insert-trigger sqlite-trigger,
308 update-trigger sqlite-trigger,
310 insert-trigger-not-null sqlite-trigger,
311 update-trigger-not-null sqlite-trigger,
314 delete-trigger-cascade sqlite-trigger,
316 delete-trigger-restrict sqlite-trigger,
319 M: sqlite-db-connection compound ( string seq -- new-string )
321 { "default" [ first number>string " " glue ] }
323 [ >reference-string ] keep
324 first2 [ "foreign-table" set ]
325 [ "foreign-table-id" set ] bi*
326 create-sqlite-triggers