qlite/libdino: optimize db access

This commit is contained in:
Marvin W 2017-04-23 10:23:11 +02:00
parent eddf17c682
commit 9728e832b1
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
4 changed files with 31 additions and 8 deletions

View File

@ -88,13 +88,7 @@ public class Conversation : Object {
}
private void on_update(Object o, ParamSpec sp) {
var update = db.conversation.update().with(db.conversation.jid_id, "=", db.get_jid_id(counterpart))
.with(db.conversation.account_id, "=", account.id);
if (counterpart.resourcepart != null) {
update.with(db.conversation.resource, "=", counterpart.resourcepart);
} else {
update.with_null(db.conversation.resource);
}
var update = db.conversation.update().with(db.conversation.id, "=", id);
switch (sp.name) {
case "type-":
update.set(db.conversation.type_, type_); break;

View File

@ -110,6 +110,8 @@ public class Database : Qlite.Database {
internal EntityFeatureTable(Database db) {
base(db, "entity_feature");
init({entity, feature});
unique({entity, feature}, "IGNORE");
index("entity_feature_idx", {entity});
}
}
@ -135,6 +137,7 @@ public class Database : Qlite.Database {
avatar = new AvatarTable(this);
entity_feature = new EntityFeatureTable(this);
init({ account, jid, message, real_jid, conversation, avatar, entity_feature });
exec("PRAGMA synchronous=0");
}
public override void migrate(long oldVersion) {

View File

@ -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())");

View File

@ -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);
}
}
}
}