]> git.jsancho.org Git - mojodb.git/blobdiff - mojo.py
Basic insertion of documents, using cPickle for fields codification
[mojodb.git] / mojo.py
diff --git a/mojo.py b/mojo.py
index aed9ec634f50fa880960b9f481b6c6b6623e287f..f3aa74456056a7d0c84eb8cb076bb502dc05e444 100644 (file)
--- a/mojo.py
+++ b/mojo.py
@@ -19,6 +19,9 @@
 #
 ##############################################################################
 
+import cPickle
+import uuid
+
 
 class Connection(object):
     def __init__(self, *args, **kwargs):
@@ -37,13 +40,19 @@ class Connection(object):
         return []
 
     def database_names(self):
-        return [unicode(x) for x in self._get_databases()]
+        try:
+            return [unicode(x) for x in self._get_databases()]
+        except:
+            return []
 
     def _get_tables(self, db_name):
         return []
 
     def collection_names(self, db_name):
-        return list(set([unicode(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
+        try:
+            return list(set([unicode(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
+        except:
+            return []
 
     def _count_rows(self, db_name, table_name):
         return 0
@@ -54,6 +63,13 @@ class Connection(object):
         except:
             return 0
 
+    def _create_database(self, db_name):
+        return None
+
+    def _create_table(self, db_name, table_name, fields):
+        # [{'name': 'id', 'type': 'char', 'size': 20, 'primary': True}]
+        return None
+
     def _get_cursor(self, db_name, query):
         # {'select': [('t1$_id', 'id'), {'select': [('t1$c1', 'value')], 'from': ['t1$c1'], 'where': [(('t1$c1', 'id'), '=', ('t1$_id', 'id'))]}], 'from': ['t1$_id']}
         return None
@@ -61,6 +77,15 @@ class Connection(object):
     def _next(self, cursor):
         return None
 
+    def _insert(self, db_name, table_name, values):
+        return None
+
+    def commit(self):
+        pass
+
+    def rollback(self):
+        pass
+
 
 class Database(object):
     def __init__(self, connection, db_name):
@@ -76,6 +101,12 @@ class Database(object):
     def __repr__(self):
         return "Database(%r, %r)" % (self.connection, self.db_name)
 
+    def _create_database(self):
+        return self.connection._create_database(self.db_name)
+
+    def exists(self):
+        return (self.db_name in self.connection.database_names())
+
     def collection_names(self):
         return self.connection.collection_names(self.db_name)
 
@@ -88,6 +119,23 @@ class Collection(object):
     def __repr__(self):
         return "Collection(%r, %r)" % (self.database, self.table_name)
 
+    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': 32, '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': 'value', 'type': 'text', 'null': False},
+            {'name': 'number', 'type': 'float'},
+            ]
+        return self.database.connection._create_table(self.database.db_name, '%s$%s' % (self.table_name, field_name), fields)
+
     def _get_fields(self):
         tables = self.database.connection._get_tables(self.database.db_name)
         return [unicode(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)]
@@ -98,13 +146,58 @@ class Collection(object):
     def find(self, *args, **kwargs):
         return Cursor(self, *args, **kwargs)
 
+    def insert(self, doc_or_docs):
+        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()
+
+        if not type(doc_or_docs) in (list, tuple):
+            docs = [doc_or_docs]
+        else:
+            docs = doc_or_docs
+        for doc in docs:
+            if not '_id' in doc:
+                doc['_id'] = uuid.uuid4().hex
+            self._insert_document(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
+        fields = self._get_fields()
+        self.database.connection._insert(self.database.db_name, table_id, {'id': doc['_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': doc['_id'],
+                'value': cPickle.dumps(doc[f]),
+                }
+            if type(doc[f]) in (int, float):
+                values['number'] = doc[f]
+            self.database.connection._insert(self.database.db_name, table_f, values)
+
 
 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)
-        self.cursor = self._get_cursor()
+        if self.collection.exists():
+            self.fields = self._get_fields(fields)
+            self.cursor = self._get_cursor()
+        else:
+            self.fields = None
+            self.cursor = None
 
     def __iter__(self):
         return self
@@ -126,12 +219,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,17 +244,31 @@ 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 is None:
+            raise StopIteration
+
         if self.cursor:
             res = self.collection.database.connection._next(self.cursor)
             if res is None:
@@ -173,12 +280,7 @@ class Cursor(object):
                 fields_without_id = filter(lambda x: x != '_id', self.fields)
                 for i in xrange(len(fields_without_id)):
                     if not res[i + 1] is None:
-                        document[fields_without_id[i]] = res[i + 1]
+                        document[fields_without_id[i]] = cPickle.loads(res[i + 1])
                 return document
         else:
             return None
-
-
-Errors = {
-    'mix_fields_error': 'You cannot currently mix including and excluding fields. Contact us if this is an issue.',
-    }