summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2014-12-24 17:52:04 +0000
committerMichaël Ball <michael.ball@gmail.com>2014-12-24 17:52:04 +0000
commit601198884d58c0f3825e7108a9adb4dc4353ff5c (patch)
tree61740d70677e3da94ded71d8a9a6ec741dd5b24c /common
parent765a2d306b1d64480933999af96a7df6e9053934 (diff)
Better searching methods
Diffstat (limited to 'common')
-rw-r--r--common/utils.py32
1 files changed, 16 insertions, 16 deletions
diff --git a/common/utils.py b/common/utils.py
index 484179a..288673e 100644
--- a/common/utils.py
+++ b/common/utils.py
@@ -1,23 +1,25 @@
-def make_where_clause(params):
- """Create a where clause for each key-value pair in a dict, joined
- by AND.
-
- Parameters
- ----------
- params : dict
- A dict of keys and values
+def make_where_clause(params, join_operator="AND"):
+ """Create a where clause from the param.
+
+ Args:
+ params : A dict where key is the column and the value a comparison
+ operator
+ join_operator: string to join comparisons. Should be "AND" or "OR"
"""
where_items = []
where_clause = None
try:
- for key in params.keys():
- where_items.append("%s=:%s" % (key, key))
+ for (column, operator) in params.items():
+ condition_subphrase = " ".join(("%s", operator, ":%s"))
+ where_items.append(condition_subphrase % (column, column))
where_statement = None
if len(where_items) > 1:
- where_statement = " AND ".join(where_items)
+ # surround join operator with spaces
+ join_string = "".join((" ", join_operator, " "))
+ where_statement = join_string.join(where_items)
else:
where_statement = where_items[0]
@@ -29,12 +31,10 @@ def make_where_clause(params):
def update_clause_from_dict(data):
- """Create an update clause from a dictionary
+ """Create an update clause
- Parameters
- __________
- data: dict
- A dict of the new value and the column name as key
+ Args:
+ data: A dict of the new value and the column name as key
"""
update_items = []