From: Javier Sancho Date: Thu, 6 Feb 2014 16:06:00 +0000 (+0100) Subject: Field _id in documents is treated as one more field: can be string, list, dictionary... X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=commitdiff_plain;h=2c1872fd5004c8464e146081b61783de47b87e2d Field _id in documents is treated as one more field: can be string, list, dictionary, etc --- diff --git a/collection.py b/collection.py index bbc56ca..1dad488 100644 --- a/collection.py +++ b/collection.py @@ -34,12 +34,6 @@ class Collection(object): def exists(self): return (self.database.exists() and self.table_name in self.database.collection_names()) - def _create_table(self): - fields = [ - {'name': 'id', 'type': 'char', 'size': 512, 'primary': True}, - ] - return self.database.connection._create_table(self.database.db_name, '%s$_id' % self.table_name, fields) - def _create_field(self, field_name): fields = [ {'name': 'id', 'type': 'char', 'size': 512, 'primary': True}, @@ -62,37 +56,33 @@ class Collection(object): if not self.database.db_name in self.database.connection.database_names(): self.database._create_database() if not self.table_name in self.database.collection_names(): - self._create_table() + self._create_field('_id') if not type(doc_or_docs) in (list, tuple): docs = [doc_or_docs] else: docs = doc_or_docs for doc in docs: + doc_id = uuid.uuid4().hex if not '_id' in doc: - doc['_id'] = uuid.uuid4().hex - self._insert_document(doc) + doc['_id'] = doc_id + self._insert_document(doc_id, doc) if type(doc_or_docs) in (list, tuple): return [d['_id'] for d in docs] else: return docs[0]['_id'] - def _insert_document(self, doc): - table_id = '%s$_id' % self.table_name + def _insert_document(self, doc_id, doc): fields = self._get_fields() - coded_id = msgpack.dumps(doc['_id']) - self.database.connection._insert(self.database.db_name, table_id, {'id': coded_id}) for f in doc: - if f == '_id': - continue if not f in fields: self._create_field(f) - table_f = '%s$%s' % (self.table_name, f) values = { - 'id': coded_id, + 'id': doc_id, 'value': msgpack.dumps(doc[f]), } if type(doc[f]) in (int, float): values['number'] = doc[f] + table_f = '%s$%s' % (self.table_name, f) self.database.connection._insert(self.database.db_name, table_f, values) diff --git a/cursor.py b/cursor.py index ef63280..e07f237 100644 --- a/cursor.py +++ b/cursor.py @@ -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 type(v) in (int, float): + field_name = 'number' + field_v = v + else: + field_name = 'value' + field_v = msgpack.dumps(v) if k == '_id': - query['where'].append(((table_id, 'id'), '=', msgpack.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)) + field_q = (table_id, field_name) else: - field_q = self._get_cursor_field(table_id, table_f) - query['where'].append((field_q, '=', msgpack.dumps(v))) + 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)