Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Replace QueryBuilder.insert_value_pair with typesafe versions
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanlong committed Nov 24, 2018
1 parent 1c1ddb1 commit c83ee89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 53 deletions.
76 changes: 38 additions & 38 deletions src/DataBaseWriteAccess.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());

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

Expand Down Expand Up @@ -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());

Expand Down
30 changes: 21 additions & 9 deletions src/QueryBuilder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/TestQueryBuilder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
});
Expand Down

0 comments on commit c83ee89

Please sign in to comment.