Use SQL UPSERT clause when updating contacts

This saves a round trip in the common case of a contact already
existing. Requires SQLite 3.24 at a minimum, so bump it in meson.build.
This commit is contained in:
Michael Gratton 2019-10-25 15:51:13 +11:00
parent 3e7a9daccc
commit 12d0cced35
2 changed files with 16 additions and 25 deletions

View file

@ -59,7 +59,7 @@ target_webkit = '2.24'
glib = dependency('glib-2.0', version: '>=' + target_glib)
gmime = dependency('gmime-2.6', version: '>= 2.6.17')
gtk = dependency('gtk+-3.0', version: '>=' + target_gtk)
sqlite = dependency('sqlite3', version: '>= 3.12')
sqlite = dependency('sqlite3', version: '>= 3.24')
webkit2gtk = dependency('webkit2gtk-4.0', version: '>=' + target_webkit)
# Secondary deps - keep sorted alphabetically

View file

@ -140,31 +140,22 @@ internal class Geary.ContactStoreImpl : ContactStore, BaseObject {
Contact updated,
GLib.Cancellable? cancellable)
throws GLib.Error {
Contact? existing = do_fetch_contact(
cx, updated.email, cancellable
);
Db.Statement stmt = cx.prepare("""
INSERT INTO ContactTable(
normalized_email, email, real_name, flags, highest_importance
) VALUES(?, ?, ?, ?, ?)
ON CONFLICT(email) DO UPDATE SET
real_name = excluded.real_name,
flags = excluded.flags,
highest_importance = excluded.highest_importance
""");
stmt.bind_string(0, updated.normalized_email);
stmt.bind_string(1, updated.email.make_valid());
stmt.bind_string(2, updated.real_name.make_valid());
stmt.bind_string(3, updated.flags.serialize());
stmt.bind_int(4, updated.highest_importance);
if (existing == null) {
// Not found, so just insert it
Db.Statement stmt = cx.prepare(
"INSERT INTO ContactTable(normalized_email, email, real_name, flags, highest_importance) "
+ "VALUES(?, ?, ?, ?, ?)");
stmt.bind_string(0, updated.normalized_email);
stmt.bind_string(1, updated.email);
stmt.bind_string(2, updated.real_name);
stmt.bind_string(3, updated.flags.serialize());
stmt.bind_int(4, updated.highest_importance);
stmt.exec(cancellable);
} else {
Db.Statement stmt = cx.prepare(
"UPDATE ContactTable SET real_name=?, flags=?, highest_importance=? WHERE email=?");
stmt.bind_string(0, updated.real_name);
stmt.bind_string(1, updated.flags.serialize());
stmt.bind_int(2, updated.highest_importance);
stmt.bind_string(3, updated.email);
stmt.exec(cancellable);
}
stmt.exec(cancellable);
}
}