From 6b95d3a55ad17f9e884442211234e56ffd9735b6 Mon Sep 17 00:00:00 2001 From: totoro Date: Sat, 10 Nov 2018 21:25:43 +0900 Subject: [PATCH] add support of Foreign key "on update" and "on delete" directive --- ds_files/ds_types.anubis | 24 ++++++++++++++++++------ model/fields.anubis | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- model/types/hk_foreign_key_clause.anubis | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ model/types/hk_foreign_key_clause_on_directive.anubis | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ model/types/hk_model_field.anubis | 55 +++++++++++++++++++++++++++++++------------------------ 5 files changed, 295 insertions(+), 32 deletions(-) create mode 100644 model/types/hk_foreign_key_clause.anubis create mode 100644 model/types/hk_foreign_key_clause_on_directive.anubis diff --git a/ds_files/ds_types.anubis b/ds_files/ds_types.anubis index aab23f2..472444b 100644 --- a/ds_files/ds_types.anubis +++ b/ds_files/ds_types.anubis @@ -92,6 +92,7 @@ public define List(DS_Type_File) hayamiki_lib_types_description = component(extern_type("HK_App_Name", model_types+"hk_app_name.anubis"), "app_name"), component(string, "table_name"), component(list, string, "searchable_fields"), + component(list, extern_type("HK_Foreign_Key_Clause", model_types+"hk_foreign_key_clause.anubis"), "hk_fk_clause"), ]), alternative("integer_field", [ component(extern_type("HK_Int_Choices", model_types+"hk_int_choices.anubis"), "choices", comment("integer of arbitrary size ")), @@ -124,12 +125,23 @@ public define List(DS_Type_File) hayamiki_lib_types_description = enum("none", comment("Nothing special, normal behaviour")), enum("rich_editor", comment("Use richt text editor for that area")) ]), - -// ds_type("HK_FK_Attr", [ -// alternative("searchable", [ -// component(list, string, "searchable_fields") -// ]) -// ]), + + ds_type("HK_Foreign_Key_Clause_On_Directive", [ + enum("set_null", comment("Set to null")), + enum("set_default", comment("Set to Default value")), + enum("cascade", comment("cascading the action")), + enum("restrict", comment("restrict")), + enum("no_action", comment("Do nothing, this is the default behaviour")) + ]), + + ds_type("HK_Foreign_Key_Clause", [ + alternative("on_delete", [ + component(extern_type("HK_Foreign_Key_Clause_On_Directive", model_types+"hk_foreign_key_clause_on_directive.anubis"), "on_delete") + ]), + alternative("on_update", [ + component(extern_type("HK_Foreign_Key_Clause_On_Directive", model_types+"hk_foreign_key_clause_on_directive.anubis"), "on_update") + ]) + ]), ds_type("HK_Int_Choices", [ enum("no_choice", comment("No list choice is available. Hence the user must enter the value")), diff --git a/model/fields.anubis b/model/fields.anubis index 91542d1..f568880 100644 --- a/model/fields.anubis +++ b/model/fields.anubis @@ -147,6 +147,20 @@ define String fax then "fax" }. +public define String + to_String + ( + HK_Foreign_Key_Clause_On_Directive directive + )= + if directive is + { + set_null then "SET NULL", + set_default then "SET DEFAULT", + cascade then "CASCADE", + restrict then "RESTRICT", + no_action then "NO ACTION" + } +. public define String to_Anubis_source @@ -174,9 +188,64 @@ public define HK_Model_Field ( String table_name )= - foreign_key(this, table_name, []). + foreign_key(this, table_name, [], []). + + +public define HK_Model_Field + foreign_key + ( + HK_App_Name hk_app_name, + String table_name + )= + foreign_key(hk_app_name, table_name, [], []). + +public define HK_Model_Field + foreign_key + ( + HK_App_Name hk_app_name, + String table_name, + List(String) searchable_fields + )= + foreign_key(hk_app_name, table_name, searchable_fields, []). + +public define HK_Model_Field + foreign_key + ( + HK_App_Name hk_app_name, + String table_name, + List(HK_Foreign_Key_Clause) fk_clause + )= + foreign_key(hk_app_name, table_name, [], fk_clause). + +public define HK_Model_Field + foreign_key + ( + String table_name, + List(String) searchable_fields + )= + foreign_key(this, table_name, searchable_fields, []) +. + +public define HK_Model_Field + foreign_key + ( + String table_name, + List(String) searchable_fields, + List(HK_Foreign_Key_Clause) fk_clause + )= + foreign_key(this, table_name, searchable_fields, fk_clause) +. public define HK_Model_Field + foreign_key + ( + String table_name, + List(HK_Foreign_Key_Clause) fk_clause + )= + foreign_key(this, table_name, [], fk_clause) +. + +public define HK_Model_Field text_field = text_field(none) @@ -240,7 +309,7 @@ public define JsonValue json_member("dt_attribute", json_string(to_String(attr))) ], - foreign_key(app_name, table_name,_) then // foreign key to 'id' in another (or same) table and column_name for choice selector. + foreign_key(app_name, table_name, _, _) then // foreign key to 'id' in another (or same) table and column_name for choice selector. [ json_member("type", json_string("foreign_key")), json_member("app_name", to_JSON(app_name)), json_member("table_name", json_string(table_name)), diff --git a/model/types/hk_foreign_key_clause.anubis b/model/types/hk_foreign_key_clause.anubis new file mode 100644 index 0000000..fcacb59 --- /dev/null +++ b/model/types/hk_foreign_key_clause.anubis @@ -0,0 +1,85 @@ +/* + * Created by 伝作 (Densaku). + * Types & Messages generator written by フランスのトトロ aka (David RENÉ) + * Date: 2018-11-10 + * Time: 17:14:38 + * + */ + +transmit system/muscle.anubis +transmit system/convert.anubis +transmit tools/basis.anubis + +transmit hayamiki_lib/model/types/hk_foreign_key_clause_on_directive.anubis + + + +public type HK_Foreign_Key_Clause: + on_delete( + HK_Foreign_Key_Clause_On_Directive on_delete + ), + on_update( + HK_Foreign_Key_Clause_On_Directive on_update + ) +. + + HK_Foreign_Key_Clause message format + ==================================== + + +public define Message + to_Message + ( + HK_Foreign_Key_Clause _hk_foreign_key_clause + )= + with _hk_foreign_key_clause_message = message((Word32)0), // + forget(add_string(_hk_foreign_key_clause_message, "__TYPE__", "HK_Foreign_Key_Clause")); + if _hk_foreign_key_clause is + { + //Alternative on_delete + on_delete(_on_delete) then + forget(add_string(_hk_foreign_key_clause_message, "__TYPE_ALT__", "on_delete")); + // [type = HK_Foreign_Key_Clause_On_Directive] on_delete.on_delete + forget(add_message(_hk_foreign_key_clause_message, "on_delete", to_Message(_on_delete))), + + //Alternative on_update + on_update(_on_update) then + forget(add_string(_hk_foreign_key_clause_message, "__TYPE_ALT__", "on_update")); + // [type = HK_Foreign_Key_Clause_On_Directive] on_update.on_update + forget(add_message(_hk_foreign_key_clause_message, "on_update", to_Message(_on_update))), + + }; + _hk_foreign_key_clause_message +. + +public define Maybe(HK_Foreign_Key_Clause) + from_Message + ( + Message _hk_foreign_key_clause_message + )= + if find_string(_hk_foreign_key_clause_message, "__TYPE__") is {failure then failure, success(__type__) then + if find_string(_hk_foreign_key_clause_message, "__TYPE_ALT__") is {failure then failure, success(__type_alt__) then + if __type__ = "HK_Foreign_Key_Clause" then + //Alternative on_delete + if __type_alt__ = "on_delete" then //Alternative on_delete + // [type = HK_Foreign_Key_Clause_On_Directive] on_delete.on_delete + if find_message(_hk_foreign_key_clause_message, "on_delete") is {failure then failure, success(_on_delete__msg) then + if (Maybe(HK_Foreign_Key_Clause_On_Directive))from_Message(_on_delete__msg) is {failure then failure, success(_on_delete_) then + + success(on_delete(_on_delete_)) + }} + else //Alternative on_update + if __type_alt__ = "on_update" then //Alternative on_update + // [type = HK_Foreign_Key_Clause_On_Directive] on_update.on_update + if find_message(_hk_foreign_key_clause_message, "on_update") is {failure then failure, success(_on_update__msg) then + if (Maybe(HK_Foreign_Key_Clause_On_Directive))from_Message(_on_update__msg) is {failure then failure, success(_on_update_) then + + success(on_update(_on_update_)) + }} + else + failure //No valid Alternative found ! + else + failure //Type not found in message ! + }} +. + diff --git a/model/types/hk_foreign_key_clause_on_directive.anubis b/model/types/hk_foreign_key_clause_on_directive.anubis new file mode 100644 index 0000000..f02f31f --- /dev/null +++ b/model/types/hk_foreign_key_clause_on_directive.anubis @@ -0,0 +1,90 @@ +/* + * Created by 伝作 (Densaku). + * Types & Messages generator written by フランスのトトロ aka (David RENÉ) + * Date: 2018-11-10 + * Time: 17:14:38 + * + */ + +transmit system/muscle.anubis +transmit system/convert.anubis +transmit tools/basis.anubis + + + + +public type HK_Foreign_Key_Clause_On_Directive: + set_null, //Set to null + set_default, //Set to Default value + cascade, //cascading the action + restrict, //restrict + no_action //Do nothing, this is the default behaviour +. + + HK_Foreign_Key_Clause_On_Directive message format + ================================================= + + +public define Message + to_Message + ( + HK_Foreign_Key_Clause_On_Directive _hk_foreign_key_clause_on_directive + )= + with _hk_foreign_key_clause_on_directive_message = message((Word32)0), // + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE__", "HK_Foreign_Key_Clause_On_Directive")); + if _hk_foreign_key_clause_on_directive is + { + //Alternative set_null (NO TYPE) + set_null then + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__", "set_null")), + + //Alternative set_default (NO TYPE) + set_default then + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__", "set_default")), + + //Alternative cascade (NO TYPE) + cascade then + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__", "cascade")), + + //Alternative restrict (NO TYPE) + restrict then + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__", "restrict")), + + //Alternative no_action (NO TYPE) + no_action then + forget(add_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__", "no_action")), + + }; + _hk_foreign_key_clause_on_directive_message +. + +public define Maybe(HK_Foreign_Key_Clause_On_Directive) + from_Message + ( + Message _hk_foreign_key_clause_on_directive_message + )= + if find_string(_hk_foreign_key_clause_on_directive_message, "__TYPE__") is {failure then failure, success(__type__) then + if find_string(_hk_foreign_key_clause_on_directive_message, "__TYPE_ALT__") is {failure then failure, success(__type_alt__) then + if __type__ = "HK_Foreign_Key_Clause_On_Directive" then + //Alternative set_null (NO TYPE) + if __type_alt__ = "set_null" then + success(set_null) + else //Alternative set_default (NO TYPE) + if __type_alt__ = "set_default" then + success(set_default) + else //Alternative cascade (NO TYPE) + if __type_alt__ = "cascade" then + success(cascade) + else //Alternative restrict (NO TYPE) + if __type_alt__ = "restrict" then + success(restrict) + else //Alternative no_action (NO TYPE) + if __type_alt__ = "no_action" then + success(no_action) + else + failure //No valid Alternative found ! + else + failure //Type not found in message ! + }} +. + diff --git a/model/types/hk_model_field.anubis b/model/types/hk_model_field.anubis index 8b5eab1..ebfbc16 100644 --- a/model/types/hk_model_field.anubis +++ b/model/types/hk_model_field.anubis @@ -1,8 +1,8 @@ /* * Created by 伝作 (Densaku). * Types & Messages generator written by フランスのトトロ aka (David RENÉ) - * Date: 2017-09-16 - * Time: 09:48:18 + * Date: 2018-11-10 + * Time: 17:14:38 * */ @@ -14,54 +14,56 @@ transmit hayamiki_lib/model/types/hk_phone_field_attr.anubis transmit hayamiki_lib/model/types/hk_text_field_attr.anubis transmit hayamiki_lib/model/types/hk_text_choices.anubis transmit hayamiki_lib/model/types/hk_int_choices.anubis +transmit hayamiki_lib/model/types/hk_foreign_key_clause.anubis transmit hayamiki_lib/model/types/hk_app_name.anubis transmit hayamiki_lib/model/types/hk_datetime_field_attr.anubis public type HK_Model_Field: - p_key, - boolean_field, //true or false + p_key, + boolean_field, //true or false date_field( - HK_Datetime_Field_Attr hk_dt_f_attr //date with the precision of the day + HK_Datetime_Field_Attr hk_dt_f_attr //date with the precision of the day ), time_field( - HK_Datetime_Field_Attr hk_dt_f_attr //time with the precision of the second + HK_Datetime_Field_Attr hk_dt_f_attr //time with the precision of the second ), datetime_field( - HK_Datetime_Field_Attr hk_dt_f_attr //datetime with the precision of the second + HK_Datetime_Field_Attr hk_dt_f_attr //datetime with the precision of the second ), //foreign key to 'id' in another (or same) table and column_name for choice selector) foreign_key( - HK_App_Name app_name, - String table_name, - List(String) searchable_fields + HK_App_Name app_name, + String table_name, + List(String) searchable_fields, + List(HK_Foreign_Key_Clause) hk_fk_clause ), integer_field( - HK_Int_Choices choices //integer of arbitrary size + HK_Int_Choices choices //integer of arbitrary size ), - float_field, + float_field, char_field( - HK_Text_Choices choices, - Int size //text of maximal size 'size' (number of characters) + HK_Text_Choices choices, + Int size //text of maximal size 'size' (number of characters) ), password_field( - Int min_size //the password field doesn't show the password string or store it in hashed version into database + Int min_size //the password field doesn't show the password string or store it in hashed version into database ), //text of variable size text_field( - HK_Text_Field_Attr hk_text_field_attr + HK_Text_Field_Attr hk_text_field_attr ), - color_field, //String which representing RGBA color like HTML + color_field, //String which representing RGBA color like HTML //phone field phone_field( - HK_Phone_Field_Attr hk_ph_f_attr //phone line number type like (cellphone, landline, fax, etc) + HK_Phone_Field_Attr hk_ph_f_attr //phone line number type like (cellphone, landline, fax, etc) ), - url_field, //url field for website and so forth - email_field //email field + url_field, //url field for website and so forth + email_field //email field . HK_Model_Field message format @@ -105,14 +107,16 @@ public define Message //Alternative foreign_key //foreign key to 'id' in another (or same) table and column_name for choice selector) - foreign_key(_app_name, _table_name, __searchable_fields) then + foreign_key(_app_name, _table_name, __searchable_fields, __hk_fk_clause) then forget(add_string(_hk_model_field_message, "__TYPE_ALT__", "foreign_key")); // [type = HK_App_Name] foreign_key.app_name forget(add_message(_hk_model_field_message, "app_name", to_Message(_app_name))); // [type = String] foreign_key.table_name forget(add_string(_hk_model_field_message, "table_name", _table_name)); // [type = List(String)] foreign_key.searchable_fields - map_forget((String _searchable_fields) |-> add_string(_hk_model_field_message, "searchable_fields", _searchable_fields), __searchable_fields), + map_forget((String _searchable_fields) |-> add_string(_hk_model_field_message, "searchable_fields", _searchable_fields), __searchable_fields); + // [type = List(HK_Foreign_Key_Clause)] foreign_key.hk_fk_clause + map_forget((HK_Foreign_Key_Clause _hk_fk_clause) |-> add_message(_hk_model_field_message, "hk_fk_clause", to_Message(_hk_fk_clause)), __hk_fk_clause), //Alternative integer_field integer_field(_choices) then @@ -216,9 +220,12 @@ public define Maybe(HK_Model_Field) if find_string(_hk_model_field_message, "table_name") is {failure then failure, success(_table_name_) then // [type = List(String)] foreign_key.searchable_fields with _searchable_fields_ = find_string_list(_hk_model_field_message, "searchable_fields"), + // [type = List(HK_Foreign_Key_Clause)] foreign_key.hk_fk_clause + with mb__hk_fk_clause_ = map_escape(( Message msg ) |-> (Maybe(HK_Foreign_Key_Clause)) from_Message(msg), find_messages(_hk_model_field_message, "hk_fk_clause")), + if mb__hk_fk_clause_ is {failure then failure, success(_hk_fk_clause_) then - success(foreign_key(_app_name_, _table_name_, _searchable_fields_)) - }}} + success(foreign_key(_app_name_, _table_name_, _searchable_fields_, _hk_fk_clause_)) + }}}} else //Alternative integer_field if __type_alt__ = "integer_field" then //Alternative integer_field // [type = HK_Int_Choices] integer_field.choices -- libgit2 0.21.4