]> 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 73a0c1361ace1aaba045091ce9599f5bbddd742e..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,23 @@ class Cursor(object):
             query['where'] = []
             for k, v in self.spec.iteritems():
                 table_f = '%s$%s' % (self.collection.table_name, k)
-                field_q = self._get_cursor_field(table_id, table_f)
-                query['where'].append((field_q, '=', v))
+                if type(v) in (int, float):
+                    field_name = 'number'
+                    field_v = v
+                else:
+                    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)
 
-    def _get_cursor_field(self, table_id, table_field):
+    def _get_cursor_field(self, table_id, table_field, field_name='value'):
         return {
-            'select': [(table_field, 'value')],
+            'select': [(table_field, field_name)],
             'from': [table_field],
             'where': [((table_field, 'id'), '=', (table_id, 'id'))],
             }
@@ -112,11 +121,11 @@ class Cursor(object):
             else:
                 document = {}
                 if '_id' in self.fields:
-                    document['_id'] = res[0]
+                    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