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 fry
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 sequences.deep
13 TUPLE: sqlite-db path ;
15 : <sqlite-db> ( path -- sqlite-db )
21 TUPLE: sqlite-db-connection < db-connection ;
23 : <sqlite-db-connection> ( handle -- db-connection )
24 sqlite-db-connection new-db-connection
29 M: sqlite-db db-open ( db -- db-connection )
30 path>> sqlite-open <sqlite-db-connection> ;
32 M: sqlite-db-connection db-close ( handle -- ) sqlite-close ;
34 TUPLE: sqlite-statement < statement ;
36 TUPLE: sqlite-result-set < result-set has-more? ;
38 M: sqlite-db-connection <simple-statement> ( str in out -- obj )
39 <prepared-statement> ;
41 M: sqlite-db-connection <prepared-statement> ( str in out -- obj )
42 sqlite-statement new-statement ;
44 : sqlite-maybe-prepare ( statement -- statement )
46 db-connection get handle>> over sql>> sqlite-prepare
50 M: sqlite-statement dispose ( statement -- )
52 [ [ sqlite3_reset drop ] keep sqlite-finalize ] when* ;
54 M: sqlite-result-set dispose ( result-set -- )
57 : reset-bindings ( statement -- )
59 handle>> [ sqlite3_reset drop ] [ sqlite3_clear_bindings drop ] bi ;
61 M: sqlite-statement low-level-bind ( statement -- )
62 [ handle>> ] [ bind-params>> ] bi
63 [ [ key>> ] [ value>> ] [ type>> ] tri sqlite-bind-type ] with each ;
65 M: sqlite-statement bind-statement* ( statement -- )
67 dup bound?>> [ dup reset-bindings ] when
70 GENERIC: sqlite-bind-conversion ( tuple obj -- array )
72 TUPLE: sqlite-low-level-binding < low-level-binding key type ;
73 : <sqlite-low-level-binding> ( key value type -- obj )
74 sqlite-low-level-binding new
79 M: sql-spec sqlite-bind-conversion ( tuple spec -- array )
80 [ column-name>> ":" prepend ]
81 [ slot-name>> rot get-slot-named ]
82 [ type>> ] tri <sqlite-low-level-binding> ;
84 M: literal-bind sqlite-bind-conversion ( tuple literal-bind -- array )
85 nip [ key>> ] [ value>> ] [ type>> ] tri
86 <sqlite-low-level-binding> ;
88 M: generator-bind sqlite-bind-conversion ( tuple generate-bind -- array )
90 [ generator-singleton>> eval-generator tuck ] [ slot-name>> ] bi
92 [ [ key>> ] [ type>> ] bi ] dip
93 swap <sqlite-low-level-binding> ;
95 M: sqlite-statement bind-tuple ( tuple statement -- )
97 in-params>> [ sqlite-bind-conversion ] with map
98 ] keep bind-statement ;
100 ERROR: sqlite-last-id-fail ;
102 : last-insert-id ( -- id )
103 db-connection get handle>> sqlite3_last_insert_rowid
104 dup zero? [ sqlite-last-id-fail ] when ;
106 M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- )
107 execute-statement last-insert-id swap set-primary-key ;
109 M: sqlite-result-set #columns ( result-set -- n )
110 handle>> sqlite-#columns ;
112 M: sqlite-result-set row-column ( result-set n -- obj )
113 [ handle>> ] [ sqlite-column ] bi* ;
115 M: sqlite-result-set row-column-typed ( result-set n -- obj )
116 dup pick out-params>> nth type>>
117 [ handle>> ] 2dip sqlite-column-typed ;
119 M: sqlite-result-set advance-row ( result-set -- )
120 dup handle>> sqlite-next >>has-more? drop ;
122 M: sqlite-result-set more-rows? ( result-set -- ? )
125 M: sqlite-statement query-results ( query -- result-set )
127 dup handle>> sqlite-result-set new-result-set
130 M: sqlite-db-connection <insert-db-assigned-statement> ( tuple -- statement )
134 remove-db-assigned-id
135 dup [ ", " 0% ] [ column-name>> 0% ] interleave
138 dup type>> +random-id+ = [
141 column-name>> ":" prepend dup 0%
143 ] [ type>> ] tri <generator-bind> 1,
151 M: sqlite-db-connection <insert-user-assigned-statement> ( tuple -- statement )
152 <insert-db-assigned-statement> ;
154 M: sqlite-db-connection bind# ( spec obj -- )
156 [ column-name>> ":" next-sql-counter surround dup 0% ]
158 ] dip <literal-bind> 1, ;
160 M: sqlite-db-connection bind% ( spec -- )
161 dup 1, column-name>> ":" prepend 0% ;
163 M: sqlite-db-connection persistent-table ( -- assoc )
165 { +db-assigned-id+ { "integer" "integer" f } }
166 { +user-assigned-id+ { f f f } }
167 { +random-id+ { "integer" "integer" f } }
168 { +foreign-id+ { "integer" "integer" "references" } }
170 { +on-update+ { f f "on update" } }
171 { +on-delete+ { f f "on delete" } }
172 { +restrict+ { f f "restrict" } }
173 { +cascade+ { f f "cascade" } }
174 { +set-null+ { f f "set null" } }
175 { +set-default+ { f f "set default" } }
177 { BOOLEAN { "boolean" "boolean" f } }
178 { INTEGER { "integer" "integer" f } }
179 { BIG-INTEGER { "bigint" "bigint" f } }
180 { SIGNED-BIG-INTEGER { "bigint" "bigint" f } }
181 { UNSIGNED-BIG-INTEGER { "bigint" "bigint" f } }
182 { TEXT { "text" "text" f } }
183 { VARCHAR { "text" "text" f } }
184 { DATE { "date" "date" f } }
185 { TIME { "time" "time" f } }
186 { DATETIME { "datetime" "datetime" f } }
187 { TIMESTAMP { "timestamp" "timestamp" f } }
188 { DOUBLE { "real" "real" f } }
189 { BLOB { "blob" "blob" f } }
190 { FACTOR-BLOB { "blob" "blob" f } }
191 { URL { "text" "text" f } }
192 { +autoincrement+ { f f "autoincrement" } }
193 { +unique+ { f f "unique" } }
194 { +default+ { f f "default" } }
195 { +null+ { f f "null" } }
196 { +not-null+ { f f "not null" } }
197 { system-random-generator { f f f } }
198 { secure-random-generator { f f f } }
199 { random-generator { f f f } }
202 : insert-trigger ( -- string )
205 CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
206 BEFORE INSERT ON ${table-name}
208 SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"')
209 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
212 ] with-string-writer ;
214 : insert-trigger-not-null ( -- string )
217 CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
218 BEFORE INSERT ON ${table-name}
220 SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"')
221 WHERE NEW.${table-id} IS NOT NULL
222 AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
225 ] with-string-writer ;
227 : update-trigger ( -- string )
230 CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
231 BEFORE UPDATE ON ${table-name}
233 SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"')
234 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
237 ] with-string-writer ;
239 : update-trigger-not-null ( -- string )
242 CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
243 BEFORE UPDATE ON ${table-name}
245 SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"')
246 WHERE NEW.${table-id} IS NOT NULL
247 AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL;
250 ] with-string-writer ;
252 : delete-trigger-restrict ( -- string )
255 CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
256 BEFORE DELETE ON ${foreign-table-name}
258 SELECT RAISE(ROLLBACK, 'delete on table "${foreign-table-name}" violates foreign key constraint "fkd_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"')
259 WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = OLD.${foreign-table-id}) IS NOT NULL;
262 ] with-string-writer ;
264 : delete-trigger-cascade ( -- string )
267 CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id
268 BEFORE DELETE ON ${foreign-table-name}
270 DELETE from ${table-name} WHERE ${table-id} = OLD.${foreign-table-id};
273 ] with-string-writer ;
275 : can-be-null? ( -- ? )
276 "sql-spec" get modifiers>> [ +not-null+ = ] any? not ;
278 : delete-cascade? ( -- ? )
279 "sql-spec" get modifiers>> { +on-delete+ +cascade+ } swap subseq? ;
281 : sqlite-trigger, ( string -- )
282 { } { } <simple-statement> 3, ;
284 : create-sqlite-triggers ( -- )
286 insert-trigger sqlite-trigger,
287 update-trigger sqlite-trigger,
289 insert-trigger-not-null sqlite-trigger,
290 update-trigger-not-null sqlite-trigger,
293 delete-trigger-cascade sqlite-trigger,
295 delete-trigger-restrict sqlite-trigger,
298 : create-db-triggers ( sql-specs -- )
299 [ modifiers>> [ +foreign-id+ = ] deep-any? ] filter
301 [ class>> db-table-name "db-table" set ]
304 [ column-name>> "table-id" set ]
306 modifiers>> [ [ +foreign-id+ = ] deep-any? ] filter
308 [ second db-table-name "foreign-table-name" set ]
309 [ third "foreign-table-id" set ] bi
310 create-sqlite-triggers
315 : sqlite-create-table ( sql-specs class-name -- )
317 "create table " 0% 0%
320 dup column-name>> [ "table-id" set ] [ 0% ] bi
322 dup type>> lookup-create-type 0%
330 [ "," 0% ] [ column-name>> 0% ] interleave
336 M: sqlite-db-connection create-sql-statement ( class -- statement )
338 [ sqlite-create-table ]
339 [ drop create-db-triggers ] 2bi
342 M: sqlite-db-connection drop-sql-statement ( class -- statements )
343 [ nip "drop table " 0% 0% ";" 0% ] query-make ;
345 M: sqlite-db-connection compound ( string seq -- new-string )
347 { "default" [ first number>string " " glue ] }
348 { "references" [ >reference-string ] }
352 M: sqlite-db-connection parse-db-error
354 { 1 [ string>> parse-sqlite-sql-error ] }