| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | ! Copyright (C) 2005, 2008 Chris Double, Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-05 20:29:14 -04:00
										 |  |  | USING: alien arrays assocs classes compiler db hashtables | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  | io.files kernel math math.parser namespaces prettyprint fry | 
					
						
							| 
									
										
										
										
											2008-09-05 20:29:14 -04:00
										 |  |  | sequences strings classes.tuple alien.c-types continuations | 
					
						
							|  |  |  | db.sqlite.lib db.sqlite.ffi db.tuples words db.types combinators | 
					
						
							| 
									
										
										
										
											2009-11-05 18:03:24 -05:00
										 |  |  | math.intervals io locals nmake accessors vectors math.ranges random | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  | math.bitwise db.queries destructors db.tuples.private interpolate | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -04:00
										 |  |  | io.streams.string make db.private sequences.deep | 
					
						
							| 
									
										
										
										
											2009-02-20 23:59:01 -05:00
										 |  |  | db.errors.sqlite ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | IN: db.sqlite | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | TUPLE: sqlite-db path ;
 | 
					
						
							| 
									
										
										
										
											2008-02-27 14:21:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-02 13:11:56 -04:00
										 |  |  | : <sqlite-db> ( path -- sqlite-db )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  |     sqlite-db new
 | 
					
						
							| 
									
										
										
										
											2008-10-02 13:11:56 -04:00
										 |  |  |         swap >>path ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | TUPLE: sqlite-db-connection < db-connection ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <sqlite-db-connection> ( handle -- db-connection )
 | 
					
						
							|  |  |  |     sqlite-db-connection new-db-connection | 
					
						
							|  |  |  |         swap >>handle ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-db db-open ( db -- db-connection )
 | 
					
						
							|  |  |  |     path>> sqlite-open <sqlite-db-connection> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-db-connection db-close ( handle -- ) sqlite-close ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-18 17:01:31 -04:00
										 |  |  | TUPLE: sqlite-statement < statement ;
 | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-06 01:37:12 -04:00
										 |  |  | TUPLE: sqlite-result-set < result-set has-more? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection <simple-statement> ( str in out -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  |     <prepared-statement> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection <prepared-statement> ( str in out -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-09-09 15:44:14 -04:00
										 |  |  |     sqlite-statement new-statement ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  | : sqlite-maybe-prepare ( statement -- statement )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 01:37:12 -04:00
										 |  |  |     dup handle>> [ | 
					
						
							| 
									
										
										
										
											2008-12-17 22:04:17 -05:00
										 |  |  |         db-connection get handle>> over sql>> sqlite-prepare | 
					
						
							| 
									
										
										
										
											2008-04-06 01:37:12 -04:00
										 |  |  |         >>handle | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | M: sqlite-statement dispose ( statement -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 01:37:12 -04:00
										 |  |  |     handle>> | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  |     [ [ sqlite3_reset drop ] keep sqlite-finalize ] when* ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | M: sqlite-result-set dispose ( result-set -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 01:37:12 -04:00
										 |  |  |     f >>handle drop ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-19 23:09:36 -04:00
										 |  |  | : reset-bindings ( statement -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  |     sqlite-maybe-prepare | 
					
						
							| 
									
										
										
										
											2008-04-19 23:09:36 -04:00
										 |  |  |     handle>> [ sqlite3_reset drop ] [ sqlite3_clear_bindings drop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-statement low-level-bind ( statement -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-03 21:19:20 -04:00
										 |  |  |     [ handle>> ] [ bind-params>> ] bi
 | 
					
						
							|  |  |  |     [ [ key>> ] [ value>> ] [ type>> ] tri sqlite-bind-type ] with each ;
 | 
					
						
							| 
									
										
										
										
											2008-04-19 23:09:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-statement bind-statement* ( statement -- )
 | 
					
						
							|  |  |  |     sqlite-maybe-prepare | 
					
						
							| 
									
										
										
										
											2008-09-02 02:54:07 -04:00
										 |  |  |     dup bound?>> [ dup reset-bindings ] when
 | 
					
						
							| 
									
										
										
										
											2008-04-19 23:09:36 -04:00
										 |  |  |     low-level-bind ;
 | 
					
						
							| 
									
										
										
										
											2008-02-27 19:28:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  | GENERIC: sqlite-bind-conversion ( tuple obj -- array )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-21 14:11:19 -04:00
										 |  |  | TUPLE: sqlite-low-level-binding < low-level-binding key type ;
 | 
					
						
							|  |  |  | : <sqlite-low-level-binding> ( key value type -- obj )
 | 
					
						
							|  |  |  |     sqlite-low-level-binding new
 | 
					
						
							|  |  |  |         swap >>type | 
					
						
							|  |  |  |         swap >>value | 
					
						
							|  |  |  |         swap >>key ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  | M: sql-spec sqlite-bind-conversion ( tuple spec -- array )
 | 
					
						
							|  |  |  |     [ column-name>> ":" prepend ] | 
					
						
							|  |  |  |     [ slot-name>> rot get-slot-named ] | 
					
						
							| 
									
										
										
										
											2008-04-21 14:11:19 -04:00
										 |  |  |     [ type>> ] tri <sqlite-low-level-binding> ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: literal-bind sqlite-bind-conversion ( tuple literal-bind -- array )
 | 
					
						
							| 
									
										
										
										
											2008-04-21 14:11:19 -04:00
										 |  |  |     nip [ key>> ] [ value>> ] [ type>> ] tri
 | 
					
						
							|  |  |  |     <sqlite-low-level-binding> ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 18:03:24 -05:00
										 |  |  | M:: generator-bind sqlite-bind-conversion ( tuple generate-bind -- array )
 | 
					
						
							|  |  |  |     generate-bind generator-singleton>> eval-generator :> obj | 
					
						
							|  |  |  |     generate-bind slot-name>> :> name | 
					
						
							|  |  |  |     obj name tuple set-slot-named | 
					
						
							|  |  |  |     generate-bind key>> obj generate-bind type>> <sqlite-low-level-binding> ;
 | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-27 19:28:32 -05:00
										 |  |  | M: sqlite-statement bind-tuple ( tuple statement -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  |         in-params>> [ sqlite-bind-conversion ] with map
 | 
					
						
							|  |  |  |     ] keep bind-statement ;
 | 
					
						
							| 
									
										
										
										
											2008-02-27 19:28:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 20:06:19 -04:00
										 |  |  | ERROR: sqlite-last-id-fail ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  | : last-insert-id ( -- id )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 22:04:17 -05:00
										 |  |  |     db-connection get handle>> sqlite3_last_insert_rowid | 
					
						
							| 
									
										
										
										
											2008-10-08 20:06:19 -04:00
										 |  |  |     dup zero? [ sqlite-last-id-fail ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection insert-tuple-set-key ( tuple statement -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-07 16:27:35 -04:00
										 |  |  |     execute-statement last-insert-id swap set-primary-key ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-result-set #columns ( result-set -- n )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |     handle>> sqlite-#columns ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-result-set row-column ( result-set n -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |     [ handle>> ] [ sqlite-column ] bi* ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:59:29 -05:00
										 |  |  | M: sqlite-result-set row-column-typed ( result-set n -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |     dup pick out-params>> nth type>> | 
					
						
							| 
									
										
										
										
											2008-10-02 13:11:56 -04:00
										 |  |  |     [ handle>> ] 2dip sqlite-column-typed ;
 | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  | M: sqlite-result-set advance-row ( result-set -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |     dup handle>> sqlite-next >>has-more? drop ;
 | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-result-set more-rows? ( result-set -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |     has-more?>> ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 00:28:33 -05:00
										 |  |  | M: sqlite-statement query-results ( query -- result-set )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 01:26:05 -04:00
										 |  |  |     sqlite-maybe-prepare | 
					
						
							| 
									
										
										
										
											2008-09-09 15:44:14 -04:00
										 |  |  |     dup handle>> sqlite-result-set new-result-set | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  |     dup advance-row ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection <insert-db-assigned-statement> ( tuple -- statement )
 | 
					
						
							| 
									
										
										
										
											2008-02-11 00:11:16 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-02-25 15:50:42 -05:00
										 |  |  |         "insert into " 0% 0% | 
					
						
							|  |  |  |         "(" 0% | 
					
						
							| 
									
										
										
										
											2008-04-28 17:48:55 -04:00
										 |  |  |         remove-db-assigned-id | 
					
						
							| 
									
										
										
										
											2008-04-06 02:22:52 -04:00
										 |  |  |         dup [ ", " 0% ] [ column-name>> 0% ] interleave
 | 
					
						
							| 
									
										
										
										
											2008-02-25 15:50:42 -05:00
										 |  |  |         ") values(" 0% | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |         [ ", " 0% ] [ | 
					
						
							|  |  |  |             dup type>> +random-id+ = [ | 
					
						
							| 
									
										
										
										
											2008-04-28 18:17:19 -04:00
										 |  |  |                 [ slot-name>> ] | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2008-04-21 14:11:19 -04:00
										 |  |  |                     column-name>> ":" prepend dup 0% | 
					
						
							|  |  |  |                     random-id-generator | 
					
						
							| 
									
										
										
										
											2008-04-28 18:17:19 -04:00
										 |  |  |                 ] [ type>> ] tri <generator-bind> 1, | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |             ] [ | 
					
						
							|  |  |  |                 bind% | 
					
						
							|  |  |  |             ] if
 | 
					
						
							|  |  |  |         ] interleave
 | 
					
						
							| 
									
										
										
										
											2008-02-25 15:50:42 -05:00
										 |  |  |         ");" 0% | 
					
						
							| 
									
										
										
										
											2008-04-20 18:47:43 -04:00
										 |  |  |     ] query-make ;
 | 
					
						
							| 
									
										
										
										
											2008-02-25 15:50:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection <insert-user-assigned-statement> ( tuple -- statement )
 | 
					
						
							| 
									
										
										
										
											2008-04-28 17:48:55 -04:00
										 |  |  |     <insert-db-assigned-statement> ;
 | 
					
						
							| 
									
										
										
										
											2008-02-11 00:11:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection bind# ( spec obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-02 13:11:56 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-12-06 19:58:45 -05:00
										 |  |  |         [ column-name>> ":" next-sql-counter surround dup 0% ] | 
					
						
							| 
									
										
										
										
											2008-10-02 13:11:56 -04:00
										 |  |  |         [ type>> ] bi
 | 
					
						
							|  |  |  |     ] dip <literal-bind> 1, ;
 | 
					
						
							| 
									
										
										
										
											2008-04-18 13:43:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection bind% ( spec -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  |     dup 1, column-name>> ":" prepend 0% ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection persistent-table ( -- assoc )
 | 
					
						
							| 
									
										
										
										
											2008-02-11 00:11:16 -05:00
										 |  |  |     H{ | 
					
						
							| 
									
										
										
										
											2008-09-27 15:07:39 -04:00
										 |  |  |         { +db-assigned-id+ { "integer" "integer" f } } | 
					
						
							|  |  |  |         { +user-assigned-id+ { f f f } } | 
					
						
							|  |  |  |         { +random-id+ { "integer" "integer" f } } | 
					
						
							| 
									
										
										
										
											2008-09-27 17:26:21 -04:00
										 |  |  |         { +foreign-id+ { "integer" "integer" "references" } } | 
					
						
							| 
									
										
										
										
											2008-09-27 16:56:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-10 16:32:36 -04:00
										 |  |  |         { +on-update+ { f f "on update" } } | 
					
						
							| 
									
										
										
										
											2008-09-27 16:56:43 -04:00
										 |  |  |         { +on-delete+ { f f "on delete" } } | 
					
						
							|  |  |  |         { +restrict+ { f f "restrict" } } | 
					
						
							|  |  |  |         { +cascade+ { f f "cascade" } } | 
					
						
							|  |  |  |         { +set-null+ { f f "set null" } } | 
					
						
							|  |  |  |         { +set-default+ { f f "set default" } } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  |         { BOOLEAN { "boolean" "boolean" f } } | 
					
						
							| 
									
										
										
										
											2008-09-27 15:07:39 -04:00
										 |  |  |         { INTEGER { "integer" "integer" f } } | 
					
						
							| 
									
										
										
										
											2008-09-27 16:56:43 -04:00
										 |  |  |         { BIG-INTEGER { "bigint" "bigint" f } } | 
					
						
							|  |  |  |         { SIGNED-BIG-INTEGER { "bigint" "bigint" f } } | 
					
						
							|  |  |  |         { UNSIGNED-BIG-INTEGER { "bigint" "bigint" f } } | 
					
						
							|  |  |  |         { TEXT { "text" "text" f } } | 
					
						
							|  |  |  |         { VARCHAR { "text" "text" f } } | 
					
						
							|  |  |  |         { DATE { "date" "date" f } } | 
					
						
							|  |  |  |         { TIME { "time" "time" f } } | 
					
						
							|  |  |  |         { DATETIME { "datetime" "datetime" f } } | 
					
						
							|  |  |  |         { TIMESTAMP { "timestamp" "timestamp" f } } | 
					
						
							|  |  |  |         { DOUBLE { "real" "real" f } } | 
					
						
							|  |  |  |         { BLOB { "blob" "blob" f } } | 
					
						
							|  |  |  |         { FACTOR-BLOB { "blob" "blob" f } } | 
					
						
							|  |  |  |         { URL { "text" "text" f } } | 
					
						
							| 
									
										
										
										
											2008-04-20 17:57:50 -04:00
										 |  |  |         { +autoincrement+ { f f "autoincrement" } } | 
					
						
							|  |  |  |         { +unique+ { f f "unique" } } | 
					
						
							|  |  |  |         { +default+ { f f "default" } } | 
					
						
							|  |  |  |         { +null+ { f f "null" } } | 
					
						
							|  |  |  |         { +not-null+ { f f "not null" } } | 
					
						
							|  |  |  |         { system-random-generator { f f f } } | 
					
						
							|  |  |  |         { secure-random-generator { f f f } } | 
					
						
							|  |  |  |         { random-generator { f f f } } | 
					
						
							| 
									
										
										
										
											2008-02-11 00:11:16 -05:00
										 |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  | : insert-trigger ( -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE INSERT ON ${table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-20 15:52:38 -05:00
										 |  |  |             SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"') | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |             WHERE  (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : insert-trigger-not-null ( -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fki_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE INSERT ON ${table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-20 15:52:38 -05:00
										 |  |  |             SELECT RAISE(ROLLBACK, 'insert on table "${table-name}" violates foreign key constraint "fki_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"') | 
					
						
							|  |  |  |             WHERE NEW.${table-id} IS NOT NULL | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |                 AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : update-trigger ( -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE UPDATE ON ${table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-20 15:52:38 -05:00
										 |  |  |             SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"') | 
					
						
							|  |  |  |             WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : update-trigger-not-null ( -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fku_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE UPDATE ON ${table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-20 15:52:38 -05:00
										 |  |  |             SELECT RAISE(ROLLBACK, 'update on table "${table-name}" violates foreign key constraint "fku_${table-name}_$table-id}_${foreign-table-name}_${foreign-table-id}_id"') | 
					
						
							|  |  |  |             WHERE NEW.${table-id} IS NOT NULL | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |                 AND (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = NEW.${table-id}) IS NULL; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-trigger-restrict ( -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE DELETE ON ${foreign-table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-20 15:52:38 -05:00
										 |  |  |             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"') | 
					
						
							|  |  |  |             WHERE (SELECT ${foreign-table-id} FROM ${foreign-table-name} WHERE ${foreign-table-id} = OLD.${foreign-table-id}) IS NOT NULL; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-trigger-cascade ( -- string )
 | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |     """ | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         CREATE TRIGGER fkd_${table-name}_${table-id}_${foreign-table-name}_${foreign-table-id}_id | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |         BEFORE DELETE ON ${foreign-table-name} | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         FOR EACH ROW BEGIN | 
					
						
							| 
									
										
										
										
											2009-02-12 18:29:31 -05:00
										 |  |  |             DELETE from ${table-name} WHERE ${table-id} = OLD.${foreign-table-id}; | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |         END; | 
					
						
							| 
									
										
										
										
											2009-09-23 13:52:42 -04:00
										 |  |  |     """ interpolate | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  |     ] with-string-writer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : can-be-null? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     "sql-spec" get modifiers>> [ +not-null+ = ] any? not ;
 | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : delete-cascade? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-09-30 12:00:44 -04:00
										 |  |  |     "sql-spec" get modifiers>> { +on-delete+ +cascade+ } swap subseq? ;
 | 
					
						
							| 
									
										
										
										
											2008-09-30 00:43:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sqlite-trigger, ( string -- )
 | 
					
						
							|  |  |  |     { } { } <simple-statement> 3, ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-sqlite-triggers ( -- )
 | 
					
						
							|  |  |  |     can-be-null? [ | 
					
						
							|  |  |  |         insert-trigger sqlite-trigger, | 
					
						
							|  |  |  |         update-trigger sqlite-trigger, | 
					
						
							|  |  |  |     ] [  | 
					
						
							|  |  |  |         insert-trigger-not-null sqlite-trigger, | 
					
						
							|  |  |  |         update-trigger-not-null sqlite-trigger, | 
					
						
							|  |  |  |     ] if
 | 
					
						
							|  |  |  |     delete-cascade? [ | 
					
						
							|  |  |  |         delete-trigger-cascade sqlite-trigger, | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         delete-trigger-restrict sqlite-trigger, | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:11:26 -05:00
										 |  |  | : create-db-triggers ( sql-specs -- )
 | 
					
						
							|  |  |  |     [ modifiers>> [ +foreign-id+ = ] deep-any? ] filter
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ class>> db-table-name "db-table" set ] | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2009-02-20 21:11:26 -05:00
										 |  |  |             [ "sql-spec" set ] | 
					
						
							|  |  |  |             [ column-name>> "table-id" set ] | 
					
						
							|  |  |  |             [ ] tri
 | 
					
						
							|  |  |  |             modifiers>> [ [ +foreign-id+ = ] deep-any? ] filter
 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2009-02-20 21:11:26 -05:00
										 |  |  |                 [ second db-table-name "foreign-table-name" set ] | 
					
						
							|  |  |  |                 [ third "foreign-table-id" set ] bi
 | 
					
						
							|  |  |  |                 create-sqlite-triggers | 
					
						
							|  |  |  |             ] each
 | 
					
						
							|  |  |  |         ] bi
 | 
					
						
							|  |  |  |     ] each ;
 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sqlite-create-table ( sql-specs class-name -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         "create table " 0% 0% | 
					
						
							|  |  |  |         "(" 0% [ ", " 0% ] [ | 
					
						
							|  |  |  |             dup "sql-spec" set
 | 
					
						
							|  |  |  |             dup column-name>> [ "table-id" set ] [ 0% ] bi
 | 
					
						
							|  |  |  |             " " 0% | 
					
						
							|  |  |  |             dup type>> lookup-create-type 0% | 
					
						
							|  |  |  |             modifiers 0% | 
					
						
							|  |  |  |         ] interleave
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |         find-primary-key [ | 
					
						
							|  |  |  |             ", " 0% | 
					
						
							|  |  |  |             "primary key(" 0% | 
					
						
							|  |  |  |             [ "," 0% ] [ column-name>> 0% ] interleave
 | 
					
						
							|  |  |  |             ")" 0% | 
					
						
							|  |  |  |         ] unless-empty
 | 
					
						
							|  |  |  |         ");" 0% | 
					
						
							|  |  |  |     ] 2bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-db-connection create-sql-statement ( class -- statement )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ sqlite-create-table ] | 
					
						
							| 
									
										
										
										
											2009-02-20 21:11:26 -05:00
										 |  |  |         [ drop create-db-triggers ] 2bi
 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |     ] query-make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-db-connection drop-sql-statement ( class -- statements )
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:11:26 -05:00
										 |  |  |     [ nip "drop table " 0% 0% ";" 0% ] query-make ;
 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:35:53 -05:00
										 |  |  | M: sqlite-db-connection compound ( string seq -- new-string )
 | 
					
						
							| 
									
										
										
										
											2008-04-20 01:52:05 -04:00
										 |  |  |     over { | 
					
						
							| 
									
										
										
										
											2008-12-04 01:06:02 -05:00
										 |  |  |         { "default" [ first number>string " " glue ] } | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  |         { "references" [ >reference-string ] } | 
					
						
							| 
									
										
										
										
											2008-09-28 18:16:55 -04:00
										 |  |  |         [ 2drop ] | 
					
						
							| 
									
										
										
										
											2008-04-20 01:52:05 -04:00
										 |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2009-02-20 23:59:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sqlite-db-connection parse-db-error | 
					
						
							|  |  |  |     dup n>> { | 
					
						
							|  |  |  |         { 1 [ string>> parse-sqlite-sql-error ] } | 
					
						
							|  |  |  |         [ drop ] | 
					
						
							|  |  |  |     } case ;
 |