From 74e4ee8688b4c4379b88447297f9a441451ac274 Mon Sep 17 00:00:00 2001 From: totoro Date: Fri, 17 Mar 2017 01:49:00 +0100 Subject: [PATCH] add file generation/web_args.anubis for managing the generation of all function in relation with web args Add the generation of from_web_arg_to_update_field --- calexium_lib | 2 +- hayamiki_generator.aproj | 3 +++ src/generation/fields.anubis | 27 +++------------------------ src/generation/files.anubis | 38 ++++++++++++++++++++------------------ src/generation/types.anubis | 53 ++--------------------------------------------------- src/generation/web_args.anubis | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 211 insertions(+), 94 deletions(-) create mode 100644 src/generation/web_args.anubis diff --git a/calexium_lib b/calexium_lib index 955d2c3..e861869 160000 --- a/calexium_lib +++ b/calexium_lib @@ -1 +1 @@ -Subproject commit 955d2c3164da1b4e21cb6f51070301842b3239ad +Subproject commit e8618692387dc76379b129c0b3de63c3083f0277 diff --git a/hayamiki_generator.aproj b/hayamiki_generator.aproj index 44cd342..9364bc3 100644 --- a/hayamiki_generator.aproj +++ b/hayamiki_generator.aproj @@ -33,6 +33,8 @@ + + @@ -530,6 +532,7 @@ + diff --git a/src/generation/fields.anubis b/src/generation/fields.anubis index bcc430b..82dea4c 100644 --- a/src/generation/fields.anubis +++ b/src/generation/fields.anubis @@ -79,8 +79,8 @@ public define String foreign_key(app,fk) then fill("INTEGER", 15) + format_attributes(attributes)+" REFERENCES "+fk+"(id) ", integer_field(_) then fill("INTEGER", 15) + format_attributes(attributes), float_field then fill("DOUBLE", 15) + format_attributes(attributes), - char_field(_,size) then fill("VARCHAR("+size+")", 15) +format_attributes(attributes), - password_field(_) then fill("TEXT", 15) + format_attributes(attributes), + char_field(_,size) then fill("VARCHAR("+size+")", 15) + format_attributes(attributes), + password_field(size) then fill("VARCHAR("+size+")", 15) + format_attributes(attributes), text_field(_) then fill("TEXT", 15) + format_attributes(attributes), }. @@ -106,28 +106,7 @@ public define String text_field(_) then "\"\"" }. -public define String - from_web_arg - ( - HK_Model_Field t, - String name - ) = - if t is - { - //anubis(_T) then "constant_byte_array(0,0)", - p_key then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", - //binary then "constant_byte_array(0,0)", - boolean_field then "with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"),", - date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_date,", - time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_time,", - datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_datetime,", - foreign_key(_,_) then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", - integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\")", - float_field then "get_Float(lwa, __prefix__+\""+name+"\")", - char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\")", - password_field(min_size) then "get_String(lwa, __prefix__+\""+name+"_1\")", - text_field(_) then "get_String(lwa, __prefix__+\""+name+"\")" - }. + public define String from_DB_cursor diff --git a/src/generation/files.anubis b/src/generation/files.anubis index 768f418..8d5637b 100644 --- a/src/generation/files.anubis +++ b/src/generation/files.anubis @@ -25,6 +25,7 @@ read generation/cxm_lib_menu.anubis read generation/fn_delete.anubis read generation/choices_selector.anubis read generation/vt_edit.anubis +read generation/web_args.anubis read check.anubis define Maybe(One) @@ -52,24 +53,25 @@ define Maybe(One) failure then println("can't create file "+file_name);failure, success(fd) then with strm = make_stream(fd), - if generate_header(strm, columns) is { failure then failure, success(_) then - if generate_densaku_type(table, dest_dir, use_densaku) is { failure then failure, success(_) then - if generate_type(strm, table, use_densaku) is { failure then failure, success(_) then - if generate_get_default(strm, table) is { failure then failure, success(_) then - if generate_extract_web_arg(strm, table) is { failure then failure, success(_) then - if generate_extract_db_cursor(strm, table) is { failure then failure, success(_) then - if generate_get_all(strm, table) is { failure then failure, success(_) then - if generate_get_all_with_clause(strm, table)is { failure then failure, success(_) then - if generate_get_one(strm, table) is { failure then failure, success(_) then - if generate_get_one_with_clause(strm, table)is { failure then failure, success(_) then - if generate_update(strm, table) is { failure then failure, success(_) then - if generate_delete(strm, table) is { failure then failure, success(_) then - if generate_show_string(strm, table) is { failure then failure, success(_) then - if generate_selector(strm, table) is { failure then failure, success(_) then - if generate_choices_selector(strm, table) is { failure then failure, success(_) then - if generate_VT_all_view(strm, table) is { failure then failure, success(_) then - if generate_VT_all_edit(strm, table) is { failure then failure, success(_) then - success(unique)}}}}}}}}}}}}}}}}} + if generate_header(strm, columns) is { failure then failure, success(_) then + if generate_densaku_type(table, dest_dir, use_densaku) is { failure then failure, success(_) then + if generate_type(strm, table, use_densaku) is { failure then failure, success(_) then + if generate_get_default(strm, table) is { failure then failure, success(_) then + if generate_extract_web_arg_to_type(strm, table) is { failure then failure, success(_) then + if generate_extract_web_arg_to_update_field(strm, table) is { failure then failure, success(_) then + if generate_extract_db_cursor(strm, table) is { failure then failure, success(_) then + if generate_get_all(strm, table) is { failure then failure, success(_) then + if generate_get_all_with_clause(strm, table) is { failure then failure, success(_) then + if generate_get_one(strm, table) is { failure then failure, success(_) then + if generate_get_one_with_clause(strm, table) is { failure then failure, success(_) then + if generate_update(strm, table) is { failure then failure, success(_) then + if generate_delete(strm, table) is { failure then failure, success(_) then + if generate_show_string(strm, table) is { failure then failure, success(_) then + if generate_selector(strm, table) is { failure then failure, success(_) then + if generate_choices_selector(strm, table) is { failure then failure, success(_) then + if generate_VT_all_view(strm, table) is { failure then failure, success(_) then + if generate_VT_all_edit(strm, table) is { failure then failure, success(_) then + success(unique)}}}}}}}}}}}}}}}}}} }. define One diff --git a/src/generation/types.anubis b/src/generation/types.anubis index ad6f69a..c5701c2 100644 --- a/src/generation/types.anubis +++ b/src/generation/types.anubis @@ -95,58 +95,9 @@ public define Maybe(One) success(_) then success(unique) }. -define (List(String), Int) -/** - */ - _generate_extract_web_arg - ( - List(HK_Model_Column) columns, - List(String) so_far, - Int closure - )= - if columns is - { - [] then (reverse(so_far), closure), - [h . t ] then - since h is hk_column(name, col_type, _, _), - with result = from_web_arg(col_type, name), - //If the field is not editable no need to get it from the web - if result = "" then - _generate_extract_web_arg(t, so_far, closure) - else - if start_with(result, "with") then - _generate_extract_web_arg(t, [ " "+result . so_far], closure) - else - with line = " if "+fill(result, 45)+" is {failure then failure, "+fill("success("+name+")",25)+" then", - _generate_extract_web_arg(t, [ line . so_far], closure+1) - }. + -public define Maybe(One) - generate_extract_web_arg - ( - Stream stream, - HK_Table table - )= - since table is hk_table(name, short_name, model, _), - since model is hk_model(columns, _), - with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type. - since _generate_extract_web_arg(columns, [], 0) is ((List(String))list_of_web_arg, (Int) nb_closure), - //generate the type of the table that contain all components - if write_string(stream, - "\n\npublic define Maybe("+type_name+")\n"+ - " extract_"+type_name+"_from_web_arg\n"+ - " (\n"+ - " List(Web_arg) lwa,\n"+ - " String __prefix__\n"+ - " )=\n"+ //head of the definition - to_String(list_of_web_arg)+ //all editable components of the type - " success(\n"+generate_constructor(name, columns, " ")+")\n"+ //generate type construction with values got from web arg - " "+fill(nb_closure, '}')+ //add close } according to number of web arg - ".\n") is //end of the function - { - failure then println("can't write extract_"+type_name+"_from_web_arg "+type_name); failure, - success(_) then success(unique) - }. + public define Maybe(One) generate_extract_db_cursor diff --git a/src/generation/web_args.anubis b/src/generation/web_args.anubis new file mode 100644 index 0000000..ed7b267 --- /dev/null +++ b/src/generation/web_args.anubis @@ -0,0 +1,182 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 16/03/2017 + * Time: 23:38 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read tools/streams.anubis +read columns.anubis + +public define String + from_web_arg_to_type + ( + HK_Model_Field t, + String name + ) = + if t is + { + //anubis(_T) then "constant_byte_array(0,0)", + p_key then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", + //binary then "constant_byte_array(0,0)", + boolean_field then "with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"),", + date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_date,", + time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_time,", + datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_datetime,", + foreign_key(_,_) then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", + integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\")", + float_field then "get_Float(lwa, __prefix__+\""+name+"\")", + char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\")", + password_field(min_size) then "get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\")", + text_field(_) then "get_String(lwa, __prefix__+\""+name+"\")" + }. + +define (List(String), Int) +/* + */ + _generate_extract_web_arg_to_type + ( + List(HK_Model_Column) columns, + List(String) so_far, + Int closure + )= + if columns is + { + [] then (reverse(so_far), closure), + [h . t ] then + since h is hk_column(name, col_type, _, _), + with result = from_web_arg_to_type(col_type, name), + //If the field is not editable no need to get it from the web + if result = "" then + _generate_extract_web_arg_to_type(t, so_far, closure) + else + if start_with(result, "with") then + _generate_extract_web_arg_to_type(t, [ " "+result . so_far], closure) + else + with line = " if "+fill(result, 70)+" is {failure then failure, "+fill("success("+name+")",35)+" then", + _generate_extract_web_arg_to_type(t, [ line . so_far], closure+1) + } +. + +public define Maybe(One) +/* generate the function which extract the entire database table row type from web + * arguments and return maybe that type. + */ + generate_extract_web_arg_to_type + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, _), + since model is hk_model(columns, _), + with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type. + since _generate_extract_web_arg_to_type(columns, [], 0) is ((List(String))list_of_web_arg, (Int) nb_closure), + //generate the type of the table that contain all components + if write_string(stream, + "\n\npublic define Maybe("+type_name+")\n"+ + " extract_"+type_name+"_from_web_arg\n"+ + " (\n"+ + " List(Web_arg) lwa,\n"+ + " String __prefix__\n"+ + " )=\n"+ //head of the definition + to_String(list_of_web_arg)+ //all editable components of the type + " success(\n"+generate_constructor(name, columns, " ")+")\n"+ //generate type construction with values got from web arg + " "+fill(nb_closure, '}')+ //add close } according to number of web arg + ".\n") is //end of the function + { + failure then println("can't write extract_"+type_name+"_from_web_arg "+type_name); failure, + success(_) then success(unique) + } +. + + /***** UPDATE FIELD ********/ + +public define String + from_web_arg_to_update_field + ( + HK_Model_Field t, + String name + ) = + if t is + { + //anubis(_T) then "constant_byte_array(0,0)", + p_key then "[]", + boolean_field then + "with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"), [field(\""+name+"\", bind_Bool(\":v_"+name+"\", "+name+"))]", + date_field(attrs) then + if attrs = none then + "if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }" + else + "[]", + time_field(attrs) then + if attrs = none then + "if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }" + else + "[]", + datetime_field(attrs) then + if attrs = none then + "if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }" + else + "[]", + foreign_key(_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", + "with "+name+"_fk = get_DB_id(lwa, __prefix__+\""+name+"\"), [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))]" + integer_field(_) then //"get_Int(lwa, __prefix__+\""+name+"\")", + "if get_Int(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Int(\":v_"+name+"\", "+name+"))] }" + float_field then //"get_Float(lwa, __prefix__+\""+name+"\")", + "if get_Float(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Float(\":v_"+name+"\", "+name+"))] }" + char_field(_,_) then + "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }", + password_field(min_size) then + "if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }" + // if get_Password(lwa, __prefix__+"password_1", __prefix__+"password_2") is {failure then [], success(password) then [field("passwxord", bind_String(":v_password", password))] } + text_field(_) then + "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }" + //"get_String(lwa, __prefix__+\""+name+"\")" + }. + +define String +/* + */ + _generate_extract_web_arg_to_update_field + ( + List(HK_Model_Column) columns, + )= + join(" ,\n", + map((HK_Model_Column col) |-> + since col is hk_column(name, col_type, _, _), + from_web_arg_to_update_field(col_type, name), + columns + ) + ) +. + +public define Maybe(One) +/* generate the function which extract the entire or part database table row type + * from web arguments and return a list of table fields presents in that web args. + */ + generate_extract_web_arg_to_update_field + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, _), + since model is hk_model(columns, _), + with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type. + + //generate the type of the table that contain all components + if write_string(stream, + "\n\npublic define List(SQLite3_update_field)\n"+ + " extract_"+type_name+"_update_fields_from_web_arg\n"+ + " (\n"+ + " List(Web_arg) lwa,\n"+ + " String __prefix__\n"+ + " )=\n"+ //head of the definition + _generate_extract_web_arg_to_update_field(columns)+ //all editable components of the type + "\n.\n") is //end of the function + { + failure then println("can't write extract_"+type_name+"_update_fields_from_web_arg "+type_name); failure, + success(_) then success(unique) + } +. -- libgit2 0.21.4