From 1a0e85ff8cbaa0f349d7e42874178b2bb62aca96 Mon Sep 17 00:00:00 2001 From: totoro Date: Tue, 4 Jul 2017 23:08:01 +0200 Subject: [PATCH] improve code generation for p_key replace the now by db_now which was a change in Calexium lib. This avoid confusion in compilation time with primitive now remove the generation of delete function in profit of hk_sql_utils delete add trigger list in attached in table in header generation, replace read by transmit replace the function get_name by get_mb_name, where name is the type of line row of database entry. mb means Maybe type of name, because we are not sure to retrieve the data from row given index. add function "from_web_arg_to_type_with_default" which decode the type component from web arg and fill with default value the missing component fields --- calexium_lib | 2 +- hayamiki_generator.aproj | 1 + hayamiki_lib | 2 +- src/generation/columns.anubis | 22 ++++++++++++++++------ src/generation/create_table.anubis | 2 +- src/generation/fields.anubis | 9 +++++---- src/generation/files.anubis | 62 ++++++++++++++++++++++++++++++++------------------------------ src/generation/fn_delete.anubis | 40 ++++++++++++++++++++-------------------- src/generation/header.anubis | 44 ++++++++++++++++++++++---------------------- src/generation/html.anubis | 6 +++++- src/generation/json.anubis | 4 ++-- src/generation/types.anubis | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------- src/generation/vt_edit.anubis | 7 +------ src/generation/web_args.anubis | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 14 files changed, 268 insertions(+), 150 deletions(-) diff --git a/calexium_lib b/calexium_lib index 8b4b368..07f1bbc 160000 --- a/calexium_lib +++ b/calexium_lib @@ -1 +1 @@ -Subproject commit 8b4b368f40fb62c5583b14081d817309aa56337a +Subproject commit 07f1bbce4bbd814d97625f59577e98fc4f8eceea diff --git a/hayamiki_generator.aproj b/hayamiki_generator.aproj index a5804bb..027b378 100644 --- a/hayamiki_generator.aproj +++ b/hayamiki_generator.aproj @@ -92,6 +92,7 @@ + diff --git a/hayamiki_lib b/hayamiki_lib index 601f971..57345f2 160000 --- a/hayamiki_lib +++ b/hayamiki_lib @@ -1 +1 @@ -Subproject commit 601f9714449737d52f05351db81e3c22fd0b27a7 +Subproject commit 57345f2acee9ffeb19673b957d0aeb7296dfb742 diff --git a/src/generation/columns.anubis b/src/generation/columns.anubis index 62fab6a..acf6887 100644 --- a/src/generation/columns.anubis +++ b/src/generation/columns.anubis @@ -49,6 +49,7 @@ define List(String) */ _to_List_String ( + String table_name, List(HK_Model_Column) columns, Bool with_pk, //if set, add the primary key named "id" List(String) so_far @@ -59,9 +60,9 @@ define List(String) [h . t ] then since h is hk_column(name, _, _, _), if name = "id" & with_pk = false then - _to_List_String(t, with_pk, so_far) + _to_List_String(table_name, t, with_pk, so_far) else - _to_List_String(t, with_pk, [ name . so_far]) + _to_List_String(table_name, t, with_pk, [ table_name+name . so_far]) }. public define List(String) @@ -72,9 +73,18 @@ public define List(String) List(HK_Model_Column) columns, Bool with_pk )= - _to_List_String(columns, with_pk, []). - + _to_List_String("", columns, with_pk, []). +public define List(String) +/* return the list of all columns name + */ + to_List_String + ( + String table_name, + List(HK_Model_Column) columns, + Bool with_pk + )= + _to_List_String(table_name+".", columns, with_pk, []). public define String generate_constructor @@ -134,7 +144,7 @@ public define Maybe(String) if col_type is { //anubis(_T) then "constant_byte_array(0,0)", - p_key then failure, + p_key then if name = "id" then failure else success(name), //binary then "constant_byte_array(0,0)", boolean_field then success(name), date_field(attrs) then success(name), @@ -316,7 +326,7 @@ public define String since col is hk_column(name, col_type, _, _), if col_type is { - p_key then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)", + p_key then if name ="id" then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)" else "to_String("+current_type_name+"."+name+")", 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+")", diff --git a/src/generation/create_table.anubis b/src/generation/create_table.anubis index 406e29d..090d47b 100644 --- a/src/generation/create_table.anubis +++ b/src/generation/create_table.anubis @@ -50,7 +50,7 @@ define String Int fill_size )= since column is hk_column(name, col_type, attributes, _), - fill(name, fill_size)+ to_SQL_CREATE(col_type, attributes) + fill(name, fill_size)+ to_SQL_CREATE(col_type, attributes, name) . define String diff --git a/src/generation/fields.anubis b/src/generation/fields.anubis index c4ca503..4f6df6a 100644 --- a/src/generation/fields.anubis +++ b/src/generation/fields.anubis @@ -71,11 +71,12 @@ public define String ( HK_Model_Field t, List(HK_Model_Attr) attributes, + String name //name of component into the type ) = if t is { //anubis(_T) then "ByteArray", - p_key then fill("INTEGER", 15) + " PRIMARY KEY", + p_key then fill("INTEGER", 15) + (if name = "id" then " PRIMARY KEY" else ""), boolean_field then fill("BOOL", 15) + format_attributes(attributes), date_field(dt_attrs) then with new_attributes = if dt_attrs is auto_now_add then [default("(date('now', 'LOCALTIME'))")] else attributes, @@ -137,7 +138,7 @@ public define String if t is { //anubis(_T) then "constant_byte_array(0,0)", - p_key then "", + p_key then if name = "id" then "" else "bind_DB_id_or_NULL(\":v_"+name+"\", "+type_name+"."+name+")", //binary then "constant_byte_array(0,0)", boolean_field then "bind_Bool(\":v_"+name+"\", "+type_name+"."+name+")", date_field(attrs) then "bind_Date(\":v_"+name+"\", "+type_name+"."+name+")", @@ -145,10 +146,10 @@ public define String datetime_field(attrs) then if attrs is { none then "bind_Datetime(\":v_"+name+"\", "+type_name+"."+name+")", - auto_now then "bind_Datetime(\":v_"+name+"\", now)", + auto_now then "bind_Datetime(\":v_"+name+"\", db_now)", auto_now_add then if insert then - "bind_Datetime(\":v_"+name+"\", now)" + "bind_Datetime(\":v_"+name+"\", db_now)" else "" }, diff --git a/src/generation/files.anubis b/src/generation/files.anubis index ee5cf6a..f26b007 100644 --- a/src/generation/files.anubis +++ b/src/generation/files.anubis @@ -68,18 +68,18 @@ define Maybe(One) 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_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_one_json(strm, table) is { failure then failure, success(_) then - if generate_get_count_by_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_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)}}}}}}}}}}}}}}}}}}}}}} + success(unique)}}}}}}}}}}}}}}}}}}}} }. define One @@ -125,7 +125,7 @@ define Maybe(One) List(String) view, List(String) edit, // List(String) writer, - List(String) delete, +// List(String) delete, List(String) controllers, String dest_dir )= @@ -144,6 +144,7 @@ define Maybe(One) */ read tools/basis.anubis read hayamiki_lib/view/view_table_manager_types.anubis\n +read hayamiki_lib/controller/hk_controller.anubis read hayamiki_lib/controller/hk_sql_utils.anubis\n"+ join("\n", transmit)+"\n\n"+ "public define List(VTM_view)\n"+ @@ -158,14 +159,14 @@ join("\n", transmit)+"\n\n"+ //" generated_tm_writer_list =\n"+ //" [\n "+join(",\n ",writer)+"\n"+ //" ].\n\n"+ -"public define List(TM_delete)\n"+ -" generated_tm_delete_list =\n"+ -" [\n "+join(",\n ",delete)+"\n"+ -" ].\n\n"+ +//"public define List(TM_delete)\n"+ +//" generated_tm_delete_list =\n"+ +//" [\n "+join(",\n ",delete)+"\n"+ +//" ].\n\n"+ "public define List(HK_Table_controller)\n"+ " generated_hk_table_controller_list =\n"+ -" [\n "+join(",\n ",controllers)+"\n"+ -" ].\n\n" +" [\n "+join(",\n ",controllers)+"\n"+ +" ]\n.\n\n" ) is //end of the function { failure then println("can't write vtm "); failure, @@ -183,7 +184,7 @@ define One List(String) so_far_view, List(String) so_far_edit, // List(String) so_far_writer, - List(String) so_far_delete, +// List(String) so_far_delete, List(String) so_far_controllers, String dest_dir )= @@ -192,9 +193,9 @@ define One [] then if apps is { - [] then forget(generate_vtm_file(so_far_transmit, so_far_view, so_far_edit, so_far_delete, so_far_controllers, dest_dir));println("Generation finished"), + [] then forget(generate_vtm_file(so_far_transmit, so_far_view, so_far_edit, so_far_controllers, dest_dir));println("Generation finished"), [app . apps_t] then - _make_vtm(apps_t, app.hk_tables, so_far_transmit, so_far_view, so_far_edit, so_far_delete, so_far_controllers, dest_dir) + _make_vtm(apps_t, app.hk_tables, so_far_transmit, so_far_view, so_far_edit, so_far_controllers, dest_dir) }, @@ -204,25 +205,26 @@ define One with view = "vtm_view(\""+name+"\", get_viewable_"+name+", get_all_"+name+"_view_name)", with edit = "vtm_edit(\""+name+"\", get_editable_"+name+")", // with writer = "tm_writer(\""+name+"\", update_"+name+"_from_type, update_"+name+"_from_fields)", - with delete = "tm_delete(\""+name+"\", delete_"+name+")", +// with delete = "tm_delete(\""+name+"\", delete_"+name+")", with controller = "hk_table_controller(\""+name+"\", - //Create - update_"+name+"_from_type, - update_"+name+"_from_fields, - //Read - (SQLite3DataBase db, String clause) |-> get_count_by_clause(db, \""+name+"\", clause), - get_one_"+name+"_to_json, - get_all_"+name+"_by_clause_to_json, - //Delete - delete_"+name+", + //Create + update_"+name+"_from_type, + update_"+name+"_from_fields, + //Read + (SQLite3DataBase db, String clause) |-> get_count_by_clause(db, \""+name+"\", clause), + get_one_"+name+"_to_json, + get_all_"+name+"_by_clause_to_json, + //Delete + (SQLite3DataBase db, List(Int) ids) |-> delete_from_ids(db, \""+name+"\", ids), - //view interface - //selector - get_selector_"+name+"\n"+ -")", + //view interface + //selector + get_selector_"+name+", + hk_trigger_set(var([]), var([])) //initialize the triggers list as empty + )", - _make_vtm(apps, t, [transmit . so_far_transmit], [view . so_far_view], [edit . so_far_edit], [delete . so_far_delete], [controller . so_far_controllers], dest_dir) + _make_vtm(apps, t, [transmit . so_far_transmit], [view . so_far_view], [edit . so_far_edit], [controller . so_far_controllers], dest_dir) }. define One @@ -232,7 +234,7 @@ define One String dest_dir ) = - _make_vtm(apps, [], [], [], [], [], [], dest_dir). + _make_vtm(apps, [], [], [], [], [], dest_dir). define Maybe(One) diff --git a/src/generation/fn_delete.anubis b/src/generation/fn_delete.anubis index 26cc94d..ca80148 100644 --- a/src/generation/fn_delete.anubis +++ b/src/generation/fn_delete.anubis @@ -25,26 +25,26 @@ public define Maybe(One) 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"+ - //SQL query - "read tools/int.anubis\n\n"+ - "public define Maybe(One)\n"+ - " delete_"+name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " List(Int) ids\n"+ - " )=\n"+ - //DELETE SQL query - " if sql_query_timeout(db,\n"+ - " \"DELETE FROM "+name+"\n"+ - " WHERE id IN (\"+join(\", \", to_List_String(ids))+\");\",\n"+ - " [],\n"+ - " \"delete_"+name+"\") is\n"+ - " {\n"+ - " error(_) then failure,\n"+ - " ok(_, _, _) then success(unique)\n"+ - " }.\n\n" + if write_string(stream, "" +// "\n\n"+ +// //SQL query +// "read tools/int.anubis\n\n"+ +// "public define Maybe(One)\n"+ +// " delete_"+name+"\n"+ +// " (\n"+ +// " SQLite3DataBase db,\n"+ +// " List(Int) ids\n"+ +// " )=\n"+ +// //DELETE SQL query +// " if sql_query_timeout(db,\n"+ +// " \"DELETE FROM "+name+"\n"+ +// " WHERE id IN (\"+join(\", \", to_List_String(ids))+\");\",\n"+ +// " [],\n"+ +// " \"delete_"+name+"\") is\n"+ +// " {\n"+ +// " error(_) then failure,\n"+ +// " ok(_, _, _) then success(unique)\n"+ +// " }.\n\n" // + // "public define Maybe(One)\n"+ // " delete_"+name+"\n"+ diff --git a/src/generation/header.anubis b/src/generation/header.anubis index f79b113..ff99b11 100644 --- a/src/generation/header.anubis +++ b/src/generation/header.anubis @@ -50,30 +50,30 @@ public define Maybe(One) * Time: "+_Int_to_ISO_8601_time(now)+" * */ -read tools/basis.anubis +transmit tools/basis.anubis -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 -read calexium_lib/database/db_utils.anubis -read calexium_lib/database/db_types.anubis -read hayamiki_lib/view/types/hk_form.anubis -read hayamiki_lib/view/types/hk_view.anubis -read hayamiki_lib/view/types/hk_filter.anubis -read hayamiki_lib/types/hayamiki.anubis -read hayamiki_lib/controller/hk_sql_utils.anubis -read system/logger.anubis -read system/datetime.anubis -read system/convert.anubis -read system/string.anubis +transmit calexium_lib/web/CXM_making_a_web_site.anubis +transmit calexium_lib/web/CXM_common.anubis +transmit calexium_lib/web/CXM_web_dump.anubis +transmit calexium_lib/web/CXM_web_arg_utils.anubis +transmit calexium_lib/web/CXM_json.anubis +transmit calexium_lib/web/widgets/icons_set.anubis +transmit data_base/sqlite.anubis +transmit data_base/db_tools.anubis +transmit calexium_lib/database/db_utils.anubis +transmit calexium_lib/database/db_types.anubis +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 hayamiki_lib/types/hayamiki.anubis +transmit hayamiki_lib/controller/hk_sql_utils.anubis +transmit system/logger.anubis +transmit system/datetime.anubis +transmit system/convert.anubis +transmit system/string.anubis -read calexium_lib/net_services_protocols/logger_service.anubis -read app/app_loggers.anubis +transmit calexium_lib/net_services_protocols/logger_service.anubis +transmit app/app_loggers.anubis ") is failure then failure else foreign_table_header(stream, columns) . diff --git a/src/generation/html.anubis b/src/generation/html.anubis index a95ea14..aa94f99 100644 --- a/src/generation/html.anubis +++ b/src/generation/html.anubis @@ -89,7 +89,11 @@ public define String since column is hk_column(name, col_type, _, _), if col_type is { - p_key then to_HTML_cell_link(column, data_name, general_type_name), + p_key then + if name = "id" then + to_HTML_cell_link(column, data_name, general_type_name) + else + to_HTML_cell_text(column, data_name, general_type_name), boolean_field then to_HTML_cell_icon(column, data_name, general_type_name), date_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name), time_field(attrs) then to_HTML_cell_text(column, data_name, general_type_name), diff --git a/src/generation/json.anubis b/src/generation/json.anubis index 3d5a32d..c5a6748 100644 --- a/src/generation/json.anubis +++ b/src/generation/json.anubis @@ -23,7 +23,7 @@ public define String since col is hk_column(name, col_type, _, _), if col_type is { - p_key then current_type_name+".id", + p_key then current_type_name+"."+name, 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+"))", @@ -55,7 +55,7 @@ public define String since col is hk_column(name, col_type, _, _), if col_type is { - p_key then "json_member(\""+name+"\", json_int("+current_type_name+".id))", + p_key then "json_member(\""+name+"\", json_int("+current_type_name+"."+name+"))", 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+"))", diff --git a/src/generation/types.anubis b/src/generation/types.anubis index 0200580..9f0705a 100644 --- a/src/generation/types.anubis +++ b/src/generation/types.anubis @@ -109,9 +109,11 @@ public define Maybe(One) //generate the type of the table that contain all components if write_string(stream, - "\n\ndefine List(String)\n "+name+"_columns =\n ["+ //head of the definition + "\n\npublic define List(String)\n "+name+"_columns =\n ["+ //head of the definition join(",\n ",enclose("\"", to_List_String(columns, false)))+ //all columns name wihtout id - " ].\n\n") is //end of the list + " ].\n\n"+ + "public define String\n"+ + " "+name+"_select_fields = \""+join(", ",to_List_String(name, columns, true))+"\".\n\n") is //end of the list { failure then println("can't write generate_columns_list "+type_name); failure, success(_) then success(unique) @@ -132,27 +134,36 @@ public define Maybe(One) if write_string(stream, "\n\n"+ //SQL query +// "public define Maybe("+type_name+")\n"+ +// " get_"+name+"\n"+ +// " (\n"+ +// " SQLite3DataBase db,\n"+ +// " String idx\n"+ +// " )=\n"+ +// " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" WHERE id=\"+idx+\";\", [], \"get_"+name+"\") is\n"+ +// " {\n"+ +// " error(_) then failure,\n"+ +// " ok(_, cursor, _) then extract_"+name+"(cursor)\n"+ +// " }.\n\n"+ + //SQL query "public define Maybe("+type_name+")\n"+ - " get_"+name+"\n"+ + " get_mb_"+name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ " String idx\n"+ " )=\n"+ - " if sql_query_timeout(db, \"SELECT "+join(", ", enclose("\\\"",to_List_String(columns, true)))+" FROM "+name+" WHERE id=\"+idx+\";\", [], \"get_"+name+"\") is\n"+ - " {\n"+ - " error(_) then failure,\n"+ - " ok(_, cursor, _) then extract_"+name+"(cursor)\n"+ - " }.\n\n"+ - //SQL query + " get_"+name+"_by_clause(db, \" id = \"+idx).\n\n"+ + "public define Maybe("+type_name+")\n"+ - " get_"+name+"\n"+ + " get_mb_"+name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ " Int idx\n"+ " )=\n"+ - " get_"+name+"(db, to_decimal(idx)).\n\n"+ + " get_"+name+"_by_clause(db, \" id = \"+to_decimal(idx)).\n\n"+ + "public define Maybe("+type_name+")\n"+ - " get_"+name+"\n"+ + " get_mb_"+name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ " DB_id idx\n"+ @@ -160,9 +171,27 @@ public define Maybe(One) " if idx is\n"+ " {\n"+ " none then failure,\n"+ - " db_id(_idx) then get_"+name+"(db, to_decimal(_idx))\n"+ + " db_id(_idx) then get_mb_"+name+"(db, _idx)\n"+ " }"+ - ".\n\n" + + ".\n\n"+ + "public define "+type_name+"\n"+ + " get_"+name+"\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " DB_id idx\n"+ + " )=\n"+ + " if idx is\n"+ + " {\n"+ + " none then get_default_"+name+",\n"+ + " db_id(_idx) then\n"+ + " if get_"+name+"_by_clause(db, \" id = \"+to_decimal(_idx)) is\n"+ + " {\n"+ + " failure then get_default_"+name+",\n"+ + " success(r) then r\n"+ + " }\n"+ + " }"+ + ".\n\n" ) is //end of the function { failure then println("can't write generate_get_one "+type_name); failure, @@ -268,7 +297,7 @@ public define Maybe(One) if write_string(stream, "\n\n"+ //SQL query - "public define One\n"+ + "public define Maybe(Int)\n"+ " update_"+name+"\n"+ " (\n"+ " SQLite3DataBase db,\n"+ @@ -278,11 +307,24 @@ public define Maybe(One) " {\n"+ //INSERT SQL query " none then\n"+ - " forget(sql_query_timeout(db,\n"+ + " if sql_query_timeout(db,\n"+ " \"INSERT INTO "+name+"\n"+ insert_values(columns, " ")+";\",\n"+ generate_Bind_list(columns, name, " ", true)+",\n"+ - " \"update_"+name+" (INSERT)\")),\n"+ + " \"update_"+name+" (INSERT)\") is\n"+ + " {\n"+ + " error(sql_error) then\n"+ + " //logError(debug_log, db_error(sql_error, table_name));\n"+ + " failure,\n"+ + " ok(_, cursor, _) then\n"+ + " if sql_query_scalar(db, \"SELECT last_insert_rowid()\") is\n"+ + " {\n"+ + " error(_) then failure,\n"+ + " ok(datum) then\n"+ + " with r = if datum is integer(n) then n else 0,\n"+ + " success(r)\n"+ + " }\n"+ + " },\n"+ //UPDATE SQL query " db_id(idx) then\n"+ " if sql_query_timeout(db,\n"+ @@ -291,38 +333,39 @@ public define Maybe(One) " WHERE id = \"+idx+\";\",\n"+ generate_Bind_list(columns, name, " ", false)+",\n"+ " \"update_"+name+" (UPDATE)\") is { \n"+ - " error(sql_error)\n"+ - " then logError(debug_log, db_error(sql_error, \"update_"+name+"\")), ok(_, _, _) then unique }\n"+ + " error(sql_error) then\n"+ + " logError(debug_log, db_error(sql_error, \"update_"+name+"\")); failure,\n"+ + " ok(_, _, _) then success(idx) }\n"+ " }.\n\n"+ - "public define Maybe(One)\n"+ - " update_"+name+"\n"+ - " (\n"+ - " SQLite3DataBase db,\n"+ - " DB_id db_id,\n"+ - " List(SQLite3_update_field) fields\n"+ - " )=\n"+ - " if db_id is\n"+ - " {\n"+ - //INSERT SQL query - " none then\n"+ - " since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),\n"+ - " if sql_query_timeout(db, \"INSERT INTO "+name+" \"+insert_columns+\" VALUES \"+insert_values+\";\", binds, \"update_"+name+" (INSERT)\") is\n"+ - " {\n"+ - " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+ - " ok(_, _, _) then success(unique)\n"+ - " },\n"+ - //UPDATE SQL query - " db_id(idx) then\n"+ - " since make_update_clause_and_binds(fields) is (binds, set_string),\n"+ - " if sql_query_timeout(db, \"UPDATE "+name+" SET \"+set_string +\" WHERE id = \"+idx+\";\", binds, \"update_"+name+" (UPDATE)\") is\n"+ - " {\n"+ - " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+ - " ok(_, _, _) then success(unique)\n"+ - " }\n"+ - " }.\n\n"+ +// "public define Maybe(One)\n"+ +// " update_"+name+"\n"+ +// " (\n"+ +// " SQLite3DataBase db,\n"+ +// " DB_id db_id,\n"+ +// " List(SQLite3_update_field) fields\n"+ +// " )=\n"+ +// " if db_id is\n"+ +// " {\n"+ +// //INSERT SQL query +// " none then\n"+ +// " since make_insert_clause_and_binds(fields) is (binds, insert_columns, insert_values),\n"+ +// " if sql_query_timeout(db, \"INSERT INTO "+name+" \"+insert_columns+\" VALUES \"+insert_values+\";\", binds, \"update_"+name+" (INSERT)\") is\n"+ +// " {\n"+ +// " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+ +// " ok(_, _, _) then success(unique)\n"+ +// " },\n"+ +// //UPDATE SQL query +// " db_id(idx) then\n"+ +// " since make_update_clause_and_binds(fields) is (binds, set_string),\n"+ +// " if sql_query_timeout(db, \"UPDATE "+name+" SET \"+set_string +\" WHERE id = \"+idx+\";\", binds, \"update_"+name+" (UPDATE)\") is\n"+ +// " {\n"+ +// " error(sql_error) then logError(debug_log, db_error(sql_error, \"update_"+name+"\"));failure,\n"+ +// " ok(_, _, _) then success(unique)\n"+ +// " }\n"+ +// " }.\n\n"+ - "public define Maybe(One)\n"+ + "public define Maybe(Int)\n"+ " update_"+name+"_from_type\n"+ " (\n"+ " SQLite3DataBase db,\n"+ @@ -331,16 +374,16 @@ public define Maybe(One) " if extract_"+type_name+"_from_web_arg(lwa, \"\") is\n"+ " {\n"+ " failure then logError(debug_log, \"update_"+name+" extract_"+type_name+"_from_web_arg is failure\"); failure,\n"+ - " success("+name+") then success(update_"+name+"(db, "+name+"))\n"+ + " success("+name+") then update_"+name+"(db, "+name+")\n"+ " }.\n\n"+ - "public define Maybe(One)\n"+ + "public define Maybe(Int)\n"+ " update_"+name+"_from_fields\n"+ " (\n"+ " SQLite3DataBase db,\n"+ " List(Web_arg) lwa\n"+ " )=\n"+ - " update_"+name+"(db, get_DB_id(lwa, \"id\"),extract_"+type_name+"_update_fields_from_web_arg(lwa, \"\"))\n"+ + " insert_or_update(db, \""+name+"\", get_DB_id(lwa, \"id\"), extract_"+type_name+"_update_fields_from_web_arg(lwa, \"\"))\n"+ ".\n\n" ) is //end of the function @@ -373,7 +416,7 @@ public define Maybe(One) " {\n"+ " none then default_select,\n"+ " db_id(idx) then \n"+ - " if get_"+name+"(db, idx) is\n"+ + " if get_mb_"+name+"(db, idx) is\n"+ " {\n"+ " failure then default_select,\n"+ " success(obj) then "+show_string+"\n"+ diff --git a/src/generation/vt_edit.anubis b/src/generation/vt_edit.anubis index 76e7da5..280f994 100644 --- a/src/generation/vt_edit.anubis +++ b/src/generation/vt_edit.anubis @@ -153,12 +153,7 @@ public define Maybe(One) " with "+table_name+" = if action is\n"+ " {\n"+ " new_entry then get_default_"+table_name+"\n"+ - " edit_entry(idx) then\n"+ - " if get_"+table_name+"(db, idx) is\n"+ - " {\n"+ - " failure then get_default_"+table_name+",\n"+ - " success(data) then data\n"+ - " }\n"+ + " edit_entry(idx) then get_"+table_name+"(db, idx)\n"+ " },\n"+ _generate_get_editable_if_else(table_name, edit_view, "")+ ".\n" diff --git a/src/generation/web_args.anubis b/src/generation/web_args.anubis index 50defe2..54f25eb 100644 --- a/src/generation/web_args.anubis +++ b/src/generation/web_args.anubis @@ -8,6 +8,7 @@ read hayamiki_lib/types/hayamiki.anubis read tools/streams.anubis +read fields.anubis read columns.anubis public define String @@ -35,7 +36,7 @@ public define String phone_field then "get_String(lwa, __prefix__+\""+name+"\")", email_field then "get_String(lwa, __prefix__+\""+name+"\")" }. - + define (List(String), Int) /* */ @@ -62,6 +63,52 @@ define (List(String), Int) _generate_extract_web_arg_to_type(t, [ line . so_far], closure+1) } . + +public define String + from_web_arg_to_type_with_default + ( + HK_Model_Field col_type, + String name + ) = + if col_type is + { + //anubis(_T) then "constant_byte_array(0,0)", + p_key then "get_DB_id(lwa, __prefix__+\""+name+"\"),", + //binary then "constant_byte_array(0,0)", + boolean_field then "get_Bool(lwa, __prefix__+\""+name+"\"),", + date_field(attrs) then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_date,", + time_field(attrs) then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_time,", + datetime_field(attrs) then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," else "get_dummy_DB_datetime,", + foreign_key(_,_,_) then "get_DB_id(lwa, __prefix__+\""+name+"\"),", + integer_field(_) then "get_Int(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + float_field then "get_Float(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + char_field(_,_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + password_field(min_size) then "if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is { failure then "+to_Anubis_default(col_type)+", success(pass) then pass},", + text_field(_) then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + color_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + phone_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+"),", + email_field then "get_String(lwa, __prefix__+\""+name+"\", "+to_Anubis_default(col_type)+")," + }. + +define List(String) +/* + */ + _generate_extract_web_arg_to_type_with_default + ( + List(HK_Model_Column) columns, + List(String) so_far + )= + if columns is + { + [] then reverse(so_far), + [h . t ] then + since h is hk_column(name, col_type, _, _), + with result = from_web_arg_to_type_with_default(col_type, name), + //If the field is not editable no need to get it from the web + _generate_extract_web_arg_to_type_with_default(t, [ " "+result . so_far]) + + } +. public define Maybe(One) /* generate the function which extract the entire database table row type from web @@ -87,7 +134,18 @@ public define Maybe(One) 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 + ".\n\n"+ + "\n\npublic define "+type_name+"\n"+ + " extract_"+type_name+"_from_web_arg_with_default\n"+ + " (\n"+ + " List(Web_arg) lwa,\n"+ + " String __prefix__\n"+ + " )=\n"+ //head of the definition + " "+name+"(\n"+ + to_String(_generate_extract_web_arg_to_type_with_default(columns, []))+ + ")\n"+ //generate type construction with values got from web arg + ".\n\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) @@ -105,28 +163,32 @@ public define String if t is { //anubis(_T) then "constant_byte_array(0,0)", - p_key then "[]", + p_key then + if name ="id" then + "[]" + else + "if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))] }", boolean_field then "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 is { none then "if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }", - auto_now then "[field(\""+name+"\", bind_Date(\":v_"+name+"\", now))]", + auto_now then "[field(\""+name+"\", bind_Date(\":v_"+name+"\", db_now))]", auto_now_add then "[]" }, time_field(attrs) then if attrs is { none then "if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }", - auto_now then "[field(\""+name+"\", bind_Time(\":v_"+name+"\", now))]", + auto_now then "[field(\""+name+"\", bind_Time(\":v_"+name+"\", db_now))]", auto_now_add then "[]" }, datetime_field(attrs) then if attrs is { none then "if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }", - auto_now then "[field(\""+name+"\", bind_Datetime(\":v_"+name+"\", now))]", + auto_now then "[field(\""+name+"\", bind_Datetime(\":v_"+name+"\", db_now))]", auto_now_add then "[]" }, foreign_key(_,_,_) then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),", -- libgit2 0.21.4