]> git.jsancho.org Git - mojodb.git/commitdiff
Custom serializer in connection object; default is msgpack
authorJavier Sancho <jsf@jsancho.org>
Wed, 12 Feb 2014 15:45:05 +0000 (16:45 +0100)
committerJavier Sancho <jsf@jsancho.org>
Wed, 12 Feb 2014 15:45:05 +0000 (16:45 +0100)
MySQL.py
collection.py
connection.py
cursor.py

index e983a670fa957f2d082f21b6c41a729512a05716..98365b4d50d22629584ae39db071cd33cf8e7ae2 100644 (file)
--- 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:
index 3c93b8b3192f909f1d31b01100fa5072e8fb7e01..e0f00026ba5dea8c411cfc4cd84782bd78df1582 100644 (file)
@@ -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
index add83e3b116c881979c1eaf0ec4369f2dc441975..7623047ca1bd0634081a1f0ba8c696e2af85e140 100644 (file)
@@ -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)
index 6246872c35ab3c66c2d1d5c32c949deb22565864..eec8b65e71d865e7b0bc6fd1112d11cd4d4f06f1 100644 (file)
--- 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