db_utils.anubis
3.95 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
/*
*
* User: David RENE
* Date: 11/12/2007
* Time: 01:16
* (c) Calexium
*
*/
read tools/basis.anubis
read system/logger.anubis
read data_base/sqlite.anubis
read calexium_lib/net_services_protocols/logger_service.anubis
public define Result(SQLite3Error, One)
sql_transaction
(
SQLite3DataBase db,
String sql_command,
String message
) =
// logDebug(debug_log, "Entering into transaction [" + message + "].");
if db_do_transaction(
db,
(One _) |->
if sql_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( (SQLite3Error err) |-> logError("DB", db_error(err,message))),
60000, // max 60s
100 // retry every 100 ms
) is
{
error(err_and_result) then
// logDebug(debug_log, "Exiting from transaction [" + message + "] with error.");
if err_and_result is (err, mb_result) then
error(err),
ok(_) then
// logDebug(debug_log, "Exiting from transaction [" + message + "].");
ok(unique)
}.
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
if sql_query_timeout(db, sql_query, initial_bindings, 60, 100) is
{
error(sql_error) then logError("DB", db_error(sql_error,msg)); error(sql_error),
ok(headers, cursor, reset) then ok(headers, cursor, reset)
}.
// deprecated. Use one of the previous ones.
public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
sql_query_timeout
(
SQLite3DataBase db, //database handle
String sql_query, //sql query itself
String msg //message to be shown if an error occure
) =
//we try with 30 sec of timeout
if sql_query_timeout(db, sql_query, [], 60, 100) is
{
error(sql_error) then logError("DB", db_error(sql_error,msg));failure,
ok(headers, cursor, reset) then
success((SQLite3HeadersOrRow h_or_r) |-> if h_or_r is
{
headers then
with cols = map((String c) |-> (SQLite3Datum)text(c), headers(unique)),
row((Int n) |-> force(nth(n, cols), no_such_column)),
next_row then cursor(unique)
})
}.
// -- Extractors HELPERS ---------------
public define List(String)
db_get_string_list
(
SQLite3HeadersOrRow -> SQLite3Row table_cursor,
List(String) so_far
) =
if table_cursor(next_row) 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])
}.
public define List(Int)
db_get_integer_list
(
SQLite3HeadersOrRow -> SQLite3Row table_cursor,
List(Int) so_far
) =
if table_cursor(next_row) 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])
}.