From: Javier Sancho Date: Wed, 12 Feb 2014 15:45:05 +0000 (+0100) Subject: Custom serializer in connection object; default is msgpack X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=commitdiff_plain;h=8893f5128b8e8790f186c8bf354a3bb721d69bcb Custom serializer in connection object; default is msgpack --- diff --git a/MySQL.py b/MySQL.py index e983a67..98365b4 100644 --- a/MySQL.py +++ b/MySQL.py @@ -84,9 +84,10 @@ class Connection(connection.Connection): Constraint = Constraint Literal = Literal - def __init__(self, *args, **kwargs): - self._db_con = MySQLdb.connect(*args, **kwargs) - self._db_con_autocommit = MySQLdb.connect(*args, **kwargs) + def __init__(self, host="localhost", user=None, passwd=None, *args, **kwargs): + self._db_con = MySQLdb.connect(host=host, user=user, passwd=passwd) + self._db_con_autocommit = MySQLdb.connect(host=host, user=user, passwd=passwd) + super(Connection, self).__init__(*args, **kwargs) def query(self, sql, db=None): if db is None: diff --git a/collection.py b/collection.py index 3c93b8b..e0f0002 100644 --- a/collection.py +++ b/collection.py @@ -19,7 +19,6 @@ # ############################################################################## -import msgpack from cursor import Cursor import uuid @@ -86,7 +85,7 @@ class Collection(object): values = { 'id': doc_id, 'name': field_name, - 'value': msgpack.dumps(field_value), + 'value': self.database.connection.serializer.dumps(field_value), } if type(field_value) in (int, float): values['number'] = field_value diff --git a/connection.py b/connection.py index add83e3..7623047 100644 --- a/connection.py +++ b/connection.py @@ -29,8 +29,12 @@ class Connection(object): Constraint = dbutils.Constraint Literal = dbutils.Literal - def __init__(self, *args, **kwargs): - self._db_con = None + def __init__(self, serializer=None, *args, **kwargs): + if serializer is None: + import msgpack + self.serializer = msgpack + else: + self.serializer = serializer def __getattr__(self, db_name): return Database(self, db_name) diff --git a/cursor.py b/cursor.py index 6246872..eec8b65 100644 --- a/cursor.py +++ b/cursor.py @@ -19,7 +19,6 @@ # ############################################################################## -import msgpack class Cursor(object): def __init__(self, collection, spec=None, fields=None, **kwargs): @@ -32,6 +31,8 @@ class Cursor(object): self.Constraint = collection.database.connection.Constraint self.Literal = collection.database.connection.Literal + self.serializer = collection.database.connection.serializer + self.collection = collection self.spec = spec if self.collection.exists(): @@ -116,7 +117,7 @@ class Cursor(object): field_type = 'number' else: field_type = 'value' - field_value = msgpack.dumps(field_value) + field_value = self.serializer.dumps(field_value) fields = [self.Field(table_field, 'id')] tables = [table_field] @@ -139,11 +140,11 @@ class Cursor(object): else: document = {} if '_id' in self.fields: - document['_id'] = msgpack.loads(res[0]) + document['_id'] = self.serializer.loads(res[0]) fields_without_id = filter(lambda x: x != '_id', self.fields) for i in xrange(len(fields_without_id)): if not res[i + 1] is None: - document[fields_without_id[i]] = msgpack.loads(res[i + 1]) + document[fields_without_id[i]] = self.serializer.loads(res[i + 1]) return document else: return None