summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2018-07-16 16:49:27 +0200
committerSebastian Reichel <sre@ring0.de>2018-07-17 12:17:08 +0200
commita79d89ad7a6d51ba36a79a74f969c5076862e338 (patch)
tree39322afac696862b535b2d743f28b76a0f99256b /src
parentf097216aa6edaa5e9bcc1c6a785631978cfe3953 (diff)
downloadserial-barcode-scanner-a79d89ad7a6d51ba36a79a74f969c5076862e338.tar.bz2
database: add add_category(name)
Diffstat (limited to 'src')
-rw-r--r--src/database/database.vala18
-rw-r--r--src/database/db-interface.vala3
2 files changed, 20 insertions, 1 deletions
diff --git a/src/database/database.vala b/src/database/database.vala
index cd2b947..ab9c5d2 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -127,6 +127,7 @@ public class DataBase : Object {
queries["user_disable"] = "UPDATE users SET disabled = ? WHERE id = ?";
queries["last_timestamp"] = "SELECT timestamp FROM sales ORDER BY timestamp DESC LIMIT 1";
queries["category_list"] = "SELECT id, name FROM categories";
+ queries["category_add"] = "INSERT INTO categories('name') VALUES (?)";
queries["supplier_list"] = "SELECT id, name, postal_code, city, street, phone, website FROM supplier";
queries["supplier_get"] = "SELECT id, name, postal_code, city, street, phone, website FROM supplier WHERE id = ?";
queries["supplier_add"] = "INSERT INTO supplier('name', 'postal_code', 'city', 'street', 'phone', 'website') VALUES (?, ?, ?, ?, ?, ?)";
@@ -966,6 +967,23 @@ public class DataBase : Object {
return result;
}
+ public void add_category(string name) throws DBusError, IOError, DatabaseError {
+ /* check if category already exists */
+ foreach(var c in get_category_list()) {
+ if(name == c.name) {
+ return;
+ }
+ }
+
+ statements["category_add"].reset();
+ statements["category_add"].bind_text(1, name);
+ int rc = statements["category_add"].step();
+
+ if(rc != Sqlite.DONE) {
+ throw new DatabaseError.INTERNAL_ERROR(_("internal error: %d"), rc);
+ }
+ }
+
public Supplier[] get_supplier_list() throws DBusError, IOError, DatabaseError {
Supplier[] result = {};
diff --git a/src/database/db-interface.vala b/src/database/db-interface.vala
index 2a6603f..63a0148 100644
--- a/src/database/db-interface.vala
+++ b/src/database/db-interface.vala
@@ -52,7 +52,8 @@ public interface Database : Object {
public abstract bool user_exists(int user) throws DBusError, IOError, DatabaseError;
public abstract bool user_equals(UserInfo u) throws DBusError, IOError, DatabaseError;
public abstract int64 get_timestamp_of_last_purchase() throws DBusError, IOError;
- public abstract Category[] get_category_list() throws DBusError, IOError;
+ public abstract Category[] get_category_list() throws DBusError, IOError, DatabaseError;
+ public abstract int add_category(string name) throws DBusError, IOError, DatabaseError;
public abstract Supplier[] get_supplier_list() throws DBusError, IOError;
public abstract Supplier get_supplier(int id) throws DBusError, IOError;
public abstract void add_supplier(string name, string postal_code, string city, string street, string phone, string website) throws DBusError, IOError, DatabaseError;