diff --git a/cli/util.py b/cli/util.py index 8705d36..eff1b63 100644 --- a/cli/util.py +++ b/cli/util.py @@ -1,12 +1,32 @@ import os import tempfile +db_encoding = 'utf8' + +def value_to_db(value): + if type(value) == unicode: + return value.encode(db_encoding) + return value + +def value_from_db(value): + if type(value) == str: + return unicode(value, db_encoding) + return value + def execute_query(cursor, query, bindings): for (key, val) in bindings.items(): if val == None: query = query.replace('%(' + key + ')d', 'NULL') + bindings = map_dict(value_to_db, bindings) cursor.execute(query, bindings) +def fetchone(cursor): + return map(value_from_db, cursor.fetchone()) + +def fetchall(cursor): + return map(lambda row: map(value_from_db, row), + cursor.fetchall()) + def make_result_dict(cursor, row): d = {} for i in xrange(len(row)): @@ -14,14 +34,14 @@ def make_result_dict(cursor, row): return d def fetchone_dict(cursor): - row = cursor.fetchone() + row = fetchone(cursor) if row != None: return make_result_dict(cursor, row) return None def fetchall_dict(cursor): return map(lambda r: make_result_dict(cursor, r), - cursor.fetchall()) + fetchall(cursor)) def first(lst): return lst[0] @@ -56,6 +76,12 @@ def mapcond(fun, predicate, lst): return x return map(mapfun, lst) +def map_dict(fun, d): + res = {} + for key in d: + res[key] = fun(d[key]) + return res + def maptup(fun, lst): return tuple(map(fun, lst))