diff --git a/database/db_binds.anubis b/database/db_binds.anubis new file mode 100644 index 0000000..ff428b9 --- /dev/null +++ b/database/db_binds.anubis @@ -0,0 +1,101 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 17/03/2017 + * Time: 00:14 + * © Calexium + */ + + + extended version of bind for SQLite3. + +public define SQLite3Bind + bind_DB_id_or_NULL + ( + String _name, + DB_id _value + )= + if _value is + { + none then bind_NULL(_name) + db_id(value) then bind_Int(_name, value) + } +. + +public define SQLite3Bind + bind_DB_id_or_NULL + ( + String _name, + Maybe(DB_id) mb_value + )= + if mb_value is + { + failure then bind_NULL(_name) + success(id) then bind_DB_id_or_NULL(_name, id) + } +. + +public define SQLite3Bind + bind_Int_or_NULL + ( + String name, + Maybe(Int) mb_value + )= + if mb_value is + { + failure then bind_NULL(name), + success(value) then 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_Time + ( + String name, + DB_time d + )= + bind_String(name, time(d)). + +public define SQLite3Bind + bind_Bool + ( + String name, + Bool value + )= + bind_Int(name, to_DBInt(value)). + +public define SQLite3Bind + bind_BLOB + ( + String name, + $V value + )= + bind_ByteArray(name, serialize(value)). + diff --git a/database/db_types.anubis b/database/db_types.anubis index 02084f1..b2ed5a6 100644 --- a/database/db_types.anubis +++ b/database/db_types.anubis @@ -1,6 +1,6 @@ /* * Created by PyramIDE. - * User: Totoro + * User: フランスのトトロ aka (David RENÉ) * Date: 14/11/2011 * Time: 22:41 * @@ -89,9 +89,7 @@ public define Int none then (Int)-1, pk(_idx) then _idx }. - - public define String to_String ( @@ -99,8 +97,6 @@ public define String )= date(d). - - public define String to_String ( @@ -129,8 +125,7 @@ public define Int )= if value then 1 else 0. -public type SQLite3_update_field: - field(String column, SQLite3Bind bind). + * Some Bind helper * diff --git a/database/db_update_fields.anubis b/database/db_update_fields.anubis new file mode 100644 index 0000000..015dd74 --- /dev/null +++ b/database/db_update_fields.anubis @@ -0,0 +1,43 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 17/03/2017 + * Time: 00:11 + * © Calexium + */ + + +public type SQLite3_update_field: + field(String column, SQLite3Bind bind). + +public define (List(SQLite3Bind), String) + make_update_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]) + } +. + +public define (List(SQLite3Bind), String) + make_insert_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]) + } +. diff --git a/database/db_utils.anubis b/database/db_utils.anubis index 3db732d..12c7406 100644 --- a/database/db_utils.anubis +++ b/database/db_utils.anubis @@ -287,92 +287,10 @@ public define List(Int) 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_DB_id_or_NULL - ( - String _name, - DB_id _value - )= - if _value is - { - none then bind_NULL(_name) - db_id(value) then bind_Int(_name, value) - } -. - - -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)). - -public define SQLite3Bind - bind_BLOB - ( - String name, - $V value - )= - bind_ByteArray(name, serialize(value)). + + // -- Migration HELPERS --------------- public define DbBind -- libgit2 0.21.4