Extract PersistenceContainer in its own file

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent d5c0044342
commit dd47a7b8b3
4 files changed
+114 -109

No files matched your search

+4
View File
@@ -299,6 +299,7 @@
56BB6EA91D3009B100A1CA52 /* SchedulingWatchdog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BB6EA81D3009B100A1CA52 /* SchedulingWatchdog.swift */; };
56BCA65C2B79246300B3A7F8 /* Mutex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BCA65B2B79246300B3A7F8 /* Mutex.swift */; };
56BF2282241781C5003D86EB /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BF2281241781C5003D86EB /* UtilsTests.swift */; };
56CB43A12BD9880D001C8ED9 /* PersistenceContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CB43A02BD9880D001C8ED9 /* PersistenceContainer.swift */; };
56CC922C201DFFB900CB597E /* DropWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC922B201DFFB900CB597E /* DropWhileCursorTests.swift */; };
56CC9243201E034D00CB597E /* PrefixWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9242201E034D00CB597E /* PrefixWhileCursorTests.swift */; };
56CC9246201E058100CB597E /* DropFirstCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9245201E058000CB597E /* DropFirstCursorTests.swift */; };
@@ -815,6 +816,7 @@
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSavepointTests.swift; sourceTree = "<group>"; };
56C48E731C9A9923005DF1D9 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
56C494401ED7255500CC72AF /* GRDBDeploymentTarget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = GRDBDeploymentTarget.xcconfig; sourceTree = "<group>"; };
56CB43A02BD9880D001C8ED9 /* PersistenceContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceContainer.swift; sourceTree = "<group>"; };
56CC922B201DFFB900CB597E /* DropWhileCursorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropWhileCursorTests.swift; sourceTree = "<group>"; };
56CC9242201E034D00CB597E /* PrefixWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrefixWhileCursorTests.swift; sourceTree = "<group>"; };
56CC9245201E058000CB597E /* DropFirstCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropFirstCursorTests.swift; sourceTree = "<group>"; };
@@ -1715,6 +1717,7 @@
56D110D728AFC84000E64463 /* PersistableRecord+Insert.swift */,
56D110DC28AFC8B400E64463 /* PersistableRecord+Save.swift */,
56FA0C3828B20ABE00B2DFF7 /* PersistableRecord+Upsert.swift */,
56CB43A02BD9880D001C8ED9 /* PersistenceContainer.swift */,
56A238A11B9C753B0082EB20 /* Record.swift */,
560D92461C672C4B00F4F92B /* TableRecord.swift */,
);
@@ -2291,6 +2294,7 @@
56CEB4FA1EAA2F4D00BFAF62 /* FTS3.swift in Sources */,
56D110CE28AFC72100E64463 /* MutablePersistableRecord+Delete.swift in Sources */,
563082E42430B6BE00C14A05 /* DatabaseCancellable.swift in Sources */,
56CB43A12BD9880D001C8ED9 /* PersistenceContainer.swift in Sources */,
56B56C772BD56A17000DA88F /* RowImpl.swift in Sources */,
564CE59D21B7A8B500652B19 /* RemoveDuplicates.swift in Sources */,
5698AD351DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */,
-109
View File
@@ -324,115 +324,6 @@ extension EncodableRecord {
}
}
// MARK: - PersistenceContainer
/// A container for database values to store in a database row.
///
/// `PersistenceContainer` is the argument of the
/// ``EncodableRecord/encode(to:)-k9pf`` method.
public struct PersistenceContainer: Sendable {
// The ordering of the OrderedDictionary helps generating always the same
// SQL queries, and hit the statement cache.
private var storage: OrderedDictionary<CaseInsensitiveIdentifier, DatabaseValue>
/// The value associated with the given column.
///
/// The setter accepts any ``DatabaseValueConvertible`` type, but the
/// getter always returns a ``DatabaseValue``.
public subscript(_ column: String) -> (any DatabaseValueConvertible)? {
get {
storage[CaseInsensitiveIdentifier(rawValue: column)]
}
set {
storage.updateValue(
newValue?.databaseValue ?? .null,
forKey: CaseInsensitiveIdentifier(rawValue: column))
}
}
/// The value associated with the given column.
///
/// The setter accepts any ``DatabaseValueConvertible`` type, but the
/// getter always returns a ``DatabaseValue``.
public subscript(_ column: some ColumnExpression) -> (any DatabaseValueConvertible)? {
get { self[column.name] }
set { self[column.name] = newValue }
}
init() {
storage = OrderedDictionary()
}
init(minimumCapacity: Int) {
storage = OrderedDictionary(minimumCapacity: minimumCapacity)
}
/// Convenience initializer from a record
init<Record: EncodableRecord>(_ record: Record) throws {
self.init()
try record.encode(to: &self)
}
/// Convenience initializer from a database connection and a record
@usableFromInline
init(_ db: Database, _ record: some EncodableRecord & TableRecord) throws {
let databaseTableName = type(of: record).databaseTableName
let columnCount = try db.columns(in: databaseTableName).count
self.init(minimumCapacity: columnCount) // Optimization
try record.encode(to: &self)
}
/// Columns stored in the container, ordered like values.
var columns: [String] { storage.keys.map(\.rawValue) }
/// Values stored in the container, ordered like columns.
var values: [DatabaseValue] { storage.values }
/// Returns ``DatabaseValue/null`` if column is not defined
func databaseValue(at column: String) -> DatabaseValue {
storage[CaseInsensitiveIdentifier(rawValue: column)] ?? .null
}
@usableFromInline
func changesIterator(from container: PersistenceContainer) -> AnyIterator<(String, DatabaseValue)> {
var newValueIterator = storage.makeIterator()
return AnyIterator {
// Loop until we find a change, or exhaust columns:
while let (column, newDbValue) = newValueIterator.next() {
let oldDbValue = container.storage[column] ?? .null
if newDbValue != oldDbValue {
return (column.rawValue, oldDbValue)
}
}
return nil
}
}
}
extension PersistenceContainer: RandomAccessCollection {
public typealias Index = Int
public var startIndex: Int { storage.startIndex }
public var endIndex: Int { storage.endIndex }
/// Returns the (column, value) pair at given index.
public subscript(position: Int) -> (String, DatabaseValue) {
let element = storage[position]
return (element.key.rawValue, element.value)
}
}
extension Row {
convenience init<Record: EncodableRecord>(_ record: Record) throws {
try self.init(PersistenceContainer(record))
}
convenience init(_ container: PersistenceContainer) {
self.init(impl: ArrayRowImpl(columns: container.lazy.map { ($0, $1) }))
}
}
// MARK: - DatabaseDataEncodingStrategy
/// `DatabaseDataEncodingStrategy` specifies how `EncodableRecord` types that
+106
View File
@@ -0,0 +1,106 @@
/// A container for database values to store in a database row.
///
/// `PersistenceContainer` is the argument of the
/// ``EncodableRecord/encode(to:)-k9pf`` method.
public struct PersistenceContainer: Sendable {
// The ordering of the OrderedDictionary helps generating always the same
// SQL queries, and hit the statement cache.
private var storage: OrderedDictionary<CaseInsensitiveIdentifier, DatabaseValue>
/// The value associated with the given column.
///
/// The setter accepts any ``DatabaseValueConvertible`` type, but the
/// getter always returns a ``DatabaseValue``.
public subscript(_ column: String) -> (any DatabaseValueConvertible)? {
get {
storage[CaseInsensitiveIdentifier(rawValue: column)]
}
set {
storage.updateValue(
newValue?.databaseValue ?? .null,
forKey: CaseInsensitiveIdentifier(rawValue: column))
}
}
/// The value associated with the given column.
///
/// The setter accepts any ``DatabaseValueConvertible`` type, but the
/// getter always returns a ``DatabaseValue``.
public subscript(_ column: some ColumnExpression) -> (any DatabaseValueConvertible)? {
get { self[column.name] }
set { self[column.name] = newValue }
}
init() {
storage = OrderedDictionary()
}
init(minimumCapacity: Int) {
storage = OrderedDictionary(minimumCapacity: minimumCapacity)
}
/// Convenience initializer from a record
init<Record: EncodableRecord>(_ record: Record) throws {
self.init()
try record.encode(to: &self)
}
/// Convenience initializer from a database connection and a record
@usableFromInline
init(_ db: Database, _ record: some EncodableRecord & TableRecord) throws {
let databaseTableName = type(of: record).databaseTableName
let columnCount = try db.columns(in: databaseTableName).count
self.init(minimumCapacity: columnCount) // Optimization
try record.encode(to: &self)
}
/// Columns stored in the container, ordered like values.
var columns: [String] { storage.keys.map(\.rawValue) }
/// Values stored in the container, ordered like columns.
var values: [DatabaseValue] { storage.values }
/// Returns ``DatabaseValue/null`` if column is not defined
func databaseValue(at column: String) -> DatabaseValue {
storage[CaseInsensitiveIdentifier(rawValue: column)] ?? .null
}
@usableFromInline
func changesIterator(from container: PersistenceContainer) -> AnyIterator<(String, DatabaseValue)> {
var newValueIterator = storage.makeIterator()
return AnyIterator {
// Loop until we find a change, or exhaust columns:
while let (column, newDbValue) = newValueIterator.next() {
let oldDbValue = container.storage[column] ?? .null
if newDbValue != oldDbValue {
return (column.rawValue, oldDbValue)
}
}
return nil
}
}
}
extension PersistenceContainer: RandomAccessCollection {
public typealias Index = Int
public var startIndex: Int { storage.startIndex }
public var endIndex: Int { storage.endIndex }
/// Returns the (column, value) pair at given index.
public subscript(position: Int) -> (String, DatabaseValue) {
let element = storage[position]
return (element.key.rawValue, element.value)
}
}
extension Row {
convenience init<Record: EncodableRecord>(_ record: Record) throws {
try self.init(PersistenceContainer(record))
}
convenience init(_ container: PersistenceContainer) {
self.init(impl: ArrayRowImpl(columns: container.lazy.map { ($0, $1) }))
}
}
+4
View File
@@ -314,6 +314,7 @@
56C0539D22ACEECD0029D27D /* RemoveDuplicates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0538F22ACEECD0029D27D /* RemoveDuplicates.swift */; };
56C7A6AE1D2DFF6100EFB0C2 /* FoundationNSDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* FoundationNSDateTests.swift */; };
56C9EB9B2495324700FE5D55 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 56C9EB9A2495324700FE5D55 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; };
56CB43A42BD9884A001C8ED9 /* PersistenceContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CB43A22BD9884A001C8ED9 /* PersistenceContainer.swift */; };
56CC9234201E009000CB597E /* DropWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9231201DFFF600CB597E /* DropWhileCursorTests.swift */; };
56CC923B201E033400CB597E /* PrefixWhileCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC923A201E033400CB597E /* PrefixWhileCursorTests.swift */; };
56CC9249201E058900CB597E /* DropFirstCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC9248201E058900CB597E /* DropFirstCursorTests.swift */; };
@@ -834,6 +835,7 @@
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSavepointTests.swift; sourceTree = "<group>"; };
56C494421ED726C500CC72AF /* GRDBDeploymentTarget.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = GRDBDeploymentTarget.xcconfig; path = SQLiteCustom/GRDBDeploymentTarget.xcconfig; sourceTree = "<group>"; };
56C9EB9A2495324700FE5D55 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = grdb_config.h; path = SQLiteCustom/grdb_config.h; sourceTree = "<group>"; };
56CB43A22BD9884A001C8ED9 /* PersistenceContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersistenceContainer.swift; sourceTree = "<group>"; };
56CC9231201DFFF600CB597E /* DropWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropWhileCursorTests.swift; sourceTree = "<group>"; };
56CC923A201E033400CB597E /* PrefixWhileCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrefixWhileCursorTests.swift; sourceTree = "<group>"; };
56CC9248201E058900CB597E /* DropFirstCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropFirstCursorTests.swift; sourceTree = "<group>"; };
@@ -1721,6 +1723,7 @@
56D110E328AFC90800E64463 /* PersistableRecord+Insert.swift */,
56D110E428AFC90800E64463 /* PersistableRecord+Save.swift */,
56FA0C4028B20ADB00B2DFF7 /* PersistableRecord+Upsert.swift */,
56CB43A22BD9884A001C8ED9 /* PersistenceContainer.swift */,
56A238A11B9C753B0082EB20 /* Record.swift */,
560D92461C672C4B00F4F92B /* TableRecord.swift */,
);
@@ -2115,6 +2118,7 @@
5656A8732295BD56001FF3FF /* QueryInterfaceRequest.swift in Sources */,
566B91151FA4C3F50012D5B0 /* DatabaseCollation.swift in Sources */,
566A86CD2BD57A0100D5562E /* PreparedRequest.swift in Sources */,
56CB43A42BD9884A001C8ED9 /* PersistenceContainer.swift in Sources */,
5674A7041F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */,
566A86BE2BD57A0100D5562E /* SQLInterpolation.swift in Sources */,
563082EA2430B6CD00C14A05 /* DatabaseCancellable.swift in Sources */,