/* * 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"+ " HK_Referer referer,\n"+ " String filter\n"+ " )=\n"+ " with clause = referer_to_sql_clause(referer, \""+name+"\", "+name+"_columns),\n\n"+ " //with _and_ = if length(clause) > 0 & length(filter) > 0 then \" AND \" else \"\",\n"+ " if get_all_"+name+"_by_clause(db, clause+filter) 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) } .