aboutsummaryrefslogtreecommitdiff
path: root/qlite/src
diff options
context:
space:
mode:
authorMarvin W <git@larma.de>2017-04-23 10:23:11 +0200
committerMarvin W <git@larma.de>2017-04-26 21:48:53 +0200
commit9728e832b18e8bbfcc0e011b5b1da9afe6021bf3 (patch)
tree301d6d127b7a94bd7f0f1ab7f496d5d26c007a3f /qlite/src
parenteddf17c68274c3e8ecf86a13858243159ffe9714 (diff)
downloaddino-9728e832b18e8bbfcc0e011b5b1da9afe6021bf3.tar.gz
dino-9728e832b18e8bbfcc0e011b5b1da9afe6021bf3.zip
qlite/libdino: optimize db access
Diffstat (limited to 'qlite/src')
-rw-r--r--qlite/src/database.vala5
-rw-r--r--qlite/src/table.vala23
2 files changed, 27 insertions, 1 deletions
diff --git a/qlite/src/database.vala b/qlite/src/database.vala
index 0427df16..cc46ee21 100644
--- a/qlite/src/database.vala
+++ b/qlite/src/database.vala
@@ -77,6 +77,9 @@ public class Database {
meta_table.update().with(meta_name, "=", "version").set(meta_int_val, expected_version).perform();
}
}
+ foreach (Table t in tables) {
+ t.post();
+ }
}
internal int errcode() {
@@ -136,7 +139,7 @@ public class Database {
return statement;
}
- internal void exec(string sql) throws DatabaseError {
+ public void exec(string sql) throws DatabaseError {
ensure_init();
if (db.exec(sql) != OK) {
throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
diff --git a/qlite/src/table.vala b/qlite/src/table.vala
index b4f2fee7..bd3fcc36 100644
--- a/qlite/src/table.vala
+++ b/qlite/src/table.vala
@@ -7,6 +7,7 @@ public class Table {
public string name { get; private set; }
protected Column[]? columns;
private string constraints = "";
+ private string[] post_statements = {};
public Table(Database db, string name) {
this.db = db;
@@ -32,6 +33,22 @@ public class Table {
}
}
+ public void add_post_statement(string stmt) {
+ post_statements += stmt;
+ }
+
+ public void index(string index_name, Column[] columns, bool unique = false) {
+ string stmt = @"CREATE $(unique ? "UNIQUE" : "") INDEX IF NOT EXISTS $index_name ON $name (";
+ bool first = true;
+ foreach (Column c in columns) {
+ if (!first) stmt += ", ";
+ stmt += c.name;
+ first = false;
+ }
+ stmt += ")";
+ add_post_statement(stmt);
+ }
+
private void ensure_init() throws DatabaseError {
if (columns == null) throw new DatabaseError.NOT_INITIALIZED(@"Table $name was not initialized, call init()");
}
@@ -114,6 +131,12 @@ public class Table {
db.exec(@"DROP TABLE _$(name)_$old_version");
}
}
+
+ internal void post() throws DatabaseError {
+ foreach (string stmt in post_statements) {
+ db.exec(stmt);
+ }
+ }
}
} \ No newline at end of file