Skip to content

Commit

Permalink
Rev3864, Fix newsfeed sql query with many parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcutme committed Apr 29, 2019
1 parent 8dd3a84 commit 9b27441
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
11 changes: 6 additions & 5 deletions plugins/Newsfeed/NewsfeedPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from Plugin import PluginManager
from Db import DbQuery
from Debug import Debug
from util import helper


@PluginManager.registerTo("UiWebsocket")
Expand Down Expand Up @@ -66,14 +67,14 @@ def actionFeedQuery(self, to, limit=10, day_limit=3):
query = " UNION ".join(query_parts)

if ":params" in query:
query = query.replace(":params", ",".join(["?"] * len(params)))
res = site.storage.query(query + " ORDER BY date_added DESC LIMIT %s" % limit, params * query_raw.count(":params"))
else:
res = site.storage.query(query + " ORDER BY date_added DESC LIMIT %s" % limit)
query_params = map(helper.sqlquote, params)
query = query.replace(":params", ",".join(query_params))

res = site.storage.query(query + " ORDER BY date_added DESC LIMIT %s" % limit)

except Exception as err: # Log error
self.log.error("%s feed query %s error: %s" % (address, name, Debug.formatException(err)))
stats.append({"site": site.address, "feed_name": name, "error": str(err), "query": query})
stats.append({"site": site.address, "feed_name": name, "error": str(err)})
continue

for row in res:
Expand Down
2 changes: 1 addition & 1 deletion src/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Config(object):

def __init__(self, argv):
self.version = "0.6.5"
self.rev = 3863
self.rev = 3864
self.argv = argv
self.action = None
self.pending_changes = {}
Expand Down
10 changes: 3 additions & 7 deletions src/Db/DbCursor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
import re
from util import helper


# Special sqlite cursor

Expand All @@ -12,12 +14,6 @@ def __init__(self, conn, db):
self.cursor = conn.cursor()
self.logging = False

def quoteValue(self, value):
if type(value) is int:
return str(value)
else:
return "'%s'" % value.replace("'", "''")

def execute(self, query, params=None):
self.db.last_query_time = time.time()
if isinstance(params, dict) and "?" in query: # Make easier select and insert by allowing dict params
Expand All @@ -35,7 +31,7 @@ def execute(self, query, params=None):
operator = "IN"
if len(value) > 100:
# Embed values in query to avoid "too many SQL variables" error
query_values = ",".join(map(self.quoteValue, value))
query_values = ",".join(map(helper.sqlquote, value))
else:
query_values = ",".join(["?"] * len(value))
values += value
Expand Down
7 changes: 7 additions & 0 deletions src/util/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def getFreeSpace():
return free_space


def sqlquote(value):
if type(value) is int:
return str(value)
else:
return "'%s'" % value.replace("'", "''")


def shellquote(*args):
if len(args) == 1:
return '"%s"' % args[0].replace('"', "")
Expand Down

0 comments on commit 9b27441

Please sign in to comment.