diff --git a/controller/hk_controller.anubis b/controller/hk_controller.anubis index d370787..55792ef 100644 --- a/controller/hk_controller.anubis +++ b/controller/hk_controller.anubis @@ -6,6 +6,80 @@ * © Calexium */ - +read tools/basis.anubis +transmit hayamiki_lib/types/hayamiki.anubis transmit hayamiki_lib/controller/hk_delete.anubis +transmit hayamiki_lib/controller/hk_trigger.anubis + transmit kayamiki_lib/controller/hk_rows_import.anubis + +transmit calexium_lib/web/CXM_common.anubis +transmit calexium_lib/web/CXM_making_a_web_site.anubis +transmit calexium_lib/database/db_types.anubis + + //CRUD basis function + + //Create + //Read + //Update + //Delete + +public type HK_Table_controller: + hk_table_controller( + String table_name, + //Create + (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type, + (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(Int) write_from_fields, + //Read + (SQLite3DataBase db, String clause) -> Int get_count, + (SQLite3DataBase db, List(String) columns, Bool resolve_fk, DB_id) -> JsonValue get_one_json, + (SQLite3DataBase db, List(String) columns, Bool resolve_fk, HK_Referer referer, String clause, String order_by) -> JsonValue get_rows_json, + //Update + //Delete + (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete, + + //view interface + //selector + (SQLite3DataBase db, HK_Referer referer, String filter) -> List((List(CoreAttrs), WebArgValue, String)) get_selector, + + //Triggers of that table, can be dynamically updated + HK_Trigger_Set triggers + ) +. + +public define Maybe(HK_Table_controller) + get_hk_controller + ( + List(HK_Table_controller) controllers, + String _table_name + )= + if controllers is + { + [] then failure, + [h . t ] then + if h.table_name = _table_name then + success(h) + else + get_hk_controller(t, _table_name) + } +. + + +public define One + add_trigger_to_table + ( + List(HK_Table_controller) controllers, + String _table_name, + HK_Trigger trigger_to_add + )= + if get_hk_controller(controllers, _table_name) is + { + failure then unique + success(ctrl) then + since trigger_to_add is hk_trigger(name, t_type, call_back), + if t_type is + { + before_update then add_trigger(ctrl.triggers.before_update, trigger_to_add), + after_update then add_trigger(ctrl.triggers.after_update, trigger_to_add) + } + } +. diff --git a/controller/hk_delete.anubis b/controller/hk_delete.anubis index bc6d97a..759e879 100644 --- a/controller/hk_delete.anubis +++ b/controller/hk_delete.anubis @@ -9,19 +9,18 @@ read calexium_lib/web/CXM_common.anubis read calexium_lib/web/CXM_web_arg_utils.anubis //TODO move to hayamiki lib as view part of MVC -read hayamiki_lib/view/view_table_manager.anubis +//read hayamiki_lib/view/view_table_manager.anubis +read hayamiki_lib/controller/hk_sql_utils.anubis public define Result(String, String) hk_delete_row ( List(Web_arg) lwa, - TM_delete_list d_list, //list of all delete row function for database SQLite3DataBase db )= - if get_String(lwa, "table_name") is {failure then error("CANT_GET_ARGUMENT"), success(table_name) then - if get_Int(lwa, "id") is {failure then error("CANT_GET_ARGUMENT"), success(id) then - if tm_get_delete(table_name, d_list) is {failure then error("DELETE_FUNC_DOESNT_EXIST"), success(hk_delete) then - if hk_delete(db, [id]) is {failure then error("DELETE_ROW_DB_ERROR"), success(_) then - ok("DELETE_ROW_OK") - }}}}. - + if get_String(lwa, "table_name") is {failure then error("CANT_GET_ARGUMENT"), success(table_name) then + with ids = get_Int_list(lwa, "id"), + if delete_from_ids(db, table_name, ids) is {failure then error("DELETE_ROW_DB_ERROR"), success(_) then + ok("DELETE_ROW_OK") + }} +. diff --git a/controller/hk_sql_utils.anubis b/controller/hk_sql_utils.anubis index d770e87..c6af667 100644 --- a/controller/hk_sql_utils.anubis +++ b/controller/hk_sql_utils.anubis @@ -6,6 +6,7 @@ * © Calexium */ +read data_base/sqlite.anubis read calexium_lib/database/db_utils.anubis read hayamiki_lib/types/hayamiki.anubis @@ -93,3 +94,46 @@ public define List(Int) ok(_, cursor, _) then db_get_integer_list(cursor) } . + +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) + } + } +. diff --git a/controller/hk_trigger.anubis b/controller/hk_trigger.anubis new file mode 100644 index 0000000..e1d596e --- /dev/null +++ b/controller/hk_trigger.anubis @@ -0,0 +1,88 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 18/06/2017 + * Time: 23:12 + * © Calexium + */ + +transmit calexium_lib/web/CXM_making_a_web_site.anubis + +public type HK_Trigger_Result: + ok, + error, + abort +. + +public type HK_Trigger_Type: + before_update, + after_update +. + +public type HK_Trigger: + hk_trigger( + String trigger_name, + HK_Trigger_Type trigger_type, + ((SQLite3DataBase db, WEB_Session session ) -> HK_Trigger_Result) trigger_call_back) +. + +public type HK_Trigger_Set: + hk_trigger_set( + Var(List(HK_Trigger)) before_update, //must be called before update the requested + Var(List(HK_Trigger)) after_update //must be called after update the requested + ) +. + +public define Maybe(HK_Trigger) + get_trigger + ( + List(HK_Trigger) triggers, + String _trigger_name + ) = + if triggers is + { + [] then failure, + [h . t] then + if h.trigger_name = _trigger_name then + success(h) + else + get_trigger(t, _trigger_name) + } +. + +public define One + add_trigger + ( + Var(List(HK_Trigger)) triggers, + HK_Trigger added_trigger + )= + println(">>> TRIGGER "+added_trigger.trigger_name+" added"); + triggers <- [added_trigger . *triggers] +. + + +define List(HK_Trigger) + _remove_trigger + ( + List(HK_Trigger) triggers, + String _trigger_name + )= + if triggers is + { + [] then [], + [h . t] then + if h.trigger_name = _trigger_name then + _remove_trigger(t, _trigger_name) + else + [h . _remove_trigger(t, _trigger_name)] + } +. + +public define One + remove_trigger + ( + Var(List(HK_Trigger)) triggers, + String trigger_name + )= + triggers <- _remove_trigger(*triggers, trigger_name) +. diff --git a/view/view_table_manager.anubis b/view/view_table_manager.anubis index 3e42ac0..49be902 100644 --- a/view/view_table_manager.anubis +++ b/view/view_table_manager.anubis @@ -84,44 +84,29 @@ public define Maybe(VTM_edit) // ) = // _tm_get_writer(table_name, tm_wlist.list). -public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One)) - _tm_get_delete - ( - String table_name, - List(TM_delete) list - ) = - if list is - { - [] then failure, - [ h . t ] then - since h is tm_delete(tb_name, delete_func), - if tb_name = table_name then - success(delete_func) - else - _tm_get_delete(table_name, t) - }. - -public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One)) - tm_get_delete - ( - String table_name, - TM_delete_list tm_dlist - ) = - _tm_get_delete(table_name, tm_dlist.list). +//public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One)) +// _tm_get_delete +// ( +// String table_name, +// List(TM_delete) list +// ) = +// if list is +// { +// [] then failure, +// [ h . t ] then +// since h is tm_delete(tb_name, delete_func), +// if tb_name = table_name then +// success(delete_func) +// else +// _tm_get_delete(table_name, t) +// }. +// +//public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One)) +// tm_get_delete +// ( +// String table_name, +// TM_delete_list tm_dlist +// ) = +// _tm_get_delete(table_name, tm_dlist.list). + -public define Maybe(HK_Table_controller) - get_hk_controller - ( - String _table_name, - List(HK_Table_controller) controllers - )= - if controllers is - { - [] then failure, - [h . t ] then - if h.table_name = _table_name then - success(h) - else - get_hk_controller(_table_name, t) - } -. diff --git a/view/view_table_manager_types.anubis b/view/view_table_manager_types.anubis index 4452259..5ad85bd 100644 --- a/view/view_table_manager_types.anubis +++ b/view/view_table_manager_types.anubis @@ -10,12 +10,13 @@ transmit hayamiki_lib/view/types/hk_form.anubis transmit hayamiki_lib/view/types/hk_view.anubis transmit hayamiki_lib/view/types/hk_filter.anubis transmit calexium_lib/web/CXM_common.anubis -read hayamiki_lib/types/hayamiki.anubis +transmit hayamiki_lib/controller/hk_controller.anubis +//read hayamiki_lib/types/hayamiki.anubis -read calexium_lib/web/CXM_common.anubis -read calexium_lib/web/CXM_making_a_web_site.anubis -read calexium_lib/database/db_types.anubis + +//read calexium_lib/web/CXM_common.anubis +//read calexium_lib/database/db_types.anubis public type VTM_edit: vtm_edit(String table_name, (SQLite3DataBase db, HK_Form_action action, String edit_view_name) -> HK_Form get_edit). @@ -23,49 +24,10 @@ public type VTM_edit: public type VTM_edit_list: vtm_edit_list(List(VTM_edit) list). -//public type TM_writer: -// tm_writer( -// String table_name, -// (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type, -// (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_fields -// ) -//. - //CRUD basis function + - //Create - //Read - //Update - //Delete - -public type HK_Table_controller: - hk_table_controller( - String table_name, - //Create - (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type, - (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_fields, - //Read - (SQLite3DataBase db, String clause) -> Int get_count, - (SQLite3DataBase db, List(String) columns, Bool resolve_fk, DB_id) -> JsonValue get_one_json, - (SQLite3DataBase db, List(String) columns, Bool resolve_fk, HK_Referer referer, String clause, String order_by) -> JsonValue get_rows_json, - //Update - //Delete - (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete, - - //view interface - //selector - (SQLite3DataBase db, HK_Referer referer, String filter) -> List((List(CoreAttrs), WebArgValue, String)) get_selector - ) -. - -//public type TM_writer_list: -// tm_writer_list(List(TM_writer) list). -public type TM_delete: - tm_delete(String table_name, (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete). -public type TM_delete_list: - tm_delete_list(List(TM_delete) list). - + public type VTM_view: vtm_view(String table_name, (String view_name) -> VT_view get_view, (One) -> List((String, List(HK_Filter))) get_all_view_name). @@ -75,6 +37,4 @@ public type VTM_view_list: public type VTM: vtm(VTM_view_list view_list, VTM_edit_list edit_list, - //TM_writer_list writer_list, - TM_delete_list delete_list, List(HK_Table_controller) hk_controllers). -- libgit2 0.21.4