extractor.anubis 7.61 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 25/03/2017
 * Time: 03:18
 * © David RENÉ
 */

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,
//    mobile_field                        then "text"+cursor_index,
    url_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, constraint, _),
  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+"])"
.
  

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 if component_name =\"__HK_SHOW_STRING__\" then ((Int -> SQLite3Datum) cursor) |-> json_member(\"__HK_SHOW_STRING__\", json_string(get_"+constructor_name+"_show_string(db, db_id((Int)db_integer(cursor)(0)))))"+
  "\n"+indent+"else ((Int -> SQLite3Datum) cursor) |-> json_member(component_name, json_null)".


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, 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 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\n"+

"public define String get_"+name+"_show_string(SQLite3DataBase  db, DB_id _idx).\n\n"+
"define (Int -> SQLite3Datum) -> JsonMember\n"+
"  get_extractor\n"+
"  (\n"+
"    SQLite3DataBase db,\n"+
"    String          component_name,\n"+
"    Bool            resolve_fk\n"+
"  )=\n"+generate_json_extractor(name, "cursor", columns, "  ")+
"\n.\n\n"+

"public define List(((Int -> SQLite3Datum) -> JsonMember))\n"+
"  make_"+name+"_json_extractor\n"+
"  (\n"+
"    SQLite3DataBase db,\n"+
"    List(String)    columns,\n"+
"    Bool            resolve_fk\n"+
"  )=\n"+
"  map((String column) |-> get_extractor(db, column, resolve_fk), columns)\n"+
".\n"
  ) is   //end of the function
  {
    failure     then  println("can't write generate_extract_db_cursor "+type_name); failure,
    success(_)  then  success(unique)
  }.