Qlite: Return OptionalRow instead of Row?, add ability to remove columns on version upgrade
This commit is contained in:
parent
4f34e43116
commit
c2643a45b0
|
@ -121,7 +121,7 @@ public class Database : Qlite.Database {
|
||||||
public AvatarTable avatar { get; private set; }
|
public AvatarTable avatar { get; private set; }
|
||||||
public EntityFeatureTable entity_feature { get; private set; }
|
public EntityFeatureTable entity_feature { get; private set; }
|
||||||
|
|
||||||
public Database(string fileName) {
|
public Database(string fileName) throws DatabaseError {
|
||||||
base(fileName, VERSION);
|
base(fileName, VERSION);
|
||||||
account = new AccountTable(this);
|
account = new AccountTable(this);
|
||||||
jid = new JidTable(this);
|
jid = new JidTable(this);
|
||||||
|
@ -174,7 +174,7 @@ public class Database : Qlite.Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Account? get_account_by_id(int id) {
|
private Account? get_account_by_id(int id) {
|
||||||
Row? row = account.row_with(account.id, id);
|
Row? row = account.row_with(account.id, id).inner;
|
||||||
if (row != null) {
|
if (row != null) {
|
||||||
return get_account_from_row(row);
|
return get_account_from_row(row);
|
||||||
}
|
}
|
||||||
|
@ -300,7 +300,7 @@ public class Database : Qlite.Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message? get_message_by_id(int id) {
|
public Message? get_message_by_id(int id) {
|
||||||
Row? row = message.row_with(message.id, id);
|
Row? row = message.row_with(message.id, id).inner;
|
||||||
if (row != null) {
|
if (row != null) {
|
||||||
return get_message_from_row(row);
|
return get_message_from_row(row);
|
||||||
}
|
}
|
||||||
|
@ -427,7 +427,7 @@ public class Database : Qlite.Database {
|
||||||
|
|
||||||
|
|
||||||
private int get_jid_id(Jid jid_obj) {
|
private int get_jid_id(Jid jid_obj) {
|
||||||
Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string());
|
Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string()).inner;
|
||||||
return row != null ? row[jid.id] : add_jid(jid_obj);
|
return row != null ? row[jid.id] : add_jid(jid_obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class AccountSettingWidget : Plugins.AccountSettingsWidget, Box {
|
||||||
public void set_account(Account account) {
|
public void set_account(Account account) {
|
||||||
this.account = account;
|
this.account = account;
|
||||||
try {
|
try {
|
||||||
Qlite.Row? row = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id);
|
Qlite.Row? row = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id).inner;
|
||||||
if (row == null) {
|
if (row == null) {
|
||||||
fingerprint.set_markup(@"Own fingerprint\n<span font='8'>Will be generated on first connect</span>");
|
fingerprint.set_markup(@"Own fingerprint\n<span font='8'>Will be generated on first connect</span>");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -99,7 +99,7 @@ public class Manager : StreamInteractionModule, Object {
|
||||||
private void on_store_created(Account account, Store store) {
|
private void on_store_created(Account account, Store store) {
|
||||||
Qlite.Row? row = null;
|
Qlite.Row? row = null;
|
||||||
try {
|
try {
|
||||||
row = db.identity.row_with(db.identity.account_id, account.id);
|
row = db.identity.row_with(db.identity.account_id, account.id).inner;
|
||||||
} catch (Error e) {
|
} catch (Error e) {
|
||||||
// Ignore error
|
// Ignore error
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,15 +51,19 @@ public class Database {
|
||||||
meta_table.create_table_at_version(expected_version);
|
meta_table.create_table_at_version(expected_version);
|
||||||
long old_version = 0;
|
long old_version = 0;
|
||||||
try {
|
try {
|
||||||
Row? row = meta_table.row_with(meta_name, "version");
|
old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1];
|
||||||
old_version = row == null ? -1 : (long) row[meta_int_val];
|
|
||||||
} catch (DatabaseError e) {
|
} catch (DatabaseError e) {
|
||||||
old_version = -1;
|
old_version = -1;
|
||||||
}
|
}
|
||||||
|
if (old_version == -1) {
|
||||||
|
foreach (Table t in tables) {
|
||||||
|
t.create_table_at_version(expected_version);
|
||||||
|
}
|
||||||
|
meta_table.insert().value(meta_name, "version").value(meta_int_val, expected_version).perform();
|
||||||
|
} else if (expected_version != old_version) {
|
||||||
foreach (Table t in tables) {
|
foreach (Table t in tables) {
|
||||||
t.create_table_at_version(old_version);
|
t.create_table_at_version(old_version);
|
||||||
}
|
}
|
||||||
if (expected_version != old_version) {
|
|
||||||
foreach (Table t in tables) {
|
foreach (Table t in tables) {
|
||||||
t.add_columns_for_version(old_version, expected_version);
|
t.add_columns_for_version(old_version, expected_version);
|
||||||
}
|
}
|
||||||
|
@ -117,9 +121,9 @@ public class Database {
|
||||||
return new DeleteBuilder(this);
|
return new DeleteBuilder(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row.RowIterator query_sql(string sql, string[]? args = null) throws DatabaseError {
|
public RowIterator query_sql(string sql, string[]? args = null) throws DatabaseError {
|
||||||
ensure_init();
|
ensure_init();
|
||||||
return new Row.RowIterator(this, sql, args);
|
return new RowIterator(this, sql, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Statement prepare(string sql) throws DatabaseError {
|
public Statement prepare(string sql) throws DatabaseError {
|
||||||
|
|
|
@ -131,20 +131,20 @@ public class QueryBuilder : StatementBuilder {
|
||||||
public int64 count() throws DatabaseError {
|
public int64 count() throws DatabaseError {
|
||||||
this.column_selector = @"COUNT($column_selector) AS count";
|
this.column_selector = @"COUNT($column_selector) AS count";
|
||||||
this.single_result = true;
|
this.single_result = true;
|
||||||
return row().get_integer("count");
|
return row_().get_integer("count");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row? row() throws DatabaseError {
|
private Row? row_() throws DatabaseError {
|
||||||
if (!single_result) throw new DatabaseError.NON_UNIQUE("query is not suited to return a single row, but row() was called.");
|
if (!single_result) throw new DatabaseError.NON_UNIQUE("query is not suited to return a single row, but row() was called.");
|
||||||
return iterator().next_value();
|
return iterator().next_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get<T>(Column<T> field) throws DatabaseError {
|
public RowOption row() throws DatabaseError {
|
||||||
Row row = row();
|
return new RowOption(row_());
|
||||||
if (row != null) {
|
|
||||||
return row[field];
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
public T get<T>(Column<T> field) throws DatabaseError {
|
||||||
|
return row()[field];
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Statement prepare() throws DatabaseError {
|
public override Statement prepare() throws DatabaseError {
|
||||||
|
@ -155,8 +155,8 @@ public class QueryBuilder : StatementBuilder {
|
||||||
return stmt;
|
return stmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row.RowIterator iterator() throws DatabaseError {
|
public RowIterator iterator() throws DatabaseError {
|
||||||
return new Row.RowIterator.from_query_builder(this);
|
return new RowIterator.from_query_builder(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
class OrderingTerm {
|
class OrderingTerm {
|
||||||
|
|
|
@ -50,6 +50,7 @@ public class Row {
|
||||||
public bool has_real(string field) {
|
public bool has_real(string field) {
|
||||||
return real_map.has_key(field) && real_map[field] != null;
|
return real_map.has_key(field) && real_map[field] != null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class RowIterator {
|
public class RowIterator {
|
||||||
private Statement stmt;
|
private Statement stmt;
|
||||||
|
@ -74,6 +75,22 @@ public class Row {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class RowOption {
|
||||||
|
public Row? inner { get; private set; }
|
||||||
|
|
||||||
|
public RowOption(Row? row) {
|
||||||
|
this.inner = row;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool is_present() {
|
||||||
|
return inner != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get<T>(Column<T> field, T def = null) {
|
||||||
|
if (inner == null || field.is_null(inner)) return def;
|
||||||
|
return field[inner];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,7 +57,7 @@ public class Table {
|
||||||
return db.delete().from(this);
|
return db.delete().from(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Row? row_with<T>(Column<T> column, T value) throws DatabaseError {
|
public RowOption row_with<T>(Column<T> column, T value) throws DatabaseError {
|
||||||
ensure_init();
|
ensure_init();
|
||||||
if (!column.unique && !column.primary_key) throw new DatabaseError.NON_UNIQUE(@"$(column.name) is not suited to identify a row, but used with row_with()");
|
if (!column.unique && !column.primary_key) throw new DatabaseError.NON_UNIQUE(@"$(column.name) is not suited to identify a row, but used with row_with()");
|
||||||
return select().with(column, "=", value).row();
|
return select().with(column, "=", value).row();
|
||||||
|
@ -97,7 +97,26 @@ public class Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete_columns_for_version(long old_version, long new_version) throws DatabaseError {
|
public void delete_columns_for_version(long old_version, long new_version) throws DatabaseError {
|
||||||
// TODO: Rename old table, create table at new_version, transfer data
|
bool column_deletion_required = false;
|
||||||
|
string column_list = null;
|
||||||
|
foreach (Column c in columns) {
|
||||||
|
if (c.min_version <= new_version && c.max_version >= new_version) {
|
||||||
|
if (column_list == null) {
|
||||||
|
column_list = c.name;
|
||||||
|
} else {
|
||||||
|
column_list += ", " + c.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!(c.min_version <= new_version && c.max_version >= new_version) && c.min_version <= old_version && c.max_version >= old_version) {
|
||||||
|
column_deletion_required = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (column_deletion_required) {
|
||||||
|
db.exec(@"ALTER TABLE $name RENAME TO _$(name)_$old_version");
|
||||||
|
create_table_at_version(new_version);
|
||||||
|
db.exec(@"INSERT INTO $name ($column_list) SELECT $column_list FROM _$(name)_$old_version");
|
||||||
|
db.exec(@"DROP TABLE _$(name)_$old_version");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue