| 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 * | 155 * |
| 156 * [comparisonObject] is the object for comparison. | 156 * [comparisonObject] is the object for comparison. |
| 157 */ | 157 */ |
| 158 void filter(String filterString, Object comparisonObject) { | 158 void filter(String filterString, Object comparisonObject) { |
| 159 var parts = filterString.split(' '); | 159 var parts = filterString.split(' '); |
| 160 if (parts.length != 2 || !_relationMapping.containsKey(parts[1])) { | 160 if (parts.length != 2 || !_relationMapping.containsKey(parts[1])) { |
| 161 throw new ArgumentError( | 161 throw new ArgumentError( |
| 162 "Invalid filter string '$filterString'."); | 162 "Invalid filter string '$filterString'."); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // TODO: do value transformation on [comparisonObject] | 165 var name = parts[0]; |
| 166 var comparison = parts[1]; |
| 167 var propertyName = _convertToDatastoreName(name); |
| 166 | 168 |
| 167 var propertyName = _convertToDatastoreName(parts[0]); | 169 // 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 |
| 171 // `package:gcloud`. |
| 172 if (comparisonObject is! datastore.Key) { |
| 173 var encoded = _db.modelDB.toDatastoreValue(_kind, name, comparisonObject); |
| 174 |
| 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 } |
| 168 _filters.add(new datastore.Filter( | 187 _filters.add(new datastore.Filter( |
| 169 _relationMapping[parts[1]], propertyName, comparisonObject)); | 188 _relationMapping[comparison], propertyName, comparisonObject)); |
| 170 } | 189 } |
| 171 | 190 |
| 172 /** | 191 /** |
| 173 * Adds an order to this [Query]. | 192 * Adds an order to this [Query]. |
| 174 * | 193 * |
| 175 * [orderString] has the form "-name" where 'name' is a fieldName of the model | 194 * [orderString] has the form "-name" where 'name' is a fieldName of the model |
| 176 * and the optional '-' says whether the order is descending or ascending. | 195 * and the optional '-' says whether the order is descending or ascending. |
| 177 */ | 196 */ |
| 178 void order(String orderString) { | 197 void order(String orderString) { |
| 179 // TODO: validate [orderString] (e.g. is name valid) | 198 // TODO: validate [orderString] (e.g. is name valid) |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 416 |
| 398 Future<List<Model>> _lookupHelper( | 417 Future<List<Model>> _lookupHelper( |
| 399 DatastoreDB db, List<Key> keys, | 418 DatastoreDB db, List<Key> keys, |
| 400 {datastore.Transaction datastoreTransaction}) { | 419 {datastore.Transaction datastoreTransaction}) { |
| 401 var entityKeys = keys.map(db.modelDB.toDatastoreKey).toList(); | 420 var entityKeys = keys.map(db.modelDB.toDatastoreKey).toList(); |
| 402 return db.datastore.lookup(entityKeys, transaction: datastoreTransaction) | 421 return db.datastore.lookup(entityKeys, transaction: datastoreTransaction) |
| 403 .then((List<datastore.Entity> entities) { | 422 .then((List<datastore.Entity> entities) { |
| 404 return entities.map(db.modelDB.fromDatastoreEntity).toList(); | 423 return entities.map(db.modelDB.fromDatastoreEntity).toList(); |
| 405 }); | 424 }); |
| 406 } | 425 } |
| OLD | NEW |