settings_sqlite3.anubis 3.86 KB
/*
 * Created by PyramIDE.
 * User: Totoro
 * Date: 11/08/2012
 * Time: 17:44
 * 
 *
 */

read tools/basis.anubis
read data_base/sqlite.anubis
read data_base/db_tools.anubis
read xlib/database/db_utils.anubis


public define Maybe(String)
  select_settings
  (
    SQLite3DataBase db,
    String          name,
  )=
  if sql_query_timeout(db, "SELECT value FROM settings WHERE name = :name;", [bind_String(":name",name)], "select_settings") is
  {
    error(_)         then failure,  //error in the SQL request
    ok(_, cursor, _) then
    if cursor(unique) is
      {
        error(_)      then failure,
        no_more_row   then failure,  //can't find the var_value in the table, because the row is empty
        row(current)  then success(text(current)(0))
      }
  }.

public define String
  select_settings
  (
    SQLite3DataBase db,
    String          var,
    String          default
  )=
  if select_settings(db, var) is
  {
    failure         then default,  //error in the SQL request
    success(value)  then value
  }.

public define Int
  select_settings
  (
    SQLite3DataBase db,
    String          var,
    Int             default
  )=
  if select_settings(db, var) is
  {
    failure         then default,  //error in the SQL request
    success(value)  then 
      if decimal_scan(value) is
      {
        failure     then default,
        success(v)  then v
      }
  }.

public define Bool
  select_settings
  (
    SQLite3DataBase db,
    String          var,
    Bool            default
  )=
  if select_settings(db, var) is
  {
    failure         then default,  //error in the SQL request
    success(value)  then 
      if value = "true"       then true
      else if value = "false" then false
      // old values
      else if value = "1" then true
      else if value = "0" then false
      // failback
      else if decimal_scan(value) is
      {
        failure     then default,
        success(v)  then v!=0
      }
  }.

public define One
  update_settings
  (
    SQLite3DataBase db,
    String          name,
    Bool            value
  )= 
  //with bool_str = if value is true then "true" else "false",
  with binds = (List(SQLite3Bind))[bind_String(":name", name), bind_Bool(":value", value)],
  if select_settings(db, name) is success(_) then 
    forget(sql_query_timeout(db, "UPDATE settings SET value = :value WHERE name = :name;", binds, "update_settings"))
  else
    forget(sql_query_timeout(db, "INSERT INTO settings (name, value) VALUES (:name, :value)", binds, "update_settings"))
  .
  
public define One
  update_settings
  (
    SQLite3DataBase db,
    String          name,
    Int             value
  )= 
  with binds = (List(SQLite3Bind))[bind_String(":name", name), bind_Int(":value", value)],
  if select_settings(db, name) is success(_) then 
    forget(sql_query_timeout(db, "UPDATE settings SET value = :value WHERE name = :name;", binds, "update_settings"))
  else
    forget(sql_query_timeout(db, "INSERT INTO settings (name, value) VALUES (:name, :value)", binds, "update_settings"))
  .

       
public define One
  update_settings
  (
    SQLite3DataBase db,
    String          name,
    String          value
  )= 
  with binds = (List(SQLite3Bind))[bind_String(":name", name), bind_String(":value", value)],
  if select_settings(db, name) is success(_) then 
    forget(sql_query_timeout(db, "UPDATE settings SET value = :value WHERE name = :name;", binds, "update_settings"))
  else
    forget(sql_query_timeout(db, "INSERT INTO settings (name, value) VALUES (:name, :value)", binds, "update_settings"))
  .


public define Bool
  create_settings_table
  (
    SQLite3DataBase db
  )=
  if sql_query_timeout(db, "CREATE TABLE IF NOT EXISTS settings ( 
    id        INTEGER PRIMARY KEY,
    name      TEXT    UNIQUE,
    value     TEXT 
    );", [], "create_settings_table") is
  {
    error(_)    then false,  //error in the SQL request
    ok(_, _, _) then true
  }
.