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

transmit tools/basis.anubis
transmit system/convert.anubis
transmit system/muscle.anubis
transmit calexium_lib/CXM_message_constants.anubis
transmit calexium_lib/web/CXM_json.anubis
transmit hayamiki_lib/model/types/hk_model_field.anubis
transmit hayamiki_lib/model/types/hk_app_name.anubis
transmit hayamiki_lib/model/types/hk_text_field_attr.anubis
transmit hayamiki_lib/model/types/hk_datetime_field_attr.anubis
transmit hayamiki_lib/model/types/hk_phone_field_attr.anubis
transmit hayamiki_lib/model/types/hk_int_choices.anubis
transmit hayamiki_lib/model/types/hk_text_choices.anubis

  ***  'HK_Model_Field' (data types of table columns).
  
   From the point of view of your Anubis program, these data will be of types: 

   Name            | Anubis type       | Type within the database
   ----------------+-------------------+--------------------------------------------------
   p_key           | DB_id             | primary key (integer) automatically add in 1st
                                         position in every model table
   boolean_field   | Bool              | boolean
   date_field      | DB_date           | text (date in ISO-8601 format: yyyy-mm-dd)
   time_field      | DB_time           | text (date in ISO-8601 format: hh:mm:ss)
   datetime_field  | DB_datetime       | text (date in ISO-8601 format: yyyy-mm-dd hh:mm:ss)
   foreign_key     | Int               | integer
   integer_field   | Int               | arbitrary size integer (numeric)
   float_field     | Float             | 
   char_field      | String            | max size text
   text_field      | String            | arbitrary size text
   color_field     | String            | String which representing RGBA color like HTML



public define JsonValue
  to_JSON
  (
    HK_App_Name hk_app_name
  )=
  with members = 
    if hk_app_name is
    {
      this            then  [ json_member("type", json_string("this")) ],
                              
      app_name(name)  then  [ json_member("type", json_string("app_name")),
                              json_member("name", json_string(name))
                            ],
                            
      none            then  [ json_member("type", json_string("none")) ],                            
    },
  
  json_object("_HK_APP_NAME", members).
  
define String
  to_String
  (
    HK_Text_Field_Attr attr
  )=
  if attr is
  {
    none          then  "none",
    rich_editor   then  "rich_editor"
  }.

public define String
  to_Anubis_source
  (
    HK_Text_Field_Attr attr
  )=
  to_String(attr)
.

public define String
  to_Anubis_source
  (
    List(HK_Foreign_Key_Active_Filter) active_filters
  )=
  "["+
    join(", ",
      map((HK_Foreign_Key_Active_Filter active_filter) |->
        since active_filter is active_filter(column, fk_column),
        "active_filter(\""+column+"\", \""+fk_column+"\")"
        ,
        active_filters
      )
    )+
  "]"
.

define HK_Text_Field_Attr
  to_HK_Text_Field_Attr
  (
    String  txt_attr_str
  )=
  if txt_attr_str = "none"         then none         else
  if txt_attr_str = "rich_editor"  then rich_editor
  else none
.
  
define HK_Text_Field_Attr
  get_text_attribute
  (
    Message msg
  )=
  if find_string(msg, "text_attribute") is 
  { 
    failure             then  none, 
    success(text_attrib)  then  to_HK_Text_Field_Attr(text_attrib)
  }.
  


define String
  to_String
  (
    HK_Datetime_Field_Attr attr
  )=
  if attr is
  {
    none          then  "none",
    auto_now      then  "auto_now",
    auto_now_add  then  "auto_now_add"
  }.

define HK_Datetime_Field_Attr
  to_HK_Datetime_Field_Attr
  (
    String  dt_attr_str
  )=
  if dt_attr_str = "none"         then none         else
  if dt_attr_str = "auto_now"     then auto_now     else
  if dt_attr_str = "auto_now_add" then auto_now_add
  else none.
  
define HK_Datetime_Field_Attr
  get_dt_attribute
  (
    Message msg
  )=
  if find_string(msg, "dt_attribute") is 
  { 
    failure             then  none, 
    success(dt_attrib)  then  to_HK_Datetime_Field_Attr(dt_attrib)
  }.

define String
  to_String
  (
    HK_Phone_Field_Attr attr
  )=
  if attr is
  {
    generic   then  "generic",
    landline  then  "landline",
    cellphone then  "cellphone",
    fax       then  "fax"
  }.
  
public define String
  to_String
  (
    HK_Foreign_Key_Clause_On_Directive directive
  )=
  if directive is
  {
    set_null    then "SET NULL",
    set_default then "SET DEFAULT",
    cascade     then "CASCADE",
    restrict    then "RESTRICT",
    no_action   then "NO ACTION"
  }
.

public define String
  to_Anubis_source
  (
    HK_Phone_Field_Attr attr
  )=
  to_String(attr)
.

//default initialization
public define HK_Model_Field
  char_field
  (
    Int size
  )=
  char_field(no_choice, size).

public define HK_Model_Field
  integer_field
  =
  integer_field(no_choice).
  
