db_types.anubis 5.62 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 14/11/2011
 * Time: 22:41
 * 
 */
transmit system/convert.anubis
transmit tools/basis.anubis
transmit tools/ISO-8601.anubis

transmit calexium_lib/database/types/db_id.anubis
transmit calexium_lib/database/types/db_date.anubis
transmit calexium_lib/database/types/db_time.anubis
transmit calexium_lib/database/types/db_datetime.anubis
transmit calexium_lib/database/types/db_integer.anubis
transmit calexium_lib/web/CXM_json.anubis

/* DB_id will is deprecated. Please use next version below all upper case */
//public type DB_id:
//  none,
//  db_id(Int value).

//public type DB_ID:
//  none,
//  pk(Int value).

public define Maybe(One)
  add_DB_id
  (
    Message msg,
    String  name,
    DB_id   db_id
  )=
  add_message(msg,  name,  to_Message(db_id))
.

public define Maybe(DB_id)
  find_DB_id
  (
    Message msg,
    String  name,
  )=
  if find_message(msg, name) is 
  {
    failure               then failure,
    success(_db_id__msg)  then (Maybe(DB_id))from_Message(_db_id__msg)
  }
.

public define DB_id
  find_DB_id
  (
    Message msg,      //Message in where we search the DB_id
    String  name,     //name of DB_id in the Message
    DB_id   default   //default value
  )=
  if find_DB_id(msg, name) is 
  {
    failure         then default,
    success(db_id)  then db_id
  }
.

public define String
  to_String
  (
    DB_id idx
  )=
  if idx is
  {
    none        then "none",
    db_id(_idx) then abs_to_decimal(_idx)
  }.

public define Maybe(Int)
  to_mb_Int
  (
    DB_id idx
  )=
  if idx is
  {
    none        then failure,
    db_id(_idx) then success(_idx)
  }.
  
public define JsonValue
  to_JsonValue
  (
    DB_id idx
  )=
  if idx is
  {
    none        then json_string("none"),
    db_id(_idx) then json_int(_idx)
  }.
  
public define String
  String s + DB_id idx =
  s + to_String(idx)
.

public define String
  DB_id idx + String s =
  to_String(idx) + s
.
  
public define Int
  to_Int
  (
    DB_id idx
  )=
  if idx is
  {
    none        then (Int)0,
    db_id(_idx) then _idx
  }
.

public define Word32
  to_Word32
  (
    DB_id idx
  )=
  if idx is
  {
    none        then 0xFFFFFFFF,
    db_id(_idx) then truncate_to_Word32(_idx)
  }
.

public define DB_id
  to_DB_id
  (
    Int value
  )=
  if value < 0 then 
    none 
  else 
    db_id(value)
.

public define Maybe(DB_id)
  to_mb_DB_id
  (
    String  db_id_str
  )=
  if decimal_scan(db_id_str) is
  {
    failure             then failure,
    success(db_id_int)  then  success(to_DB_id(db_id_int))
  }
.

public define Maybe(DB_id)
  to_mb_DB_id
  (
    Maybe(String)  mb_db_id_str
  )=
  if mb_db_id_str is
  {
    failure             then failure,
    success(db_id_str)  then to_mb_DB_id(db_id_str)
  }
.



/**
 * produce path from db_id to optimize fs access and never have more than 100 folders
 */
public define String
  to_fs_folder
  (
    DB_id idx
  )=
  if idx is 
  {
    none        then "none/",
    db_id(_idx) then 
      with idx_str      = abs_to_decimal(_idx),
      //if the idx value is less than 10, we add 0 as prefix of that idx. i.e. if the idx is 3 the final_idx_str will be 03 which will be cut as 0/3/.
      //the idx < 10 will be located in the 0 folder.
      with final_idx_str= if _idx < 10 then "0"+idx_str else idx_str,
      with result_path  = join("/", reverse(map_select((Bool first, Word8 char) |-> if first then (false, failure) else (false, success(implode([char]))), true, reverse(explode(final_idx_str)))) + [idx_str])+"/",
      //println("to_fs_folder result_path "+result_path);
      result_path
  }
.

//public define String
//  to_String
//  (
//    DB_ID idx
//  )=
//  if idx is
//  {
//    none     then "none",
//    pk(_idx) then abs_to_decimal(_idx)
//  }.
//
//public define Int
//  to_Int
//  (
//    DB_ID idx
//  )=
//  if idx is
//  {
//    none     then (Int)-1,
//    pk(_idx) then _idx
//  }.

public define String
  to_String
  (
    DB_date d
  )=
  date(d).

public define String
  String s + DB_date d =
  s + to_String(d)
.

public define String
  String s + Maybe(DB_date) mb_date =
  if mb_date is 
  {
    failure       then s + "unknow date",
    success(date) then s + date
  }
.

public define String
  DB_date d + String s =
  to_String(d) + s
.

public define String
  date_not_null
  (
    DB_date d,
    String  default
  )=
  if d.date = "{NULL}" then
    default
  else
    d.date
.

public define String
  to_String
  (
    DB_datetime dt
  )=
  datetime(dt).

public define String
  String s + DB_datetime dt =
  s + to_String(dt)
.

public define String
  DB_datetime dt + String s =
  to_String(dt) + s
.

public define String
  get_time
  (
    DB_datetime dt
  )=
  with ba_dt = to_byte_array(dt.datetime),
  to_string(extract(ba_dt, 11, 19))
.

public define String
  to_String
  (
    DB_time t
  )=
  time(t).

public define String
  String s + DB_time t =
  s + to_String(t)
.

public define String
  DB_time t + String s =
  to_String(t) + s
.

public define String
  to_DBString
  (
    Bool value
  )=
  if value then "1" else "0".
  
public define Int
  to_DBInt
  (
    Bool value
  )=
  if value then 1 else 0.
  

  

 * Some Bind helper *
 

  
  
/********** Some default type values ********************/

public define DB_datetime
  get_dummy_DB_datetime
  =
  db_datetime("").

public define DB_datetime
  db_now
  =
  db_datetime(_Int_to_ISO_8601_datetime(now)).

public define DB_date
  db_now
  =
  db_date(_Int_to_ISO_8601_date(now)).

public define DB_time
  db_now
  =
  db_time(_Int_to_ISO_8601_time(now)).
  
public define DB_time
  get_dummy_DB_time
  =
  db_time("").
  
public define DB_date
  get_dummy_DB_date
  =
  db_date("").