settings.anubis 2.19 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 calexium_lib/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=" + db_make_sql_string(var) + ";", "select_settings") is
  {
    failure then failure,  //error in the SQL request
    success(table_cursor) then
      if table_cursor(next_row) 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
  )= 
  if select_settings(db, var) is success(_) then 
    forget(sql_query_timeout(db, "UPDATE settings SET var_value = @value WHERE var_name = @name;", [bind_String("@name", var), bind_String("@value", value)], "update_settings"))
  else
    forget(sql_query_timeout(db, "INSERT INTO settings (var_name, var_value) VALUES (@name, @value)", [bind_String("@name", var), bind_String("@value", value)], "update_settings"))
  .