diff --git a/src/DataBaseWriteAccess.vala b/src/DataBaseWriteAccess.vala index a7d49adbf..c6ee214f1 100644 --- a/src/DataBaseWriteAccess.vala +++ b/src/DataBaseWriteAccess.vala @@ -134,13 +134,13 @@ public class FeedReader.DataBase : DataBaseReadOnly { m_db.simple_query("BEGIN TRANSACTION"); var query = new QueryBuilder(QueryType.INSERT_OR_REPLACE, "main.feeds"); - query.insert_value_pair("feed_id", "$FEEDID"); - query.insert_value_pair("name", "$FEEDNAME"); - query.insert_value_pair("url", "$FEEDURL"); - query.insert_value_pair("category_id", "$CATID"); - query.insert_value_pair("subscribed", "1"); - query.insert_value_pair("xmlURL", "$XMLURL"); - query.insert_value_pair("iconURL", "$ICONURL"); + query.insert_param("feed_id", "$FEEDID"); + query.insert_param("name", "$FEEDNAME"); + query.insert_param("url", "$FEEDURL"); + query.insert_param("category_id", "$CATID"); + query.insert_int("subscribed", 1); + query.insert_param("xmlURL", "$XMLURL"); + query.insert_param("iconURL", "$ICONURL"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -185,10 +185,10 @@ public class FeedReader.DataBase : DataBaseReadOnly { m_db.simple_query("BEGIN TRANSACTION"); var query = new QueryBuilder(QueryType.INSERT_OR_IGNORE, "main.tags"); - query.insert_value_pair("tagID", "$TAGID"); - query.insert_value_pair("title", "$LABEL"); - query.insert_value_pair("\"exists\"", "1"); - query.insert_value_pair("color", "$COLOR"); + query.insert_param("tagID", "$TAGID"); + query.insert_param("title", "$LABEL"); + query.insert_int("\"exists\"", 1); + query.insert_param("color", "$COLOR"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -260,12 +260,12 @@ public class FeedReader.DataBase : DataBaseReadOnly { m_db.simple_query("BEGIN TRANSACTION"); var query = new QueryBuilder(QueryType.INSERT_OR_REPLACE, "main.categories"); - query.insert_value_pair("categorieID", "$CATID"); - query.insert_value_pair("title", "$FEEDNAME"); - query.insert_value_pair("orderID", "$ORDERID"); - query.insert_value_pair("\"exists\"", "1"); - query.insert_value_pair("Parent", "$PARENT"); - query.insert_value_pair("Level", "$LEVEL"); + query.insert_param("categorieID", "$CATID"); + query.insert_param("title", "$FEEDNAME"); + query.insert_param("orderID", "$ORDERID"); + query.insert_int("\"exists\"", 1); + query.insert_param("Parent", "$PARENT"); + query.insert_param("Level", "$LEVEL"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -420,19 +420,19 @@ public class FeedReader.DataBase : DataBaseReadOnly { m_db.simple_query("BEGIN TRANSACTION"); var query = new QueryBuilder(QueryType.INSERT_OR_IGNORE, "main.articles"); - query.insert_value_pair("articleID", "$ARTICLEID"); - query.insert_value_pair("feedID", "$FEEDID"); - query.insert_value_pair("title", "$TITLE"); - query.insert_value_pair("author", "$AUTHOR"); - query.insert_value_pair("url", "$URL"); - query.insert_value_pair("html", "$HTML"); - query.insert_value_pair("preview", "$PREVIEW"); - query.insert_value_pair("unread", "$UNREAD"); - query.insert_value_pair("marked", "$MARKED"); - query.insert_value_pair("date", "$DATE"); - query.insert_value_pair("guidHash", "$GUIDHASH"); - query.insert_value_pair("lastModified", "$LASTMODIFIED"); - query.insert_value_pair("contentFetched", "0"); + query.insert_param("articleID", "$ARTICLEID"); + query.insert_param("feedID", "$FEEDID"); + query.insert_param("title", "$TITLE"); + query.insert_param("author", "$AUTHOR"); + query.insert_param("url", "$URL"); + query.insert_param("html", "$HTML"); + query.insert_param("preview", "$PREVIEW"); + query.insert_param("unread", "$UNREAD"); + query.insert_param("marked", "$MARKED"); + query.insert_param("date", "$DATE"); + query.insert_param("guidHash", "$GUIDHASH"); + query.insert_param("lastModified", "$LASTMODIFIED"); + query.insert_int("contentFetched", 0); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -502,8 +502,8 @@ public class FeedReader.DataBase : DataBaseReadOnly { private void write_taggings(Article article) { var query = new QueryBuilder(QueryType.INSERT_OR_REPLACE, "main.taggings"); - query.insert_value_pair("articleID", "$ARTICLEID"); - query.insert_value_pair("tagID", "$TAGID"); + query.insert_param("articleID", "$ARTICLEID"); + query.insert_param("tagID", "$TAGID"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -526,9 +526,9 @@ public class FeedReader.DataBase : DataBaseReadOnly { private void write_enclosures(Article article) { var query = new QueryBuilder(QueryType.INSERT_OR_REPLACE, "main.Enclosures"); - query.insert_value_pair("articleID", "$ARTICLEID"); - query.insert_value_pair("url", "$URL"); - query.insert_value_pair("type", "$TYPE"); + query.insert_param("articleID", "$ARTICLEID"); + query.insert_param("url", "$URL"); + query.insert_param("type", "$TYPE"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); @@ -715,9 +715,9 @@ public class FeedReader.DataBase : DataBaseReadOnly { m_db.simple_query("BEGIN TRANSACTION"); var query = new QueryBuilder(QueryType.INSERT_OR_IGNORE, "main.CachedActions"); - query.insert_value_pair("action", "$ACTION"); - query.insert_value_pair("id", "$ID"); - query.insert_value_pair("argument", "$ARGUMENT"); + query.insert_param("action", "$ACTION"); + query.insert_param("id", "$ID"); + query.insert_param("argument", "$ARGUMENT"); Sqlite.Statement stmt = m_db.prepare(query.to_string()); diff --git a/src/QueryBuilder.vala b/src/QueryBuilder.vala index a798b4be1..0b6b9a81d 100644 --- a/src/QueryBuilder.vala +++ b/src/QueryBuilder.vala @@ -39,7 +39,7 @@ public class FeedReader.QueryBuilder : GLib.Object { m_table = table; } - public void insert_value_pair(string field, string value) + private void insert_value_pair(string field, string value) requires (m_type == QueryType.INSERT || m_type == QueryType.INSERT_OR_IGNORE || m_type == QueryType.INSERT_OR_REPLACE) @@ -48,32 +48,44 @@ public class FeedReader.QueryBuilder : GLib.Object { m_values.add(value); } + public void insert_param(string field, string value) + requires (value.has_prefix("$") && !value.contains("'")) + { + insert_value_pair(field, value); + } + + public void insert_int(string field, int64 value) + { + insert_value_pair(field, value.to_string()); + } + public void select_field(string field) requires (m_type == QueryType.SELECT) { m_fields.add(field); } - public void update_param(string field, string value) + private void update(string field, string value) requires (m_type == QueryType.UPDATE) - requires (value.has_prefix("$") && !value.contains("'")) { m_fields.add(field); m_values.add(value); } + public void update_param(string field, string value) + requires (value.has_prefix("$") && !value.contains("'")) + { + update(field, value); + } + public void update_string(string field, string value) - requires (m_type == QueryType.UPDATE) { - m_fields.add(field); - m_values.add(SQLite.quote_string(value)); + update(field, SQLite.quote_string(value)); } public void update_int(string field, int64 value) - requires (m_type == QueryType.UPDATE) { - m_fields.add(field); - m_values.add(value.to_string()); + update(field, value.to_string()); } private void where_equal(string field, string safe_value) diff --git a/src/TestQueryBuilder.vala b/src/TestQueryBuilder.vala index a7bf7eee3..de91d5ab0 100644 --- a/src/TestQueryBuilder.vala +++ b/src/TestQueryBuilder.vala @@ -14,24 +14,24 @@ void main(string[] args) Test.add_data_func("/querybuilder/simple-insert", () => { var query = new QueryBuilder(QueryType.INSERT, "example"); - query.insert_value_pair("asdf", "$VALUE"); - query.insert_value_pair("othercol", "5"); + query.insert_param("asdf", "$VALUE"); + query.insert_int("othercol", 5); assert(query.to_string() == "INSERT INTO example (asdf, othercol) VALUES ($VALUE, 5)"); }); Test.add_data_func("/querybuilder/simple-insert-or-ignore", () => { var query = new QueryBuilder(QueryType.INSERT_OR_IGNORE, "example"); - query.insert_value_pair("asdf", "$VALUE"); - query.insert_value_pair("othercol", "5"); + query.insert_param("asdf", "$VALUE"); + query.insert_int("othercol", 5); assert(query.to_string() == "INSERT OR IGNORE INTO example (asdf, othercol) VALUES ($VALUE, 5)"); }); Test.add_data_func("/querybuilder/simple-insert-or-replace", () => { var query = new QueryBuilder(QueryType.INSERT_OR_REPLACE, "example"); - query.insert_value_pair("asdf", "$VALUE"); - query.insert_value_pair("othercol", "5"); + query.insert_param("asdf", "$VALUE"); + query.insert_int("othercol", 5); assert(query.to_string() == "INSERT OR REPLACE INTO example (asdf, othercol) VALUES ($VALUE, 5)"); });