db_types.anubis 3.72 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 14/11/2011
 * Time: 22:41
 * 
 */
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 String
  to_String
  (
    DB_id idx
  )=
  if idx is
  {
    none        then "none",
    db_id(_idx) then abs_to_decimal(_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)-1,
    db_id(_idx) then _idx
  }.

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

/**
 * 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
  DB_date d + String s =
  to_String(d) + s
.

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
  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("").