json.anubis 7.06 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 25/03/2017
 * Time: 00:54
 * © David RENÉ
 */

read hayamiki_lib/types/hayamiki.anubis
read tools/basis.anubis
read columns.anubis

public define String
/** to_String return the content of the given model column 'col' in String format.
 * 
 */
  to_JSON_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 current_type_name+"."+name,
    boolean_field                       then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
    date_field(attrs)                   then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
    time_field(attrs)                   then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
    datetime_field(attrs)               then "to_JSON_String(to_String("+current_type_name+"."+name+"))",
    foreign_key(app,foreign_table,_,_,_)  then "to_JSON_String(get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+"))",
    integer_field(_)                    then current_type_name+"."+name,
    float_field                         then "float_to_string("+current_type_name+"."+name+",5)",
    char_field(_,_)                     then "to_JSON_String("+current_type_name+"."+name+")",
    password_field(_)                   then "to_JSON_String("+current_type_name+"."+name+")",
    text_field(_)                       then "to_JSON_String("+current_type_name+"."+name+")",
    color_field                         then "to_JSON_String("+current_type_name+"."+name+")",
    phone_field(_)                      then "to_JSON_String("+current_type_name+"."+name+")",
//    mobile_field                        then "to_JSON_String("+current_type_name+"."+name+")",
    url_field                           then "to_JSON_String("+current_type_name+"."+name+")",
    email_field                         then "to_JSON_String("+current_type_name+"."+name+")",    
  }
.


  
public define String
/** to_String return the content of the given model column 'col' in String format.
 * 
 */
  to_JSON
  (
    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 "json_member(\""+name+"\", json_int("+current_type_name+"."+name+"))",
    boolean_field                       then "json_member(\""+name+"\", json_bool("+current_type_name+"."+name+"))",
    date_field(attrs)                   then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    time_field(attrs)                   then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    datetime_field(attrs)               then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    foreign_key(app,foreign_table,_,_,_)  then "json_member(\""+name+"\", if resolve_fk then json_string(get_"+foreign_table+"_show_string(db, "+current_type_name+"."+name+"))) else json_int("+current_type_name+"."+name+")",
    integer_field(_)                    then "json_member(\""+name+"\", json_int("+current_type_name+"."+name+"))",
    float_field                         then "json_member(\""+name+"\", json_float("+current_type_name+"."+name+",5))",
    char_field(_,_)                     then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    password_field(_)                   then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    text_field(_)                       then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    color_field                         then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    phone_field(_)                      then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
//    mobile_field                        then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    url_field                           then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
    email_field                         then "json_member(\""+name+"\", json_string("+current_type_name+"."+name+"))",
  }
.

public define String
  from_DB_cursor_to_JSON
  (
    HK_Model_Column col,    //Model of the column
    String          cursor, //db cursor name
    Int             index   //current index of the column in the cursor
  ) =
  with cursor_index = "("+cursor+")("+index+")",
  since col is hk_column(name, col_type, _, _),
  if col_type is 
  {
    p_key                               then "json_member(\""+name+"\", json_int((Int)db_integer"+cursor_index+"))",
    boolean_field                       then "json_member(\""+name+"\", json_bool(db_bool"+cursor_index+"))",
    date_field(_)                       then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    time_field(_)                       then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    datetime_field(_)                   then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",      
    foreign_key(app,foreign_table,_,_,_)  then 
      "    if resolve_fk then\n"+
      "      json_member(\""+name+"\", json_string(get_"+foreign_table+"_show_string(db, db_id((Int)db_integer"+cursor_index+"))))\n"+
      "    else\n"+
      "      json_member(\""+name+"\", json_int((Int)db_integer"+cursor_index+"))",
    integer_field(_)                    then "json_member(\""+name+"\", json_int((Int)db_integer"+cursor_index+"))",
    float_field                         then "json_member(\""+name+"\", json_float(db_float"+cursor_index+",5))",
    char_field(_,_)                     then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    password_field(_)                   then "json_member(\""+name+"\", json_string(\"******\"))",
    text_field(_)                       then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    color_field                         then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    phone_field(_)                      then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
//    mobile_field                        then "json_member(\""+name+"\", json_string(text"+cursor_index+"))",
    url_field                           then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))",
    email_field                         then "json_member(\""+name+"\", json_string(not_null_String(text"+cursor_index+")))"   
  }
.