settings_sqlite3.anubis
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Created by PyramIDE.
* User: Totoro
* Date: 11/08/2012
* Time: 17:44
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
read tools/basis.anubis
read data_base/sqlite.anubis
read data_base/db_tools.anubis
read calexium_lib/database/db_utils.anubis
public define Maybe(String)
select_settings
(
SQLite3DataBase db,
String var,
)=
if sql_query_timeout(db, "SELECT var_value FROM settings WHERE var_name=" + db_make_sql_string(var) + ";", [], "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 var,
Bool value
)=
with bool_str = if value is true then "true" else "false",
with binds = (List(SQLite3Bind))[bind_String(":name", var), bind_String(":value", bool_str)],
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
(
SQLite3DataBase db,
String var,
Int value
)=
with binds = (List(SQLite3Bind))[bind_String(":name", var), bind_Int(":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
(
SQLite3DataBase 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"))
.