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 ##############################################################################
25 def __init__(self, collection, spec=None, fields=None, **kwargs):
26 if spec and not type(spec) is dict:
27 raise Exception("spec must be an instance of dict")
29 self.Query = collection.database.connection.Query
30 self.Field = collection.database.connection.Field
31 self.Table = collection.database.connection.Table
32 self.Constraint = collection.database.connection.Constraint
33 self.Literal = collection.database.connection.Literal
35 self.collection = collection
37 if self.collection.exists():
38 self.fields = self._get_fields(fields)
39 self.cursor = self._get_cursor()
47 def _get_fields(self, fields):
48 set_all_fields = set(self.collection._get_fields())
50 res_fields = list(set_all_fields)
51 elif type(fields) is dict:
52 fields_without_id = filter(lambda x: x[0] != '_id', fields.iteritems())
53 if fields_without_id[0][1]:
58 res_fields = set(set_all_fields)
59 for f in fields_without_id:
60 if f[1] and f[0] in set_all_fields:
64 raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
67 res_fields.discard(f[0])
69 raise Exception("You cannot currently mix including and excluding fields. Contact us if this is an issue.")
70 if '_id' in fields and not fields['_id']:
71 res_fields.discard('_id')
74 res_fields = list(res_fields)
76 set_fields = set(list(fields))
78 res_fields = list(set_all_fields.intersection(set_fields))
82 def _get_cursor(self):
83 table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
85 fields = [self.Field(table_id, 'value')]
86 for f in filter(lambda x: x != '_id', self.fields):
87 fields.append(self._get_cursor_field(f))
91 constraints = [self.Constraint('=', self.Field(table_id, 'name'), self.Literal('_id'))]
93 for k, v in self.spec.iteritems():
94 constraints.append(self._get_cursor_constraint(k, v))
96 query = self.Query(fields, tables, constraints)
97 return self.collection.database.connection._get_cursor(query)
99 def _get_cursor_field(self, field_name):
100 table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
101 table_field = self.Table(self.collection.database.db_name, '%s$%s' % (self.collection.table_name, field_name.split(".")[0]))
103 fields = [self.Field(table_field, 'value')]
104 tables = [table_field]
106 self.Constraint('=', self.Field(table_field, 'id'), self.Field(table_id, 'id')),
107 self.Constraint('=', self.Field(table_field, 'name'), self.Literal(field_name)),
109 return self.Query(fields, tables, constraints)
111 def _get_cursor_constraint(self, field_name, field_value):
112 table_id = self.Table(self.collection.database.db_name, '%s$_id' % self.collection.table_name)
113 table_field = self.Table(self.collection.database.db_name, '%s$%s' % (self.collection.table_name, field_name.split(".")[0]))
115 if type(field_value) in (int, float):
116 field_type = 'number'
119 field_value = msgpack.dumps(field_value)
121 fields = [self.Field(table_field, 'id')]
122 tables = [table_field]
124 self.Constraint('or', self.Constraint('=', self.Field(table_field, 'name'), self.Literal(field_name)),
125 self.Constraint('starts', self.Field(table_field, 'name'), self.Literal('%s..' % field_name))),
126 self.Constraint('=', self.Field(table_field, field_type), self.Literal(field_value)),
129 return self.Constraint('in', self.Field(table_id, 'id'), self.Query(fields, tables, constraints))
132 if self.cursor is None:
136 res = self.collection.database.connection._next(self.cursor)
141 if '_id' in self.fields:
142 document['_id'] = msgpack.loads(res[0])
143 fields_without_id = filter(lambda x: x != '_id', self.fields)
144 for i in xrange(len(fields_without_id)):
145 if not res[i + 1] is None:
146 document[fields_without_id[i]] = msgpack.loads(res[i + 1])