Commit 529092e06cade1790e0791fb346e2daa3cb987e5

Authored by totoro
1 parent 601f9714

add dynamic trigger

move the HK_Table_Controller into controller/hk_controller.anubis
remove the old TM_delete and the TM_writer
add generic insert_or_update in hk_sql_utils which replace the generated function for each table.
hk_delete_row now use a generic delete function.
controller/hk_controller.anubis
@@ -6,6 +6,80 @@ @@ -6,6 +6,80 @@
6 * © Calexium 6 * © Calexium
7 */ 7 */
8 8
9 - 9 +read tools/basis.anubis
  10 +transmit hayamiki_lib/types/hayamiki.anubis
10 transmit hayamiki_lib/controller/hk_delete.anubis 11 transmit hayamiki_lib/controller/hk_delete.anubis
  12 +transmit hayamiki_lib/controller/hk_trigger.anubis
  13 +
11 transmit kayamiki_lib/controller/hk_rows_import.anubis 14 transmit kayamiki_lib/controller/hk_rows_import.anubis
  15 +
  16 +transmit calexium_lib/web/CXM_common.anubis
  17 +transmit calexium_lib/web/CXM_making_a_web_site.anubis
  18 +transmit calexium_lib/database/db_types.anubis
  19 +
  20 + //CRUD basis function +
  21 + //Create
  22 + //Read
  23 + //Update
  24 + //Delete
  25 +
  26 +public type HK_Table_controller:
  27 + hk_table_controller(
  28 + String table_name,
  29 + //Create
  30 + (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type,
  31 + (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(Int) write_from_fields,
  32 + //Read
  33 + (SQLite3DataBase db, String clause) -> Int get_count,
  34 + (SQLite3DataBase db, List(String) columns, Bool resolve_fk, DB_id) -> JsonValue get_one_json,
  35 + (SQLite3DataBase db, List(String) columns, Bool resolve_fk, HK_Referer referer, String clause, String order_by) -> JsonValue get_rows_json,
  36 + //Update
  37 + //Delete
  38 + (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete,
  39 +
  40 + //view interface
  41 + //selector
  42 + (SQLite3DataBase db, HK_Referer referer, String filter) -> List((List(CoreAttrs), WebArgValue, String)) get_selector,
  43 +
  44 + //Triggers of that table, can be dynamically updated
  45 + HK_Trigger_Set triggers
  46 + )
  47 +.
  48 +
  49 +public define Maybe(HK_Table_controller)
  50 + get_hk_controller
  51 + (
  52 + List(HK_Table_controller) controllers,
  53 + String _table_name
  54 + )=
  55 + if controllers is
  56 + {
  57 + [] then failure,
  58 + [h . t ] then
  59 + if h.table_name = _table_name then
  60 + success(h)
  61 + else
  62 + get_hk_controller(t, _table_name)
  63 + }
  64 +.
  65 +
  66 +
  67 +public define One
  68 + add_trigger_to_table
  69 + (
  70 + List(HK_Table_controller) controllers,
  71 + String _table_name,
  72 + HK_Trigger trigger_to_add
  73 + )=
  74 + if get_hk_controller(controllers, _table_name) is
  75 + {
  76 + failure then unique
  77 + success(ctrl) then
  78 + since trigger_to_add is hk_trigger(name, t_type, call_back),
  79 + if t_type is
  80 + {
  81 + before_update then add_trigger(ctrl.triggers.before_update, trigger_to_add),
  82 + after_update then add_trigger(ctrl.triggers.after_update, trigger_to_add)
  83 + }
  84 + }
  85 +.
controller/hk_delete.anubis
@@ -9,19 +9,18 @@ @@ -9,19 +9,18 @@
9 read calexium_lib/web/CXM_common.anubis 9 read calexium_lib/web/CXM_common.anubis
10 read calexium_lib/web/CXM_web_arg_utils.anubis 10 read calexium_lib/web/CXM_web_arg_utils.anubis
11 //TODO move to hayamiki lib as view part of MVC 11 //TODO move to hayamiki lib as view part of MVC
12 -read hayamiki_lib/view/view_table_manager.anubis 12 +//read hayamiki_lib/view/view_table_manager.anubis
  13 +read hayamiki_lib/controller/hk_sql_utils.anubis
13 14
14 public define Result(String, String) 15 public define Result(String, String)
15 hk_delete_row 16 hk_delete_row
16 ( 17 (
17 List(Web_arg) lwa, 18 List(Web_arg) lwa,
18 - TM_delete_list d_list, //list of all delete row function for database  
19 SQLite3DataBase db 19 SQLite3DataBase db
20 )= 20 )=
21 - if get_String(lwa, "table_name") is {failure then error("CANT_GET_ARGUMENT"), success(table_name) then  
22 - if get_Int(lwa, "id") is {failure then error("CANT_GET_ARGUMENT"), success(id) then  
23 - if tm_get_delete(table_name, d_list) is {failure then error("DELETE_FUNC_DOESNT_EXIST"), success(hk_delete) then  
24 - if hk_delete(db, [id]) is {failure then error("DELETE_ROW_DB_ERROR"), success(_) then  
25 - ok("DELETE_ROW_OK")  
26 - }}}}.  
27 - 21 + if get_String(lwa, "table_name") is {failure then error("CANT_GET_ARGUMENT"), success(table_name) then
  22 + with ids = get_Int_list(lwa, "id"),
  23 + if delete_from_ids(db, table_name, ids) is {failure then error("DELETE_ROW_DB_ERROR"), success(_) then
  24 + ok("DELETE_ROW_OK")
  25 + }}
  26 +.
controller/hk_sql_utils.anubis
@@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
6 * © Calexium 6 * © Calexium
7 */ 7 */
8 8
  9 +read data_base/sqlite.anubis
9 read calexium_lib/database/db_utils.anubis 10 read calexium_lib/database/db_utils.anubis
10 read hayamiki_lib/types/hayamiki.anubis 11 read hayamiki_lib/types/hayamiki.anubis
11 12
@@ -93,3 +94,46 @@ public define List(Int) @@ -93,3 +94,46 @@ public define List(Int)
93 ok(_, cursor, _) then db_get_integer_list(cursor) 94 ok(_, cursor, _) then db_get_integer_list(cursor)
94 } 95 }
95 . 96 .
  97 +
  98 +public define Maybe(Int)
  99 + insert_or_update
  100 + (
  101 + SQLite3DataBase db,
  102 + String table_name,
  103 + DB_id db_id,
  104 + List(SQLite3_update_field) fields
  105 + )=
  106 + if db_id is
  107 + {
  108 + none then
  109 + since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),
  110 + if sql_query_timeout(db, "INSERT INTO "+table_name+" "+insert_columns+" VALUES "+insert_values+";", binds, table_name+" (INSERT)") is
  111 + {
  112 + error(sql_error) then
  113 + //logError(debug_log, db_error(sql_error, table_name));
  114 + failure,
  115 + ok(_, cursor, _) then
  116 + if sql_query_scalar(db, "SELECT last_insert_rowid()") is
  117 + {
  118 + error(_) then failure,
  119 + ok(datum) then
  120 + with result1 = if datum is integer(n) then n else 0,
  121 + println(table_name+" inserted id ["+result1+"]");
  122 + success(result1)
  123 + }
  124 +// SQLite3DataBase db,
  125 +// String sql_command
  126 +// )
  127 +// success(unique)
  128 + },
  129 + db_id(idx) then
  130 + since make_update_clause_and_binds(fields) is (binds, set_string),
  131 + if sql_query_timeout(db, "UPDATE "+table_name+" SET "+set_string +" WHERE id = "+idx+";", binds, table_name+" (UPDATE)") is
  132 + {
  133 + error(sql_error) then
  134 + //logError(debug_log, db_error(sql_error, table_name));
  135 + failure,
  136 + ok(_, _, _) then success(idx)
  137 + }
  138 + }
  139 +.
