SQL_get_all.anubis 5.58 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 17/05/2025
 * Time: 23:52
 * © David RENÉ
 */



read hayamiki_lib/types/hayamiki.anubis
read tools/streams.anubis
read ../columns.anubis

public define Maybe(One)
  generate_postgresql_get_all
  (
    Stream    stream,
    HK_Table  table
  )=
  since table is hk_table(name, short_name, model, _),
  since model is hk_model(columns, constraints, _),
  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 -> DbRow 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 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_postgresql_get_all_with_clause
  (
    Stream    stream,
    HK_Table  table
  )=
  since table is hk_table(name, short_name, model, _),
  since model is hk_model(columns, constraints, _),
  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 -> BdRow)\n"+
     "  get_all_"+name+"_by_clause\n"+
     "  (\n"+
     "    DataBase  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"+
    "    DataBase  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"+
//"    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"+
"    DataBase        db,\n"+
"    List(String)    columns,\n"+
"    Bool            resolve_fk,\n"+
"    HK_Referer      referer,\n"+
"    String          filter,\n"+
"    String          order_by\n"+
"  )=\n"+
"  with    ref_clause = referer_to_sql_clause(referer, \""+name+"\", "+name+"_columns),\n"+
"  with final_columns = if length(columns) = 0 then [\"id\". "+name+"_columns] else columns,\n\n"+
"  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_array([]),\n"+
"    success(cursor) then\n"+
"      json_array(\n"+
//"        _get_all_"+name+"_to_json(db, make_"+name+"_json_extractor(db, final_columns, resolve_fk), cursor, [])\n"+
"        get_all_to_json(db, make_"+name+"_json_extractor(db, final_columns, resolve_fk), 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)
  }
.