db_get_helpers.anubis 6.36 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 15/12/2019
 * Time: 13:32
 * © David RENÉ
 */

read tools/basis.anubis
read system/logger.anubis
read xlib/net_services_protocols/logger_client.anubis
read system/string.anubis
read data_base/db_tools.anubis
read data_base/sqlite.anubis

transmit xlib/database/db_types.anubis

// -- Extractors HELPERS ---------------


// sqlite3 API
public define String
  db_get_String
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_String")); "",
    no_more_row       then "",    //can't find the symbol in the table, because the row is empty
    row(explorer)     then text(explorer)(0)
 }
.
 
public define List(String) 
  db_get_List_String
  ( 
    One -> DbRow    table_cursor,
    List(String)    so_far
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_string_list")); reverse(so_far),
    no_more_row       then reverse(so_far),    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with s = text(explorer)(0),
      db_get_List_String(table_cursor, [s . so_far])
 }.
 
// sqlite3 API
public define List(String) 
  db_get_List_String
  ( 
    One -> SQLite3Row      table_cursor,
    List(String)           so_far
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_string_list")); reverse(so_far),
    no_more_row       then reverse(so_far),    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with s = text(explorer)(0),
      db_get_List_String(table_cursor, [s . so_far])
 }.

// sqlite3 API
public define List(String) 
  db_get_List_String
  ( 
    One -> SQLite3Row      table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_string_list")); [],
    no_more_row       then [],    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with s = text(explorer)(0),
      [ s . db_get_List_String(table_cursor)]
 }.

public define Bool
  db_get_Bool
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_Bool")); false,
    no_more_row       then false,    //can't find the symbol in the table, because the row is empty
    row(explorer)     then db_bool(explorer)(0)
 }
.

// sqlite3 API
public define Int
  db_get_Int
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_integer")); 0,
    no_more_row       then 0,
    row(explorer)     then (Int)db_integer(explorer)(0)
 }
.

public define Maybe(Int)
  db_get_mb_Int
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_mb_integer")); failure,
    no_more_row       then failure,
    row(explorer)     then db_integer(explorer)(0)
 }.
 
// sqlite3 API
public define List(Int) 
  db_get_List_Int
  ( 
    One -> SQLite3Row table_cursor,
    List(Int)         so_far
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far),
    no_more_row       then reverse(so_far),    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with s = (Int)db_integer(explorer)(0),
      db_get_List_Int(table_cursor, [s . so_far])
 }.

public define List(Int) 
  db_get_List_Int
  ( 
    One -> SQLite3Row table_cursor
  ) =
  db_get_List_Int(table_cursor, []).

// sqlite3 API
public define Float
  db_get_Float
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_Float")); 0.0,
    no_more_row       then 0.0,
    row(explorer)     then db_float(explorer)(0)
 }.
 
// sqlite3 API
public define DB_id
  db_get_DB_id
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_DB_id")); none,
    no_more_row       then none,
    row(explorer)     then 
      with result = (Int)db_integer(explorer)(0),
        if result = -1 | result = 0 then none else db_id(result)
 }
.

public define Maybe(DB_id)
  db_get_mb_DB_id
  ( 
    One -> SQLite3Row table_cursor
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_mb_DB_id")); failure,
    no_more_row       then failure,
    row(explorer)     then 
      with result = (Int)db_integer(explorer)(0),
        if result = -1 | result = 0 then success(none) else success(db_id(result))
 }
.

// sqlite3 API
public define List(DB_id) 
  db_get_List_DB_id
  ( 
    One -> SQLite3Row table_cursor,
    List(DB_id)       so_far
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_List_DB_id")); reverse(so_far),
    no_more_row       then reverse(so_far),    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with result = (Int)db_integer(explorer)(0),
      db_get_List_DB_id(table_cursor, [db_id(result) . so_far])
 }.

public define List(DB_id) 
  db_get_List_DB_id
  ( 
    One -> SQLite3Row table_cursor
  ) =
  db_get_List_DB_id(table_cursor, []).


 /***********************************/
 /* SQLAPI */
 
public define List(Int) 
  db_get_List_Int
  ( 
    One -> DbRow   table_cursor,
    List(Int)           so_far
  ) =
  if table_cursor(unique) is
  {
    error(sql_error)  then logError("", "DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far),
    no_more_row       then reverse(so_far),    //can't find the symbol in the table, because the row is empty
    row(explorer)     then 
      with s = (Int)db_integer(explorer)(0),
      db_get_List_Int(table_cursor, [s . so_far])
 }.
 
public define List(Int) 
  db_get_List_Int
  ( 
    One -> DbRow   table_cursor,
  ) =
  db_get_List_Int(table_cursor, []).
  
  
 Help to construct clause and a list of bind according to list of SQLite3_update_field.
 This is useful when we want to construct a SQL query with only needs fields.