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

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

read fields.anubis

public define Maybe(HK_Model_Column)
/* return the HK_Model_Column corresponding of the given column name
 */
  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 Maybe(List(HK_Model_Column))
/* return all columns matching with the list of given list of columns to get
 */
  get_HK_Model_Column_list_from_name_list
  (
    List(HK_Model_Column) columns,      //columns in wich we search 
    List(String)          names_to_find  //Column name to find in previous list
  )=
  map_escape((String column_name) |-> get_column_type_from_name(columns, column_name), names_to_find)
.
  
define List(String)
/* return the list of all columns name
 * 
 */
  _to_List_String
  (
    String                table_name,
    List(HK_Model_Column) columns,
    Bool                  with_pk,  //if set, add the primary key named "id"
    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(table_name, t, with_pk, so_far)
        else 
          _to_List_String(table_name, t, with_pk, [ table_name+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, []).

public define List(String)
/* return the list of all columns name
 */
  to_List_String
  (
    String                table_name,
    List(HK_Model_Column) columns,
    Bool                  with_pk
  )=
  _to_List_String(table_name+".", columns, with_pk, []).

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, attributes, _),
      with result = to_Bind(col_type, attributes, 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            p_key_id,   //true if the "id" column must be returned
    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 if name = "id" & p_key_id = false then failure else success(name),
    //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),
    float_field                         then success(name),
    char_field(_,_)                     then success(name),
    password_field(_)                   then success(name),
    text_field(_)                       then success(name),
    color_field                         then success(name),
    phone_field(attr)                   then success(name),
    url_field                           then success(name),
    email_field                         then success(name)
  }.

public define List(String)
  columns_name
  (
    List(HK_Model_Column) columns,
    Bool                  p_key_id,   //true if we need to have the first primary_key column named "id"
    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, p_key_id, insert) is
      { 
        failure         then columns_name(t, p_key_id, insert, so_far)
        success(result) then 
          if insert then
            columns_name(t, p_key_id, insert, [result . so_far])
          else
            columns_name(t, p_key_id, 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,
    Bool                  p_key_id,
    String                indent
  )=
  with columns_list = columns_name(columns, p_key_id, 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, false, []),
  indent+"  "+join(", ", columns_list).


public define Maybe(String)
/** Here we generate the VT_Edit entry according to the model
 */
  get_HK_Form_Entry
  (
    HK_Model_Column col,
    String          type_name //name of the instanciated type 
  ) =
  since col is hk_column(col_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+"."+col_name+"))"),
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then success("boolean(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
    date_field(attrs)                   then if attrs is 
                                              {
                                                none         then success("date(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, 10, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".date, 10, "+to_Anubis_source(help)+")")
                                              },
    time_field(attrs)                   then if attrs is 
                                              {
                                                none         then success("time(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, 10, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".time, 10, "+to_Anubis_source(help)+")")
                                              },
    datetime_field(attrs)               then  if attrs is 
                                              {
                                                none         then success("datetime(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, "+to_Anubis_source(help)+")"),
                                                auto_now     then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, 18, "+to_Anubis_source(help)+")"),
                                                auto_now_add then success("information(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+".datetime, 18, "+to_Anubis_source(help)+")")
                                              },
    foreign_key(app,foreign_table,fields,active_filters,_)      then
                                                  if length(fields) = 0 then
                                                    success("foreign_text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", hk_form_selector(get_selector_"+foreign_table+"), "+to_Anubis_source(active_filters)+", "+to_Anubis_source(help)+")")
                                                  else
                                                    with search_fields = join(", ", map((String field) |-> "\""+field+"\"",fields)),
                                                    success("foreign_text_search(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", \""+foreign_table+"\", ["+search_fields+"], "+to_Anubis_source(active_filters)+", "+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(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+col_name+"), "+to_Anubis_source(help)+")")
                                              else
                                                success("integer(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
                                          }
    float_field                         then  success("float(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", hk_form_selector(get_choices_selector_"+to_lower(type_name)+"_"+col_name+"), "+to_Anubis_source(help)+")")
                                              else
                                                success("text(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")")
                                          }
    password_field(min_size)            then success("password(\""+to_upper(col_name)+"\", \""+col_name+"\", "+to_Anubis_source(help)+")")
    text_field(txt_attr)                then success("text_area(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(txt_attr)+", "+to_Anubis_source(help)+")"),
    color_field                         then success("color(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
    phone_field(ph_attr)                then success("phone(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(ph_attr)+", "+to_Anubis_source(help)+")"),
//    mobile_field                        then success("mobile_phone(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
    url_field                           then success("url(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_name+", "+to_Anubis_source(help)+")"),
    email_field                         then success("email(\""+to_upper(col_name)+"\", \""+col_name+"\", "+type_name+"."+col_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_HK_Form_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
/** 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 
  { 
    p_key                               then if name ="id" then "get_"+general_type_name+"_show_string(db, "+current_type_name+".id)" else "to_String("+current_type_name+"."+name+")",
    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+")",
    float_field                         then "float_to_string("+current_type_name+"."+name+",5)",
    char_field(_,_)                     then current_type_name+"."+name,
    password_field(_)                   then current_type_name+"."+name,
    text_field(_)                       then current_type_name+"."+name,
    color_field                         then current_type_name+"."+name,
    phone_field(_)                      then current_type_name+"."+name,
    url_field                           then current_type_name+"."+name,
    email_field                         then current_type_name+"."+name
  }.