| 
									
										
										
										
											2008-02-03 16:06:31 -05:00
										 |  |  | ! Copyright (C) 2008 Chris Double, Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-09-17 23:07:21 -04:00
										 |  |  | USING: alien.c-types alien.data arrays assocs kernel math math.parser | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | namespaces sequences db.sqlite.ffi db combinators | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  | continuations db.types calendar.format serialize | 
					
						
							| 
									
										
										
										
											2008-03-11 01:18:57 -04:00
										 |  |  | io.streams.byte-array byte-arrays io.encodings.binary | 
					
						
							| 
									
										
										
										
											2008-07-09 20:11:38 -04:00
										 |  |  | io.backend db.errors present urls io.encodings.utf8 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  | io.encodings.string accessors shuffle io db.private ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | IN: db.sqlite.lib | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-30 19:00:42 -04:00
										 |  |  | ERROR: sqlite-error < db-error n string ;
 | 
					
						
							|  |  |  | ERROR: sqlite-sql-error < sql-error n string ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 22:22:51 -05:00
										 |  |  | : <sqlite-sql-error> ( n string -- error )
 | 
					
						
							|  |  |  |     \ sqlite-sql-error new
 | 
					
						
							|  |  |  |         swap >>string | 
					
						
							|  |  |  |         swap >>n ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-30 19:00:42 -04:00
										 |  |  | : throw-sqlite-error ( n -- * )
 | 
					
						
							|  |  |  |     dup sqlite-error-messages nth sqlite-error ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sqlite-statement-error ( -- * )
 | 
					
						
							| 
									
										
										
										
											2008-05-30 19:00:42 -04:00
										 |  |  |     SQLITE_ERROR | 
					
						
							| 
									
										
										
										
											2009-02-21 22:22:51 -05:00
										 |  |  |     db-connection get handle>> sqlite3_errmsg <sqlite-sql-error> throw ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sqlite-check-result ( n -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-05-30 19:00:42 -04:00
										 |  |  |         { SQLITE_OK [ ] } | 
					
						
							|  |  |  |         { SQLITE_ERROR [ sqlite-statement-error ] } | 
					
						
							|  |  |  |         [ throw-sqlite-error ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 20:43:07 -04:00
										 |  |  | : sqlite-open ( path -- db )
 | 
					
						
							|  |  |  |     normalize-path | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  |     "void*" <c-object> | 
					
						
							|  |  |  |     [ sqlite3_open sqlite-check-result ] keep *void* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sqlite-close ( db -- )
 | 
					
						
							|  |  |  |     sqlite3_close sqlite-check-result ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | : sqlite-prepare ( db sql -- handle )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 20:11:38 -04:00
										 |  |  |     utf8 encode dup length "void*" <c-object> "void*" <c-object> | 
					
						
							| 
									
										
										
										
											2008-04-23 03:46:21 -04:00
										 |  |  |     [ sqlite3_prepare_v2 sqlite-check-result ] 2keep
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  |     drop *void* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | : sqlite-bind-parameter-index ( handle name -- index )
 | 
					
						
							|  |  |  |     sqlite3_bind_parameter_index ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parameter-index ( handle name text -- handle name text )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  |     [ dupd sqlite-bind-parameter-index ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : sqlite-bind-text ( handle index text -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 20:11:38 -04:00
										 |  |  |     utf8 encode dup length SQLITE_TRANSIENT | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  |     sqlite3_bind_text sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-int ( handle i n -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  |     sqlite3_bind_int sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-int64 ( handle i n -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  |     sqlite3_bind_int64 sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  | : sqlite-bind-uint64 ( handle i n -- )
 | 
					
						
							|  |  |  |     sqlite3-bind-uint64 sqlite-check-result ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-double ( handle i x -- )
 | 
					
						
							|  |  |  |     sqlite3_bind_double sqlite-check-result ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sqlite-bind-null ( handle i -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  |     sqlite3_bind_null sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  | : sqlite-bind-blob ( handle i byte-array -- )
 | 
					
						
							|  |  |  |     dup length SQLITE_TRANSIENT | 
					
						
							|  |  |  |     sqlite3_bind_blob sqlite-check-result ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | : sqlite-bind-text-by-name ( handle name text -- )
 | 
					
						
							|  |  |  |     parameter-index sqlite-bind-text ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-int-by-name ( handle name int -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  |     parameter-index sqlite-bind-int ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-int64-by-name ( handle name int64 -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |     parameter-index sqlite-bind-int64 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sqlite-bind-uint64-by-name ( handle name int64 -- )
 | 
					
						
							|  |  |  |     parameter-index sqlite-bind-uint64 ;
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  | : sqlite-bind-boolean-by-name ( handle name obj -- )
 | 
					
						
							|  |  |  |     >boolean 1 0 ? parameter-index sqlite-bind-int ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  | : sqlite-bind-double-by-name ( handle name double -- )
 | 
					
						
							|  |  |  |     parameter-index sqlite-bind-double ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  | : sqlite-bind-blob-by-name ( handle name blob -- )
 | 
					
						
							|  |  |  |     parameter-index sqlite-bind-blob ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | : sqlite-bind-null-by-name ( handle name obj -- )
 | 
					
						
							|  |  |  |     parameter-index drop sqlite-bind-null ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  | : (sqlite-bind-type) ( handle key value type -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  |     dup array? [ first ] when
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { INTEGER [ sqlite-bind-int-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  |         { BIG-INTEGER [ sqlite-bind-int64-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |         { SIGNED-BIG-INTEGER [ sqlite-bind-int64-by-name ] } | 
					
						
							|  |  |  |         { UNSIGNED-BIG-INTEGER [ sqlite-bind-uint64-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  |         { BOOLEAN [ sqlite-bind-boolean-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  |         { TEXT [ sqlite-bind-text-by-name ] } | 
					
						
							|  |  |  |         { VARCHAR [ sqlite-bind-text-by-name ] } | 
					
						
							|  |  |  |         { DOUBLE [ sqlite-bind-double-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-04-23 23:23:22 -04:00
										 |  |  |         { DATE [ timestamp>ymd sqlite-bind-text-by-name ] } | 
					
						
							|  |  |  |         { TIME [ timestamp>hms sqlite-bind-text-by-name ] } | 
					
						
							|  |  |  |         { DATETIME [ timestamp>ymdhms sqlite-bind-text-by-name ] } | 
					
						
							|  |  |  |         { TIMESTAMP [ timestamp>ymdhms sqlite-bind-text-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  |         { BLOB [ sqlite-bind-blob-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  |         { FACTOR-BLOB [ object>bytes sqlite-bind-blob-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-06-12 19:04:01 -04:00
										 |  |  |         { URL [ present sqlite-bind-text-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-04-28 17:48:55 -04:00
										 |  |  |         { +db-assigned-id+ [ sqlite-bind-int-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |         { +random-id+ [ sqlite-bind-int64-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  |         { NULL [ sqlite-bind-null-by-name ] } | 
					
						
							| 
									
										
										
										
											2008-02-12 18:10:56 -05:00
										 |  |  |         [ no-sql-type ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  | : sqlite-bind-type ( handle key value type -- )
 | 
					
						
							|  |  |  |     #! null and empty values need to be set by sqlite-bind-null-by-name | 
					
						
							|  |  |  |     over [ | 
					
						
							|  |  |  |         NULL = [ 2drop NULL NULL ] when
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop NULL  | 
					
						
							|  |  |  |     ] if* (sqlite-bind-type) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-13 03:10:43 -04:00
										 |  |  | : sqlite-finalize ( handle -- ) sqlite3_finalize sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2009-02-19 19:26:11 -05:00
										 |  |  | : sqlite-reset ( handle -- ) sqlite3_reset sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-04-19 23:09:36 -04:00
										 |  |  | : sqlite-clear-bindings ( handle -- )
 | 
					
						
							|  |  |  |     sqlite3_clear_bindings sqlite-check-result ;
 | 
					
						
							| 
									
										
										
										
											2008-03-13 03:10:43 -04:00
										 |  |  | : sqlite-#columns ( query -- int ) sqlite3_column_count ;
 | 
					
						
							|  |  |  | : sqlite-column ( handle index -- string ) sqlite3_column_text ;
 | 
					
						
							| 
									
										
										
										
											2008-03-16 00:21:53 -04:00
										 |  |  | : sqlite-column-name ( handle index -- string ) sqlite3_column_name ;
 | 
					
						
							|  |  |  | : sqlite-column-type ( handle index -- string ) sqlite3_column_type ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:59:29 -05:00
										 |  |  | : sqlite-column-blob ( handle index -- byte-array/f )
 | 
					
						
							|  |  |  |     [ sqlite3_column_bytes ] 2keep
 | 
					
						
							|  |  |  |     pick zero? [ | 
					
						
							|  |  |  |         3drop f
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         sqlite3_column_blob swap memory>byte-array | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  | : sqlite-column-typed ( handle index type -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-03-05 20:59:29 -05:00
										 |  |  |     dup array? [ first ] when
 | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-04-28 17:48:55 -04:00
										 |  |  |         { +db-assigned-id+ [ sqlite3_column_int64  ] } | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |         { +random-id+ [ sqlite3-column-uint64 ] } | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  |         { INTEGER [ sqlite3_column_int ] } | 
					
						
							| 
									
										
										
										
											2008-03-05 20:08:33 -05:00
										 |  |  |         { BIG-INTEGER [ sqlite3_column_int64 ] } | 
					
						
							| 
									
										
										
										
											2008-04-19 20:27:54 -04:00
										 |  |  |         { SIGNED-BIG-INTEGER [ sqlite3_column_int64 ] } | 
					
						
							|  |  |  |         { UNSIGNED-BIG-INTEGER [ sqlite3-column-uint64 ] } | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  |         { BOOLEAN [ sqlite3_column_int 1 = ] } | 
					
						
							| 
									
										
										
										
											2008-03-10 14:56:58 -04:00
										 |  |  |         { DOUBLE [ sqlite3_column_double ] } | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  |         { TEXT [ sqlite3_column_text ] } | 
					
						
							| 
									
										
										
										
											2008-03-05 20:59:29 -05:00
										 |  |  |         { VARCHAR [ sqlite3_column_text ] } | 
					
						
							|  |  |  |         { DATE [ sqlite3_column_text dup [ ymd>timestamp ] when ] } | 
					
						
							|  |  |  |         { TIME [ sqlite3_column_text dup [ hms>timestamp ] when ] } | 
					
						
							|  |  |  |         { TIMESTAMP [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] } | 
					
						
							|  |  |  |         { DATETIME [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] } | 
					
						
							|  |  |  |         { BLOB [ sqlite-column-blob ] } | 
					
						
							| 
									
										
										
										
											2008-06-12 19:04:01 -04:00
										 |  |  |         { URL [ sqlite3_column_text dup [ >url ] when ] } | 
					
						
							| 
									
										
										
										
											2008-10-18 16:15:27 -04:00
										 |  |  |         { FACTOR-BLOB [ sqlite-column-blob dup [ bytes>object ] when ] } | 
					
						
							| 
									
										
										
										
											2008-02-20 12:30:48 -05:00
										 |  |  |         [ no-sql-type ] | 
					
						
							| 
									
										
										
										
											2008-02-15 15:01:44 -05:00
										 |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 16:47:01 -05:00
										 |  |  | : sqlite-row ( handle -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  |     dup sqlite-#columns [ sqlite-column ] with map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-11 19:52:22 -05:00
										 |  |  | : sqlite-step-has-more-rows? ( prepared -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-05-30 20:21:20 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { SQLITE_ROW [ t ] } | 
					
						
							|  |  |  |         { SQLITE_DONE [ f ] } | 
					
						
							|  |  |  |         [ sqlite-check-result f ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2008-02-01 18:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 01:14:27 -05:00
										 |  |  | : sqlite-next ( prepared -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-02-15 00:39:20 -05:00
										 |  |  |     sqlite3_step sqlite-step-has-more-rows? ;
 |