db_utils.anubis
11 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
*
* User: David RENE
* Date: 11/12/2007
* Time: 01:16
* (c) Calexium
*
*/
read tools/basis.anubis
read system/logger.anubis
read system/string.anubis
read data_base/db_tools.anubis
read data_base/sqlite.anubis
transmit calexium_lib/database/db_types.anubis
transmit calexium_lib/database/db_utils.anubis
transmit calexium_lib/database/db_binds.anubis
transmit calexium_lib/database/db_update_fields.anubis
read calexium_lib/net_services_protocols/logger_client.anubis
public define Int one_year_seconds = 365 * 86400. // amount of seconds during 1 year
// public define Int one_month_seconds = 31 * 86400.
public define Int one_week_seconds = 7 * 86400. // amount of seconds during 1 week
public define Int one_day_seconds = 86400. // amount of seconds during 1 day
define String
__utime_to_string
(
UTime t
) =
to_decimal(t.seconds) + "." + zero_pad_n(6, t.microseconds ) + " s".
define One
__logLongQueries
(
UTime t0,
String sql_query,
String msg
) =
with t = unow - t0,
if t.seconds > 0 then
logTrace("", "SQL", logMask("sql_profiling"), "[SQL LONG QUERY] " + __utime_to_string(t) + " from ("+msg+") executing [" + sql_query + "]")
else
unique.
define One
__logQueries
(
UTime t0,
String sql_query,
String msg
) =
logTrace("", "SQL_TRACE", logMask("sql_trace"), "[SQL] " + __utime_to_string(unow - t0) + " from ("+msg+") executing [" + sql_query + "]").
public define Result(DbError, One)
sql_transaction
(
Database db,
String sql_command,
String message
) =
// logDebug(debug_log, "Entering into transaction [" + message + "].");
with t0 = unow,
if db_do_transaction(
db,
(One _) |->
if db_query(db, sql_command) is
{
error(err) then logError("", "DB", db_error(err,message));failure //error in the SQL request
ok(_,cursor,_) then success(unique)
},
success( (DbError err) |-> logError("", "DB", db_error(err,message))),
600000, // max 600s
200, // retry every 100 ms
(DbError _) |-> false
) is
{
error(err_and_result) then
// logDebug(debug_log, "Exiting from transaction [" + message + "] with error.");
__logLongQueries(t0, sql_command, message);
if err_and_result is (err, mb_result) then error(err),
ok(_) then
// logDebug(debug_log, "Exiting from transaction [" + message + "].");
__logLongQueries(t0, sql_command, message);
ok(unique)
}.
public define DbQueryResult
sql_query_timeout
(
Database db, //database handle
String sql_query, //sql query itself
List(DbBind) initial_bindings,
String msg //message to be shown if an error occure
) =
//we try with 30 sec of timeout
with t0 = unow,
if sql_query_timeout(db, sql_query, initial_bindings, 60, 100, (DbError _) |-> false) is
{
error(sql_error) then logError("", "DB", db_error(sql_error,msg) + " executing [" + sql_query + "]");
__logLongQueries(t0, sql_query, msg);
error(sql_error),
ok(headers, cursor, reset) then
__logLongQueries(t0, sql_query, msg);
ok(headers, cursor, reset)
}.
public define DbQueryResult
sql_query_timeout
(
Database db, //database handle
String sql_query, //sql query itself
String msg //message to be shown if an error occure
) =
sql_query_timeout(db, sql_query, [], msg).
public define Maybe(One -> DbRow)
sql_query_timeout
(
Database db, //database handle
String sql_query, //sql query itself
String msg //message to be shown if an error occure
) =
if sql_query_timeout(db, sql_query, [], msg) is
{
error(_) then failure,
ok(_,cursor,_) then success(cursor)
}.
// deprecated. Use one of the previous ones.
public define SQLite3QueryResult
sql_query_timeout
(
SQLite3DataBase db, //database handle
String sql_query, //sql query itself
List(SQLite3Bind) initial_bindings,
String msg //message to be shown if an error occure
) =
//we try with 30 sec of timeout
//println(" executing [" + sql_query + "]");
with t0 = unow,
if sql_query_timeout(db, sql_query, initial_bindings, 120, 100) is
{
error(sql_error) then
logError("", "DB", db_error(sql_error,msg) /*+ " executing [" + sql_query + "]"*/);
//println(" executing [" + sql_query + "]");
__logQueries(t0, "[sql_query_timeout Error] "+sql_query, msg);
__logLongQueries(t0, sql_query, msg);
error(sql_error),
ok(headers, cursor, reset) then
//__logQueries(t0, sql_query, msg);
__logLongQueries(t0, sql_query, msg);
ok(headers, cursor, reset)
}.
public define SQLite3QueryResult
sql_query_timeout
(
SQLite3DataBase db, //database handle
String sql_query, //sql query itself
String msg //message to be shown if an error occure
) =
sql_query_timeout(db, sql_query, [], msg).
public define SQLite3Row
sql_query_statement_timeout
(
SQLite3Stmt sql_command_stmt, //sql query itself
List(SQLite3Bind) initial_bindings,
String msg //message to be shown if an error occure
) =
//we try with 60 sec of timeout with
//println(" executing [" + msg + "]");
with t0 = unow,
if sql_query_statement_timeout(sql_command_stmt, initial_bindings, 120, 100, msg) is
{
error(sql_error) then
logError("", "DB", db_error(sql_error,msg) /*+ " executing [" + sql_query + "]"*/);
//println(" executing [" + sql_command_stmt + "]");
__logQueries(t0, "[sql_query_statement_timeout Error]", msg);
__logLongQueries(t0, "stmt", msg);
error(sql_error),
no_more_row then
//__logQueries(t0, "stmt", msg);
__logLongQueries(t0, "stmt", msg);
no_more_row
row(data) then
//__logQueries(t0, "stmt", msg);
__logLongQueries(t0, "stmt", msg);
row(data)
}.
// -- Extractors HELPERS ---------------
// sqlite3 API
public define String
db_get_String
(
One -> SQLite3Row table_cursor
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_String")); "",
no_more_row then "", //can't find the symbol in the table, because the row is empty
row(explorer) then text(explorer)(0)
}.
public define List(String)
db_get_string_list
(
One -> DbRow table_cursor,
List(String) so_far
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_string_list")); reverse(so_far),
no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty
row(explorer) then
with s = text(explorer)(0),
db_get_string_list(table_cursor, [s . so_far])
}.
// sqlite3 API
public define List(String)
db_get_string_list
(
One -> SQLite3Row table_cursor,
List(String) so_far
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_string_list")); reverse(so_far),
no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty
row(explorer) then
with s = text(explorer)(0),
db_get_string_list(table_cursor, [s . so_far])
}.
// sqlite3 API
public define List(String)
db_get_string_list
(
One -> SQLite3Row table_cursor
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_string_list")); [],
no_more_row then [], //can't find the symbol in the table, because the row is empty
row(explorer) then
with s = text(explorer)(0),
[ s . db_get_string_list(table_cursor)]
}.
// sqlite3 API
public define Int
db_get_integer
(
One -> SQLite3Row table_cursor
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_integer")); 0,
no_more_row then 0,
row(explorer) then (Int)db_integer(explorer)(0)
}.
public define Maybe(Int)
db_get_mb_integer
(
One -> SQLite3Row table_cursor
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_mb_integer")); failure,
no_more_row then failure,
row(explorer) then db_integer(explorer)(0)
}.
// sqlite3 API
public define List(Int)
db_get_integer_list
(
One -> SQLite3Row table_cursor,
List(Int) so_far
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far),
no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty
row(explorer) then
with s = (Int)db_integer(explorer)(0),
db_get_integer_list(table_cursor, [s . so_far])
}.
public define List(Int)
db_get_integer_list
(
One -> SQLite3Row table_cursor
) =
db_get_integer_list(table_cursor, []).
// sqlite3 API
public define Float
db_get_Float
(
One -> SQLite3Row table_cursor
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_Float")); 0.0,
no_more_row then 0.0,
row(explorer) then db_float(explorer)(0)
}.
public define List(Int)
db_get_integer_list
(
One -> DbRow table_cursor,
List(Int) so_far
) =
if table_cursor(unique) is
{
error(sql_error) then logError("", "DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far),
no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty
row(explorer) then
with s = (Int)db_integer(explorer)(0),
db_get_integer_list(table_cursor, [s . so_far])
}.
public define List(Int)
db_get_integer_list
(
One -> DbRow table_cursor,
) =
db_get_integer_list(table_cursor, []).
Help to construct clause and a list of bind according to list of SQLite3_update_field.
This is useful when we want to construct a SQL query with only needs fields.
// -- Migration HELPERS ---------------
public define DbBind
bind_String
(
String name,
String value
) =
db_bind(force(sub_string(name, 1, length(name) - 1), name), db_text(value)).
public define DbBind
bind_ByteArray
(
String name,
ByteArray value
) =
db_bind(force(sub_string(name, 1, length(name) - 1), name), db_blob(value)).
public define DbBind
bind_Int
(
String name,
Int value
) =
db_bind(force(sub_string(name, 1, length(name) - 1), name), db_integer(value)).
public define DbBind
bind_NULL
(
String name,
) =
db_bind(force(sub_string(name, 1, length(name) - 1), name), null).