From 1551de799e8b9b2adbce0116a3d8729626229070 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Wed, 16 May 2012 03:41:55 +0200 Subject: add lokal backend --- Makefile | 4 ++-- db.vala | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.vala | 15 ++++++++++----- 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 db.vala diff --git a/Makefile b/Makefile index 1480c34..1d993f3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PREFIX=/usr/local -barcode-scanner: main.vala serial.vala web.vala - valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 $^ +barcode-scanner: main.vala serial.vala web.vala db.vala + valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 $^ install: barcode-scanner install -m755 barcode-scanner $(DESTDIR)$(PREFIX)/bin/barcode-scanner diff --git a/db.vala b/db.vala new file mode 100644 index 0000000..6467e6c --- /dev/null +++ b/db.vala @@ -0,0 +1,66 @@ +public class Database { + private Sqlite.Database db; + private Sqlite.Statement insert_stmt; + private Sqlite.Statement product_stmt; + uint64 user = 0; + private static string insert_query = "INSERT INTO purchases ('user', 'product') VALUES (?, ?)"; + private static string product_query = "SELECT name FROM products 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!"); + } + } + + public void login(uint64 id) { + this.user = id; + } + + public void logout() { + this.user = 0; + } + + public void buy(uint64 article) { + this.insert_stmt.reset(); + this.insert_stmt.bind_text(1, "%llu".printf(user)); + this.insert_stmt.bind_text(2, "%llu".printf(article)); + + int rc = this.insert_stmt.step(); + + if(rc != Sqlite.DONE) + error("[interner Fehler: %d]".printf(rc)); + } + + 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 is_logged_in() { + return (user != 0); + } +} diff --git a/main.vala b/main.vala index 80fa44f..97eb612 100644 --- a/main.vala +++ b/main.vala @@ -1,5 +1,5 @@ public Serial s; -public Web w; +public Database w; public static int main(string[] args) { if(args.length < 2) { @@ -8,7 +8,7 @@ public static int main(string[] args) { } s = new Serial(args[1], 9600, 8, 1); - w = new Web(); + w = new Database("shop.db"); char[] detected = {}; @@ -34,13 +34,18 @@ public static void interpret(string data) { string str_id = data.substring(5); uint64 id = uint64.parse(str_id); - if(w.is_logged_in()) + if(w.is_logged_in()) { + stdout.printf("logout\n"); w.logout(); - else + } + else { + stdout.printf("login: %llu\n".printf(id)); w.login(id); - + } } else { uint64 id = uint64.parse(data); w.buy(id); + + stdout.printf("gekaufter Artikel: %s\n", w.get_product_name(id)); } } -- cgit v1.2.3