/* * 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, color_field then "text"+cursor_index, phone_field then "text"+cursor_index, email_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) }.