OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of gcloud.db; | 5 part of gcloud.db; |
6 | 6 |
7 /** | 7 /** |
8 * A function definition for transactional functions. | 8 * A function definition for transactional functions. |
9 * | 9 * |
10 * The function will be given a [Transaction] object which can be used to make | 10 * The function will be given a [Transaction] object which can be used to make |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 class Query { | 113 class Query { |
114 final _relationMapping = const <String, datastore.FilterRelation> { | 114 final _relationMapping = const <String, datastore.FilterRelation> { |
115 '<': datastore.FilterRelation.LessThan, | 115 '<': datastore.FilterRelation.LessThan, |
116 '<=': datastore.FilterRelation.LessThanOrEqual, | 116 '<=': datastore.FilterRelation.LessThanOrEqual, |
117 '>': datastore.FilterRelation.GreatherThan, | 117 '>': datastore.FilterRelation.GreatherThan, |
118 '>=': datastore.FilterRelation.GreatherThanOrEqual, | 118 '>=': datastore.FilterRelation.GreatherThanOrEqual, |
119 '=': datastore.FilterRelation.Equal, | 119 '=': datastore.FilterRelation.Equal, |
120 'IN': datastore.FilterRelation.In, | |
121 }; | 120 }; |
122 | 121 |
123 final DatastoreDB _db; | 122 final DatastoreDB _db; |
124 final datastore.Transaction _transaction; | 123 final datastore.Transaction _transaction; |
125 final String _kind; | 124 final String _kind; |
126 | 125 |
127 final Partition _partition; | 126 final Partition _partition; |
128 final Key _ancestorKey; | 127 final Key _ancestorKey; |
129 | 128 |
130 final List<datastore.Filter> _filters = []; | 129 final List<datastore.Filter> _filters = []; |
(...skipping 13 matching lines...) Expand all Loading... |
144 * Adds a filter to this [Query]. | 143 * Adds a filter to this [Query]. |
145 * | 144 * |
146 * [filterString] has form "name OP" where 'name' is a fieldName of the | 145 * [filterString] has form "name OP" where 'name' is a fieldName of the |
147 * model and OP is an operator. The following operators are supported: | 146 * model and OP is an operator. The following operators are supported: |
148 * | 147 * |
149 * * '<' (less than) | 148 * * '<' (less than) |
150 * * '<=' (less than or equal) | 149 * * '<=' (less than or equal) |
151 * * '>' (greater than) | 150 * * '>' (greater than) |
152 * * '>=' (greater than or equal) | 151 * * '>=' (greater than or equal) |
153 * * '=' (equal) | 152 * * '=' (equal) |
154 * * 'IN' (in - `comparisonObject` must be a list) | |
155 * | 153 * |
156 * [comparisonObject] is the object for comparison. | 154 * [comparisonObject] is the object for comparison. |
157 */ | 155 */ |
158 void filter(String filterString, Object comparisonObject) { | 156 void filter(String filterString, Object comparisonObject) { |
159 var parts = filterString.split(' '); | 157 var parts = filterString.split(' '); |
160 if (parts.length != 2 || !_relationMapping.containsKey(parts[1])) { | 158 if (parts.length != 2 || !_relationMapping.containsKey(parts[1])) { |
161 throw new ArgumentError( | 159 throw new ArgumentError( |
162 "Invalid filter string '$filterString'."); | 160 "Invalid filter string '$filterString'."); |
163 } | 161 } |
164 | 162 |
165 var name = parts[0]; | 163 var name = parts[0]; |
166 var comparison = parts[1]; | 164 var comparison = parts[1]; |
167 var propertyName = _convertToDatastoreName(name); | 165 var propertyName = _convertToDatastoreName(name); |
168 | 166 |
169 // This is for backwards compatibility: We allow [datastore.Key]s for now. | 167 // This is for backwards compatibility: We allow [datastore.Key]s for now. |
170 // TODO: We should remove the condition in a major version update of | 168 // TODO: We should remove the condition in a major version update of |
171 // `package:gcloud`. | 169 // `package:gcloud`. |
172 if (comparisonObject is! datastore.Key) { | 170 if (comparisonObject is! datastore.Key) { |
173 var encoded = _db.modelDB.toDatastoreValue(_kind, name, comparisonObject); | 171 comparisonObject = _db.modelDB.toDatastoreValue(_kind, name, |
174 | 172 comparisonObject, forComparison: true); |
175 // We encode Lists as repeated properties normally, and the encoding of | |
176 // `['abc']` will just be `'abc'` (see [ListProperty]). | |
177 // But for IN filters, we need to treat them as lists. | |
178 if (comparison == 'IN' && | |
179 comparisonObject is List && | |
180 comparisonObject.length == 1 && | |
181 encoded is! List) { | |
182 encoded = [encoded]; | |
183 } | |
184 | |
185 comparisonObject = encoded; | |
186 } | 173 } |
187 _filters.add(new datastore.Filter( | 174 _filters.add(new datastore.Filter( |
188 _relationMapping[comparison], propertyName, comparisonObject)); | 175 _relationMapping[comparison], propertyName, comparisonObject)); |
189 } | 176 } |
190 | 177 |
191 /** | 178 /** |
192 * Adds an order to this [Query]. | 179 * Adds an order to this [Query]. |
193 * | 180 * |
194 * [orderString] has the form "-name" where 'name' is a fieldName of the model | 181 * [orderString] has the form "-name" where 'name' is a fieldName of the model |
195 * and the optional '-' says whether the order is descending or ascending. | 182 * and the optional '-' says whether the order is descending or ascending. |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 | 403 |
417 Future<List<Model>> _lookupHelper( | 404 Future<List<Model>> _lookupHelper( |
418 DatastoreDB db, List<Key> keys, | 405 DatastoreDB db, List<Key> keys, |
419 {datastore.Transaction datastoreTransaction}) { | 406 {datastore.Transaction datastoreTransaction}) { |
420 var entityKeys = keys.map(db.modelDB.toDatastoreKey).toList(); | 407 var entityKeys = keys.map(db.modelDB.toDatastoreKey).toList(); |
421 return db.datastore.lookup(entityKeys, transaction: datastoreTransaction) | 408 return db.datastore.lookup(entityKeys, transaction: datastoreTransaction) |
422 .then((List<datastore.Entity> entities) { | 409 .then((List<datastore.Entity> entities) { |
423 return entities.map(db.modelDB.fromDatastoreEntity).toList(); | 410 return entities.map(db.modelDB.fromDatastoreEntity).toList(); |
424 }); | 411 }); |
425 } | 412 } |
OLD | NEW |