Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: lib/src/db/db.dart

Issue 1520863002: Implement support for converting db values to datastore values with db.Query.filter (Closed) Base URL: git@github.com:dart-lang/gcloud.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/datastore_impl.dart ('k') | lib/src/db/model_db.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 }
OLDNEW
« no previous file with comments | « lib/src/datastore_impl.dart ('k') | lib/src/db/model_db.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698