Embed query values if more than 100 specified
This commit is contained in:
parent
e8026ef074
commit
13c453d610
1 changed files with 19 additions and 3 deletions
|
@ -12,6 +12,12 @@ class DbCursor:
|
||||||
self.cursor = conn.cursor()
|
self.cursor = conn.cursor()
|
||||||
self.logging = False
|
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):
|
def execute(self, query, params=None):
|
||||||
self.db.last_query_time = time.time()
|
self.db.last_query_time = time.time()
|
||||||
if isinstance(params, dict) and "?" in query: # Make easier select and insert by allowing dict params
|
if isinstance(params, dict) and "?" in query: # Make easier select and insert by allowing dict params
|
||||||
|
@ -22,10 +28,20 @@ class DbCursor:
|
||||||
for key, value in params.items():
|
for key, value in params.items():
|
||||||
if type(value) is list:
|
if type(value) is list:
|
||||||
if key.startswith("not__"):
|
if key.startswith("not__"):
|
||||||
query_wheres.append(key.replace("not__", "") + " NOT IN (" + ",".join(["?"] * len(value)) + ")")
|
field = key.replace("not__", "")
|
||||||
|
operator = "NOT IN"
|
||||||
else:
|
else:
|
||||||
query_wheres.append(key + " IN (" + ",".join(["?"] * len(value)) + ")")
|
field = key
|
||||||
values += value
|
operator = "IN"
|
||||||
|
if len(value) > 100:
|
||||||
|
# Embed values in query to avoid "too many SQL variables" error
|
||||||
|
query_values = ",".join(map(self.quoteValue, value))
|
||||||
|
else:
|
||||||
|
query_values = ",".join(["?"] * len(value))
|
||||||
|
values += value
|
||||||
|
query_wheres.append("%s %s (%s)" %
|
||||||
|
(field, operator, query_values)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if key.startswith("not__"):
|
if key.startswith("not__"):
|
||||||
query_wheres.append(key.replace("not__", "") + " != ?")
|
query_wheres.append(key.replace("not__", "") + " != ?")
|
||||||
|
|
Loading…
Reference in a new issue