]> git.jsancho.org Git - mojodb.git/blobdiff - mojo.py
Basic spec searching in find function
[mojodb.git] / mojo.py
diff --git a/mojo.py b/mojo.py
index aed9ec634f50fa880960b9f481b6c6b6623e287f..16adc442ea6acdea1bc7bfb4976b7fb51ea1a978 100644 (file)
--- a/mojo.py
+++ b/mojo.py
@@ -101,6 +101,9 @@ class Collection(object):
 
 class Cursor(object):
     def __init__(self, collection, spec=None, fields=None, **kwargs):
+        if spec and not type(spec) is dict:
+            raise Exception("spec must be an instance of dict")
+
         self.collection = collection
         self.spec = spec
         self.fields = self._get_fields(fields)
@@ -126,12 +129,12 @@ class Cursor(object):
                     if first:
                         res_fields.add(f[0])
                     else:
-                        raise Exception(Errors['mix_fields_error'])
+                        raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
                 elif not f[1]:
                     if not first:
                         res_fields.discard(f[0])
                     else:
-                        raise Exception(Errors['mix_fields_error'])
+                        raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
             if '_id' in fields and not fields['_id']:
                 res_fields.discard('_id')
             else:
@@ -151,16 +154,27 @@ class Cursor(object):
         query['select'] = [(table_id, 'id')]
         for f in filter(lambda x: x != '_id', self.fields):
             table_f = '%s$%s' % (self.collection.table_name, f)
-            q = {}
-            q['select'] = [(table_f, 'value')]
-            q['from'] = [table_f]
-            q['where'] = [((table_f, 'id'), '=', (table_id, 'id'))]
+            q = self._get_cursor_field(table_id, table_f)
             query['select'].append(q)
 
         query['from'] = [table_id]
 
+        if self.spec:
+            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))
+
         return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
 
+    def _get_cursor_field(self, table_id, table_field):
+        return {
+            'select': [(table_field, 'value')],
+            'from': [table_field],
+            'where': [((table_field, 'id'), '=', (table_id, 'id'))],
+            }
+
     def next(self):
         if self.cursor:
             res = self.collection.database.connection._next(self.cursor)
@@ -177,8 +191,3 @@ class Cursor(object):
                 return document
         else:
             return None
-
-
-Errors = {
-    'mix_fields_error': 'You cannot currently mix including and excluding fields. Contact us if this is an issue.',
-    }