controller/hk_trigger.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: フランスのトトロ aka (David RENÉ)
  4 + * Date: 18/06/2017
  5 + * Time: 23:12
  6 + * © Calexium
  7 + */
  8 +
  9 +transmit calexium_lib/web/CXM_making_a_web_site.anubis
  10 +
  11 +public type HK_Trigger_Result:
  12 + ok,
  13 + error,
  14 + abort
  15 +.
  16 +
  17 +public type HK_Trigger_Type:
  18 + before_update,
  19 + after_update
  20 +.
  21 +
  22 +public type HK_Trigger:
  23 + hk_trigger(
  24 + String trigger_name,
  25 + HK_Trigger_Type trigger_type,
  26 + ((SQLite3DataBase db, WEB_Session session ) -> HK_Trigger_Result) trigger_call_back)
  27 +.
  28 +
  29 +public type HK_Trigger_Set:
  30 + hk_trigger_set(
  31 + Var(List(HK_Trigger)) before_update, //must be called before update the requested
  32 + Var(List(HK_Trigger)) after_update //must be called after update the requested
  33 + )
  34 +.
  35 +
  36 +public define Maybe(HK_Trigger)
  37 + get_trigger
  38 + (
  39 + List(HK_Trigger) triggers,
  40 + String _trigger_name
  41 + ) =
  42 + if triggers is
  43 + {
  44 + [] then failure,
  45 + [h . t] then
  46 + if h.trigger_name = _trigger_name then
  47 + success(h)
  48 + else
  49 + get_trigger(t, _trigger_name)
  50 + }
  51 +.
  52 +
  53 +public define One
  54 + add_trigger
  55 + (
  56 + Var(List(HK_Trigger)) triggers,
  57 + HK_Trigger added_trigger
  58 + )=
  59 + println(">>> TRIGGER "+added_trigger.trigger_name+" added");
  60 + triggers <- [added_trigger . *triggers]
  61 +.
  62 +
  63 +
  64 +define List(HK_Trigger)
  65 + _remove_trigger
  66 + (
  67 + List(HK_Trigger) triggers,
  68 + String _trigger_name
  69 + )=
  70 + if triggers is
  71 + {
  72 + [] then [],
  73 + [h . t] then
  74 + if h.trigger_name = _trigger_name then
  75 + _remove_trigger(t, _trigger_name)
  76 + else
  77 + [h . _remove_trigger(t, _trigger_name)]
  78 + }
  79 +.
  80 +
  81 +public define One
  82 + remove_trigger
  83 + (
  84 + Var(List(HK_Trigger)) triggers,
  85 + String trigger_name
  86 + )=
  87 + triggers <- _remove_trigger(*triggers, trigger_name)
  88 +.
