X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=blobdiff_plain;f=collection.py;fp=collection.py;h=664961a040680aebadf96e4e3724e1e765713b9c;hp=0000000000000000000000000000000000000000;hb=792f961fb96bb8533e540970c54f43b958d77296;hpb=f63673b9ed08886c9ac1582f94f0e63103985497 diff --git a/collection.py b/collection.py new file mode 100644 index 0000000..664961a --- /dev/null +++ b/collection.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# mojo, a Python library for implementing document based databases +# Copyright (C) 2013-2014 by Javier Sancho Fernandez +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## + +import cPickle +from cursor import Cursor +import uuid + +class Collection(object): + def __init__(self, database, table_name): + self.database = database + self.table_name = unicode(table_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 count(self): + return self.database.connection._count(self.database.db_name, self.table_name) + + 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)