diff options
author | Brendan Long <self@brendanlong.com> | 2017-10-29 04:07:29 +0300 |
---|---|---|
committer | Brendan Long <self@brendanlong.com> | 2017-10-29 05:00:27 +0300 |
commit | 909a0523c28a4713fac0a5efa896cc1185bec1d7 (patch) | |
tree | 22072e9c2c698c5e470cc65a47d878a0ad13ce53 | |
parent | bc331f55387e3b60151019db7230b3679fd06b0e (diff) |
Use Value and transform
-rw-r--r-- | src/DataBaseReadOnly.vala | 4 | ||||
-rw-r--r-- | src/SQLite.vala | 26 |
2 files changed, 26 insertions, 4 deletions
diff --git a/src/DataBaseReadOnly.vala b/src/DataBaseReadOnly.vala index 0cee7b13..6086c60b 100644 --- a/src/DataBaseReadOnly.vala +++ b/src/DataBaseReadOnly.vala @@ -139,7 +139,7 @@ public class FeedReader.DataBaseReadOnly : GLib.Object { public uint get_unread_total() { var query = "SELECT COUNT(*) FROM articles WHERE unread = ?"; - var rows = m_db.execute(query, { ArticleStatus.UNREAD.to_string() }); + var rows = m_db.execute(query, { ArticleStatus.UNREAD }); assert(rows.size == 1 && rows[0].size == 1); return (int)rows[0][0]; } @@ -147,7 +147,7 @@ public class FeedReader.DataBaseReadOnly : GLib.Object { public uint get_marked_total() { var query = "SELECT COUNT(*) FROM articles WHERE marked = ?"; - var rows = m_db.execute(query, { ArticleStatus.MARKED.to_string() }); + var rows = m_db.execute(query, { ArticleStatus.MARKED }); assert(rows.size == 1 && rows[0].size == 1); return (int)rows[0][0]; } diff --git a/src/SQLite.vala b/src/SQLite.vala index b6e77209..2f045766 100644 --- a/src/SQLite.vala +++ b/src/SQLite.vala @@ -74,7 +74,7 @@ public class SQLite : GLib.Object { } } - public Gee.List<Gee.List<Value?>> execute(string query, string?[]? params = null) + public Gee.List<Gee.List<Value?>> execute(string query, Value?[]? params = null) { Sqlite.Statement stmt; int rc = m_db.prepare_v2(query, query.length, out stmt); @@ -91,7 +91,29 @@ public class SQLite : GLib.Object { if(param == null) stmt.bind_null(i); else - stmt.bind_text(i, param); + { + // The order of operations matters here because floats and doubles + // are transformable to int, and anything is transformable to + // string + if(param.holds(typeof(float)) || param.holds(typeof(double))) + { + var as_double = Value(typeof(double)); + param.transform(ref as_double); + stmt.bind_double(i, (double)as_double); + } + else if(Value.type_transformable(param.type(), typeof(int64))) + { + var as_int = Value(typeof(int64)); + param.transform(ref as_int); + stmt.bind_int64(i, (int64)as_int); + } + else + { + var as_string = Value(typeof(string)); + param.transform(ref as_string); + stmt.bind_text(i, (string)as_string); + } + } ++i; } } |