]> git.jsancho.org Git - mojodb.git/blobdiff - cursor.py
Field _id in documents is treated as one more field: can be string, list, dictionary...
[mojodb.git] / cursor.py
index ca9e2a4cc0f902dd8a3261b854ba35d64207b97e..e07f237e792f0da9ab0005ab2a3bd327faea930a 100644 (file)
--- a/cursor.py
+++ b/cursor.py
@@ -19,7 +19,7 @@
 #
 ##############################################################################
 
-import cPickle
+import msgpack
 
 class Cursor(object):
     def __init__(self, collection, spec=None, fields=None, **kwargs):
@@ -77,7 +77,7 @@ class Cursor(object):
         query = {}
         table_id = '%s$_id' % self.collection.table_name
 
-        query['select'] = [(table_id, 'id')]
+        query['select'] = [(table_id, 'value')]
         for f in filter(lambda x: x != '_id', self.fields):
             table_f = '%s$%s' % (self.collection.table_name, f)
             q = self._get_cursor_field(table_id, table_f)
@@ -89,14 +89,17 @@ class Cursor(object):
             query['where'] = []
             for k, v in self.spec.iteritems():
                 table_f = '%s$%s' % (self.collection.table_name, k)
-                if k == u'_id':
-                    query['where'].append(((table_id, 'id'), '=', cPickle.dumps(v)))
-                elif type(v) in (int, float):
-                    field_q = self._get_cursor_field(table_id, table_f, field_name='number')
-                    query['where'].append((field_q, '=', v))
+                if type(v) in (int, float):
+                    field_name = 'number'
+                    field_v = v
                 else:
-                    field_q = self._get_cursor_field(table_id, table_f)
-                    query['where'].append((field_q, '=', cPickle.dumps(v)))
+                    field_name = 'value'
+                    field_v = msgpack.dumps(v)
+                if k == '_id':
+                    field_q = (table_id, field_name)
+                else:
+                    field_q = self._get_cursor_field(table_id, table_f, field_name=field_name)
+                query['where'].append((field_q, '=', field_v))
 
         return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
 
@@ -117,12 +120,12 @@ class Cursor(object):
                 raise StopIteration
             else:
                 document = {}
-                if u'_id' in self.fields:
-                    document[u'_id'] = cPickle.loads(res[0])
-                fields_without_id = filter(lambda x: x != u'_id', self.fields)
+                if '_id' in self.fields:
+                    document['_id'] = msgpack.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]] = cPickle.loads(res[i + 1])
+                        document[fields_without_id[i]] = msgpack.loads(res[i + 1])
                 return document
         else:
             return None