1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # mojo, a Python library for implementing document based databases
5 # Copyright (C) 2013-2014 by Javier Sancho Fernandez <jsf at jsancho dot org>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 class Connection(object):
24 def __init__(self, *args, **kwargs):
27 def __getattr__(self, db_name):
28 return Database(self, db_name)
30 def __getitem__(self, *args, **kwargs):
31 return self.__getattr__(*args, **kwargs)
34 return "Connection(%s)" % self._db_con
36 def _get_databases(self):
39 def database_names(self):
40 return [unicode(x) for x in self._get_databases()]
42 def _get_tables(self, db_name):
45 def collection_names(self, db_name):
46 return list(set([unicode(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
48 def _count_rows(self, db_name, table_name):
51 def _count(self, db_name, table_name):
53 return self._count_rows(db_name, table_name + '$_id')
57 def _get_cursor(self, db_name, query):
58 # {'select': [('t1$_id', 'id'), {'select': [('t1$c1', 'value')], 'from': ['t1$c1'], 'where': [(('t1$c1', 'id'), '=', ('t1$_id', 'id'))]}], 'from': ['t1$_id']}
61 def _next(self, cursor):
65 class Database(object):
66 def __init__(self, connection, db_name):
67 self.connection = connection
68 self.db_name = unicode(db_name)
70 def __getattr__(self, table_name):
71 return Collection(self, table_name)
73 def __getitem__(self, *args, **kwargs):
74 return self.__getattr__(*args, **kwargs)
77 return "Database(%r, %r)" % (self.connection, self.db_name)
79 def collection_names(self):
80 return self.connection.collection_names(self.db_name)
83 class Collection(object):
84 def __init__(self, database, table_name):
85 self.database = database
86 self.table_name = unicode(table_name)
89 return "Collection(%r, %r)" % (self.database, self.table_name)
91 def _get_fields(self):
92 tables = self.database.connection._get_tables(self.database.db_name)
93 return [unicode(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)]
96 return self.database.connection._count(self.database.db_name, self.table_name)
98 def find(self, *args, **kwargs):
99 return Cursor(self, *args, **kwargs)
102 class Cursor(object):
103 def __init__(self, collection, spec=None, fields=None, **kwargs):
104 if spec and not type(spec) is dict:
105 raise Exception("spec must be an instance of dict")
107 self.collection = collection
109 self.fields = self._get_fields(fields)
110 self.cursor = self._get_cursor()
115 def _get_fields(self, fields):
116 set_all_fields = set(self.collection._get_fields())
118 res_fields = list(set_all_fields)
119 elif type(fields) is dict:
120 fields_without_id = filter(lambda x: x[0] != '_id', fields.iteritems())
121 if fields_without_id[0][1]:
126 res_fields = set(set_all_fields)
127 for f in fields_without_id:
128 if f[1] and f[0] in set_all_fields:
132 raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
135 res_fields.discard(f[0])
137 raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
138 if '_id' in fields and not fields['_id']:
139 res_fields.discard('_id')
141 res_fields.add('_id')
142 res_fields = list(res_fields)
144 set_fields = set(list(fields))
145 set_fields.add('_id')
146 res_fields = list(set_all_fields.intersection(set_fields))
150 def _get_cursor(self):
152 table_id = '%s$_id' % self.collection.table_name
154 query['select'] = [(table_id, 'id')]
155 for f in filter(lambda x: x != '_id', self.fields):
156 table_f = '%s$%s' % (self.collection.table_name, f)
157 q = self._get_cursor_field(table_id, table_f)
158 query['select'].append(q)
160 query['from'] = [table_id]
164 for k, v in self.spec.iteritems():
165 table_f = '%s$%s' % (self.collection.table_name, k)
166 field_q = self._get_cursor_field(table_id, table_f)
167 query['where'].append((field_q, '=', v))
169 return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
171 def _get_cursor_field(self, table_id, table_field):
173 'select': [(table_field, 'value')],
174 'from': [table_field],
175 'where': [((table_field, 'id'), '=', (table_id, 'id'))],
180 res = self.collection.database.connection._next(self.cursor)
185 if '_id' in self.fields:
186 document['_id'] = res[0]
187 fields_without_id = filter(lambda x: x != '_id', self.fields)
188 for i in xrange(len(fields_without_id)):
189 if not res[i + 1] is None:
190 document[fields_without_id[i]] = res[i + 1]