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

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

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

read model.anubis

public define String
/** Return the corresponding Anubis type of the field
 */
  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 "DB_id",
    integer_field(_)                    then "Int",
    float_field                         then "Float",
    char_field(_,_)                     then "String",
    password_field(_)                   then "String",
    text_field(_)                       then "String",
    color_field                         then "String",
    phone_field(_)                      then "String",
    url_field                           then "String",
    email_field                         then "String"
  }.

public define String
/** Return the corresponding Anubis type of the field
 */
  to_densaku_type
  (
    HK_Model_Field  t
  ) =
  if t is 
  { 
    p_key                               then "extern_type(\"DB_id\", \"xlib/database/db_types.anubis\")",
    boolean_field                       then "bool",
    date_field(_)                       then "extern_type(\"DB_date\", \"xlib/database/db_types.anubis\")",
    time_field(_)                       then "extern_type(\"DB_time\", \"xlib/database/db_types.anubis\")",
    datetime_field(_)                   then "extern_type(\"DB_datetime\", \"xlib/database/db_types.anubis\")",      
    foreign_key(_,_,_,_,_)              then "extern_type(\"DB_id\", \"xlib/database/db_types.anubis\")",
    integer_field(_)                    then "int",
    float_field                         then "float",
    char_field(_,_)                     then "string",
    password_field(_)                   then "string",
    text_field(_)                       then "string",
    color_field                         then "string",
    phone_field(_)                      then "string",
    url_field                           then "string",    
    email_field                         then "string"
  }.
  
public define String
  to_sqlite_SQL_CREATE
  (
    HK_Model_Field      t,
    List(HK_Model_Attr) attributes,
    String              name      //name of component into the type    
  ) =
  if t is 
  {
    //anubis(_T)                          then "ByteArray", 
    p_key                               then fill("INTEGER", 15)  + (if name = "id" then " PRIMARY KEY AUTOINCREMENT" else ""),
    boolean_field                       then fill("BOOL", 15)     + format_attributes(attributes),
    date_field(dt_attrs)                then 
      with new_attributes = if dt_attrs is auto_now_add then [default("(date('now', 'LOCALTIME'))")] else attributes,   
      fill("DATE", 15)     + format_attributes(attributes),
    time_field(dt_attrs)                then 
      with new_attributes = if dt_attrs is auto_now_add then [default("(time('now', 'LOCALTIME'))")] else attributes,    
      fill("TIME", 15)     + format_attributes(new_attributes),
    datetime_field(dt_attrs)            then
      with new_attributes = if dt_attrs is auto_now_add then [default("(datetime('now', 'LOCALTIME'))")] else attributes,
      fill("DATETIME", 15) + format_attributes(new_attributes),
    foreign_key(app,fk,_,_,fk_clause)   then fill("INTEGER", 15)  + format_attributes(attributes)+" REFERENCES "+fk+"(id) "+format_fk_clause(fk_clause),
    integer_field(_)                    then fill("INTEGER", 15)  + format_attributes(attributes),
    float_field                         then fill("DOUBLE", 15)   + format_attributes(attributes), 
    char_field(_,size)                  then fill("VARCHAR("+size+")", 15) + format_attributes(attributes),
    password_field(size)                then fill("VARCHAR("+size+")", 15) + format_attributes(attributes),
    text_field(_)                       then fill("TEXT", 15)     + format_attributes(attributes),
    color_field                         then fill("VARCHAR(8)",15)+ format_attributes(attributes),
    phone_field(_)                      then fill("VARCHAR(20)", 15)     + format_attributes(attributes),
//    mobile_field                        then fill("VARCHAR(20)", 15)     + format_attributes(attributes),
    url_field                           then fill("VARCHAR(2048)", 15)   + format_attributes(attributes),
    email_field                         then fill("VARCHAR(254)", 15)    + format_attributes(attributes),
}.