view/view_table_manager.anubis
@@ -84,44 +84,29 @@ public define Maybe(VTM_edit) @@ -84,44 +84,29 @@ public define Maybe(VTM_edit)
84 // ) = 84 // ) =
85 // _tm_get_writer(table_name, tm_wlist.list). 85 // _tm_get_writer(table_name, tm_wlist.list).
86 86
87 -public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One))  
88 - _tm_get_delete  
89 - (  
90 - String table_name,  
91 - List(TM_delete) list  
92 - ) =  
93 - if list is  
94 - {  
95 - [] then failure,  
96 - [ h . t ] then  
97 - since h is tm_delete(tb_name, delete_func),  
98 - if tb_name = table_name then  
99 - success(delete_func)  
100 - else  
101 - _tm_get_delete(table_name, t)  
102 - }.  
103 -  
104 -public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One))  
105 - tm_get_delete  
106 - (  
107 - String table_name,  
108 - TM_delete_list tm_dlist  
109 - ) =  
110 - _tm_get_delete(table_name, tm_dlist.list). 87 +//public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One))
  88 +// _tm_get_delete
  89 +// (
  90 +// String table_name,
  91 +// List(TM_delete) list
  92 +// ) =
  93 +// if list is
  94 +// {
  95 +// [] then failure,
  96 +// [ h . t ] then
  97 +// since h is tm_delete(tb_name, delete_func),
  98 +// if tb_name = table_name then
  99 +// success(delete_func)
  100 +// else
  101 +// _tm_get_delete(table_name, t)
  102 +// }.
  103 +//
  104 +//public define Maybe((SQLite3DataBase db, List(Int) l_int) -> Maybe(One))
  105 +// tm_get_delete
  106 +// (
  107 +// String table_name,
  108 +// TM_delete_list tm_dlist
  109 +// ) =
  110 +// _tm_get_delete(table_name, tm_dlist.list).
  111 +
