summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2014-12-13 16:41:35 +0000
committerMichaël Ball <michael.ball@gmail.com>2014-12-13 16:41:35 +0000
commitcd630834a985be4b39a673d022e180de3ff20517 (patch)
tree0cfdcba2570ca7925e3777104448e83501fa328c /common
parent86e68485b0848e25ef6d5b579c7d80f286d094a1 (diff)
Initial commit
Diffstat (limited to 'common')
-rw-r--r--common/__init__.py0
-rw-r--r--common/utils.py57
2 files changed, 57 insertions, 0 deletions
diff --git a/common/__init__.py b/common/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/__init__.py
diff --git a/common/utils.py b/common/utils.py
new file mode 100644
index 0000000..484179a
--- /dev/null
+++ b/common/utils.py
@@ -0,0 +1,57 @@
+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
+ """
+
+ where_items = []
+ where_clause = None
+
+ try:
+ for key in params.keys():
+ where_items.append("%s=:%s" % (key, key))
+
+ where_statement = None
+ if len(where_items) > 1:
+ where_statement = " AND ".join(where_items)
+ else:
+ where_statement = where_items[0]
+
+ where_clause = " ".join(("WHERE", where_statement))
+ except AttributeError:
+ pass
+
+ return where_clause
+
+
+def update_clause_from_dict(data):
+ """Create an update clause from a dictionary
+
+ Parameters
+ __________
+ data: dict
+ A dict of the new value and the column name as key
+ """
+
+ update_items = []
+ set_statement = None
+ update_clause = None
+
+ try:
+ for key in data.keys():
+ update_items.append("%s = :%s", (key, key))
+
+ if len(update_items) > 1:
+ update_clause = ", ".join(update_items)
+ else:
+ update_clause = update_items[0]
+
+ set_statement = " ".join(("SET", update_clause))
+ except AttributeError:
+ pass
+
+ return set_statement