diff --git a/hayamiki_generator.aproj b/hayamiki_generator.aproj index 7ca345d..8631004 100644 --- a/hayamiki_generator.aproj +++ b/hayamiki_generator.aproj @@ -525,6 +525,7 @@ + @@ -532,9 +533,13 @@ + + + + diff --git a/src/generation/SQL_get_all.anubis b/src/generation/SQL_get_all.anubis new file mode 100644 index 0000000..58cd695 --- /dev/null +++ b/src/generation/SQL_get_all.anubis @@ -0,0 +1,130 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 25/03/2017 + * Time: 04:26 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read tools/streams.anubis +read columns.anubis + +public define Maybe(One) + generate_get_all + ( + 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\n"+ + "public define List("+type_name+")\n"+ + " _get_all_"+name+"\n"+ + " (\n"+ + " One -> SQLite3Row table_cursor,\n"+ + " List("+type_name+") so_far\n"+ + " ) =\n"+ + " if extract_"+name+"(table_cursor) is\n"+ + " {\n"+ + " failure then reverse(so_far),\n"+ + " success(data) then _get_all_"+name+"(table_cursor, [data . so_far])\n"+ + " }.\n"+ + "\n\n" + //SQL query +// "public define List("+type_name+")\n"+ +// " get_all_"+name+"\n"+ +// " (\n"+ +// " SQLite3DataBase db,\n"+ +// " )=\n"+ +// " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+";\", [], \"get_all_"+name+"\") is\n"+ +// " {\n"+ +// " error(_) then [],\n"+ +// " ok(_, cursor, _) then _get_all_"+name+"(cursor, [])\n"+ +// " }.\n"+ +// "\n\n" + ) is //end of the function + { + failure then println("can't write generate_get_all "+type_name); failure, + success(_) then success(unique) + }. + + +public define Maybe(One) + generate_get_all_with_clause + ( + 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, + //SQL query + "public define Maybe(One -> SQLite3Row)\n"+ + " get_all_"+name+"_by_clause\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " String clause\n"+ + " )=\n"+ + " with final_clause = if clause = \"\" then\n"+ + " \"\"\n"+ + " else\n"+ + " \"WHERE \"+clause,\n"+ + "\n"+ + " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" \"+final_clause+\";\", [], \"get_all_"+name+"_by_clause\") is\n"+ + " {\n"+ + " error(_) then failure,\n"+ + " ok(_, cursor, _) then success(cursor)\n"+ + " }\n"+ + ".\n"+ + "\n"+ + "public define List("+type_name+")\n"+ + " get_all_"+name+"_by_clause_to_list_type\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " String clause\n"+ + " )=\n"+ + " if get_all_"+name+"_by_clause(db, clause) is\n"+ + " {\n"+ + " failure then [],\n"+ + " success(cursor) then _get_all_"+name+"(cursor, [])\n"+ + " }\n"+ + ".\n"+ + "public define List(JsonValue)\n"+ + " _get_all_"+name+"_to_json\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " One -> SQLite3Row table_cursor,\n"+ + " List(JsonValue) so_far\n"+ + " ) =\n"+ + " if extract_"+name+"_cursor_to_json(db, table_cursor) is\n"+ + " {\n"+ + " failure then reverse(so_far),\n"+ + " success(data) then _get_all_"+name+"_to_json(db, table_cursor, [data . so_far])\n"+ + " }\n"+ + ".\n"+ +"public define JsonMember\n"+ +" get_all_"+name+"_by_clause_to_json\n"+ +" (\n"+ +" SQLite3DataBase db,\n"+ +" String clause\n"+ +" )=\n"+ +" if get_all_"+name+"_by_clause(db, clause) is\n"+ +" {\n"+ +" failure then json_member(\"rows\", json_array([])),\n"+ +" success(cursor) then json_member(\"rows\", json_array(_get_all_"+name+"_to_json(db, cursor, [])))\n"+ +" }\n"+ +".\n" + ) is //end of the function + { + failure then println("can't write generate_get_all "+type_name); failure, + success(_) then success(unique) + } +. diff --git a/src/generation/SQL_get_count.anubis b/src/generation/SQL_get_count.anubis new file mode 100644 index 0000000..ebfa344 --- /dev/null +++ b/src/generation/SQL_get_count.anubis @@ -0,0 +1,51 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 25/03/2017 + * Time: 01:57 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read tools/basis.anubis +read tools/streams.anubis +read system/string.anubis + + +public define Maybe(One) + generate_get_count_by_clause + ( + 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, + //SQL query + "public define Int\n"+ + " get_count_"+name+"_by_clause\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " String clause\n"+ + " )=\n"+ + " with final_clause = if clause = \"\" then\n"+ + " \"\"\n"+ + " else\n"+ + " \"WHERE \"+clause,\n"+ + "\n"+ + " if sql_query_timeout(db, \"SELECT COUNT(\\\"id\\\") FROM "+name+" \"+final_clause+\";\", [], \"get_count_"+name+"_by_clause\") is\n"+ + " {\n"+ + " error(_) then 0,\n"+ + " ok(_, cursor, _) then db_get_integer(cursor)\n"+ + " }\n"+ + ".\n\n" + + ) is //end of the function + { + failure then println("can't write generate_get_all "+type_name); failure, + success(_) then success(unique) + } +. diff --git a/src/generation/choices_selector.anubis b/src/generation/choices_selector.anubis index 9e91fea..8918873 100644 --- a/src/generation/choices_selector.anubis +++ b/src/generation/choices_selector.anubis @@ -29,6 +29,7 @@ define Maybe(One) " get_choices_selector_"+table_name+"_"+column_name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ + " String clause\n"+ " )=\n"+ " [\n"+ concat(map((HK_Text_Choice hk_txt_choice) |-> @@ -57,6 +58,7 @@ define Maybe(One) " get_choices_selector_"+table_name+"_"+column_name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ + " String clause\n"+ " )=\n"+ " [\n"+ concat(map((HK_Int_Choice hk_int_choice) |-> diff --git a/src/generation/columns.anubis b/src/generation/columns.anubis index 556c94a..211ba51 100644 --- a/src/generation/columns.anubis +++ b/src/generation/columns.anubis @@ -74,34 +74,6 @@ public define List(String) _to_List_String(columns, with_pk, []). -define List(String) -/** - */ - components - ( - List(HK_Model_Column) columns, - String cursor_name, - List(String) so_far, - Int idx - )= - if columns is - { - [] then reverse(so_far), - [h . t ] then - since h is hk_column(name, col_type, _, _), - with line = fill(from_DB_cursor(col_type, cursor_name, idx), 35)+ "/* "+name+" */", - components(t, cursor_name, [line . so_far], idx + 1 ) - }. - -public define String - generate_constructor_from_cursor - ( - String constructor_name, - String cursor_name, - List(HK_Model_Column) columns, - String indent - )= - indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", components(columns, cursor_name, [], 0))+"\n"+indent+")". public define String generate_constructor @@ -316,13 +288,7 @@ public define String } }. -public define String - make_list_view_header - ( - List(String) list_view, - String indent - )= - indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)). + @@ -338,10 +304,8 @@ public define String ) = since col is hk_column(name, col_type, _, _), if col_type is - { - //anubis(_T) then "constant_byte_array(0,0)", + { p_key then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)", - //binary then "constant_byte_array(0,0)", boolean_field then "to_String("+current_type_name+"."+name+")", date_field(attrs) then "to_String("+current_type_name+"."+name+")", time_field(attrs) then "to_String("+current_type_name+"."+name+")", @@ -353,3 +317,5 @@ public define String password_field(_) then current_type_name+"."+name, text_field(_) then current_type_name+"."+name }. + + diff --git a/src/generation/extractor.anubis b/src/generation/extractor.anubis new file mode 100644 index 0000000..a25adfc --- /dev/null +++ b/src/generation/extractor.anubis @@ -0,0 +1,158 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 25/03/2017 + * Time: 03:18 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read json.anubis +read tools/streams.anubis + +public define String + from_DB_cursor + ( + HK_Model_Field t, + String cursor, //db cursor name + Int index //current index of the column in the cursor + ) = + with cursor_index = "("+cursor+")("+index+")", + if t is + { + p_key then "db_id((Int)db_integer"+cursor_index+")", + boolean_field then "db_bool"+cursor_index, + date_field(_) then "db_date(text"+cursor_index+")", + time_field(_) then "db_time(text"+cursor_index+")", + datetime_field(_) then "db_datetime(text"+cursor_index+")", + foreign_key(_,_) then "db_id((Int)db_integer"+cursor_index+")", + integer_field(_) then "(Int)db_integer"+cursor_index, + float_field then "db_float"+cursor_index, + char_field(_,_) then "text"+cursor_index, + password_field(_) then "text"+cursor_index, + text_field(_) then "text"+cursor_index + }. + +define List(String) +/** + */ + components + ( + List(HK_Model_Column) columns, + String cursor_name, + List(String) so_far, + Int idx + )= + if columns is + { + [] then reverse(so_far), + [h . t ] then + since h is hk_column(name, col_type, _, _), + with line = fill(from_DB_cursor(col_type, cursor_name, idx), 35)+ "/* "+name+" */", + components(t, cursor_name, [line . so_far], idx + 1 ) + }. + +public define String + generate_constructor_from_cursor + ( + String constructor_name, + String cursor_name, + List(HK_Model_Column) columns, + String indent + )= + indent+constructor_name+"(\n"+indent+" "+join(",\n"+indent+" ", components(columns, cursor_name, [], 0))+"\n"+indent+")". + + +public define Maybe(One) + generate_extract_db_cursor_to_mb_type + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, display), + 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\n"+ + "public define Maybe("+type_name+")\n"+ + " extract_"+name+"\n"+ + " (\n"+ + " One -> SQLite3Row table_cursor\n"+ + " ) =\n"+ + " if table_cursor(unique) is\n"+ + " {\n"+ + " error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+ + " no_more_row then failure,\n"+ + " row(cursor) then\n"+ + " success(\n"+generate_constructor_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg + " }.\n") is //end of the function + { + failure then println("can't write generate_extract_db_cursor "+type_name); failure, + success(_) then success(unique) + }. + /************* JSON *************/ + +define List(String) +/** + */ + to_json_member + ( + List(HK_Model_Column) columns, + String cursor_name, + List(String) so_far, + Int idx + )= + if columns is + { + [] then reverse(so_far), + [h . t ] then + since h is hk_column(name, col_type, _, _), + with line = fill(from_DB_cursor_to_JSON(h, cursor_name, idx), 135)+ "/* "+name+" */", + to_json_member(t, cursor_name, [line . so_far], idx + 1 ) + } +. + +public define String + generate_json_object_from_cursor + ( + String constructor_name, + String cursor_name, + List(HK_Model_Column) columns, + String indent + )= + indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])". + + +public define Maybe(One) + generate_extract_db_cursor_to_json + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, display), + 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\n"+ + "public define Maybe(JsonValue)\n"+ + " extract_"+name+"_cursor_to_json\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " One -> SQLite3Row table_cursor\n"+ + " ) =\n"+ + " if table_cursor(unique) is\n"+ + " {\n"+ + " error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+ + " no_more_row then failure,\n"+ + " row(cursor) then\n"+ + " success("+generate_json_object_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg + " }\n.\n" + ) is //end of the function + { + failure then println("can't write generate_extract_db_cursor "+type_name); failure, + success(_) then success(unique) + }. diff --git a/src/generation/fields.anubis b/src/generation/fields.anubis index 10c1042..b8eb3e9 100644 --- a/src/generation/fields.anubis +++ b/src/generation/fields.anubis @@ -106,28 +106,7 @@ public define String -public define String - from_DB_cursor - ( - HK_Model_Field t, - String cursor, //db cursor name - Int index //current index of the column in the cursor - ) = - with cursor_index = "("+cursor+")("+index+")", - if t is - { - p_key then "db_id((Int)db_integer"+cursor_index+")", - boolean_field then "db_bool"+cursor_index, - date_field(_) then "db_date(text"+cursor_index+")", - time_field(_) then "db_time(text"+cursor_index+")", - datetime_field(_) then "db_datetime(text"+cursor_index+")", - foreign_key(_,_) then "db_id((Int)db_integer"+cursor_index+")", - integer_field(_) then "(Int)db_integer"+cursor_index, - float_field then "db_float"+cursor_index, - char_field(_,_) then "text"+cursor_index, - password_field(_) then "text"+cursor_index, - text_field(_) then "text"+cursor_index - }. + public define String to_Bind diff --git a/src/generation/files.anubis b/src/generation/files.anubis index 8a41fd6..ba0fea0 100644 --- a/src/generation/files.anubis +++ b/src/generation/files.anubis @@ -19,13 +19,17 @@ read tools/ISO-8601.anubis read generation/header.anubis read generation/types.anubis read generation/densaku.anubis +read generation/extractor.anubis read generation/create_table.anubis read generation/hk_menu.anubis read generation/cxm_lib_menu.anubis read generation/fn_delete.anubis read generation/choices_selector.anubis read generation/vt_edit.anubis +read generation/vt_view.anubis read generation/web_args.anubis +read generation/SQL_get_count.anubis +read generation/SQL_get_all.anubis read check.anubis define Maybe(One) @@ -59,11 +63,13 @@ define Maybe(One) 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_extract_db_cursor_to_mb_type(strm, table) is { failure then failure, success(_) then + if generate_extract_db_cursor_to_json(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_get_count_by_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 @@ -71,7 +77,7 @@ define Maybe(One) 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)}}}}}}}}}}}}}}}}}} + success(unique)}}}}}}}}}}}}}}}}}}}} }. define One diff --git a/src/generation/header.anubis b/src/generation/header.anubis index 860ae16..e3a998f 100644 --- a/src/generation/header.anubis +++ b/src/generation/header.anubis @@ -56,6 +56,7 @@ read calexium_lib/web/CXM_making_a_web_site.anubis read calexium_lib/web/CXM_common.anubis read calexium_lib/web/CXM_web_dump.anubis read calexium_lib/web/CXM_web_arg_utils.anubis +read calexium_lib/web/CXM_json.anubis read calexium_lib/web/widgets/icons_set.anubis read data_base/sqlite.anubis read data_base/db_tools.anubis diff --git a/src/generation/json.anubis b/src/generation/json.anubis new file mode 100644 index 0000000..0188678 --- /dev/null +++ b/src/generation/json.anubis @@ -0,0 +1,92 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 25/03/2017 + * Time: 00:54 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read tools/basis.anubis +read columns.anubis + +public define String +/** to_String return the content of the given model column 'col' in String format. + * + */ + to_JSON_String + ( + HK_Model_Column col, //Model of the column + String current_type_name, //Instance name of the type i.e. toto (this is instance of Toto_Type below) + String general_type_name //Type declaration name i.e. Toto_Type + ) = + since col is hk_column(name, col_type, _, _), + if col_type is + { + p_key then current_type_name+".id", + boolean_field then "to_JSON_String(to_String("+current_type_name+"."+name+"))", + date_field(attrs) then "to_JSON_String(to_String("+current_type_name+"."+name+"))", + time_field(attrs) then "to_JSON_String(to_String("+current_type_name+"."+name+"))", + datetime_field(attrs) then "to_JSON_String(to_String("+current_type_name+"."+name+"))", + foreign_key(app,foreign_table) then "to_JSON_String(get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+"))", + integer_field(_) then current_type_name+"."+name, + float_field then "float_to_string("+current_type_name+"."+name+",5)", + char_field(_,_) then "to_JSON_String("+current_type_name+"."+name+")", + password_field(_) then "to_JSON_String("+current_type_name+"."+name+")", + text_field(_) then "to_JSON_String("+current_type_name+"."+name+")" + } +. + + + +public define String +/** to_String return the content of the given model column 'col' in String format. + * + */ + to_JSON + ( + HK_Model_Column col, //Model of the column + String current_type_name, //Instance name of the type i.e. toto (this is instance of Toto_Type below) + String general_type_name //Type declaration name i.e. Toto_Type + ) = + since col is hk_column(name, col_type, _, _), + if col_type is + { + p_key then "json_member(\""+name+"\", json_int("+current_type_name+".id))", + boolean_field then "json_member(\""+name+"\", json_bool("+current_type_name+"."+name+"))", + date_field(attrs) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))", + time_field(attrs) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))", + datetime_field(attrs) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))", + foreign_key(app,foreign_table) then "json_member(\""+name+"\", json_string(get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+")))", + integer_field(_) then "json_member(\""+name+"\", json_int("+current_type_name+"."+name+"))", + float_field then "json_member(\""+name+"\", json_float("+current_type_name+"."+name+",5))", + char_field(_,_) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))", + password_field(_) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))", + text_field(_) then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))" + } +. + +public define String + from_DB_cursor_to_JSON + ( + HK_Model_Column col, //Model of the column + String cursor, //db cursor name + Int index //current index of the column in the cursor + ) = + with cursor_index = "("+cursor+")("+index+")", + since col is hk_column(name, col_type, _, _), + if col_type is + { + p_key then "json_member(\""+name+"\", json_int((Int)db_integer"+cursor_index+"))", + boolean_field then "json_member(\""+name+"\", json_bool(db_bool"+cursor_index+"))", + date_field(_) then "json_member(\""+name+"\", json_string(text"+cursor_index+"))", + time_field(_) then "json_member(\""+name+"\", json_string(text"+cursor_index+"))", + datetime_field(_) then "json_member(\""+name+"\", json_string(text"+cursor_index+"))", + foreign_key(app,foreign_table) then "json_member(\""+name+"\", json_string(get_"+foreign_table+"_show_string(db, db_id((Int)db_integer"+cursor_index+"))))", + integer_field(_) then "json_member(\""+name+"\", json_int((Int)db_integer"+cursor_index+"))", + float_field then "json_member(\""+name+"\", json_float(db_float"+cursor_index+",5))", + char_field(_,_) then "json_member(\""+name+"\", json_string(text"+cursor_index+"))", + password_field(_) then "json_member(\""+name+"\", json_string(\"******\"))", + text_field(_) then "json_member(\""+name+"\", json_string(text"+cursor_index+"))" + } +. diff --git a/src/generation/types.anubis b/src/generation/types.anubis index d99a70f..dda137a 100644 --- a/src/generation/types.anubis +++ b/src/generation/types.anubis @@ -99,115 +99,10 @@ public define Maybe(One) -public define Maybe(One) - generate_extract_db_cursor - ( - Stream stream, - HK_Table table - )= - since table is hk_table(name, short_name, model, display), - 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\n"+ - "public define Maybe("+type_name+")\n"+ - " extract_"+name+"\n"+ - " (\n"+ - " One -> SQLite3Row table_cursor\n"+ - " ) =\n"+ - " if table_cursor(unique) is\n"+ - " {\n"+ - " error(sql_error) then println(db_error(sql_error, \"extract_"+name+"\")); failure,\n"+ - " no_more_row then failure,\n"+ - " row(cursor) then\n"+ - " success(\n"+generate_constructor_from_cursor(name, "cursor", columns, " ")+")\n"+ //generate type construction with values got from web arg - " }.\n") is //end of the function - { - failure then println("can't write generate_extract_db_cursor "+type_name); failure, - success(_) then success(unique) - }. -public define Maybe(One) - generate_get_all - ( - 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\n"+ - "public define List("+type_name+")\n"+ - " _get_all_"+name+"\n"+ - " (\n"+ - " One -> SQLite3Row table_cursor,\n"+ - " List("+type_name+") so_far\n"+ - " ) =\n"+ - " if extract_"+name+"(table_cursor) is\n"+ - " {\n"+ - " failure then reverse(so_far),\n"+ - " success(data) then _get_all_"+name+"(table_cursor, [data . so_far])\n"+ - " }.\n"+ - "\n\n" - //SQL query -// "public define List("+type_name+")\n"+ -// " get_all_"+name+"\n"+ -// " (\n"+ -// " SQLite3DataBase db,\n"+ -// " )=\n"+ -// " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+";\", [], \"get_all_"+name+"\") is\n"+ -// " {\n"+ -// " error(_) then [],\n"+ -// " ok(_, cursor, _) then _get_all_"+name+"(cursor, [])\n"+ -// " }.\n"+ -// "\n\n" - ) is //end of the function - { - failure then println("can't write generate_get_all "+type_name); failure, - success(_) then success(unique) - }. -public define Maybe(One) - generate_get_all_with_clause - ( - 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, - //SQL query - "public define List("+type_name+")\n"+ - " get_all_"+name+"_by_clause\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " String clause\n"+ - " )=\n"+ - " with final_clause = if clause = \"\" then\n"+ - " \"\"\n"+ - " else\n"+ - " \"WHERE \"+clause,\n"+ - "\n"+ - " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" \"+final_clause+\";\", [], \"get_all_"+name+"_by_clause\") is\n"+ - " {\n"+ - " error(_) then [],\n"+ - " ok(_, cursor, _) then _get_all_"+name+"(cursor, [])\n"+ - " }.\n" - - ) is //end of the function - { - failure then println("can't write generate_get_all "+type_name); failure, - success(_) then success(unique) - }. + public define Maybe(One) generate_get_one @@ -284,7 +179,7 @@ public define Maybe(One) " {\n"+ " error(_) then failure,\n"+ " ok(_, cursor, _) then extract_"+name+"(cursor)\n"+ - " }.\n" + " }.\n\n" ) is //end of the function { failure then println("can't write generate_get_one_by_clause "+type_name); failure, @@ -458,236 +353,10 @@ public define Maybe(One) " SQLite3DataBase db,\n"+ " String clause\n"+ " )=\n"+ - " _get_selector_"+name+"(db, get_all_"+name+"_by_clause(db, clause), [])." + " _get_selector_"+name+"(db, get_all_"+name+"_by_clause_to_list_type(db, clause), [])." ) is //end of the function { failure then println("can't write generate_show_string "+type_name); failure, success(_) then success(unique) }. - -public define Maybe(One) -/* Generate - -*/ - generate_VT_view - ( - Stream stream, //stream file where to write - HK_Table table, // - HK_List_View list_view //list view to generate - )= - since table is hk_table(table_name, short_name, model, display), - since model is hk_model( columns, _), - //since display is db_display(list_view, list_edit), - - with type_name = to_upper(table_name, 1), //make the first character to upper case and be an Anubis Type. - with view_name = if list_view is - { - list_view_all then "default", - list_view(view_name, _, _) then view_name - }, - with current_list_view = if list_view is - { - list_view_all then to_List_String(columns, false), - list_view(_, cols, _) then cols - }, - //generate the type of the table that contain all components - if write_string(stream, - "\n\n"+ - //SQL query - "define VT_view\n"+ - " _get_viewable_"+table_name+"_"+view_name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " List("+type_name+") list,\n"+ - " List(VT_view_row) so_far\n"+ - " )=\n"+ - " if list is\n"+ - " {\n"+ - " [] then //last entry, add the header and the reverse the list \n"+ - " with header = vt_view_row_header([\n"+ - // view row header - make_list_view_header(current_list_view, " ")+"\n"+ - " ]),\n"+ - " vt_view(\""+table_name+"\", \""+view_name+"\", header, reverse(so_far)),\n"+ - " [ h . t ] then\n"+ - " with entry = vt_view_row(to_Int(h.id), [\n"+ - // view row - make_list_view_row(table, "h", " ", list_view)+"\n"+ - " ]),\n"+ - " _get_viewable_"+table_name+"_"+view_name+"(db, t, [entry . so_far])\n"+ - " }.\n\n"+ - "public define VT_view\n"+ - " get_viewable_"+table_name+"_"+view_name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " String clause\n"+ - " )=\n"+ - " _get_viewable_"+table_name+"_"+view_name+"(db, get_all_"+table_name+"_by_clause(db, clause), []).\n\n" - ) is //end of the function - { - failure then println("can't write generate_VT_view "+type_name); failure, - success(_) then success(unique) - }. - -public define Maybe(One) -/* Generate - -*/ - _generate_VT_view - ( - Stream stream, //stream file where to write - HK_Table table, // - List(HK_List_View) list_view, //list view to generate - Bool has_default //set to true if a default view was encountered. - )= - if list_view is - { - [] then - //if default view was not generated, we force to generate it - if has_default = false then - generate_VT_view(stream, table, list_view_all) - else - success(unique), - [view . t] then - if generate_VT_view(stream, table, view) is - { - failure then failure, - success(_) then - with new_has_default = if has_default = true then //already true, no need to go further - true - else - if view is - { - list_view_all then true, //list_view_all is default view - list_view(view_name, _, _) then - //if not we search for view named 'default' - if view_name = "default" then true else false - }, - _generate_VT_view(stream, table, t, new_has_default) - } - }. - -public define String -/* Generate - -*/ - _generate_get_viewable_if_else - ( - String table_name, // - List(HK_List_View) list_view, //list view to generate - String so_far - )= - if list_view is - { - [] then - so_far + - "\n"+ - " _get_viewable_"+table_name+"_default(db, get_all_"+table_name+"_by_clause(db, clause), [])\n", - [view . t] then - with view_name = if view is - { - list_view_all then "default", - list_view(view_name, _, _) then view_name - }, - _generate_get_viewable_if_else( table_name, t, - so_far + - " if view = \""+view_name+"\" then\n"+ - " _get_viewable_"+table_name+"_"+view_name+"(db, get_all_"+table_name+"_by_clause(db, clause), [])\n"+ - " else" - ) - }. - -public define Maybe(One) -/* Generate - -*/ - generate_get_viewable - ( - Stream stream, //stream file where to write - HK_Table table, // - List(HK_List_View) list_view //list view to generate - )= - since table is hk_table(table_name, short_name, model, display), - if write_string(stream, - "public define VT_view\n"+ - " get_viewable_"+table_name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " String view,\n"+ - " String clause\n"+ - " )=\n"+ - _generate_get_viewable_if_else(table_name, list_view, "")+ - ".\n" - ) is //end of the function - { - failure then println("can't write generate_get_viewable "+table_name); failure, - success(_) then success(unique) - }. - -public define Maybe(One) - generate_VT_all_view - ( - Stream stream, - HK_Table table - )= - since table is hk_table(name, short_name, model, display), - since model is hk_model( columns, _), - since display is hk_display(list_view, list_edit), - - with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type. - if _generate_VT_view(stream, table, list_view, false) is //list view to generate - { - failure then failure, - success(_) then generate_get_viewable(stream, table, list_view) - }. - - - public define Maybe(One) - generate_VT_edit - ( - Stream stream, - HK_Table table - )= - since table is hk_table(name, short_name, model, display), - since model is hk_model( columns, _), - since display is hk_display(list_view, list_edit), - 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 ********** EDITABLE VIEWs ***************\n"+ - //SQL query - "define VT_edit\n"+ - " _get_editable_"+name+"\n"+ - " (\n"+ - " "+type_name+" "+name+",\n"+ - " SQLite3DataBase db\n"+ - " )=\n"+ - " with edit_entries = (List(VT_edit_entry))\n"+ - " [\n"+ - edit_entries(columns, name, " ", [])+"\n"+ - " ],\n"+ - " vt_edit(\""+name+"\", edit_entries).\n\n"+ - - "public define VT_edit\n"+ - " get_editable_"+name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " VT_action action\n"+ - " )=\n"+ - " with "+name+" = if action is\n"+ - " {\n"+ - " new_entry then get_default_"+name+"\n"+ - " edit_entry(idx) then\n"+ - " if get_"+name+"(db, idx) is\n"+ - " {\n"+ - " failure then get_default_"+name+",\n"+ - " success(data) then data\n"+ - " }\n"+ - " },\n"+ - " _get_editable_"+name+"("+name+", db).\n\n" - ) is //end of the function - { - failure then println("can't write generate_VT_edit "+type_name); failure, - success(_) then success(unique) - }. + diff --git a/src/generation/vt_edit.anubis b/src/generation/vt_edit.anubis index fab8b3e..17839e9 100644 --- a/src/generation/vt_edit.anubis +++ b/src/generation/vt_edit.anubis @@ -78,6 +78,7 @@ public define Maybe(One) " )=\n"+ " with edit_entries = (List(VT_edit_entry))\n"+ " [\n"+ + " hidden(\"id\", to_String("+table_name+".id)),\n"+ edit_entries(current_edit_view, table_name, " ", [])+"\n"+ " ],\n"+ " with fk_view_entries = (List(HK_V_Edit_Tab))\n"+ diff --git a/src/generation/vt_view.anubis b/src/generation/vt_view.anubis new file mode 100644 index 0000000..bc90cdc --- /dev/null +++ b/src/generation/vt_view.anubis @@ -0,0 +1,284 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 21/03/2017 + * Time: 22:00 + * © Calexium + */ + +read hayamiki_lib/types/hayamiki.anubis +read tools/basis.anubis +read tools/streams.anubis +read system/string.anubis +read fields.anubis +read columns.anubis +read html.anubis +read json.anubis + +public define String + make_list_view_json_row + ( + HK_Table table, //table to format + String data_name, //name of data + String indent, //indentation string to use + List(HK_Model_Column) columns //list view to generate + )= + since table is hk_table(table_name, short, model, _), //get name and model of the table +// since model is hk_model( columns, _), //get all columns of table +// since display is db_display(views, _), // + //and the other set with list_view +// "\"{ "+ join("+\", ", map((HK_Model_Column column) |-> column.name+": \"+to_JSON_String(" + to_String(column, data_name, table_name)+")", columns))+"+\" }\"" + "\"{ id : \"+to_JSON_String(to_String("+data_name+".id))+\", "+ + + join("+\", ", map((HK_Model_Column column) |-> column.name+": \"+" + to_JSON_String(column, data_name, table_name), columns)) +"+\" }\"" + + . + +define String + to_VT_view_model + ( + HK_Model_Column col, + String type_name + )= + since col is hk_column(name, col_type, _, help), + if col_type is + { + p_key then "primary_key", + boolean_field then "boolean(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + date_field(attrs) then "date(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + time_field(attrs) then "time(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + datetime_field(attrs) then "datetime(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + foreign_key(app,foreign_table) then "foreign_text(\""+to_upper(name)+"\", \""+name+"\", vt_edit_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")", + integer_field(choices) then + if choices is + { + no_choice then "integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + int_choices(choices_list) then + //if choice list is NOT empty we construct the possible choices selector + if length(choices_list) > 0 then + "choices_int(\""+to_upper(name)+"\", \""+name+"\", vt_edit_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")" + else + "integer(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + } + float_field then "float(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + + //TODO make choices selection if need + char_field(choices,_) then + if choices is + { + no_choice then "text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")", + text_choices(choices_list) then + //if choice list is NOT empty we construct the possible choices selector + if length(choices_list) > 0 then + "choices_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")" + else + "text(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")" + } + password_field(min_size) then "password(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")" + text_field(txt_attr) then "text_area(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")" + }. + + +public define String + make_list_view_model + ( + List(HK_Model_Column) columns, + String type_name, + String indent + )= + indent+join(",\n"+indent, map(((HK_Model_Column col) |-> to_VT_view_model(col, type_name)), columns)) +. + +public define String + make_list_view_header + ( + List(String) list_view, + String indent + )= + indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)). + +public define Maybe(One) +/* Generate + +*/ + generate_VT_view + ( + Stream stream, //stream file where to write + HK_Table table, // + HK_List_View list_view //list view to generate + )= + since table is hk_table(table_name, short_name, model, display), + since model is hk_model( columns, _), + //since display is db_display(list_view, list_edit), + + with type_name = to_upper(table_name, 1), //make the first character to upper case and be an Anubis Type. + with view_name = if list_view is + { + list_view_all then "default", + list_view(view_name, _, _) then view_name + }, + with current_list_view = if list_view is + { + list_view_all then to_List_String(columns, false), + list_view(_, cols, _) then cols + }, + with current_view_models = if list_view is + { + list_view_all then columns, + list_view(v_name, cols, _) then + if get_HK_Model_Column_list_from_name_list(columns, cols) is + { + failure then println("generate_VT_view: can't get all specified columns for table \""+table_name+"\" view name \""+v_name+"\" cols["+join(", ",cols)+"]");[], + success(got_columns) then got_columns, + } + }, + //generate the type of the table that contain all components + if write_string(stream, + "\n\n"+ + //SQL query + "define VT_view\n"+ + " _get_viewable_"+table_name+"_"+view_name+"\n"+ + " =\n"+ + " with header = vt_view_row_header([\n"+ + // view row header + make_list_view_header(current_list_view, " ")+"\n"+ + " ]),\n"+ + " with model = vt_view_model([\n"+ + //view model + make_list_view_model(current_view_models, type_name, " ")+"\n"+ + " ]),\n"+ + " vt_view(\n"+ + " \""+table_name+"\", //Name of the database table\n"+ + " \""+view_name+"\", //Name of the view\n"+ + " model,\n"+ + " header,\n"+ +" vt_view_db_calls(\n"+ +" get_count_"+table_name+"_by_clause,\n"+ +" get_all_"+table_name+"_by_clause_to_json\n"+ +//" get_"+xxx+"_rows\n"+ +" )\n"+ +" )\n"+ +".\n\n" + +// "public define VT_view\n"+ +// " get_viewable_"+table_name+"_"+view_name+"\n"+ +// " (\n"+ +// " SQLite3DataBase db,\n"+ +// " String clause\n"+ +// " )=\n"+ +// " _get_viewable_"+table_name+"_"+view_name+"(db, get_all_"+table_name+"_by_clause(db, clause), [], [] ).\n\n" + ) is //end of the function + { + failure then println("can't write generate_VT_view "+type_name); failure, + success(_) then success(unique) + }. + +public define Maybe(One) +/* Generate + +*/ + _generate_VT_view + ( + Stream stream, //stream file where to write + HK_Table table, // + List(HK_List_View) list_view, //list view to generate + Bool has_default //set to true if a default view was encountered. + )= + if list_view is + { + [] then + //if default view was not generated, we force to generate it + if has_default = false then + generate_VT_view(stream, table, list_view_all) + else + success(unique), + [view . t] then + if generate_VT_view(stream, table, view) is + { + failure then failure, + success(_) then + with new_has_default = if has_default = true then //already true, no need to go further + true + else + if view is + { + list_view_all then true, //list_view_all is default view + list_view(view_name, _, _) then + //if not we search for view named 'default' + if view_name = "default" then true else false + }, + _generate_VT_view(stream, table, t, new_has_default) + } + }. + +public define String +/* Generate + +*/ + _generate_get_viewable_if_else + ( + String table_name, // + List(HK_List_View) list_view, //list view to generate + String so_far + )= + if list_view is + { + [] then + so_far + + "\n"+ + " _get_viewable_"+table_name+"_default\n", + [view . t] then + with view_name = if view is + { + list_view_all then "default", + list_view(view_name, _, _) then view_name + }, + _generate_get_viewable_if_else( table_name, t, + so_far + + " if view = \""+view_name+"\" then\n"+ + " _get_viewable_"+table_name+"_"+view_name+"\n"+ + " else" + ) + }. + +public define Maybe(One) +/* Generate + +*/ + generate_get_viewable + ( + Stream stream, //stream file where to write + HK_Table table, // + List(HK_List_View) list_view //list view to generate + )= + since table is hk_table(table_name, short_name, model, display), + if write_string(stream, + "public define VT_view\n"+ + " get_viewable_"+table_name+"\n"+ + " (\n"+ + " String view,\n"+ + " )=\n"+ + _generate_get_viewable_if_else(table_name, list_view, "")+ + ".\n" + ) is //end of the function + { + failure then println("can't write generate_get_viewable "+table_name); failure, + success(_) then success(unique) + }. + +public define Maybe(One) + generate_VT_all_view + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, display), + since model is hk_model( columns, _), + since display is hk_display(list_view, list_edit), + + with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type. + if _generate_VT_view(stream, table, list_view, false) is //list view to generate + { + failure then failure, + success(_) then generate_get_viewable(stream, table, list_view) + }. diff --git a/src/generation/web_args.anubis b/src/generation/web_args.anubis index 8591957..c926b2f 100644 --- a/src/generation/web_args.anubis +++ b/src/generation/web_args.anubis @@ -104,7 +104,7 @@ public define String //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+"))]", + "if get_mb_Bool(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [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+"))] }" @@ -121,7 +121,7 @@ public define String else "[]", foreign_key(_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", - "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"), [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))]" + "if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [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+"\")", -- libgit2 0.21.4