columns.anubis 14 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 13/02/2016
 * Time: 11:05
 * © Calexium 
 */

read system/string.anubis
read tools/list.anubis
read hayamiki_lib/types/model/columns.anubis

read fields.anubis

define List(String)
/* return the list of all columns name
 */
  _to_List_String
  (
    List(HK_Model_Column) columns,
    Bool                  with_pk,
    List(String)          so_far
  )=
  if columns is
  {
    []        then reverse(so_far),
    [h . t ]  then
      since h is hk_column(name, _, _, _),
        if name = "id" & with_pk = false then
          _to_List_String(t, with_pk, so_far)
        else 
          _to_List_String(t, with_pk, [ name . so_far])
  }.
  
public define List(String)
/* return the list of all columns name
 */
  to_List_String
  (
    List(HK_Model_Column) columns,
    Bool                  with_pk
  )=
  _to_List_String(columns, with_pk, []).


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 String
  generate_constructor
  (
    String                constructor_name,
    List(HK_Model_Column) columns,
    String                indent
  )=
  indent+constructor_name+"(\n"+indent+"  "+join(",\n"+indent+"  ", to_List_String(columns, true))+")".

public define List(String)
/** 
 */
  to_Bind_list
  (
    List(HK_Model_Column) columns,
    String                type_name, //name of the type    
    List(String)          so_far,
    Bool                  insert
  )=
  if columns is
  {
    []        then reverse(so_far),
    [h . t ]  then
      since h is hk_column(name, col_type, _, _),
      with result = to_Bind(col_type, type_name, name, insert),
      //we eliminate the empty string because during the join it will have an empty line
      if result = "" then
        to_Bind_list(t, type_name, so_far, insert)
      else
        to_Bind_list(t, type_name, [result . so_far], insert)
  }.
  
public define String
  generate_Bind_list
  (
    List(HK_Model_Column) columns,
    String                type_name,
    String                indent,
    Bool                  insert
  )=
  indent+"[\n"+indent+"  "+join(",\n"+indent+"  ", to_Bind_list(columns, type_name, [], insert))+"]".


public define Maybe(String)
/** Return the column name according to the model.
 * insert boolean indicate that we only want the column name during insert processing.
 * In fact some column no need to be updated hence the column name will be ignored if 
 * the insert boolean = false.
 */
  get_column_name
  (
    HK_Model_Column col,
    Bool            insert     //true if insert time else false for update
  ) =
  since col is hk_column(name, col_type, _, _),
  if col_type is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then failure,
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then success(name),
    date_field(attrs)                   then success(name),
    time_field(attrs)                   then success(name),
    datetime_field(attrs)               then  if attrs is 
                                              {
                                                none         then success(name),
                                                auto_now     then success(name),
                                                auto_now_add then
                                                  if insert then success(name) else failure
                                              },
    foreign_key(_,_)                    then success(name),
    integer_field(_)                    then success(name),
    char_field(_,_)                     then success(name),
    password_field(_)                   then success(name),
    text_field                          then success(name)
  }.

public define List(String)
  columns_name
  (
    List(HK_Model_Column) columns,
    Bool                  insert,     //true if insert time else false for update
    List(String)          so_far
  ) =
  if columns is
  {
    []      then reverse(so_far),
    [h . t] then
      if get_column_name(h, insert) is
      { 
        failure         then columns_name(t, insert, so_far)
        success(result) then 
          if insert then
            columns_name(t, insert, [result . so_far])
          else
            columns_name(t, insert, [enclose("\\\"",result)+" = :v_"+result . so_far])
      }
  }.
  
public define String
/* 
 Generate the SQL "(xxx, yyy, ...) VALUES ( :v_xxx, :v_yyy, ...)" for the insert
 SQL query.
 */
  insert_values
  (
    List(HK_Model_Column) columns,
    String                indent
  )=
  with columns_list = columns_name(columns, true, []),
  indent+"  ("+join(", ", enclose("\\\"",columns_list))+")\n"+indent+"VALUES\n"+indent+"  ("+prefixed_join(":v_", columns_list, ", ")+")".


public define String
/* 
 Generate the SQL "xxx = :v_xxx, yyy = :v_yyy, ..." for the update
 SQL query.
 */
  update_values
  (
    List(HK_Model_Column) columns,
    String                indent
  )=
  with columns_list = columns_name(columns, false, []),
  indent+"  "+join(", ", columns_list).


