From 78dffed58cc217f429a98f16cec881636f2cc444 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 22 Jan 2014 15:41:22 +0100 Subject: [PATCH] Basic spec searching in find function --- MySQL.py | 2 +- mojo.py | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/MySQL.py b/MySQL.py index 76f7312..a3046e6 100644 --- a/MySQL.py +++ b/MySQL.py @@ -49,7 +49,7 @@ class Connection(mojo.Connection): elif type(field) is dict: return "(%s)" % self._get_sql_query(db_name, field) else: - return str(field) + return "'%s'" % str(field) def _get_sql_query(self, db_name, query): sql = "SELECT " diff --git a/mojo.py b/mojo.py index aed9ec6..16adc44 100644 --- 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.', - } -- 2.39.2