Commit af1c6a960ac9d2a32ea22a01939206ccc5a8b079
1 parent
7de986f5
database/db_utils.anubis :
add make_clause_and_binds for constructing clause and bind list from SQLite3_update_field add bind_Int_or_NULL add bind_String_or_NULL add bind_Datetime add bind_Date add bind_Bool database/db_types.anubis: add to_DBInt for boolean value
Showing
2 changed files
with
93 additions
and
4 deletions
Show diff stats
calexium_lib/database/db_types.anubis
| @@ -38,14 +38,18 @@ public define String | @@ -38,14 +38,18 @@ public define String | ||
| 38 | ( | 38 | ( |
| 39 | DB_date d | 39 | DB_date d |
| 40 | )= | 40 | )= |
| 41 | - d.date. | 41 | + date(d). |
| 42 | + | ||
| 43 | + | ||
| 42 | 44 | ||
| 43 | public define String | 45 | public define String |
| 44 | to_String | 46 | to_String |
| 45 | ( | 47 | ( |
| 46 | DB_datetime d | 48 | DB_datetime d |
| 47 | )= | 49 | )= |
| 48 | - d.datetime. | 50 | + datetime(d). |
| 51 | + | ||
| 52 | + | ||
| 49 | 53 | ||
| 50 | public define String | 54 | public define String |
| 51 | to_DBString | 55 | to_DBString |
| @@ -54,6 +58,19 @@ public define String | @@ -54,6 +58,19 @@ public define String | ||
| 54 | )= | 58 | )= |
| 55 | if value then "1" else "0". | 59 | if value then "1" else "0". |
| 56 | 60 | ||
| 61 | +public define Int | ||
| 62 | + to_DBInt | ||
| 63 | + ( | ||
| 64 | + Bool value | ||
| 65 | + )= | ||
| 66 | + if value then 1 else 0. | ||
| 67 | + | ||
| 57 | public type SQLite3_update_field: | 68 | public type SQLite3_update_field: |
| 58 | field(String column, SQLite3Bind bind). | 69 | field(String column, SQLite3Bind bind). |
| 59 | 70 | ||
| 71 | + | ||
| 72 | + * Some Bind helper * | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + |
calexium_lib/database/db_utils.anubis
| @@ -13,8 +13,14 @@ read system/string.anubis | @@ -13,8 +13,14 @@ read system/string.anubis | ||
| 13 | read data_base/db_tools.anubis | 13 | read data_base/db_tools.anubis |
| 14 | read data_base/sqlite.anubis | 14 | read data_base/sqlite.anubis |
| 15 | 15 | ||
| 16 | +read calexium_lib/database/db_types.anubis | ||
| 16 | read calexium_lib/net_services_protocols/logger_service.anubis | 17 | read calexium_lib/net_services_protocols/logger_service.anubis |
| 17 | 18 | ||
| 19 | +public define Int one_year_seconds = 365 * 86400. // amount of seconds during 1 year | ||
| 20 | +// public define Int one_month_seconds = 31 * 86400. | ||
| 21 | +public define Int one_week_seconds = 7 * 86400. // amount of seconds during 1 week | ||
| 22 | +public define Int one_day_seconds = 86400. // amount of seconds during 1 day | ||
| 23 | + | ||
| 18 | define String | 24 | define String |
| 19 | __utime_to_string | 25 | __utime_to_string |
| 20 | ( | 26 | ( |
| @@ -159,7 +165,7 @@ public define List(String) | @@ -159,7 +165,7 @@ public define List(String) | ||
| 159 | db_get_string_list(table_cursor, [s . so_far]) | 165 | db_get_string_list(table_cursor, [s . so_far]) |
| 160 | }. | 166 | }. |
| 161 | 167 | ||
| 162 | -// Old sqlite3 API | 168 | +// sqlite3 API |
| 163 | public define List(String) | 169 | public define List(String) |
| 164 | db_get_string_list | 170 | db_get_string_list |
| 165 | ( | 171 | ( |
| @@ -175,7 +181,7 @@ public define List(String) | @@ -175,7 +181,7 @@ public define List(String) | ||
| 175 | db_get_string_list(table_cursor, [s . so_far]) | 181 | db_get_string_list(table_cursor, [s . so_far]) |
| 176 | }. | 182 | }. |
| 177 | 183 | ||
| 178 | -// Old sqlite3 API | 184 | +// sqlite3 API |
| 179 | public define List(Int) | 185 | public define List(Int) |
| 180 | db_get_integer_list | 186 | db_get_integer_list |
| 181 | ( | 187 | ( |
| @@ -208,7 +214,73 @@ public define List(Int) | @@ -208,7 +214,73 @@ public define List(Int) | ||
| 208 | }. | 214 | }. |
| 209 | 215 | ||
| 210 | 216 | ||
| 217 | + Help to construct clause and a list of bind according to list of SQLite3_update_field. | ||
| 218 | + This is useful when we want to construct a SQL query with only needs fields. | ||
| 219 | + | ||
| 220 | + | ||
| 221 | +public define (List(SQLite3Bind), String) | ||
| 222 | + make_clause_and_binds | ||
| 223 | + ( | ||
| 224 | + List(SQLite3_update_field) fields, | ||
| 225 | + List(SQLite3Bind) so_far, | ||
| 226 | + List(String) clause | ||
| 227 | + )= | ||
| 228 | + if fields is | ||
| 229 | + { | ||
| 230 | + [] then (so_far, join(", ",clause)), | ||
| 231 | + [ h . t ] then | ||
| 232 | + if h is field(column, bind) then | ||
| 233 | + make_clause_and_binds(t, [bind . so_far], [column+" = :v_"+column . clause]) | ||
| 234 | + }. | ||
| 235 | + | ||
| 236 | + | ||
| 237 | + extended version of bind for SQLite3. | ||
| 238 | + | ||
| 239 | +public define SQLite3Bind | ||
| 240 | + bind_Int_or_NULL | ||
| 241 | + ( | ||
| 242 | + String name, | ||
| 243 | + Int value | ||
| 244 | + )= | ||
| 245 | + if value = 0 then | ||
| 246 | + bind_NULL(name) | ||
| 247 | + else | ||
| 248 | + bind_Int(name, value). | ||
| 211 | 249 | ||
| 250 | +public define SQLite3Bind | ||
| 251 | + bind_String_or_NULL | ||
| 252 | + ( | ||
| 253 | + String name, | ||
| 254 | + String value | ||
| 255 | + )= | ||
| 256 | + if value = "" then | ||
| 257 | + bind_NULL(name) | ||
| 258 | + else | ||
| 259 | + bind_String(name, value). | ||
| 260 | + | ||
| 261 | +public define SQLite3Bind | ||
| 262 | + bind_Datetime | ||
| 263 | + ( | ||
| 264 | + String name, | ||
| 265 | + DB_datetime dt | ||
| 266 | + )= | ||
| 267 | + bind_String(name, datetime(dt)). | ||
| 268 | + | ||
| 269 | +public define SQLite3Bind | ||
| 270 | + bind_Date | ||
| 271 | + ( | ||
| 272 | + String name, | ||
| 273 | + DB_date d | ||
| 274 | + )= | ||
| 275 | + bind_String(name, date(d)). | ||
| 276 | + | ||
| 277 | +public define SQLite3Bind | ||
| 278 | + bind_Bool | ||
| 279 | + ( | ||
| 280 | + String name, | ||
| 281 | + Bool value | ||
| 282 | + )= | ||
| 283 | + bind_Int(name, to_DBInt(value)). | ||
| 212 | 284 | ||
| 213 | 285 | ||
| 214 | // -- Migration HELPERS --------------- | 286 | // -- Migration HELPERS --------------- |