From af1c6a960ac9d2a32ea22a01939206ccc5a8b079 Mon Sep 17 00:00:00 2001 From: David RENE Date: Sat, 18 Aug 2012 23:09:22 +0000 Subject: [PATCH] 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 --- calexium_lib/database/db_types.anubis | 21 +++++++++++++++++++-- calexium_lib/database/db_utils.anubis | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/calexium_lib/database/db_types.anubis b/calexium_lib/database/db_types.anubis index 12795f1..688da57 100644 --- a/calexium_lib/database/db_types.anubis +++ b/calexium_lib/database/db_types.anubis @@ -38,14 +38,18 @@ public define String ( DB_date d )= - d.date. + date(d). + + public define String to_String ( DB_datetime d )= - d.datetime. + datetime(d). + + public define String to_DBString @@ -54,6 +58,19 @@ public define String )= if value then "1" else "0". +public define Int + to_DBInt + ( + Bool value + )= + if value then 1 else 0. + public type SQLite3_update_field: field(String column, SQLite3Bind bind). + + * Some Bind helper * + + + + diff --git a/calexium_lib/database/db_utils.anubis b/calexium_lib/database/db_utils.anubis index 045a2a3..5b5a4d5 100644 --- a/calexium_lib/database/db_utils.anubis +++ b/calexium_lib/database/db_utils.anubis @@ -13,8 +13,14 @@ read system/string.anubis read data_base/db_tools.anubis read data_base/sqlite.anubis +read calexium_lib/database/db_types.anubis read calexium_lib/net_services_protocols/logger_service.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 ( @@ -159,7 +165,7 @@ public define List(String) db_get_string_list(table_cursor, [s . so_far]) }. -// Old sqlite3 API +// sqlite3 API public define List(String) db_get_string_list ( @@ -175,7 +181,7 @@ public define List(String) db_get_string_list(table_cursor, [s . so_far]) }. -// Old sqlite3 API +// sqlite3 API public define List(Int) db_get_integer_list ( @@ -208,7 +214,73 @@ public define List(Int) }. + 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. + + +public define (List(SQLite3Bind), String) + make_clause_and_binds + ( + List(SQLite3_update_field) fields, + List(SQLite3Bind) so_far, + List(String) clause + )= + if fields is + { + [] then (so_far, join(", ",clause)), + [ h . t ] then + if h is field(column, bind) then + make_clause_and_binds(t, [bind . so_far], [column+" = :v_"+column . clause]) + }. + + + extended version of bind for SQLite3. + +public define SQLite3Bind + bind_Int_or_NULL + ( + String name, + Int value + )= + if value = 0 then + bind_NULL(name) + else + bind_Int(name, value). +public define SQLite3Bind + bind_String_or_NULL + ( + String name, + String value + )= + if value = "" then + bind_NULL(name) + else + bind_String(name, value). + +public define SQLite3Bind + bind_Datetime + ( + String name, + DB_datetime dt + )= + bind_String(name, datetime(dt)). + +public define SQLite3Bind + bind_Date + ( + String name, + DB_date d + )= + bind_String(name, date(d)). + +public define SQLite3Bind + bind_Bool + ( + String name, + Bool value + )= + bind_Int(name, to_DBInt(value)). // -- Migration HELPERS --------------- -- libgit2 0.21.4