111 112
112 -public define Maybe(HK_Table_controller)  
113 - get_hk_controller  
114 - (  
115 - String _table_name,  
116 - List(HK_Table_controller) controllers  
117 - )=  
118 - if controllers is  
119 - {  
120 - [] then failure,  
121 - [h . t ] then  
122 - if h.table_name = _table_name then  
123 - success(h)  
124 - else  
125 - get_hk_controller(_table_name, t)  
126 - }  
127 -.  
view/view_table_manager_types.anubis
@@ -10,12 +10,13 @@ transmit hayamiki_lib/view/types/hk_form.anubis @@ -10,12 +10,13 @@ transmit hayamiki_lib/view/types/hk_form.anubis
10 transmit hayamiki_lib/view/types/hk_view.anubis 10 transmit hayamiki_lib/view/types/hk_view.anubis
11 transmit hayamiki_lib/view/types/hk_filter.anubis 11 transmit hayamiki_lib/view/types/hk_filter.anubis
12 transmit calexium_lib/web/CXM_common.anubis 12 transmit calexium_lib/web/CXM_common.anubis
13 -read hayamiki_lib/types/hayamiki.anubis 13 +transmit hayamiki_lib/controller/hk_controller.anubis
14 14
  15 +//read hayamiki_lib/types/hayamiki.anubis
15 16
16 -read calexium_lib/web/CXM_common.anubis  
17 -read calexium_lib/web/CXM_making_a_web_site.anubis  
18 -read calexium_lib/database/db_types.anubis 17 +
  18 +//read calexium_lib/web/CXM_common.anubis
  19 +//read calexium_lib/database/db_types.anubis
19 20
20 public type VTM_edit: 21 public type VTM_edit:
21 vtm_edit(String table_name, (SQLite3DataBase db, HK_Form_action action, String edit_view_name) -> HK_Form get_edit). 22 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: @@ -23,49 +24,10 @@ public type VTM_edit:
23 public type VTM_edit_list: 24 public type VTM_edit_list:
24 vtm_edit_list(List(VTM_edit) list). 25 vtm_edit_list(List(VTM_edit) list).
25 26
26 -//public type TM_writer:  
27 -// tm_writer(  
28 -// String table_name,  
29 -// (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type,  
30 -// (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_fields  
31 -// )  
32 -//.  
33 27
34 - //CRUD basis function +  
35 - //Create  
36 - //Read  
37 - //Update  
38 - //Delete  
39 -  
40 -public type HK_Table_controller:  
41 - hk_table_controller(  
42 - String table_name,  
43 - //Create  
44 - (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_type,  
45 - (SQLite3DataBase db, List(Web_arg) lwa) -> Maybe(One) write_from_fields,  
46 - //Read  
47 - (SQLite3DataBase db, String clause) -> Int get_count,  
48 - (SQLite3DataBase db, List(String) columns, Bool resolve_fk, DB_id) -> JsonValue get_one_json,  
49 - (SQLite3DataBase db, List(String) columns, Bool resolve_fk, HK_Referer referer, String clause, String order_by) -> JsonValue get_rows_json,  
50 - //Update  
51 - //Delete  
52 - (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete,  
53 -  
54 - //view interface  
55 - //selector  
56 - (SQLite3DataBase db, HK_Referer referer, String filter) -> List((List(CoreAttrs), WebArgValue, String)) get_selector  
57 - )  
58 -.  
59 -  
60 -//public type TM_writer_list:  
61 -// tm_writer_list(List(TM_writer) list).  
62 28
63 -public type TM_delete:  
64 - tm_delete(String table_name, (SQLite3DataBase db, List(Int) l_int) -> Maybe(One) delete).  
65 29
66 -public type TM_delete_list:  
67 - tm_delete_list(List(TM_delete) list).  
68 - 30 +
69 public type VTM_view: 31 public type VTM_view:
70 vtm_view(String table_name, (String view_name) -> VT_view get_view, (One) -> List((String, List(HK_Filter))) get_all_view_name). 32 vtm_view(String table_name, (String view_name) -> VT_view get_view, (One) -> List((String, List(HK_Filter))) get_all_view_name).
71 33
@@ -75,6 +37,4 @@ public type VTM_view_list: @@ -75,6 +37,4 @@ public type VTM_view_list:
75 public type VTM: 37 public type VTM:
76 vtm(VTM_view_list view_list, 38 vtm(VTM_view_list view_list,
77 VTM_edit_list edit_list, 39 VTM_edit_list edit_list,
78 - //TM_writer_list writer_list,  
79 - TM_delete_list delete_list,  
80 List(HK_Table_controller) hk_controllers). 40 List(HK_Table_controller) hk_controllers).