| 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 /// An implementation of [ModelDB] based on model class annotations. | 7 /// An implementation of [ModelDB] based on model class annotations. |
| 8 /// | 8 /// |
| 9 /// The two constructors will scan loaded dart libraries for classes with a | 9 /// The two constructors will scan loaded dart libraries for classes with a |
| 10 /// [Kind] annotation. | 10 /// [Kind] annotation. |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 /// [fieldName]. | 148 /// [fieldName]. |
| 149 String fieldNameToPropertyName(String kind, String fieldName) { | 149 String fieldNameToPropertyName(String kind, String fieldName) { |
| 150 var modelDescription = _kind2ModelDesc[kind]; | 150 var modelDescription = _kind2ModelDesc[kind]; |
| 151 if (modelDescription == null) { | 151 if (modelDescription == null) { |
| 152 throw new ArgumentError('The kind "$kind" is unknown.'); | 152 throw new ArgumentError('The kind "$kind" is unknown.'); |
| 153 } | 153 } |
| 154 return modelDescription.fieldNameToPropertyName(fieldName); | 154 return modelDescription.fieldNameToPropertyName(fieldName); |
| 155 } | 155 } |
| 156 | 156 |
| 157 /// Converts [value] according to the [Property] named [name] in [type]. | 157 /// Converts [value] according to the [Property] named [name] in [type]. |
| 158 Object toDatastoreValue(String kind, String fieldName, Object value) { | 158 Object toDatastoreValue(String kind, String fieldName, Object value, |
| 159 {bool forComparison: false}) { |
| 159 var modelDescription = _kind2ModelDesc[kind]; | 160 var modelDescription = _kind2ModelDesc[kind]; |
| 160 if (modelDescription == null) { | 161 if (modelDescription == null) { |
| 161 throw new ArgumentError('The kind "$kind" is unknown.'); | 162 throw new ArgumentError('The kind "$kind" is unknown.'); |
| 162 } | 163 } |
| 163 return modelDescription.encodeField(this, fieldName, value); | 164 return modelDescription.encodeField( |
| 165 this, fieldName, value, forComparison: forComparison); |
| 164 } | 166 } |
| 165 | 167 |
| 166 Iterable<_ModelDescription> get _modelDescriptions { | 168 Iterable<_ModelDescription> get _modelDescriptions { |
| 167 return _modelDesc2Type.values; | 169 return _modelDesc2Type.values; |
| 168 } | 170 } |
| 169 | 171 |
| 170 Map<String, Property> _propertiesForModel( | 172 Map<String, Property> _propertiesForModel( |
| 171 _ModelDescription modelDescription) { | 173 _ModelDescription modelDescription) { |
| 172 return _modelDesc2Properties[modelDescription]; | 174 return _modelDesc2Properties[modelDescription]; |
| 173 } | 175 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 | 452 |
| 451 String fieldNameToPropertyName(String fieldName) { | 453 String fieldNameToPropertyName(String fieldName) { |
| 452 return _field2PropertyName[fieldName]; | 454 return _field2PropertyName[fieldName]; |
| 453 } | 455 } |
| 454 | 456 |
| 455 String propertyNameToFieldName(ModelDBImpl db, String propertySearchName) { | 457 String propertyNameToFieldName(ModelDBImpl db, String propertySearchName) { |
| 456 return _property2FieldName[propertySearchName]; | 458 return _property2FieldName[propertySearchName]; |
| 457 } | 459 } |
| 458 | 460 |
| 459 Object encodeField(ModelDBImpl db, String fieldName, Object value, | 461 Object encodeField(ModelDBImpl db, String fieldName, Object value, |
| 460 {bool enforceFieldExists: true}) { | 462 {bool enforceFieldExists: true, |
| 463 bool forComparison: false}) { |
| 461 Property property = db._propertiesForModel(this)[fieldName]; | 464 Property property = db._propertiesForModel(this)[fieldName]; |
| 462 if (property != null) { | 465 if (property != null) { |
| 463 return property.encodeValue(db, value); | 466 return property.encodeValue(db, value, forComparison: forComparison); |
| 464 } | 467 } |
| 465 if (enforceFieldExists) { | 468 if (enforceFieldExists) { |
| 466 throw new ArgumentError( | 469 throw new ArgumentError( |
| 467 'A field named "$fieldName" does not exist in kind "$kind".'); | 470 'A field named "$fieldName" does not exist in kind "$kind".'); |
| 468 } | 471 } |
| 469 return null; | 472 return null; |
| 470 } | 473 } |
| 471 } | 474 } |
| 472 | 475 |
| 473 // NOTE/TODO: | 476 // NOTE/TODO: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 | 532 |
| 530 String propertyNameToFieldName(ModelDBImpl db, String propertyName) { | 533 String propertyNameToFieldName(ModelDBImpl db, String propertyName) { |
| 531 String fieldName = super.propertyNameToFieldName(db, propertyName); | 534 String fieldName = super.propertyNameToFieldName(db, propertyName); |
| 532 // If the ModelDescription doesn't know about [propertyName], it's an | 535 // If the ModelDescription doesn't know about [propertyName], it's an |
| 533 // expanded property, where propertyName == fieldName. | 536 // expanded property, where propertyName == fieldName. |
| 534 if (fieldName == null) fieldName = propertyName; | 537 if (fieldName == null) fieldName = propertyName; |
| 535 return fieldName; | 538 return fieldName; |
| 536 } | 539 } |
| 537 | 540 |
| 538 Object encodeField(ModelDBImpl db, String fieldName, Object value, | 541 Object encodeField(ModelDBImpl db, String fieldName, Object value, |
| 539 {bool enforceFieldExists: true}) { | 542 {bool enforceFieldExists: true, |
| 543 bool forComparison: false}) { |
| 540 // The [enforceFieldExists] argument is intentionally ignored. | 544 // The [enforceFieldExists] argument is intentionally ignored. |
| 541 | 545 |
| 542 Object primitiveValue = super.encodeField(db, fieldName, value, | 546 Object primitiveValue = super.encodeField(db, fieldName, value, |
| 543 enforceFieldExists: false); | 547 enforceFieldExists: false, forComparison: forComparison); |
| 544 // If superclass can't encode field, we return value here (and assume | 548 // If superclass can't encode field, we return value here (and assume |
| 545 // it's primitive) | 549 // it's primitive) |
| 546 // NOTE: Implicit assumption: | 550 // NOTE: Implicit assumption: |
| 547 // If value != null then superclass will return != null. | 551 // If value != null then superclass will return != null. |
| 548 // TODO: Ensure [value] is primitive in this case. | 552 // TODO: Ensure [value] is primitive in this case. |
| 549 if (primitiveValue == null) primitiveValue = value; | 553 if (primitiveValue == null) primitiveValue = value; |
| 550 return primitiveValue; | 554 return primitiveValue; |
| 551 } | 555 } |
| 552 } | 556 } |
| OLD | NEW |