hk_sql_utils.anubis 2.17 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 02/04/2017
 * Time: 19:23
 * © Calexium 
 */

read calexium_lib/database/db_utils.anubis
read hayamiki_lib/types/hayamiki.anubis

public define String
  referer_to_sql_clause
  (
    HK_Referer    referer,
    String        table_name,
    List(String)  table_columns
  )=
  if referer is
  {
    no_referer                                                     then " (1 = 1) ",
    hk_referer(referer_table, referer_row_id, fk_table, fk_column) then
      if referer_table = table_name then
        "id = " +referer_row_id
      else if fk_column:table_columns then
        fk_column + " = " + referer_row_id
      else
        " (1 = 1) ",
  }
.

public define Maybe(One)
  delete_from_ids
  (
    SQLite3DataBase db,
    String          table_name,    
    List(Int)       ids
  )=
  if sql_query_timeout(db,
        "DELETE FROM "+table_name+
        " WHERE id IN ("+join(", ", to_List_String(ids))+");",
         [],
        "delete_from_ids") is
  {
    error(_)    then failure,
    ok(_, _, _) then success(unique)
  }.
  
public define Int
  get_count_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT COUNT(\"id\") FROM "+table_name+" "+final_clause+";", [], "get_count_by_clause") is
  {
    error(_)         then 0,
    ok(_, cursor, _) then db_get_integer(cursor)
  }
.

public define Int
  get_count
  (
    SQLite3DataBase db,
    String          table_name
  )=
  get_count_by_clause(db, table_name, "")
.


public define List(Int)
  get_ids_by_clause
  (
    SQLite3DataBase db,
    String          table_name,
    String          clause
  )=
  with final_clause = if clause = "" then
                         ""
                      else
                        "WHERE "+clause,

  if sql_query_timeout(db, "SELECT \"id\" FROM "+table_name+" "+final_clause+";", [], "get_ids_by_clause") is
  {
    error(_)         then [],
    ok(_, cursor, _) then db_get_integer_list(cursor)
  }
.