diff options
Diffstat (limited to 'db.vala')
-rw-r--r-- | db.vala | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -8,6 +8,7 @@ public class Database { private Sqlite.Statement undo_stmt3; private Sqlite.Statement stock_stmt1; private Sqlite.Statement stock_stmt2; + private Sqlite.Statement price_stmt; int32 user = 0; uint64 product = 0; bool logged_in = false; @@ -20,6 +21,7 @@ public class Database { private static string undo_query3 = "UPDATE products SET amount = amount + 1 WHERE id = ?"; 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 = ?"; + private static string price_query = "SELECT memberprice, guestprice FROM prices WHERE product = ? AND valid_from <= ? ORDER BY valid_from DESC LIMIT 1"; public Database(string file) { int rc; @@ -69,6 +71,11 @@ public class Database { error("could not prepare second stock statement!"); } + rc = this.db.prepare_v2(price_query, -1, out price_stmt); + if(rc != Sqlite.OK) { + error("could not prepare price statement!"); + } + } public bool login(int32 id) { @@ -127,6 +134,31 @@ public class Database { } } + public int get_product_price(uint64 article) { + int64 timestamp = (new DateTime.now_utc()).to_unix(); + bool member = user != 0; + + this.price_stmt.reset(); + this.price_stmt.bind_text(1, "%llu".printf(article)); + this.price_stmt.bind_text(1, "%lld".printf(timestamp)); + + int rc = this.price_stmt.step(); + + switch(rc) { + case Sqlite.ROW: + if(member) + return this.price_stmt.column_int(0); + else + return this.price_stmt.column_int(1); + case Sqlite.DONE: + stderr.printf("unbekanntes Produkt: %llu", article); + return 0; + default: + stderr.printf("[interner Fehler: %d]", rc); + return 0; + } + } + public bool undo() { if(is_logged_in()) { uint64 pid = 0; |