public define Maybe(String)
/** Here we generate the VT_Edit entry according to the model
 */
  get_VT_edit_entry
  (
    HK_Model_Column col,
    String          type_name
  ) =
  since col is hk_column(name, col_type, _, help),
  if col_type is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then success("primary_key(to_String("+type_name+"."+name+"))"),
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then success("boolean(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
    date_field(attrs)                   then if attrs is 
                                              {
                                                none         then success("date(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, 10, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".date, 10, "+to_Anubis_source(help)+")")
                                              },
    time_field(attrs)                   then if attrs is 
                                              {
                                                none         then success("time(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, 10, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".time, 10, "+to_Anubis_source(help)+")")
                                              },
    datetime_field(attrs)               then  if attrs is 
                                              {
                                                none         then success("datetime(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, 18, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+".datetime, 18, "+to_Anubis_source(help)+")")
                                              },
    foreign_key(app,foreign_table)      then success("foreign_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(help)+")"),
    integer_field(choices)              then 
      //success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
                                          if choices is 
                                          {
                                            no_choice                     then success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
                                            int_choices(choices_list)     then
                                              //if choice list is NOT empty we construct the possible choices selector
                                              if length(choices_list) > 0 then
                                                success("choices_int(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")")
                                              else
                                                success("integer(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
                                          }

    //TODO make choices selection if need
    char_field(choices,_)               then 
                                          if choices is 
                                          {
                                            no_choice                   then success("text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")"),
                                            text_choices(choices_list)  then
                                              //if choice list is NOT empty we construct the possible choices selector
                                              if length(choices_list) > 0 then
                                                success("choices_text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", vt_edit_selector(get_choices_selector_"+to_lower(type_name)+"_"+name+"), "+to_Anubis_source(help)+")")
                                              else
                                                success("text(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")")
                                          }
    password_field(min_size)            then success("password(\""+to_upper(name)+"\", \""+name+"\", "+to_Anubis_source(help)+")")
    text_field                          then success("text_area(\""+to_upper(name)+"\", \""+name+"\", "+type_name+"."+name+", "+to_Anubis_source(help)+")")
  }.

public define String
  edit_entries
  (
    List(HK_Model_Column) columns,
    String                type_name,    
    String                indent,
    List(String)          so_far
  ) =
  if columns is
  {
    []      then   indent+"  "+join(",\n"+indent+"  ", reverse(so_far)),
    [h . t] then
      if get_VT_edit_entry(h, type_name) is
      { 
        failure         then edit_entries(t, type_name, indent, so_far)
        success(result) then edit_entries(t, type_name, indent, [result . so_far])
      }
  }.

public define String
  make_list_view_header
  (
    List(String) list_view,
    String       indent
  )=
  indent+join(",\n"+indent, map((String text) |-> "text(\""+to_upper(text)+"\")", list_view)).

public define Maybe(HK_Model_Column)
  get_column_type_from_name
  (
    List(HK_Model_Column) columns,      //columns in wich we search 
    String                name_to_find  //Column name to find in previous list
  )=
  if columns is
  {
    []      then failure,
    [h . t] then
      since h is hk_column(name, _, _, _),
      if name_to_find = name then
        success(h)
      else
        get_column_type_from_name(t, name_to_find)
  }.
  
public define String
/** to_String return the content of the given model column 'col' in String format.
 * 
 */
  to_String
  (
    HK_Model_Column col,                //Model of the column
    String          current_type_name,  //Instance name of the type i.e.  toto (this is instance of Toto_Type below)
    String          general_type_name   //Type declaration name i.e. Toto_Type
  ) =
  since col is hk_column(name, col_type, _, _),
  if col_type is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)",
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then "to_String("+current_type_name+"."+name+")",
    date_field(attrs)                   then "to_String("+current_type_name+"."+name+")",
    time_field(attrs)                   then "to_String("+current_type_name+"."+name+")",
    datetime_field(attrs)               then "to_String("+current_type_name+"."+name+")"
    foreign_key(app,foreign_table)      then "get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+")",
    integer_field(_)                    then "to_String("+current_type_name+"."+name+")",
    char_field(_,_)                     then current_type_name+"."+name,
    password_field(_)                   then current_type_name+"."+name,
    text_field                          then current_type_name+"."+name
  }.