]> gitweb.factorcode.org Git - factor.git/blob - basis/db/sqlite/lib/lib.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / db / sqlite / lib / lib.factor
1 ! Copyright (C) 2008 Chris Double, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types arrays assocs kernel math math.parser
4 namespaces sequences db.sqlite.ffi db combinators
5 continuations db.types calendar.format serialize
6 io.streams.byte-array byte-arrays io.encodings.binary
7 io.backend db.errors present urls io.encodings.utf8
8 io.encodings.string accessors shuffle io prettyprint
9 db.private ;
10 IN: db.sqlite.lib
11
12 ERROR: sqlite-error < db-error n string ;
13 ERROR: sqlite-sql-error < sql-error n string ;
14
15 : throw-sqlite-error ( n -- * )
16     dup sqlite-error-messages nth sqlite-error ;
17
18 : sqlite-statement-error ( -- * )
19     SQLITE_ERROR
20     db-connection get handle>> sqlite3_errmsg sqlite-sql-error ;
21
22 : sqlite-check-result ( n -- )
23     {
24         { SQLITE_OK [ ] }
25         { SQLITE_ERROR [ sqlite-statement-error ] }
26         [ throw-sqlite-error ]
27     } case ;
28
29 : sqlite-open ( path -- db )
30     normalize-path
31     "void*" <c-object>
32     [ sqlite3_open sqlite-check-result ] keep *void* ;
33
34 : sqlite-close ( db -- )
35     sqlite3_close sqlite-check-result ;
36
37 : sqlite-prepare ( db sql -- handle )
38     utf8 encode dup length "void*" <c-object> "void*" <c-object>
39     [ sqlite3_prepare_v2 sqlite-check-result ] 2keep
40     drop *void* ;
41
42 : sqlite-bind-parameter-index ( handle name -- index )
43     sqlite3_bind_parameter_index ;
44
45 : parameter-index ( handle name text -- handle name text )
46     [ dupd sqlite-bind-parameter-index ] dip ;
47
48 : sqlite-bind-text ( handle index text -- )
49     utf8 encode dup length SQLITE_TRANSIENT
50     sqlite3_bind_text sqlite-check-result ;
51
52 : sqlite-bind-int ( handle i n -- )
53     sqlite3_bind_int sqlite-check-result ;
54
55 : sqlite-bind-int64 ( handle i n -- )
56     sqlite3_bind_int64 sqlite-check-result ;
57
58 : sqlite-bind-uint64 ( handle i n -- )
59     sqlite3-bind-uint64 sqlite-check-result ;
60
61 : sqlite-bind-double ( handle i x -- )
62     sqlite3_bind_double sqlite-check-result ;
63
64 : sqlite-bind-null ( handle i -- )
65     sqlite3_bind_null sqlite-check-result ;
66
67 : sqlite-bind-blob ( handle i byte-array -- )
68     dup length SQLITE_TRANSIENT
69     sqlite3_bind_blob sqlite-check-result ;
70
71 : sqlite-bind-text-by-name ( handle name text -- )
72     parameter-index sqlite-bind-text ;
73
74 : sqlite-bind-int-by-name ( handle name int -- )
75     parameter-index sqlite-bind-int ;
76
77 : sqlite-bind-int64-by-name ( handle name int64 -- )
78     parameter-index sqlite-bind-int64 ;
79
80 : sqlite-bind-uint64-by-name ( handle name int64 -- )
81     parameter-index sqlite-bind-uint64 ;
82
83 : sqlite-bind-boolean-by-name ( handle name obj -- )
84     >boolean 1 0 ? parameter-index sqlite-bind-int ;
85
86 : sqlite-bind-double-by-name ( handle name double -- )
87     parameter-index sqlite-bind-double ;
88
89 : sqlite-bind-blob-by-name ( handle name blob -- )
90     parameter-index sqlite-bind-blob ;
91
92 : sqlite-bind-null-by-name ( handle name obj -- )
93     parameter-index drop sqlite-bind-null ;
94
95 : (sqlite-bind-type) ( handle key value type -- )
96     dup array? [ first ] when
97     {
98         { INTEGER [ sqlite-bind-int-by-name ] }
99         { BIG-INTEGER [ sqlite-bind-int64-by-name ] }
100         { SIGNED-BIG-INTEGER [ sqlite-bind-int64-by-name ] }
101         { UNSIGNED-BIG-INTEGER [ sqlite-bind-uint64-by-name ] }
102         { BOOLEAN [ sqlite-bind-boolean-by-name ] }
103         { TEXT [ sqlite-bind-text-by-name ] }
104         { VARCHAR [ sqlite-bind-text-by-name ] }
105         { DOUBLE [ sqlite-bind-double-by-name ] }
106         { DATE [ timestamp>ymd sqlite-bind-text-by-name ] }
107         { TIME [ timestamp>hms sqlite-bind-text-by-name ] }
108         { DATETIME [ timestamp>ymdhms sqlite-bind-text-by-name ] }
109         { TIMESTAMP [ timestamp>ymdhms sqlite-bind-text-by-name ] }
110         { BLOB [ sqlite-bind-blob-by-name ] }
111         { FACTOR-BLOB [ object>bytes sqlite-bind-blob-by-name ] }
112         { URL [ present sqlite-bind-text-by-name ] }
113         { +db-assigned-id+ [ sqlite-bind-int-by-name ] }
114         { +random-id+ [ sqlite-bind-int64-by-name ] }
115         { NULL [ sqlite-bind-null-by-name ] }
116         [ no-sql-type ]
117     } case ;
118
119 : sqlite-bind-type ( handle key value type -- )
120     #! null and empty values need to be set by sqlite-bind-null-by-name
121     over [
122         NULL = [ 2drop NULL NULL ] when
123     ] [
124         drop NULL 
125     ] if* (sqlite-bind-type) ;
126
127 : sqlite-finalize ( handle -- ) sqlite3_finalize sqlite-check-result ;
128 : sqlite-reset ( handle -- )
129 "resetting: " write dup . 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 ;
136
137 : sqlite-column-blob ( handle index -- byte-array/f )
138     [ sqlite3_column_bytes ] 2keep
139     pick zero? [
140         3drop f
141     ] [
142         sqlite3_column_blob swap memory>byte-array
143     ] if ;
144
145 : sqlite-column-typed ( handle index type -- obj )
146     dup array? [ first ] when
147     {
148         { +db-assigned-id+ [ sqlite3_column_int64  ] }
149         { +random-id+ [ sqlite3-column-uint64 ] }
150         { INTEGER [ sqlite3_column_int ] }
151         { BIG-INTEGER [ sqlite3_column_int64 ] }
152         { SIGNED-BIG-INTEGER [ sqlite3_column_int64 ] }
153         { UNSIGNED-BIG-INTEGER [ sqlite3-column-uint64 ] }
154         { BOOLEAN [ sqlite3_column_int 1 = ] }
155         { DOUBLE [ sqlite3_column_double ] }
156         { TEXT [ sqlite3_column_text ] }
157         { VARCHAR [ sqlite3_column_text ] }
158         { DATE [ sqlite3_column_text dup [ ymd>timestamp ] when ] }
159         { TIME [ sqlite3_column_text dup [ hms>timestamp ] when ] }
160         { TIMESTAMP [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] }
161         { DATETIME [ sqlite3_column_text dup [ ymdhms>timestamp ] when ] }
162         { BLOB [ sqlite-column-blob ] }
163         { URL [ sqlite3_column_text dup [ >url ] when ] }
164         { FACTOR-BLOB [ sqlite-column-blob dup [ bytes>object ] when ] }
165         [ no-sql-type ]
166     } case ;
167
168 : sqlite-row ( handle -- seq )
169     dup sqlite-#columns [ sqlite-column ] with map ;
170
171 : sqlite-step-has-more-rows? ( prepared -- ? )
172     {
173         { SQLITE_ROW [ t ] }
174         { SQLITE_DONE [ f ] }
175         [ sqlite-check-result f ]
176     } case ;
177
178 : sqlite-next ( prepared -- ? )
179     sqlite3_step sqlite-step-has-more-rows? ;