#
##############################################################################
+import cPickle
+import uuid
+
class Connection(object):
def __init__(self, *args, **kwargs):
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
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
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):
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)
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)]
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
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:
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:
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.',
- }