-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityDescription.swift
More file actions
46 lines (37 loc) · 1.39 KB
/
Copy pathEntityDescription.swift
File metadata and controls
46 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// EntityDescription.swift
// CoreModel-SQLite
//
// Created by Alsey Coleman Miller on 7/4/25.
//
import CoreModel
import SQLite
internal extension SchemaChanger {
func create(model: Model, ifNotExists: Bool = true) throws {
for entity in model.entities {
try create(entity: entity, ifNotExists: ifNotExists)
}
}
func create(entity: EntityDescription, ifNotExists: Bool = true) throws {
try create(table: entity.id.rawValue, ifNotExists: ifNotExists) { table in
table.addColumns(entity)
}
}
}
internal extension SchemaChanger.CreateTableDefinition {
mutating func addColumns(_ entity: EntityDescription) {
// add ID column
let id = ColumnDefinition(
name: SQLiteDatabase.primaryKeyColumn,
primaryKey: .init(autoIncrement: false), type: .TEXT, nullable: false, unique: true, defaultValue: .NULL, references: nil)
add(column: id)
// add attribute columns, expanding composite attributes into one column per leaf
for column in entity.attributeColumns {
add(column: ColumnDefinition(column: column))
}
// add to-one relationship columns
for relationship in entity.relationships where relationship.type == .toOne {
add(column: ColumnDefinition(relationship: relationship))
}
}
}