public define String
  to_postgresql_SQL_CREATE
  (
    HK_Model_Field      t,
    List(HK_Model_Attr) attributes,
    String              name      //name of component into the type    
  ) =
  if t is 
  {
    //anubis(_T)                          then "ByteArray", 
    p_key                               then fill("BIGINT", 15)  + (if name = "id" then " GENERATED BY DEFAULT AS IDENTITY" else ""),
    boolean_field                       then fill("BOOLEAN", 15)     + format_attributes(attributes),
    date_field(dt_attrs)                then 
      with new_attributes = if dt_attrs is auto_now_add then [default("CURRENT_DATE")] else attributes,   
      fill("DATE", 15)     + format_attributes(attributes),
    time_field(dt_attrs)                then 
      with new_attributes = if dt_attrs is auto_now_add then [default("LOCALTIME(0)")] else attributes,    
      fill("TIME", 15)     + format_attributes(new_attributes),
    datetime_field(dt_attrs)            then
      with new_attributes = if dt_attrs is auto_now_add then [default("LOCALTIMESTAMP(0)")] else attributes,
      fill("TIMESTAMP", 15) + format_attributes(new_attributes),
    foreign_key(app,fk,_,_,fk_clause)   then fill("BIGINT", 15)  + format_attributes(attributes)+" REFERENCES "+fk+"(id) "+format_fk_clause(fk_clause),
    integer_field(_)                    then fill("INTEGER", 15)  + format_attributes(attributes),
    float_field                         then fill("REAL", 15)   + format_attributes(attributes), 
    char_field(_,size)                  then fill("VARCHAR("+size+")", 15) + format_attributes(attributes),
    password_field(size)                then fill("VARCHAR("+size+")", 15) + format_attributes(attributes),
    text_field(_)                       then fill("TEXT", 15)     + format_attributes(attributes),
    color_field                         then fill("VARCHAR(8)",15)+ format_attributes(attributes),
    phone_field(_)                      then fill("VARCHAR(20)", 15)     + format_attributes(attributes),
//    mobile_field                        then fill("VARCHAR(20)", 15)     + format_attributes(attributes),
    url_field                           then fill("VARCHAR(2048)", 15)   + format_attributes(attributes),
    email_field                         then fill("VARCHAR(254)", 15)    + format_attributes(attributes),
}.

define String
  to_default_string
  (
    Maybe(String) value,
    String        internal_default
  )=
  if value is
  {
    failure       then  internal_default
    success(str)  then  
      //try to remove the quote which enclosure sql string if any
      with new_str = if first_char(str) is
        { 
          failure     then str,
          success(f)  then
            if last_char(str) is
            { failure     then str
              success(l)  then
                if f = "'" & l = "'" then
                  if sub_string(str, 1, length(str) - 2) is
                  {
                    failure then str,
                    success(unquoted_string) then
                      unquoted_string
                  }
                else
                  str
            }
         },
    "\""+new_str+"\""
  }
.
  
define String
  to_default_boolean
  (
    Maybe(String) value,
    String        internal_default
  )=
  if value is
  {
    failure       then  internal_default
    success(str)  then 
      //transform the string to real bool value
      //and finally convert that bool value to String ("true" or "false")
      to_String(to_Bool(str))
  }
.

define Maybe(String)
  get_default_value
  (
    List(HK_Model_Attr) attributes
  )=
  if attributes is
  {
    []      then  failure,  //list finished, attribute default not found
    [h . t] then
      if h is default(def_string) then 
        success(def_string)
      else
        get_default_value(t)
  }
.

public define String
  to_Anubis_default
  (
    HK_Model_Field  t,
    List(HK_Model_Attr) attributes
  ) =
  with default = get_default_value(attributes),
  
  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 to_default_boolean(default, "false"),
    date_field(_)                       then "db_date(\"\")",
    time_field(_)                       then "db_time(\"\")",
    datetime_field(_)                   then "db_datetime(\"\")",      
    foreign_key(_,_,_,_,_)              then "(DB_id)none",
    integer_field(_)                    then "(Int)0",
    float_field                         then "(Float)0.0",
    char_field(choices,size)            then to_default_string(default, "\"\""),
    password_field(min_size)            then "\"\"",
    text_field(_)                       then to_default_string(default, "\"\""),
    color_field                         then "\"#DDD\"",
    phone_field(_)                      then "\"\"",
//    mobile_field                        then "\"\"",
    url_field                           then "\"\"",
    email_field                         then "\"\""
  }.





public define String
  to_Bind
  (
    HK_Model_Field      t,
    List(HK_Model_Attr) attributes,    
    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
  ) =
  with or_null = if not_null:attributes then "" else "_or_NULL", //check if not_null is member of the list of column's attributes
  if t is 
  {
    //anubis(_T)                          then "constant_byte_array(0,0)", 
    p_key                               then if name = "id" then "" else "bind_DB_id_or_NULL(\":v_"+name+"\", "+type_name+"."+name+")",
    //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+"\", db_now)",
                                                auto_now_add then
                                                  if insert then 
                                                    "bind_Datetime(\":v_"+name+"\", db_now)"
                                                  else 
                                                    ""
                                              },
    foreign_key(_,_,_,_,_)            then "bind_DB_id_or_NULL(\":v_"+name+"\", "+type_name+"."+name+")",
    integer_field(_)                  then "bind_Int(\":v_"+name+"\", "+type_name+"."+name+")",
    float_field                       then "bind_Float(\":v_"+name+"\", "+type_name+"."+name+")",
    char_field(_,_)                   then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
    password_field(_)                 then "bind_String(\":v_"+name+"\", "+type_name+"."+name+")",
    text_field(_)                     then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
    color_field                       then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
    phone_field(_)                    then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
//    mobile_field                      then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
    url_field                         then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
    email_field                       then "bind_String"+or_null+"(\":v_"+name+"\", "+type_name+"."+name+")",
  }.