Skip to content

Commit dfea441

Browse files
committed
Composite attributes: Expand composite attribute values across columns
1 parent 4903af4 commit dfea441

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

‎Sources/CoreModelSQLite/ModelData.swift‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ internal extension ModelData {
2323
assert(entity.id == self.entity)
2424
var values: [(String, Binding?)] = [(SQLiteDatabase.primaryKeyColumn, id.rawValue.binding)]
2525
values.reserveCapacity(1 + entity.attributes.count + entity.relationships.count)
26-
for attribute in entity.attributes {
27-
let value = attributes[attribute.id] ?? .null
28-
values.append((attribute.id.rawValue, value.binding))
26+
// composite attributes contribute one column per leaf element
27+
for column in entity.attributeColumns {
28+
let root = attributes[column.path[0]] ?? .null
29+
let value = root.value(at: column.path.dropFirst()) ?? .null
30+
values.append((column.name, value.binding))
2931
}
3032
for relationship in entity.relationships where relationship.type == .toOne {
3133
let binding: Binding?
@@ -47,8 +49,8 @@ internal extension ModelData {
4749
/// DO UPDATE` that only overwrites columns the caller actually supplied.
4850
func providedColumnNames(for entity: EntityDescription) -> Set<String> {
4951
var names = Set<String>()
50-
for attribute in entity.attributes where attributes[attribute.id] != nil {
51-
names.insert(attribute.id.rawValue)
52+
for column in entity.attributeColumns where attributes[column.path[0]] != nil {
53+
names.insert(column.name)
5254
}
5355
for relationship in entity.relationships where relationship.type == .toOne && relationships[relationship.id] != nil {
5456
names.insert(relationship.id.rawValue)
@@ -68,8 +70,7 @@ internal extension ModelData {
6870
var attributes = [PropertyKey: AttributeValue]()
6971
attributes.reserveCapacity(entity.attributes.count)
7072
for attribute in entity.attributes {
71-
let binding = row[attribute.id.rawValue] ?? nil
72-
attributes[attribute.id] = try AttributeValue(binding: binding, type: attribute.type)
73+
attributes[attribute.id] = try AttributeValue.decode(attribute: attribute, row: row)
7374
}
7475
var relationships = [PropertyKey: RelationshipValue]()
7576
relationships.reserveCapacity(entity.relationships.count)

0 commit comments

Comments
 (0)