Commit 16f88339ed7fffa8e4573466da9e0b3573f9cf1f
1 parent
f80bd32b
add more helper in sql_utils:
- update_with_clause (make conditional update) - get_all_to_json (extract only fields by given extractors from the table cursor result to a list of JSON value)
Showing
1 changed file
with
60 additions
and
0 deletions
Show diff stats
controller/hk_sql_utils.anubis
| @@ -235,6 +235,46 @@ public define (String, String, String, String) -> Maybe(One -> SQLite3Row) | @@ -235,6 +235,46 @@ public define (String, String, String, String) -> Maybe(One -> SQLite3Row) | ||
| 235 | ok(_, cursor, _) then success(cursor) | 235 | ok(_, cursor, _) then success(cursor) |
| 236 | } | 236 | } |
| 237 | . | 237 | . |
| 238 | + | ||
| 239 | + /***** JSON *****/ | ||
| 240 | + | ||
| 241 | +public define Maybe(JsonValue) | ||
| 242 | + extract_cursor_to_json | ||
| 243 | + ( | ||
| 244 | + SQLite3DataBase db, | ||
| 245 | + List(((Int -> SQLite3Datum) -> JsonMember)) extractors, | ||
| 246 | + One -> SQLite3Row table_cursor | ||
| 247 | + ) = | ||
| 248 | + //get next cursor | ||
| 249 | + if table_cursor(unique) is | ||
| 250 | + { | ||
| 251 | + error(sql_error) then println(db_error(sql_error, "extract_cursor_to_json")); failure, | ||
| 252 | + no_more_row then failure, | ||
| 253 | + row(cursor) then | ||
| 254 | + success( | ||
| 255 | + json_object( | ||
| 256 | + map(((Int -> SQLite3Datum) -> JsonMember extractor) |-> extractor(cursor) , extractors) | ||
| 257 | + )) | ||
| 258 | + } | ||
| 259 | +. | ||
| 260 | + | ||
| 261 | +public define List(JsonValue) | ||
| 262 | + get_all_to_json | ||
| 263 | + ( | ||
| 264 | + SQLite3DataBase db, | ||
| 265 | + List(((Int -> SQLite3Datum) -> JsonMember)) extractors, | ||
| 266 | + One -> SQLite3Row table_cursor, | ||
| 267 | + List(JsonValue) so_far | ||
| 268 | + ) = | ||
| 269 | + if extract_cursor_to_json(db, extractors, table_cursor) is | ||
| 270 | + { | ||
| 271 | + failure then reverse(so_far), | ||
| 272 | + success(data) then get_all_to_json(db, extractors, table_cursor, [data . so_far]) | ||
| 273 | + } | ||
| 274 | +. | ||
| 275 | + | ||
| 276 | + | ||
| 277 | + /***** Type $T ******/ | ||
| 238 | 278 | ||
| 239 | define (One -> SQLite3Row, List($T)) ->List($T) | 279 | define (One -> SQLite3Row, List($T)) ->List($T) |
| 240 | _get_all | 280 | _get_all |
| @@ -459,6 +499,7 @@ public define Maybe($T) | @@ -459,6 +499,7 @@ public define Maybe($T) | ||
| 459 | success(id) then get_by_clause_to_mb_type(db, "id = "+id) | 499 | success(id) then get_by_clause_to_mb_type(db, "id = "+id) |
| 460 | } | 500 | } |
| 461 | . | 501 | . |
| 502 | + | ||
| 462 | public define Maybe(Int) | 503 | public define Maybe(Int) |
| 463 | insert_or_update | 504 | insert_or_update |
| 464 | ( | 505 | ( |
| @@ -501,3 +542,22 @@ public define Maybe(Int) | @@ -501,3 +542,22 @@ public define Maybe(Int) | ||
| 501 | } | 542 | } |
| 502 | } | 543 | } |
| 503 | . | 544 | . |
| 545 | + | ||
| 546 | +public define Maybe(One) | ||
| 547 | + update_with_clause | ||
| 548 | + ( | ||
| 549 | + SQLite3DataBase db, | ||
| 550 | + String table_name, | ||
| 551 | + String clause, | ||
| 552 | + List(SQLite3_update_field) fields | ||
| 553 | + )= | ||
| 554 | + | ||
| 555 | + since make_update_clause_and_binds(fields) is (binds, set_string), | ||
| 556 | + if sql_query_timeout(db, "UPDATE "+table_name+" SET "+set_string +" "+clause+";", binds, table_name+" (UPDATE)") is | ||
| 557 | + { | ||
| 558 | + error(sql_error) then | ||
| 559 | + //logError(debug_log, db_error(sql_error, table_name)); | ||
| 560 | + failure, | ||
| 561 | + ok(_, _, _) then success(unique) | ||
| 562 | + } | ||
| 563 | +. |