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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
public class Database {
private Sqlite.Database db;
private Sqlite.Statement insert_stmt;
private Sqlite.Statement product_stmt;
private Sqlite.Statement undo_stmt;
private Sqlite.Statement stock_stmt1;
private Sqlite.Statement stock_stmt2;
uint64 user = 0;
uint64 product = 0;
bool logged_in = false;
bool stock_mode = false;
private static string insert_query = "INSERT INTO purchases ('user', 'product', 'timestamp') VALUES (?, ?, ?)";
private static string product_query = "SELECT name FROM products WHERE id = ?";
private static string undo_query = "DELETE FROM purchases WHERE user = ? ORDER BY 'timestamp' DESC LIMIT 1";
private static string stock_query1 = "INSERT INTO restock ('user', 'product', 'amount', 'timestamp') VALUES (?, ?, ?, ?)";
private static string stock_query2 = "UPDATE products SET amount = amount + ? WHERE id = ?";
public Database(string file) {
int rc;
rc = Sqlite.Database.open (file, out db);
if(rc != Sqlite.OK) {
error("could not open database!");
}
rc = this.db.prepare_v2(insert_query, -1, out insert_stmt);
if(rc != Sqlite.OK) {
error("could not prepare insert statement!");
}
rc = this.db.prepare_v2(product_query, -1, out product_stmt);
if(rc != Sqlite.OK) {
error("could not prepare article statement!");
}
rc = this.db.prepare_v2(undo_query, -1, out undo_stmt);
if(rc != Sqlite.OK) {
error("could not prepare undo statement!");
}
rc = this.db.prepare_v2(stock_query1, -1, out stock_stmt1);
if(rc != Sqlite.OK) {
error("could not prepare first stock statement!");
}
rc = this.db.prepare_v2(stock_query2, -1, out stock_stmt2);
if(rc != Sqlite.OK) {
error("could not prepare second stock statement!");
}
}
public bool login(uint64 id) {
this.user = id;
this.logged_in = true;
return true;
}
public bool logout() {
this.user = 0;
this.logged_in = false;
return true;
}
public bool buy(uint64 article) {
if(is_logged_in()) {
int64 timestamp = (new DateTime.now_utc()).to_unix();
this.insert_stmt.reset();
this.insert_stmt.bind_text(1, "%llu".printf(user));
this.insert_stmt.bind_text(2, "%llu".printf(article));
this.insert_stmt.bind_text(3, "%llu".printf(timestamp));
int rc = this.insert_stmt.step();
if(rc != Sqlite.DONE)
error("[interner Fehler: %d]".printf(rc));
else
return true;
} else {
return false;
}
}
public string get_product_name(uint64 article) {
this.product_stmt.reset();
this.product_stmt.bind_text(1, "%llu".printf(article));
int rc = this.product_stmt.step();
switch(rc) {
case Sqlite.ROW:
return this.product_stmt.column_text(0);
case Sqlite.DONE:
return "unbekanntes Produkt: %llu".printf(article);
default:
return "[interner Fehler: %d]".printf(rc);
}
}
public bool undo() {
if(is_logged_in()) {
this.undo_stmt.reset();
this.undo_stmt.bind_text(1, "%llu".printf(user));
int rc = this.undo_stmt.step();
if(rc != Sqlite.DONE)
error("[interner Fehler: %d]".printf(rc));
else
return true;
}
return false;
}
public bool choose_stock_product(uint64 id) {
if(is_in_stock_mode()) {
product = id;
return true;
}
return false;
}
public bool add_stock_product(uint64 amount) {
if(is_in_stock_mode() && product != 0) {
int rc = 0;
int64 timestamp = (new DateTime.now_utc()).to_unix();
this.stock_stmt1.reset();
this.stock_stmt1.bind_text(1, "%llu".printf(user));
this.stock_stmt1.bind_text(2, "%llu".printf(product));
this.stock_stmt1.bind_text(3, "%llu".printf(amount));
this.stock_stmt1.bind_text(4, "%llu".printf(timestamp));
rc = this.stock_stmt1.step();
if(rc != Sqlite.DONE)
error("[interner Fehler: %d]".printf(rc));
this.stock_stmt2.reset();
this.stock_stmt2.bind_text(1, "%llu".printf(amount));
this.stock_stmt2.bind_text(2, "%llu".printf(product));
rc = this.stock_stmt2.step();
if(rc != Sqlite.DONE)
error("[interner Fehler: %d]".printf(rc));
return true;
}
return false;
}
public bool go_into_stock_mode() {
if(is_logged_in())
stock_mode = true;
return stock_mode;
}
public bool is_logged_in() {
return this.logged_in;
}
public bool is_in_stock_mode() {
return this.stock_mode;
}
}
|