hk_sql_utils.anubis
5.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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 02/04/2017
* Time: 19:23
* © Calexium
*/
read data_base/sqlite.anubis
read calexium_lib/database/db_utils.anubis
read hayamiki_lib/types/hayamiki.anubis
transmit hayamiki_lib/controller/hk_controller.anubis
public define String
referer_to_sql_clause
(
HK_Referer referer,
String table_name,
List(String) table_columns
)=
if referer is
{
no_referer then " (1 = 1) ",
hk_referer(referer_table, referer_row_id, fk_table, fk_column) then
if referer_table = table_name then
"id = " +referer_row_id
else if fk_column:table_columns then
fk_column + " = " + referer_row_id
else
" (1 = 1) ",
}
.
public define Maybe(One)
delete_from_ids
(
SQLite3DataBase db,
String table_name,
List(Int) ids
)=
if sql_query_timeout(db,
"DELETE FROM "+table_name+
" WHERE id IN ("+join(", ", to_List_String(ids))+");",
[],
"delete_from_ids") is
{
error(_) then failure,
ok(_, _, _) then success(unique)
}.
public define Int
get_count_by_clause
(
SQLite3DataBase db,
String table_name,
String clause
)=
with final_clause = if clause = "" then
""
else
"WHERE "+clause,
if sql_query_timeout(db, "SELECT COUNT(\"id\") FROM "+table_name+" "+final_clause+";", [], "get_count_by_clause") is
{
error(_) then 0,
ok(_, cursor, _) then db_get_integer(cursor)
}
.
public define Int
get_count
(
SQLite3DataBase db,
String table_name
)=
get_count_by_clause(db, table_name, "")
.
public define List(Int)
get_ids_by_clause
(
SQLite3DataBase db,
String table_name,
String clause
)=
with final_clause = if clause = "" then
""
else
"WHERE "+clause,
if sql_query_timeout(db, "SELECT \"id\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
{
error(_) then [],
ok(_, cursor, _) then db_get_integer_list(cursor)
}
.
public define List(Int)
get_ids_by_clause
(
SQLite3DataBase db,
String table_name,
String id_col,
String clause
)=
with final_clause = if clause = "" then
""
else
"WHERE "+clause,
if sql_query_timeout(db, "SELECT \""+id_col+"\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
{
error(_) then [],
ok(_, cursor, _) then db_get_integer_list(cursor)
}
.
relation_table
id = id of that relation
source_id = id of the first record in relation
target_id = id of the second record in relation
for example we search the all record targeted by source id
relation_id | source_id | target_id
-----------------------------------
0 | 1 | 1
1 | 1 | 2
2 | 1 | 4
3 | 0 | 2
if we want to get all relation from source matching with id 1 we will get a list of ids [1, 2, 4]
public define List($T)
get_relation
(
SQLite3DataBase db,
String relation_table,
String relation_source_id_col,
String relation_target_id_col,
String relation_condition, //only the right part atfer = in SQL format. This means the SQL query will be constructed with "relation_source_id_col = " relation_condition
String foreign_order,
(SQLite3DataBase db, String clause) -> List($T) get_all_by_clause_to_list_type
)=
//get
with ids = get_ids_by_clause(db, relation_table, relation_target_id_col, relation_source_id_col+" = "+relation_condition),
if length(ids) = 0 then
[]
else
with the_clause = " id IN ("+join(", ", to_List_String(ids))+") "+foreign_order,
get_all_by_clause_to_list_type(db, the_clause)
.
public define Maybe(Int)
insert_or_update
(
SQLite3DataBase db,
String table_name,
DB_id db_id,
List(SQLite3_update_field) fields
)=
if db_id is
{
none then
since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),
if sql_query_timeout(db, "INSERT INTO "+table_name+" "+insert_columns+" VALUES "+insert_values+";", binds, table_name+" (INSERT)") is
{
error(sql_error) then
//logError(debug_log, db_error(sql_error, table_name));
failure,
ok(_, cursor, _) then
if sql_query_scalar(db, "SELECT last_insert_rowid()") is
{
error(_) then failure,
ok(datum) then
with result1 = if datum is integer(n) then n else 0,
println(table_name+" inserted id ["+result1+"]");
success(result1)
}
// SQLite3DataBase db,
// String sql_command
// )
// success(unique)
},
db_id(idx) then
since make_update_clause_and_binds(fields) is (binds, set_string),
if sql_query_timeout(db, "UPDATE "+table_name+" SET "+set_string +" WHERE id = "+idx+";", binds, table_name+" (UPDATE)") is
{
error(sql_error) then
//logError(debug_log, db_error(sql_error, table_name));
failure,
ok(_, _, _) then success(idx)
}
}
.