fields.anubis 6.81 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 13/02/2016
 * Time: 10:50
 * © Calexium 
 */

read system/string.anubis
read tools/basis.anubis

read hayamiki_lib/types/model/fields.anubis
read hayamiki_lib/types/model/columns.anubis

read model.anubis

public define String
  to_Anubis_type
  (
    HK_Model_Field  t
  ) =
  if t is 
  {
    //anubis(_T)                          then "ByteArray", 
    p_key                               then "DB_id",
    //binary                              then "ByteArray",
    boolean_field                       then "Bool",
    date_field(_)                       then "DB_date",
    time_field(_)                       then "DB_time",
    datetime_field(_)                   then "DB_datetime",      
    foreign_key(_,_)                    then "Int",
    integer_field                       then "Int",
    char_field(_,_)                     then "String",
    text_field                          then "String"
  }.
  
public define String
  to_SQL_CREATE
  (
    HK_Model_Field      t,
    List(HK_Model_Attr) attributes,
  ) =
  if t is 
  {
    //anubis(_T)                          then "ByteArray", 
    p_key                               then fill("INTEGER", 15) +" PRIMARY KEY",
    boolean_field                       then fill("BOOL", 15)    +format_attributes(attributes),
    date_field(_)                       then fill("DATE", 15)    +format_attributes(attributes),
    time_field(_)                       then fill("TIME", 15)    +format_attributes(attributes),
    datetime_field(_)                   then fill("DATETIME", 15)+format_attributes(attributes),
    foreign_key(app,fk)                 then fill("INTEGER", 15) +format_attributes(attributes)+" REFERENCES "+fk+"(id) ",
    integer_field                       then fill("INTEGER", 15) +format_attributes(attributes),
    char_field(_,size)                  then fill("VARCHAR("+size+")", 15) +format_attributes(attributes),
    text_field                          then fill("TEXT", 15)    +format_attributes(attributes),
  }.

public define String
  to_Anubis_default
  (
    HK_Model_Field  t
  ) =
  if t is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then "none",
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then "false",
    date_field(_)                       then "db_date(\"\")",
    time_field(_)                       then "db_time(\"\")",
    datetime_field(_)                   then "db_datetime(\"\")",      
    foreign_key(_,_)                      then "(Int)0",
    integer_field                       then "(Int)0",
    char_field(choices,size)            then "\"\"",
    text_field                          then "\"\""
  }.

public define String
  from_web_arg
  (
    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 "get_Int(lwa, __prefix__+\""+name+"\")",
    integer_field                     then "get_Int(lwa, __prefix__+\""+name+"\")",
    char_field(_,_)                   then "get_String(lwa, __prefix__+\""+name+"\")",
    text_field                        then "get_String(lwa, __prefix__+\""+name+"\")"
  }.

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 "(Int)db_integer"+cursor_index,
    integer_field                       then "(Int)db_integer"+cursor_index,
    char_field(_,_)                     then "text"+cursor_index,
    text_field                          then "text"+cursor_index
  }.

public define String
  to_Bind
  (
    HK_Model_Field  t,
    String         type_name, //name of the type    
    String         name,      //name of component into the type
    Bool           insert     //true if insert time else false for update
  ) =
  if t is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then "",
    //binary                              then "constant_byte_array(0,0)",
    boolean_field                       then "bind_Bool(\":v_"+name+"\", "+type_name+"."+name+")",
    date_field(attrs)                   then "bind_Date(\":v_"+name+"\", "+type_name+"."+name+")",
    time_field(attrs)                   then "bind_Time(\":v_"+name+"\", "+type_name+"."+name+")",
    datetime_field(attrs)               then  if attrs is 
                                              {
                                                none         then "bind_Datetime(\":v_"+name+"\", "+type_name+"."+name+")",
                                                auto_now     then "bind_Datetime(\":v_"+name+"\", now)",
                                                auto_now_add then
                                                  if insert then 
                                                    "bind_Datetime(\":v_"+name+"\", now)"
                                                  else 
                                                    ""
                                              },
    foreign_key(_,_)                          then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")",
    integer_field                     then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")",
    char_field(_,_)                   then "bind_String(\":v_"+name+"\", "+type_name+"."+name+")",
    text_field                        then "bind_String(\":v_"+name+"\", "+type_name+"."+name+")"
  }.