settings.anubis 2.41 KB
/*
 * Created by PyramIDE.
 * User: Steve Marechal
 * Date: 09/12/2008
 * Time: 17:19
 */

read tools/basis.anubis
read data_base/db_tools.anubis

read xlib/database/db_utils.anubis


public define Maybe(String)
  select_settings
  (
    Database  db,
    String    var,
  )=
  if sql_query_timeout(db, "SELECT var_value FROM settings WHERE var_name=:var_name;", [bind_String(":var_name",var)], "select_settings") is
  {
    failure               then failure,  //error in the SQL request
    success(table_cursor) then
      if table_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
  (
    Database  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
  (
    Database  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
  (
    Database  db,
    String           var,
    Bool             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!=0
      }
  }.
  
public define One
  update_settings
  (
    Database db,
    String          var,
    String          value
  )= 
  with binds = (List(SQLite3Bind))[bind_String("@name", var),
                                   bind_String("@value", value)],
                                  
  if select_settings(db, var) is success(_) then 
    forget(sql_query_timeout(db, "UPDATE settings SET var_value = @value WHERE var_name = @name;", binds, "update_settings"))
  else
    forget(sql_query_timeout(db, "INSERT INTO settings (var_name, var_value) VALUES (@name, @value)", binds, "update_settings"))
  .
  
public define One
  update_settings
  (
    Database  db,
    String    var,
    Int       value
  )=
 update_settings(db, var, to_String(value)
.