diff --git a/src/generation/SQL_get_all.anubis b/src/generation/SQL_get_all.anubis index fe934bb..2ddd90a 100644 --- a/src/generation/SQL_get_all.anubis +++ b/src/generation/SQL_get_all.anubis @@ -53,6 +53,19 @@ public define Maybe(One) success(_) then success(unique) }. +//public define JsonMember +// get_component +// ( +// String component_name, +// Crm_price_level type +// )= +// if component_name = "id" then json_member(component_name, json_int(type.id)) +// else if component_name = "code" then json_member(component_name, json_string(type.code)) +// else if component_name = "label" then json_member(component_name, json_string(type.label)) +// else if component_name = "active" then json_member(component_name, json_bool(type.active)) +// else if component_name = "module" then json_member(component_name, json_string(type.module)) +// else json_member(component_name, json_null) +//. public define Maybe(One) generate_get_all_with_clause @@ -110,10 +123,27 @@ public define Maybe(One) " success(data) then _get_all_"+name+"_to_json(db, table_cursor, [data . so_far])\n"+ " }\n"+ ".\n"+ -"public define JsonMember\n"+ + +"public define List(JsonValue)\n"+ +" _get_all_"+name+"_to_json\n"+ +" (\n"+ +" SQLite3DataBase db,\n"+ +" List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\n"+ +" One -> SQLite3Row table_cursor,\n"+ +" List(JsonValue) so_far\n"+ +" ) =\n"+ +" if extract_"+name+"_cursor_to_json(db, extractors, table_cursor) is\n"+ +" {\n"+ +" failure then reverse(so_far),\n"+ +" success(data) then _get_all_"+name+"_to_json(db, extractors, table_cursor, [data . so_far])\n"+ +" }\n"+ +".\n"+ + +"public define JsonValue\n"+ " get_all_"+name+"_by_clause_to_json\n"+ " (\n"+ " SQLite3DataBase db,\n"+ +" List(String) columns,\n"+ " HK_Referer referer,\n"+ " String filter,\n"+ " String order_by\n"+ @@ -122,8 +152,14 @@ public define Maybe(One) " with _and_ = if length(ref_clause) > 0 & length(filter) > 0 then \" AND \" else \"\",\n"+ " if get_all_"+name+"_by_clause(db, ref_clause+_and_+filter+\" \"+order_by) 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"+ +" failure then json_array([]),\n"+ +" success(cursor) then\n"+ +" json_array(\n"+ +" if length(columns) = 0 then\n"+ +" _get_all_"+name+"_to_json(db, cursor, [])\n"+ +" else\n"+ +" _get_all_"+name+"_to_json(db, make_json_extractor(db, columns), cursor, [])\n"+ +" )\n"+ " }\n"+ ".\n" ) is //end of the function diff --git a/src/generation/columns.anubis b/src/generation/columns.anubis index e2f7ef4..62fab6a 100644 --- a/src/generation/columns.anubis +++ b/src/generation/columns.anubis @@ -242,10 +242,10 @@ public define Maybe(String) }, foreign_key(app,foreign_table,fields) then if length(fields) = 0 then - success("foreign_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")") + success("foreign_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")") else with search_fields = join(", ", map((String field) |-> "\""+field+"\"",fields)), - success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"]"+to_Anubis_source(help)+")"), + success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"], "+to_Anubis_source(help)+")"), integer_field(choices) then //success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"), diff --git a/src/generation/extractor.anubis b/src/generation/extractor.anubis index f483e9a..f776537 100644 --- a/src/generation/extractor.anubis +++ b/src/generation/extractor.anubis @@ -116,7 +116,7 @@ define List(String) to_json_member(t, cursor_name, [line . so_far], idx + 1 ) } . - + public define String generate_json_object_from_cursor ( @@ -125,7 +125,40 @@ public define String List(HK_Model_Column) columns, String indent )= - indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])". + indent+"json_object([\n"+indent+" "+join(",\n"+indent+" ", to_json_member(columns, cursor_name, [], 0))+"\n"+indent+"])" +. + + +define List(String) +/** + */ + to_get_extractor_line_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 = "if component_name =\""+name+"\" then ((Int -> SQLite3Datum) cursor) |-> "+from_DB_cursor_to_JSON(h, cursor_name, idx), + to_get_extractor_line_json_member(t, cursor_name, [line . so_far], idx + 1 ) + } +. + +public define String + generate_json_extractor + ( + String constructor_name, + String cursor_name, + List(HK_Model_Column) columns, + String indent + )= + indent+join("\n"+indent+"else ", to_get_extractor_line_json_member(columns, cursor_name, [], 0))+ + "\n"+indent+"else ((Int -> SQLite3Datum) cursor) |-> json_member(component_name, json_null)". public define Maybe(One) @@ -153,7 +186,42 @@ public define Maybe(One) " 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" + " }\n.\n"+ + "\n\n"+ + "public define Maybe(JsonValue)\n"+ + " extract_"+name+"_cursor_to_json\n"+ + " (\n"+ + " SQLite3DataBase db,\n"+ + " List(((Int -> SQLite3Datum) -> JsonMember)) extractors,\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"+ + " json_object(\n"+ + " map(((Int -> SQLite3Datum) -> JsonMember extractor) |-> extractor(cursor) , extractors)\n"+ + " ))\n"+ + " }\n.\n"+ + +"define (Int -> SQLite3Datum) -> JsonMember\n"+ +" get_extractor\n"+ +" (\n"+ +" SQLite3DataBase db,\n"+ +" String component_name,\n"+ +" )=\n"+generate_json_extractor(name, "cursor", columns, " ")+ +"\n.\n\n"+ + +"define List(((Int -> SQLite3Datum) -> JsonMember))\n"+ +" make_json_extractor\n"+ +" (\n"+ +" SQLite3DataBase db,\n"+ +" List(String) columns\n"+ +" )=\n"+ +" map((String column) |-> get_extractor(db, column), columns)\n"+ +".\n" ) is //end of the function { failure then println("can't write generate_extract_db_cursor "+type_name); failure, diff --git a/src/generation/files.anubis b/src/generation/files.anubis index 6f599d1..0bbffc1 100644 --- a/src/generation/files.anubis +++ b/src/generation/files.anubis @@ -70,6 +70,7 @@ define Maybe(One) 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_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_update(strm, table) is { failure then failure, success(_) then if generate_delete(strm, table) is { failure then failure, success(_) then @@ -78,7 +79,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 @@ -210,6 +211,7 @@ define One update_"+name+"_from_fields, //Read get_count_"+name+"_by_clause, + get_one_"+name+"_to_json, get_all_"+name+"_by_clause_to_json, //Delete delete_"+name+ diff --git a/src/generation/types.anubis b/src/generation/types.anubis index 8955ceb..55a52e7 100644 --- a/src/generation/types.anubis +++ b/src/generation/types.anubis @@ -88,7 +88,7 @@ public define Maybe(One) //generate the type of the table that contain all components if write_string(stream, - "\n\ndefine "+type_name+"\n get_default_"+name+" =\n "+name+"("+ //head of the definition + "\n\npublic define "+type_name+"\n get_default_"+name+" =\n "+name+"("+ //head of the definition join(", ",_generate_get_default(columns, []))+ //all components of the type ").\n\n") is //end of the function { @@ -174,6 +174,59 @@ public define Maybe(One) failure then println("can't write generate_get_one "+type_name); failure, success(_) then success(unique) }. + +public define Maybe(One) + generate_get_one_json + ( + Stream stream, + HK_Table table + )= + since table is hk_table(name, short_name, model, _), + since model is hk_model(columns, show_string), + 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 +"public define JsonValue\n"+ +" get_one_"+name+"_to_json\n"+ +" (\n"+ +" SQLite3DataBase db,\n"+ +" List(String) columns,\n"+ +" DB_id idx\n"+ +" )=\n"+ +" with final_columns = if length(columns) = 0 then "+name+"_columns else columns,\n"+ +" with error = json_object(map((String col_name) |-> json_member(col_name, json_null), final_columns)),\n"+ +"\n"+ +" if idx is\n"+ +" {\n"+ +" none then error,\n"+ +" db_id(_idx) then\n"+ +" if get_all_"+name+"_by_clause(db, \"id=\"+to_decimal(_idx)) is\n"+ +" {\n"+ +" failure then error,\n"+ +" success(cursor) then\n"+ +" if length(columns) = 0 then\n"+ +" if extract_"+name+"_cursor_to_json(db, cursor) is\n"+ +" {\n"+ +" failure then error,\n"+ +" success(result) then result\n"+ +" }\n"+ +" else\n"+ +" if extract_"+name+"_cursor_to_json(db, make_json_extractor(db, columns), cursor) is\n"+ +" {\n"+ +" failure then error,\n"+ +" success(result) then result\n"+ +" }\n"+ +" }\n"+ +" }\n"+ +".\n\n" + ) is //end of the function + { + failure then println("can't write generate_get_one "+type_name); failure, + success(_) then success(unique) + }. public define Maybe(One) generate_get_one_with_clause diff --git a/src/generation/vt_view.anubis b/src/generation/vt_view.anubis index a43b197..c19f1a1 100644 --- a/src/generation/vt_view.anubis +++ b/src/generation/vt_view.anubis @@ -156,12 +156,12 @@ public define Maybe(One) " \""+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"+ + " 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" -- libgit2 0.21.4