public define HK_Model_Field
  foreign_key
  (
    String  table_name
  )=
  foreign_key(this, table_name, [], [], []).


public define HK_Model_Field
  foreign_key
  (
    HK_App_Name   hk_app_name,
    String        table_name
  )=
  foreign_key(hk_app_name, table_name, [], [], []).
  
public define HK_Model_Field
  foreign_key
  (
    HK_App_Name   hk_app_name,
    String        table_name,
    List(String)  searchable_fields
  )=
  foreign_key(hk_app_name, table_name, searchable_fields, [], []).

public define HK_Model_Field
  foreign_key
  (
    HK_App_Name   hk_app_name,
    String        table_name,
    List(String)  searchable_fields,
    List(HK_Foreign_Key_Clause) fk_clause
  )=
  foreign_key(hk_app_name, table_name, searchable_fields, [], fk_clause).
  
public define HK_Model_Field
  foreign_key
  (
    HK_App_Name                 hk_app_name,
    String                      table_name,
    List(HK_Foreign_Key_Clause) fk_clause
  )=
  foreign_key(hk_app_name, table_name, [], [], fk_clause).
  
public define HK_Model_Field
  foreign_key
  (
    String        table_name,
    List(String)  searchable_fields
  )=
  foreign_key(this, table_name, searchable_fields, [], [])
.

public define HK_Model_Field
  foreign_key
  (
    String                      table_name,
    List(String)                searchable_fields,
    List(HK_Foreign_Key_Clause) fk_clause
  )=
  foreign_key(this, table_name, searchable_fields, [], fk_clause)
.

public define HK_Model_Field
  foreign_key
  (
    String                      table_name,
    List(HK_Foreign_Key_Clause) fk_clause
  )=
  foreign_key(this, table_name, [], [], fk_clause)
.
  
public define HK_Model_Field
  text_field
  =
  text_field(none)
.

 /* *
  _HK_FIELD message format:
  =============================
  +----------------+-----------+--------------------------------
  |  name          |  type     |      description
  +----------------+-----------+--------------------------------
  | "type"  | String    | type of the field. It sould be 
                                 "p_key"
                                 "boolean_field"
                                 "date_field" __________
                                 "time_field" ________  |
                                 "datetime_field" __  | |
    ________________________________________________|_|_|
   |                             "foreign_key" _________
   |                             "integer_field"
   |                             "char_field"
   |                             "text_field"
   |                              
   --> date_field or time_field or datetime_field:
      +-------------+--------+------------------------------------+
      |  name       |  type  |      description                   |
      |-------------+--------+------------------------------------|
      | "attribute" | String | 'none', 'auto_now', 'auto_now_add' |
      +-----------------------------------------------------------+

*/



public define JsonValue
  to_JSON
  (    
    HK_Model_Field hk_model_field
  )=
  with members = 
    if hk_model_field is
    {
      p_key                             then  
        [ json_member("type", json_string("p_key")) ],

      boolean_field                     then  
        [ json_member("type", json_string("boolean_field")) ],
    
      date_field(attr)                  then  
        [ json_member("type",         json_string("date_field")),
          json_member("dt_attribute", json_string(to_String(attr)))
        ],
                              
      time_field(attr)                  then  
        [ json_member("type",         json_string("time_field")),
          json_member("dt_attribute", json_string(to_String(attr)))
        ],
          
      datetime_field(attr)              then
        [ json_member("type",         json_string("datetime_field")),
          json_member("dt_attribute", json_string(to_String(attr)))
        ],
          
      foreign_key(app_name, table_name, _, _, _) then    // foreign key to 'id' in another (or same) table and column_name for choice selector.
        [ json_member("type",         json_string("foreign_key")),
          json_member("app_name",     to_JSON(app_name)),
          json_member("table_name",   json_string(table_name)),
        ],      
          
      integer_field(choices)            then   // integer of arbitrary size
      //TODO add choices if need
        [ json_member("type", json_string("integer_field")) ],
        
      float_field                       then  //float
        [ json_member("type", json_string("float_field"))],
        
      char_field    (choices, size)     then  // text of maximal size 'size' (number of characters)
      //TODO add choices if need
        [ json_member("type", json_string("char_field")),
          json_member("size", json_int(size))
        ],
          
      password_field(size)              then
        [ json_member("type", json_string("password_field")),
          json_member("size", json_int(size))
        ],
          
      text_field(attr)                  then  // text of variable size
        [ json_member("type", json_string("text_field")),
          json_member("text_attribute", json_string(to_String(attr)))
        ],
        
      color_field                       then
        [ json_member("type", json_string("color_field"))
        ],
      
      phone_field(attr)                 then
        [ json_member("type", json_string("phone_field")),
          json_member("phone_attribute", json_string(to_String(attr)))
        ],
        
      url_field                      then
        [ json_member("type", json_string("url_field"))
        ],
        
      email_field                       then
        [ json_member("type", json_string("email_field"))
        ]
    },
  
  json_object("_HK_FIELD", members).