sqlite.anubis
8.51 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
*Project* Anubis
*Title* SQLite wrapper library
*Copyright* Copyright (c) David René 2005-2006.
*Author* David René
*Created* 2005 12 27
*Revised* july 2006
*Satus* Released
*Compatibility* 1.7.1
*Overview*
This library is intended to make easy to use the SQLite Database. We tried to made
just all necessaries functions for using SQLite with no Dr Shadoko's principle.
All boring stuff are made by this library. But if, we forgotten some function, you are welcome
to add them in this file.
*Public*
read tools/basis.anubis
*Description*
This function print on current output, the content of datatabase table. The content of table
result of sql_query. All rows and columns are stored in list of list of string. the result of
print are show below.
Row_1_String1 | Row1_String2 | Row1_String3
Row_2_String1 | Row2_String2 | Row2_String3
...
public define One
db_print_table
(
List(List(String)) l
) =
if l is
{
[ ] then print("\n"),
[h . t] then
print("\n| ");
forget(map((String s) |-> print(s+" | "),h));
db_print_table(t)
}.
define One
db_print_row
(
Int32 -> SQLite3Datum row,
Int32 i
) =
if row(i) is
{
no_such_column then print("|"),
integer(n) then print(" "+n+" |"); db_print_row(row,i+1),
float(f) then print(" "+float_to_string(f,2)+" |"); db_print_row(row,i+1),
text(t) then print(" "+t+" |"); db_print_row(row,i+1),
blob(b) then print(" <blob "+length(b)+"> |"); db_print_row(row,i+1),
null then print(" NULL |"); db_print_row(row,i+1)
}.
public define Int32 -> String
text
(
Int32 -> SQLite3Datum row
) =
(Int32 i) |->
if row(i) is
{
no_such_column then "N/A",
integer(n) then integer_to_string(n),
float(f) then float_to_string(f,2),
text(t) then t,
blob(b) then " <blob "+length(b)+">",
null then " NULL "
}.
public define Int32 -> Maybe(Int32)
db_integer
(
Int32 -> SQLite3Datum row
) =
(Int32 i) |->
if row(i) is integer(n) then
success(n)
else
failure.
public define Int32 -> Int32
db_integer
(
Int32 -> SQLite3Datum row
) =
(Int32 i) |->
if row(i) is integer(n) then
n
else
0.
public define List(String)
db_make_string_list
(
List(String) so_far,
SQLite3HeadersOrRow -> SQLite3Row cursor
) =
if cursor(next_row) is
{
error(_) then so_far,
no_more_row then so_far,
row(explorer) then
with data = text(explorer)(0),
db_make_string_list( [ data . so_far], cursor)
}
.
public define One
db_print_table
(
One -> Maybe(Int32 -> SQLite3Datum) cursor
) =
if cursor(unique) is
{
failure then print("\n"),
success(row) then
print("\n|");
db_print_row(row,0);
db_print_table(cursor)
}.
define Int32
count_rows_private
(
SQLite3HeadersOrRow -> SQLite3Row cursor,
Int32 so_far
) =
if cursor(next_row) is
{
error(_) then 0,
no_more_row then so_far,
row(_) then count_rows_private(cursor, so_far + 1)
}.
/** Count the number of rows in the cursor. So this function call
* itself recursively until the cursor become empty. At each call
* it count the row.
* @param cursor, Database cursor, which must be counted
* @param so_far number of recursive call
*/
public define Int32
count_rows
(
SQLite3HeadersOrRow -> SQLite3Row cursor
) =
if cursor(next_row) is
{
error(_) then 0,
no_more_row then 0,
row(_) then count_rows_private(cursor, 1)
}.
*Description*
public define Maybe(List(String)) db_get_row(Int32 row_index, List(List(String)) row_list) =
if row_list is
{
[] then failure,
[h . t] then
if row_index = 0 then success(h)
else db_get_row(row_index-1, t)
}.
public define Maybe(String) db_row_get_datum(Int32 idx, List(String) data_list) =
if data_list is
{
[] then failure,
[h . t] then
if idx = 0 then success(h)
else db_row_get_datum(idx-1, t)
}.
public define Maybe(String) db_get_datum(Int32 datum_index, Int32 row_index, List(List(String)) rows_list) =
if db_get_row(row_index, rows_list) is
{
failure then failure,
success(row) then db_row_get_datum(datum_index, row)
}.
*Public*
*Description*
This function print on current output, the database error number with your own message
in msg
public define One print_db_error( Int32 n, String msg) =
print("Database Error: "+ msg +"\n" + "code[" + integer_to_string(n) + "]\n").
public define One print_db_error( SQLite3Error err, String msg) =
print("Database Error: "+ msg +"\n" + "code[" + err.code + "] "+err.text+"\n").
public define One print_db_error( SQLite3Error err) =
print("Database Error: code[" + err.code + "] "+err.text+"\n").
define One insertData( String theQuery) =
if l is
{
[ ] then print("\n"),
[h . t] then
print("\n| ");
forget(map((String s) |-> print(s+" | "),h));
print(t)
}.
define One master_table_each_entry( List(List(String)) l) =
if l is
{
[ ] then print(""),
[ h . t ] then
if h is
{
[ ] then print(""),
[ h . t ] then print(" type : " + h + "\n");
if t is
{
[ ] then print(""),
[ h . t ] then print(" name : " + h + "\n");
if t is
{
[ ] then print(""),
[ h . t ] then print("table name : " + h + "\n");
if t is
{
[ ] then print(""),
[ h . t ] then print(" rootpage : " + h + "\n");
if t is
{
[ ] then print(""),
[ h . t ] then print("sql string : " + h + "\n"); unique
}}}}}; master_table_each_entry(t)
}.
public define One db_print_master_table( SQLite3DataBase db) =
//make a query on the master table for ask all tables in database
//the table sqlite_master can only list by user
if sql_query(db, "select * from sqlite_master") is
{
error(n) then db_print_error(n, "Can't list all database table"),
ok(table) then
print("MASTER TABLE list\n");
master_table_each_entry(table)
}.
public define Bool db_table_exists( SQLite3DataBase db, String table) =
//if sql_query(db, "select " + table + " from sqlite_master") is
if sql_query(db, "select tbl_name from sqlite_master where tbl_name = '"+table+"'") is
{
error(n) then print("e Table '"+table+"' does not exist.\n"); false,
ok(table_content) then
if table_content is
{
[] then print("o Table '"+table+"' does not exist.\n"); false,
[ h. t] then print("content of table " +table + ":");db_print_table(table_content); true
}
}.
public define Bool db_table_exists_test( SQLite3DataBase db, String table) =
if sql_query(db, "select tbl_name from sqlite_master where tbl_name = '"+table+"'") is
{
error(n) then print("e Cannot read master_table.\n"); false,
ok(table_content) then
if table_content(next_row) is
{
error(_) then false,
no_more_row then print("Table "+table+" does not exist\n"); false,
row(row1) then print("Table "+table+" exists\n"); true
}
}.
public define Bool
db_table_exists
(
SQLite3DataBase db,
String table
) =
if sql_query(db, "select tbl_name from sqlite_master where tbl_name = '"+table+"'") is
{
error(n) then false,
ok(table_content) then
if table_content(next_row) is
{
error(_) then false,
no_more_row then false,
row(row1) then true
}
}.
define List(Int8)
db_make_sql_string
(
List(Int8) str
)
=
if str is
{
[ ] then [],
[h . t] then with tail = db_make_sql_string(t),
if h = '\'' then [h . [h . tail]]
else [h . tail]
}.
/**
* Convert to a SQL certified string and add quotes around.
*/
public define String
db_make_sql_string
(
String str
)
=
"'" + implode(db_make_sql_string(explode(str))) + "'".