summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-05-18 00:28:21 +0200
committerSebastian Reichel <sre@ring0.de>2012-05-18 00:28:21 +0200
commit6cfd0b53552cc0cad476cc089e6324ea4135efae (patch)
tree0a872dc36fcac2890b5ebda7e45d85321edde8ce
parent4e3058c7d626bfb91d694d5d846b0890cb6f6df6 (diff)
downloadserial-barcode-scanner-6cfd0b53552cc0cad476cc089e6324ea4135efae.tar.bz2
decrease stock amount if something has been bought
-rw-r--r--db.vala37
1 files changed, 26 insertions, 11 deletions
diff --git a/db.vala b/db.vala
index 4b83196..35445c0 100644
--- a/db.vala
+++ b/db.vala
@@ -1,6 +1,7 @@
public class Database {
private Sqlite.Database db;
- private Sqlite.Statement insert_stmt;
+ private Sqlite.Statement purchase_stmt1;
+ private Sqlite.Statement purchase_stmt2;
private Sqlite.Statement product_stmt;
private Sqlite.Statement undo_stmt;
private Sqlite.Statement stock_stmt1;
@@ -9,7 +10,9 @@ public class Database {
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 purchase_query1 = "INSERT INTO purchases ('user', 'product', 'timestamp') VALUES (?, ?, ?)";
+ private static string purchase_query2 = "UPDATE products SET amount = amount - 1 WHERE id = ?";
+
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";
@@ -24,9 +27,14 @@ public class Database {
error("could not open database!");
}
- rc = this.db.prepare_v2(insert_query, -1, out insert_stmt);
+ rc = this.db.prepare_v2(purchase_query1, -1, out purchase_stmt1);
+ if(rc != Sqlite.OK) {
+ error("could not prepare first purchase statement!");
+ }
+
+ rc = this.db.prepare_v2(purchase_query2, -1, out purchase_stmt2);
if(rc != Sqlite.OK) {
- error("could not prepare insert statement!");
+ error("could not prepare second purchase statement!");
}
rc = this.db.prepare_v2(product_query, -1, out product_stmt);
@@ -65,19 +73,26 @@ public class Database {
public bool buy(uint64 article) {
if(is_logged_in()) {
+ int rc = 0;
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));
+ this.purchase_stmt1.reset();
+ this.purchase_stmt1.bind_text(1, "%llu".printf(user));
+ this.purchase_stmt1.bind_text(2, "%llu".printf(article));
+ this.purchase_stmt1.bind_text(3, "%llu".printf(timestamp));
+
+ rc = this.purchase_stmt1.step();
+ if(rc != Sqlite.DONE)
+ error("[interner Fehler: %d]".printf(rc));
- int rc = this.insert_stmt.step();
+ this.purchase_stmt2.reset();
+ this.purchase_stmt2.bind_text(1, "%llu".printf(article));
+ rc = this.purchase_stmt2.step();
if(rc != Sqlite.DONE)
error("[interner Fehler: %d]".printf(rc));
- else
- return true;
+
+ return true;
} else {
return false;
}