fields.anubis 11.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * 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 xlib/CXM_message_constants.anubis
transmit xlib/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(HK_Foreign_Key_Active_Filter) fk_active_filters
  )=
  foreign_key(hk_app_name, table_name, [], fk_active_filters, []).

public define HK_Model_Field
  foreign_key
  (
    HK_App_Name   hk_app_name,
    String        table_name,
    List(String)  searchable_fields,
    List(HK_Foreign_Key_Active_Filter) fk_active_filters
  )=
  foreign_key(hk_app_name, table_name, searchable_fields, fk_active_filters, []).
  
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).