-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.ua
234 lines (195 loc) · 6.18 KB
/
lib.ua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Experimental!
GetLibraryPath ↚ (
≍"windows"Os
⨬("bin/sqlite3.so"
| ⊗□Arch{"x86" "x86_64"}
⨬("win32.dll"|"win64.dll"|0⍤"Unsupported architecture"0)
⊂"bin/sqlite3_"
)
/($"___" ⊙Sep) {"." ThisFileDir ∘}
)
Lib ← &ffi ⊂□GetLibraryPath
Call ← Lib ⊂⊟∩□
SqliteOk ← 0
SqliteRow ← 100
SqliteDone ← 101
SqliteInteger ← 1
SqliteFloat ← 2
SqliteText ← 3
SqliteBlob ← 4
SqliteNull ← 5
Statement ← {$STMT ⊙($DB) ⊙∘}
GetStatementStmt ← °□⊡0
GetStatementDb ← °□⊡1
InferredTypeNull ← $NULL 0
InferredTypeInt ← $INT 1
InferredTypeFloat ← $FLOAT 2
InferredTypeText ← $TEXT 3
InferredTypeBlob ← $BLOB 4
IsNull ← ≍NaN
IsInt ← ⨬(0|≍⁅.)/↧[⊃(=0type|≍[]△)].
IsFloat ← ⨬(0|¬≍⁅.)/↧[⊃(=0type|≍[]△)].
IsText ← /↧[⊃(=2type|=1⧻△)]
IsBlob ← ⨬(0|/↧≤255)/↧[⊃(=0type|=1⧻△)].
InferType ← (
⍣(⊢⊚[⊃(IsNull|IsInt|IsFloat|IsText|IsBlob)]|0⍤."Invalid value"◌)
⊡:[InferredTypeNull InferredTypeInt InferredTypeFloat InferredTypeText InferredTypeBlob]
)
# https://www.sqlite.org/c3ref/errcode.html
Err ← (
{∘}
Lib {"const char*" "sqlite3_errmsg" "const int*"}
)
# https://www.sqlite.org/c3ref/open.html
Open ← (
{⊙0}
Call "int" "sqlite3_open" {"const char*" "void**"}
°{⊙∘}
⍤ $"Failed to open database: _" ⟜(≍SqliteOk)
$DB
)
# https://www.sqlite.org/c3ref/close.html
Close ← (
⟜({∘}
Call "int" "sqlite3_close" {"const void*"}
)
⨬(⍤.$"Failed to close database: _" Err|◌)≍SqliteOk:
)
# https://www.sqlite.org/c3ref/exec.html
Exec ← (
{⊙⊙(0 0 "www")}:
Call "int" "sqlite3_exec" {"const void*" "const char*" "const void*" "const void*" "const char**"}
°{⊙∘}
⨬(⍤.$"Failed to exec: _"|◌)≍SqliteOk
)
# https://www.sqlite.org/c3ref/prepare.html
Prepare ← (
⊃({⊙⊙(¯1 0 0)}:
Call "int" "sqlite3_prepare_v2" {"const void*" "const char*" "int" "void**" "const char**"}
°{⊙∘}↘¯1
)(.⋅∘)
⨬(⍤.$"Failed to prepare statement: _" Err:|⊙◌)≍SqliteOk
Statement
)
# https://www.sqlite.org/c3ref/bind_blob.html
BindBlob ← (
# TODO: This doesn't work, no idea why
{⊙⊙⊙⊙∘} ⊃(⋅⋅GetStatementStmt|∘|⋅∘|⧻⋅∘|NULL|⋅⋅GetStatementDb)
Call "int" "sqlite3_bind_blob" {"const void*" "const int" "const int*" "const int" "const void*"}
⨬(⍤.$"Failed to bind blob: _" Err|◌)≍SqliteOk
)
BindInt ← (
{⊙⊙∘} ⊃(⋅⋅GetStatementStmt|⊙∘|⋅⋅GetStatementDb)
Call "int" "sqlite3_bind_int" {"const void*" "const int" "const int"}
⨬(⍤.$"Failed to bind integer: _" Err|◌)≍SqliteOk
)
BindFloat ← (
{⊙⊙∘} ⊃(⋅⋅GetStatementStmt|⊙∘|⋅⋅GetStatementDb)
Call "int" "sqlite3_bind_double" {"const void*" "const int" "const double"}
⨬(⍤.$"Failed to bind float: _" Err|◌)≍SqliteOk
)
BindNull ← (
{⊙∘} ⊃(⋅GetStatementStmt|∘|⋅GetStatementDb)
Call "int" "sqlite3_bind_null" {"const void*" "const int"}
⨬(⍤.$"Failed to bind float: _" Err|◌)≍SqliteOk
)
BindText ← (
# TODO: This doesn't work, no idea why
{⊙⊙⊙⊙∘} ⊃(⋅⋅GetStatementStmt|∘|⋅∘|¯1|NULL|⋅⋅GetStatementDb)
Call "int" "sqlite3_bind_text" {"const void*" "const int" "const char*" "int" "const void*"}
⨬(⍤.$"Failed to bind text: _" Err|◌)≍SqliteOk
)
BindValue ← (
⊃(⋅InferType|⊙∘)
⨬(BindNull⊙◌|BindInt|BindFloat|BindText|BindBlob)
)
BindValues ← ⍚BindValue +1⇡⧻.⊙¤
# https://www.sqlite.org/c3ref/bind_parameter_index.html"
BindParameterIndex ← (
{⊙∘} ⊃(⋅GetStatementStmt|∘|$"Invalid named parameter: \"_\""∘)
Call "int" "sqlite3_bind_parameter_index" {"const void*" "const char*"}
⍤:⊙:>0.
)
BindNamed ← BindValue ⊃(BindParameterIndex⊙⋅∘|⋅⊙∘)
BindNamedMap ← ⍚BindNamed °map⊙¤
# https://www.sqlite.org/c3ref/step.html
Step ← (
{∘} GetStatementStmt
Call "int" "sqlite3_step" {"const void*"}
)
StepRow ← ≍SqliteRow Step
# https://www.sqlite.org/c3ref/column_count.html
ColumnCount ← (
{∘} GetStatementStmt
Call "int" "sqlite3_column_count" {"const void*"}
)
# https://www.sqlite.org/c3ref/column_name.html
ColumnName ← (
{⊙∘} :⊙GetStatementStmt
Call "char*" "sqlite3_column_name" {"const void*" "const int"}
)
# https://www.sqlite.org/c3ref/column_blob.html
ColumnType ← (
{⊙∘} :⊙GetStatementStmt
Call "int" "sqlite3_column_type" {"const void*" "const int"}
)
# https://www.sqlite.org/c3ref/column_blob.html
ColumnBlob ← (
{⊙∘} :⊙GetStatementStmt
# 0⍤."Returning a BLOB is currently unsupported"◌
# TODO: This doesn't work as expected
Call "int*" "sqlite3_column_blob" {"const void*" "const int"}
)
# https://www.sqlite.org/c3ref/column_blob.html
ColumnFloat ← (
{⊙∘} :⊙GetStatementStmt
Call "double" "sqlite3_column_double" {"const void*" "const int"}
)
# https://www.sqlite.org/c3ref/column_blob.html
ColumnInt ← (
{⊙∘} :⊙GetStatementStmt
Call "int" "sqlite3_column_int" {"const void*" "const int"}
)
# https://www.sqlite.org/c3ref/column_blob.html
ColumnText ← (
{⊙∘} :⊙GetStatementStmt
Call "char*" "sqlite3_column_text" {"const void*" "const int"}
)
# Helper function to automatically infer column value and return it
ColumnValue ← (
⊃(⊙∘|⊙∘)
ColumnType
⊗: [SqliteInteger SqliteFloat SqliteText SqliteBlob SqliteNull]
⨬(ColumnInt|ColumnFloat|ColumnText|ColumnBlob|NaN)
)
# https://www.sqlite.org/c3ref/finalize.html
Finalize ← (
{∘} ⊃(GetStatementStmt|GetStatementDb)
Call "int" "sqlite3_finalize" {"const void*"}
⨬(⍤.$"Failed to finalize statement: _" Err|◌)≍SqliteOk
)
ExecuteInsert ← (
Step ⊃(∘|GetStatementDb|∘)
⨬(⍤.$"Failed to execute insert statement: _" Err|◌)≍SqliteDone
Finalize
)
GetColumnNames ← (
⊙¤⇡ColumnCount.
≡(□ColumnName)
)
QueryValuesInternal ↚ (
⟜(⊙[] ⇡ColumnCount)
⍢(⟜(:¤⊙.
≡(□ColumnValue)
:⊂⊃(⋅⋅∘|¤⊙∘)
)
)StepRow
◌◌
)
QueryValues ← ⊙Finalize QueryValuesInternal.
QueryMap ← (
⟜(QueryValuesInternal)
⟜(¤GetColumnNames)
Finalize
≡(□map)
)