web_args.anubis 8.98 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 16/03/2017
 * Time: 23:38
 * © Calexium 
 */

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

public define String
  from_web_arg_to_type
  (
    HK_Model_Field  t,
    String          name
  ) =
  if t is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                             then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                     then "with "+name+" = get_Bool(lwa, __prefix__+\""+name+"\"),",
    date_field(attrs)                 then if attrs = none then "get_DB_date(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_date,",
    time_field(attrs)                 then if attrs = none then "get_DB_time(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_time,",
    datetime_field(attrs)             then if attrs = none then "get_DB_datetime(lwa, __prefix__+\""+name+"\")" else "with "+name+" = get_dummy_DB_datetime,",      
    foreign_key(_,_)                  then "with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
    integer_field(_)                  then "get_Int(lwa, __prefix__+\""+name+"\")",
    float_field                       then "get_Float(lwa, __prefix__+\""+name+"\")",
    char_field(_,_)                   then "get_String(lwa, __prefix__+\""+name+"\")",
    password_field(min_size)          then "get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\")",
    text_field(_)                     then "get_String(lwa, __prefix__+\""+name+"\")",
    color_field                       then "get_String(lwa, __prefix__+\""+name+"\")",
  }.
  
define (List(String), Int)
/* 
 */
  _generate_extract_web_arg_to_type
  (
    List(HK_Model_Column) columns,
    List(String)          so_far,
    Int                   closure
  )=
  if columns is
  {
    []        then (reverse(so_far), closure),
    [h . t ]  then
      since h is hk_column(name, col_type, _, _),
      with result =  from_web_arg_to_type(col_type, name),
      //If the field is not editable no need to get it from the web
      if result = "" then
        _generate_extract_web_arg_to_type(t, so_far, closure)
      else 
        if start_with(result, "with") then
          _generate_extract_web_arg_to_type(t, [ "  "+result . so_far], closure)
        else
          with line = "  if "+fill(result, 70)+" is {failure then failure, "+fill("success("+name+")",35)+" then",
          _generate_extract_web_arg_to_type(t, [ line . so_far], closure+1)
  }
.
  
public define Maybe(One)
/* generate the function which extract the entire database table row type from web
 * arguments and return maybe that type.
 */
  generate_extract_web_arg_to_type
  (
    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.
  since _generate_extract_web_arg_to_type(columns, [], 0) is ((List(String))list_of_web_arg, (Int) nb_closure),
  //generate the type of the table that contain all components 
  if write_string(stream, 
     "\n\npublic define Maybe("+type_name+")\n"+
     "  extract_"+type_name+"_from_web_arg\n"+
     "  (\n"+
     "    List(Web_arg) lwa,\n"+
     "    String __prefix__\n"+
     "  )=\n"+   //head of the definition
     to_String(list_of_web_arg)+                       //all editable components of the type
     "    success(\n"+generate_constructor(name, columns, "      ")+")\n"+         //generate type construction with values got from web arg
     "  "+fill(nb_closure, '}')+          //add close } according to number of web arg 
     ".\n") is                                         //end of the function
  {
    failure     then  println("can't write extract_"+type_name+"_from_web_arg "+type_name); failure,
    success(_)  then  success(unique)
  }
.

 /***** UPDATE FIELD ********/

public define String
  from_web_arg_to_update_field
  (
    HK_Model_Field  t,
    String          name
  ) =
  if t is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                             then "[]",
    boolean_field                     then 
      "if get_mb_Bool(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Bool(\":v_"+name+"\", "+name+"))] }",
    date_field(attrs)                 then 
      if attrs = none then
        "if get_DB_date(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Date(\":v_"+name+"\", "+name+"))] }"
      else 
        "[]",            
    time_field(attrs)                 then 
      if attrs = none then 
        "if get_DB_time(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Time(\":v_"+name+"\", "+name+"))] }"
      else 
        "[]",      
    datetime_field(attrs)             then 
      if attrs = none then 
        "if get_DB_datetime(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Datetime(\":v_"+name+"\", "+name+"))] }"
      else 
        "[]",
    foreign_key(_,_)                  then //"with "+name+" = get_DB_id(lwa, __prefix__+\""+name+"\"),",
      "if get_mb_DB_id(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_DB_id_or_NULL(\":v_"+name+"\", "+name+"))] }"    
    integer_field(_)                  then //"get_Int(lwa, __prefix__+\""+name+"\")",
      "if get_Int(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Int(\":v_"+name+"\", "+name+"))] }"    
    float_field                       then //"get_Float(lwa, __prefix__+\""+name+"\")",
      "if get_Float(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_Float(\":v_"+name+"\", "+name+"))] }"    
    char_field(_,_)                   then 
      "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }",
    password_field(min_size)          then
      "(if get_String(lwa, \"password_pwd_change_option\", \"false\") = \"false\" then \n"+
      "    if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }\n"+
      "  else\n"+
      "    if get_Bool(lwa, __prefix__+\""+name+"_pwd_change\") then \n"+
      "      if get_Password(lwa, __prefix__+\""+name+"_1\", __prefix__+\""+name+"_2\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }\n"+   
      "    else\n"+
      "      []\n"+
      "  )"
      // if get_Password(lwa, __prefix__+"password_1", __prefix__+"password_2")     is {failure then [], success(password) then [field("passwxord", bind_String(":v_password", password))] }
    text_field(_)                     then 
      "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }",
    color_field                       then 
      "if get_String(lwa, __prefix__+\""+name+"\") is {failure then [], success("+name+") then [field(\""+name+"\", bind_String(\":v_"+name+"\", "+name+"))] }"
     //"get_String(lwa, __prefix__+\""+name+"\")"
  }.

define String
/* 
 */
  _generate_extract_web_arg_to_update_field
  (
    List(HK_Model_Column) columns,
  )=
  join("+\n",
    map((HK_Model_Column col) |->
        since col is hk_column(name, col_type, _, _), 
        "  "+from_web_arg_to_update_field(col_type, name),
      columns
    )
  )
.

public define Maybe(One)
/* generate the function which extract the entire or part database table row type 
 * from web arguments and return a list of table fields presents in that web args.
 */
  generate_extract_web_arg_to_update_field
  (
    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\npublic define List(SQLite3_update_field)\n"+
     "  extract_"+type_name+"_update_fields_from_web_arg\n"+
     "  (\n"+
     "    List(Web_arg) lwa,\n"+
     "    String __prefix__\n"+
     "  )=\n"+   //head of the definition
     _generate_extract_web_arg_to_update_field(columns)+                      //all editable components of the type
     "\n.\n") is                                         //end of the function
  {
    failure     then  println("can't write extract_"+type_name+"_update_fields_from_web_arg "+type_name); failure,
    success(_)  then  success(unique)
  }
.