From 4a34db8a057d135225e70ede89b767e89f827c8f Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 29 Jan 2014 17:05:10 +0100 Subject: [PATCH] Allow to save dictionaries, lists, tuples, etc, as document ids --- MySQL.py | 4 ++-- collection.py | 17 +++++++++-------- cursor.py | 10 ++++++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/MySQL.py b/MySQL.py index d4604d3..3ee09ae 100644 --- a/MySQL.py +++ b/MySQL.py @@ -92,7 +92,7 @@ class Connection(connection.Connection): elif type(field) is dict: return "(%s)" % self._get_sql_query(db_name, field) else: - return "'%s'" % str(field) + return "'%s'" % str(field).replace("'", "''") def _get_sql_query(self, db_name, query): sql = "SELECT " @@ -125,7 +125,7 @@ class Connection(connection.Connection): for k, v in values.iteritems(): keys.append(k) if type(v) in (str, unicode): - vals.append("'%s'" % v) + vals.append("'%s'" % v.replace("'", "''")) else: vals.append(str(v)) sql = "INSERT INTO `%s`.`%s`(%s) VALUES (%s)" % (db_name, table_name, ",".join(keys), ",".join(vals)) diff --git a/collection.py b/collection.py index 664961a..7af5caa 100644 --- a/collection.py +++ b/collection.py @@ -36,13 +36,13 @@ class Collection(object): def _create_table(self): fields = [ - {'name': 'id', 'type': 'char', 'size': 32, 'primary': True}, + {'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': 32, 'primary': True}, + {'name': 'id', 'type': 'char', 'size': 512, 'primary': True}, {'name': 'value', 'type': 'text', 'null': False}, {'name': 'number', 'type': 'float'}, ] @@ -69,19 +69,20 @@ class Collection(object): else: docs = doc_or_docs for doc in docs: - if not '_id' in doc: - doc['_id'] = uuid.uuid4().hex + if not u'_id' in doc: + doc[u'_id'] = uuid.uuid4().hex self._insert_document(doc) if type(doc_or_docs) in (list, tuple): - return [d['_id'] for d in docs] + return [d[u'_id'] for d in docs] else: - return docs[0]['_id'] + return docs[0][u'_id'] def _insert_document(self, doc): table_id = '%s$_id' % self.table_name fields = self._get_fields() - self.database.connection._insert(self.database.db_name, table_id, {'id': doc['_id']}) + coded_id = cPickle.dumps(doc['_id']) + self.database.connection._insert(self.database.db_name, table_id, {'id': coded_id}) for f in doc: if f == '_id': continue @@ -89,7 +90,7 @@ class Collection(object): self._create_field(f) table_f = '%s$%s' % (self.table_name, f) values = { - 'id': doc['_id'], + 'id': coded_id, 'value': cPickle.dumps(doc[f]), } if type(doc[f]) in (int, float): diff --git a/cursor.py b/cursor.py index 202c6c7..ca9e2a4 100644 --- a/cursor.py +++ b/cursor.py @@ -89,7 +89,9 @@ 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): + 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)) else: @@ -115,9 +117,9 @@ class Cursor(object): raise StopIteration else: document = {} - if '_id' in self.fields: - document['_id'] = res[0] - fields_without_id = filter(lambda x: x != '_id', self.fields) + if u'_id' in self.fields: + document[u'_id'] = cPickle.loads(res[0]) + fields_without_id = filter(lambda x: x != u'_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]) -- 2.39.5