X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=blobdiff_plain;f=database.py;fp=database.py;h=1b378bf468c60cb0d7645261e57ce2c2ee467a22;hp=0000000000000000000000000000000000000000;hb=792f961fb96bb8533e540970c54f43b958d77296;hpb=f63673b9ed08886c9ac1582f94f0e63103985497 diff --git a/database.py b/database.py new file mode 100644 index 0000000..1b378bf --- /dev/null +++ b/database.py @@ -0,0 +1,45 @@ +# -*- 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 . +# +############################################################################## + +from collection import Collection + +class Database(object): + def __init__(self, connection, db_name): + self.connection = connection + self.db_name = unicode(db_name) + + def __getattr__(self, table_name): + return Collection(self, table_name) + + def __getitem__(self, *args, **kwargs): + return self.__getattr__(*args, **kwargs) + + 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)