]> git.jsancho.org Git - mojodb.git/blob - mojo.py
aed9ec634f50fa880960b9f481b6c6b6623e287f
[mojodb.git] / mojo.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    mojo, a Python library for implementing document based databases
5 #    Copyright (C) 2013-2014 by Javier Sancho Fernandez <jsf at jsancho dot org>
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22
23 class Connection(object):
24     def __init__(self, *args, **kwargs):
25         self._db_con = None
26
27     def __getattr__(self, db_name):
28         return Database(self, db_name)
29
30     def __getitem__(self, *args, **kwargs):
31         return self.__getattr__(*args, **kwargs)
32
33     def __repr__(self):
34         return "Connection(%s)" % self._db_con
35
36     def _get_databases(self):
37         return []
38
39     def database_names(self):
40         return [unicode(x) for x in self._get_databases()]
41
42     def _get_tables(self, db_name):
43         return []
44
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))]))
47
48     def _count_rows(self, db_name, table_name):
49         return 0
50
51     def _count(self, db_name, table_name):
52         try:
53             return self._count_rows(db_name, table_name + '$_id')
54         except:
55             return 0
56
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']}
59         return None
60
61     def _next(self, cursor):
62         return None
63
64
65 class Database(object):
66     def __init__(self, connection, db_name):
67         self.connection = connection
68         self.db_name = unicode(db_name)
69
70     def __getattr__(self, table_name):
71         return Collection(self, table_name)
72
73     def __getitem__(self, *args, **kwargs):
74         return self.__getattr__(*args, **kwargs)
75
76     def __repr__(self):
77         return "Database(%r, %r)" % (self.connection, self.db_name)
78
79     def collection_names(self):
80         return self.connection.collection_names(self.db_name)
81
82
83 class Collection(object):
84     def __init__(self, database, table_name):
85         self.database = database
86         self.table_name = unicode(table_name)
87
88     def __repr__(self):
89         return "Collection(%r, %r)" % (self.database, self.table_name)
90
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)]
94
95     def count(self):
96         return self.database.connection._count(self.database.db_name, self.table_name)
97
98     def find(self, *args, **kwargs):
99         return Cursor(self, *args, **kwargs)
100
101
102 class Cursor(object):
103     def __init__(self, collection, spec=None, fields=None, **kwargs):
104         self.collection = collection
105         self.spec = spec
106         self.fields = self._get_fields(fields)
107         self.cursor = self._get_cursor()
108
109     def __iter__(self):
110         return self
111
112     def _get_fields(self, fields):
113         set_all_fields = set(self.collection._get_fields())
114         if fields is None:
115             res_fields = list(set_all_fields)
116         elif type(fields) is dict:
117             fields_without_id = filter(lambda x: x[0] != '_id', fields.iteritems())
118             if fields_without_id[0][1]:
119                 first = True
120                 res_fields = set()
121             else:
122                 first = False
123                 res_fields = set(set_all_fields)
124             for f in fields_without_id:
125                 if f[1] and f[0] in set_all_fields:
126                     if first:
127                         res_fields.add(f[0])
128                     else:
129                         raise Exception(Errors['mix_fields_error'])
130                 elif not f[1]:
131                     if not first:
132                         res_fields.discard(f[0])
133                     else:
134                         raise Exception(Errors['mix_fields_error'])
135             if '_id' in fields and not fields['_id']:
136                 res_fields.discard('_id')
137             else:
138                 res_fields.add('_id')
139             res_fields = list(res_fields)
140         else:
141             set_fields = set(list(fields))
142             set_fields.add('_id')
143             res_fields = list(set_all_fields.intersection(set_fields))
144
145         return res_fields
146
147     def _get_cursor(self):
148         query = {}
149         table_id = '%s$_id' % self.collection.table_name
150
151         query['select'] = [(table_id, 'id')]
152         for f in filter(lambda x: x != '_id', self.fields):
153             table_f = '%s$%s' % (self.collection.table_name, f)
154             q = {}
155             q['select'] = [(table_f, 'value')]
156             q['from'] = [table_f]
157             q['where'] = [((table_f, 'id'), '=', (table_id, 'id'))]
158             query['select'].append(q)
159
160         query['from'] = [table_id]
161
162         return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
163
164     def next(self):
165         if self.cursor:
166             res = self.collection.database.connection._next(self.cursor)
167             if res is None:
168                 raise StopIteration
169             else:
170                 document = {}
171                 if '_id' in self.fields:
172                     document['_id'] = res[0]
173                 fields_without_id = filter(lambda x: x != '_id', self.fields)
174                 for i in xrange(len(fields_without_id)):
175                     if not res[i + 1] is None:
176                         document[fields_without_id[i]] = res[i + 1]
177                 return document
178         else:
179             return None
180
181
182 Errors = {
183     'mix_fields_error': 'You cannot currently mix including and excluding fields. Contact us if this is an issue.',
184     }