]> git.jsancho.org Git - mojodb.git/blobdiff - collection.py
Introducing ObjectId
[mojodb.git] / collection.py
index 1dad48846b9077e9e08e3742c5819ca29c494926..b8d51a071af117291ff26a1e9e4f3eb406b3d55e 100644 (file)
@@ -19,9 +19,8 @@
 #
 ##############################################################################
 
-import msgpack
 from cursor import Cursor
-import uuid
+from objectid import ObjectId
 
 class Collection(object):
     def __init__(self, database, table_name):
@@ -37,6 +36,7 @@ class Collection(object):
     def _create_field(self, field_name):
         fields = [
             {'name': 'id', 'type': 'char', 'size': 512, 'primary': True},
+            {'name': 'name', 'type': 'char', 'size': 64, 'primary': True},
             {'name': 'value', 'type': 'text', 'null': False},
             {'name': 'number', 'type': 'float'},
             ]
@@ -63,7 +63,7 @@ class Collection(object):
         else:
             docs = doc_or_docs
         for doc in docs:
-            doc_id = uuid.uuid4().hex
+            doc_id = str(ObjectId())
             if not '_id' in doc:
                 doc['_id'] = doc_id
             self._insert_document(doc_id, doc)
@@ -78,11 +78,23 @@ class Collection(object):
         for f in doc:
             if not f in fields:
                 self._create_field(f)
-            values = {
-                '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)
+            self._insert_field(doc_id, table_f, f, doc[f])
+
+    def _insert_field(self, doc_id, field_table, field_name, field_value):
+        values = {
+            'id': doc_id,
+            'name': field_name,
+            'value': self.database.connection.serializer.dumps(field_value),
+            }
+        if type(field_value) in (int, float):
+            values['number'] = field_value
+
+        self.database.connection._insert(self.database.db_name, field_table, values)
+
+        if type(field_value) in (list, tuple) and not '.' in field_name:
+            for i in xrange(len(field_value)):
+                self._insert_field(doc_id, field_table, "%s..%s" % (field_name, i), field_value[i])
+        elif type(field_value) is dict:
+            for k, v in field_value.iteritems():
+                self._insert_field(doc_id, field_table, "%s.%s" % (field_name, k), v)