mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Merge remote-tracking branch 'origin/development' into rotacloud
This commit is contained in:
commit
4b7f17dbfa
44 files changed
+1520
-292
No files matched your search
@@ -3,8 +3,23 @@ Release Notes
|
||||
|
||||
## Next Version
|
||||
|
||||
- Cursors of optimized values (Strint, Int, Date, etc.) have been renamed: use FastDatabaseValueCursor and FastNullableDatabaseValueCursor instead of the deprecated ColumnCursor and NullableColumnCursor.
|
||||
- [#384](https://github.com/groue/GRDB.swift/pull/384): Improve database value decoding diagnostics
|
||||
- [#393](https://github.com/groue/GRDB.swift/pull/393): Upgrade SQLCipher to 3.4.2, enable FTS5 on GRDBCipher and new pod GRDBPlus.
|
||||
|
||||
### API diff
|
||||
|
||||
```diff
|
||||
+final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
|
||||
+@available(*, deprecated, renamed: "FastDatabaseValueCursor")
|
||||
+typealias ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastDatabaseValueCursor<Value>
|
||||
|
||||
+final class FastNullableDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
|
||||
+@available(*, deprecated, renamed: "FastNullableDatabaseValueCursor")
|
||||
+typealias NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastNullableDatabaseValueCursor<Value>
|
||||
|
||||
```
|
||||
|
||||
### Documentation Diff
|
||||
|
||||
- [Enabling FTS5 Support](README.md#enabling-fts5-support): Procedure for enabling FTS5 support in GRDB
|
||||
|
||||
@@ -140,6 +140,9 @@
|
||||
56439B581F4CA5AE0066043F /* PerformanceTests.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 56BB86111BA9886D001F9168 /* PerformanceTests.sqlite */; };
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644DE6D20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE6E20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE6F20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
564A50C81BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */; };
|
||||
564E73DF203D50B9000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73DE203D50B9000C443C /* JoinSupportTests.swift */; };
|
||||
564E73E0203D50B9000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73DE203D50B9000C443C /* JoinSupportTests.swift */; };
|
||||
@@ -148,6 +151,8 @@
|
||||
564F9C2D1F075DD200877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
564F9C311F07611600877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
564F9C341F07611900877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
564FCE5E20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */; };
|
||||
564FCE5F20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */; };
|
||||
5653EAD620944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */; };
|
||||
5653EAD720944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */; };
|
||||
5653EAD820944B4F00F46237 /* AssociationBelongsToRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC720944B4C00F46237 /* AssociationBelongsToRowScopeTests.swift */; };
|
||||
@@ -369,6 +374,10 @@
|
||||
56873BEC1F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
|
||||
56873BEF1F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
|
||||
56873BF21F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
|
||||
5690AFD82120589A001530EA /* InsertRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */; };
|
||||
5690AFD92120589A001530EA /* InsertRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */; };
|
||||
5690AFDB212058CB001530EA /* FetchRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */; };
|
||||
5690AFDC212058CB001530EA /* FetchRecordCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */; };
|
||||
5690C32A1D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; };
|
||||
5690C33B1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
|
||||
569178491CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
|
||||
@@ -862,10 +871,12 @@
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRequest.swift; sourceTree = "<group>"; };
|
||||
56439B501F4CA1DC0066043F /* GRDBOSXPerformanceComparisonTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBOSXPerformanceComparisonTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversion.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73DE203D50B9000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversionErrorTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationChainRowScopesTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC720944B4C00F46237 /* AssociationBelongsToRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationBelongsToRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC820944B4D00F46237 /* AssociationRowScopeSearchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationRowScopeSearchTests.swift; sourceTree = "<group>"; };
|
||||
@@ -951,6 +962,8 @@
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = "<group>"; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Fixits-1.2.swift"; sourceTree = "<group>"; };
|
||||
5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordCodableTests.swift; sourceTree = "<group>"; };
|
||||
5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordCodableTests.swift; sourceTree = "<group>"; };
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
|
||||
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1032,8 +1045,8 @@
|
||||
56B6EF55208CB4E3002F0ACB /* ColumnExpressionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColumnExpressionTests.swift; sourceTree = "<group>"; };
|
||||
56B7F4291BE14A1900E39BBF /* CGFloatTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloatTests.swift; sourceTree = "<group>"; };
|
||||
56B7F4391BEB42D500E39BBF /* Migration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Migration.swift; sourceTree = "<group>"; };
|
||||
56B8C2401CA1758F00510325 /* Realm.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Realm.framework; path = "Realm/build/osx/swift-4.1/Realm.framework"; sourceTree = "<group>"; };
|
||||
56B8C2411CA1758F00510325 /* RealmSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RealmSwift.framework; path = "Realm/build/osx/swift-4.1/RealmSwift.framework"; sourceTree = "<group>"; };
|
||||
56B8C2401CA1758F00510325 /* Realm.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Realm.framework; path = "Realm/build/osx/swift-4.1.2/Realm.framework"; sourceTree = "<group>"; };
|
||||
56B8C2411CA1758F00510325 /* RealmSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RealmSwift.framework; path = "Realm/build/osx/swift-4.1.2/RealmSwift.framework"; sourceTree = "<group>"; };
|
||||
56B8F49A1B4E2F3600C24296 /* GRDB.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = GRDB.xcconfig; sourceTree = "<group>"; };
|
||||
56B9649C1DA51B4C0002DA19 /* FTS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5.swift; sourceTree = "<group>"; };
|
||||
56B964B01DA51D010002DA19 /* FTS5TokenizerDescriptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5TokenizerDescriptor.swift; sourceTree = "<group>"; };
|
||||
@@ -1276,11 +1289,11 @@
|
||||
56176C581EACC2D8000F3F2B /* GRDBTests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */,
|
||||
562EA81E1F17B26F00FA528C /* Compilation */,
|
||||
56A238111B9C74A90082EB20 /* Core */,
|
||||
5698AC3E1DA2BEBB0056AF8C /* FTS */,
|
||||
56176CA01EACEE2A000F3F2B /* GRDBCipher */,
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */,
|
||||
56A238231B9C74A90082EB20 /* Migrations */,
|
||||
569978D31B539038005EBEED /* Private */,
|
||||
56300B5C1C53C38F005A543B /* QueryInterface */,
|
||||
@@ -1554,6 +1567,7 @@
|
||||
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */,
|
||||
566A8424204120B700E50BFD /* DatabaseSnapshotTests.swift */,
|
||||
56A238131B9C74A90082EB20 /* DatabaseTests.swift */,
|
||||
564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */,
|
||||
56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */,
|
||||
56A238191B9C74A90082EB20 /* DatabaseValueConvertible */,
|
||||
56A238181B9C74A90082EB20 /* DatabaseValueTests.swift */,
|
||||
@@ -1650,6 +1664,7 @@
|
||||
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */,
|
||||
566A84192041146100E50BFD /* DatabaseSnapshot.swift */,
|
||||
56A238751B9C75030082EB20 /* DatabaseValue.swift */,
|
||||
5644DE6C20F8C32E001FFDDE /* DatabaseValueConversion.swift */,
|
||||
560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */,
|
||||
563363C31C942C37000BE133 /* DatabaseWriter.swift */,
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */,
|
||||
@@ -1660,8 +1675,8 @@
|
||||
56A238781B9C75030082EB20 /* Statement.swift */,
|
||||
566B912A1FA4D0CC0012D5B0 /* StatementAuthorizer.swift */,
|
||||
560D923F1C672C3E00F4F92B /* StatementColumnConvertible.swift */,
|
||||
566B91321FA4D3810012D5B0 /* TransactionObserver.swift */,
|
||||
5605F1471C672E4000235C62 /* Support */,
|
||||
566B91321FA4D3810012D5B0 /* TransactionObserver.swift */,
|
||||
);
|
||||
path = Core;
|
||||
sourceTree = "<group>";
|
||||
@@ -1703,10 +1718,12 @@
|
||||
56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */,
|
||||
56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */,
|
||||
56DE7B2B1C41311900861EB8 /* FetchRecordClassTests.swift */,
|
||||
5690AFDA212058CB001530EA /* FetchRecordCodableTests.swift */,
|
||||
56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */,
|
||||
56DE7B251C412FDA00861EB8 /* InsertNamedValuesTests.swift */,
|
||||
56DE7B231C412F7E00861EB8 /* InsertPositionalValuesTests.swift */,
|
||||
56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */,
|
||||
5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */,
|
||||
56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */,
|
||||
56CA22211BB41565009A04C5 /* PerformanceTests.swift */,
|
||||
56B8C2401CA1758F00510325 /* Realm.framework */,
|
||||
@@ -2264,6 +2281,8 @@
|
||||
56707201208A509C006AD95A /* DateParsingTests.swift in Sources */,
|
||||
56DE7B2C1C41311900861EB8 /* FetchRecordClassTests.swift in Sources */,
|
||||
56DE7B281C41302500861EB8 /* FetchNamedValuesTests.swift in Sources */,
|
||||
5690AFD82120589A001530EA /* InsertRecordCodableTests.swift in Sources */,
|
||||
5690AFDB212058CB001530EA /* FetchRecordCodableTests.swift in Sources */,
|
||||
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */,
|
||||
56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */,
|
||||
560C98241C0E23BB00BF8471 /* PerformanceTests.swift in Sources */,
|
||||
@@ -2286,10 +2305,12 @@
|
||||
56D507841F6D7B2F00AE1C5B /* InsertRecordStructTests.swift in Sources */,
|
||||
56439B3A1F4CA1DC0066043F /* FMDatabaseQueue.m in Sources */,
|
||||
56439B3B1F4CA1DC0066043F /* FMDatabasePool.m in Sources */,
|
||||
5690AFD92120589A001530EA /* InsertRecordCodableTests.swift in Sources */,
|
||||
56439B3C1F4CA1DC0066043F /* PerformanceTests.swift in Sources */,
|
||||
56D3BE721F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */,
|
||||
56439B3D1F4CA1DC0066043F /* InsertNamedValuesTests.swift in Sources */,
|
||||
56439B3E1F4CA1DC0066043F /* PerformanceModel.xcdatamodeld in Sources */,
|
||||
5690AFDC212058CB001530EA /* FetchRecordCodableTests.swift in Sources */,
|
||||
56439B3F1F4CA1DC0066043F /* FetchPositionalValuesTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -2400,6 +2421,7 @@
|
||||
56BF6D3C1DEF47DA006039A3 /* Fixits-0-90-1.swift in Sources */,
|
||||
56DAA2E11DE9C827006E10C8 /* Cursor.swift in Sources */,
|
||||
5653EB0520944C7C00F46237 /* BelongsToAssociation.swift in Sources */,
|
||||
5644DE6F20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
565490BC1D5AE236005622CB /* DatabaseSchemaCache.swift in Sources */,
|
||||
565490BA1D5AE236005622CB /* DatabaseQueue.swift in Sources */,
|
||||
565490CD1D5AE252005622CB /* Date.swift in Sources */,
|
||||
@@ -2510,6 +2532,7 @@
|
||||
5653EB0D20944C7C00F46237 /* HasManyAssociation.swift in Sources */,
|
||||
566AD8B51D5318F4002EC1A8 /* TableDefinition.swift in Sources */,
|
||||
5698AD241DABAEFA0056AF8C /* FTS5WrapperTokenizer.swift in Sources */,
|
||||
5644DE6E20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
C96C0F2C2084A459006B2981 /* SQLiteDateParser.swift in Sources */,
|
||||
5605F1681C672E4000235C62 /* NSNumber.swift in Sources */,
|
||||
56CEB5041EAA2F4D00BFAF62 /* FTS4.swift in Sources */,
|
||||
@@ -2596,6 +2619,7 @@
|
||||
567F45AC1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */,
|
||||
5657AB621D108BA9006283EF /* FoundationNSURLTests.swift in Sources */,
|
||||
5657AB6A1D108BA9006283EF /* FoundationURLTests.swift in Sources */,
|
||||
564FCE5F20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
5653EADF20944B4F00F46237 /* AssociationHasOneSQLDerivationTests.swift in Sources */,
|
||||
56F3E74D1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */,
|
||||
56EB0AB31BCD787300A3DC55 /* DataMemoryTests.swift in Sources */,
|
||||
@@ -2758,6 +2782,7 @@
|
||||
56D496601D81304E008276D7 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
567F45A81F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */,
|
||||
56D4968D1D81316E008276D7 /* DatabaseFunctionTests.swift in Sources */,
|
||||
564FCE5E20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
5653EADE20944B4F00F46237 /* AssociationHasOneSQLDerivationTests.swift in Sources */,
|
||||
56D496661D813086008276D7 /* QueryInterfaceRequestTests.swift in Sources */,
|
||||
56F3E7491E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */,
|
||||
@@ -2960,6 +2985,7 @@
|
||||
566475A21D9810A400FF74B8 /* SQLSelectable+QueryInterface.swift in Sources */,
|
||||
5674A6E41F307F0E0095F066 /* DatabaseValueConvertible+Decodable.swift in Sources */,
|
||||
5653EB0C20944C7C00F46237 /* HasManyAssociation.swift in Sources */,
|
||||
5644DE6D20F8C32E001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
5657AAB91D107001006283EF /* NSData.swift in Sources */,
|
||||
560D92421C672C3E00F4F92B /* StatementColumnConvertible.swift in Sources */,
|
||||
56CEB5531EAA359A00BFAF62 /* SQLExpression.swift in Sources */,
|
||||
@@ -3052,7 +3078,7 @@
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
FRAMEWORK_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"$(PROJECT_DIR)/Tests/Performance/Realm/build/osx/swift-4.1",
|
||||
"$(PROJECT_DIR)/Tests/Performance/Realm/build/osx/swift-4.1.2",
|
||||
);
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
INFOPLIST_FILE = Tests/Info.plist;
|
||||
@@ -3075,7 +3101,7 @@
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
FRAMEWORK_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"$(PROJECT_DIR)/Tests/Performance/Realm/build/osx/swift-4.1",
|
||||
"$(PROJECT_DIR)/Tests/Performance/Realm/build/osx/swift-4.1.2",
|
||||
);
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
INFOPLIST_FILE = Tests/Info.plist;
|
||||
|
||||
@@ -427,7 +427,7 @@ extension DatabasePool : DatabaseReader {
|
||||
do {
|
||||
try db.beginTransaction(.deferred)
|
||||
assert(db.isInsideTransaction)
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").cursor().next()
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").makeCursor().next()
|
||||
} catch {
|
||||
readError = error
|
||||
semaphore.signal() // Release the writer queue and rethrow error
|
||||
|
||||
@@ -35,7 +35,7 @@ public class DatabaseSnapshot : DatabaseReader {
|
||||
|
||||
// Take snapshot
|
||||
// See DatabasePool.readFromCurrentState for a complete discussion
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").cursor().next()
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").makeCursor().next()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -207,6 +207,7 @@ extension DatabaseValue {
|
||||
// MARK: - Lossless conversions
|
||||
|
||||
extension DatabaseValue {
|
||||
// TODO: deprecate and rename to DatabaseValue.decode(_:sql:arguments:)
|
||||
/// Converts the database value to the type T.
|
||||
///
|
||||
/// let dbValue = "foo".databaseValue
|
||||
@@ -226,20 +227,10 @@ extension DatabaseValue {
|
||||
/// - arguments: Optional statement arguments that enhances the eventual
|
||||
/// conversion error
|
||||
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T where T : DatabaseValueConvertible {
|
||||
if let value = T.fromDatabaseValue(self) {
|
||||
return value
|
||||
}
|
||||
// Failed conversion: this is data loss, a programmer error.
|
||||
var error = "could not convert database value \(self) to \(T.self)"
|
||||
if let sql = sql {
|
||||
error += " with statement `\(sql)`"
|
||||
}
|
||||
if let arguments = arguments, !arguments.isEmpty {
|
||||
error += " arguments \(arguments)"
|
||||
}
|
||||
fatalError(error)
|
||||
return T.decode(from: self, conversionContext: sql.map { ValueConversionContext(sql: $0, arguments: arguments) })
|
||||
}
|
||||
|
||||
// TODO: deprecate and rename to DatabaseValue.decodeIfPresent(_:sql:arguments:)
|
||||
/// Converts the database value to the type Optional<T>.
|
||||
///
|
||||
/// let dbValue = "foo".databaseValue
|
||||
@@ -260,25 +251,7 @@ extension DatabaseValue {
|
||||
/// - arguments: Optional statement arguments that enhances the eventual
|
||||
/// conversion error
|
||||
public func losslessConvert<T>(sql: String? = nil, arguments: StatementArguments? = nil) -> T? where T : DatabaseValueConvertible {
|
||||
// Use fromDatabaseValue first: this allows DatabaseValue to convert NULL to .null.
|
||||
if let value = T.fromDatabaseValue(self) {
|
||||
return value
|
||||
}
|
||||
if isNull {
|
||||
// Failed conversion from null: ok
|
||||
return nil
|
||||
} else {
|
||||
// Failed conversion from a non-null database value: this is data
|
||||
// loss, a programmer error.
|
||||
var error = "could not convert database value \(self) to \(T.self)"
|
||||
if let sql = sql {
|
||||
error += " with statement `\(sql)`"
|
||||
}
|
||||
if let arguments = arguments, !arguments.isEmpty {
|
||||
error += " arguments \(arguments)"
|
||||
}
|
||||
fatalError(error)
|
||||
}
|
||||
return T.decodeIfPresent(from: self, conversionContext: sql.map { ValueConversionContext(sql: $0, arguments: arguments) })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
#if SWIFT_PACKAGE
|
||||
import CSQLite
|
||||
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
|
||||
import SQLite3
|
||||
#endif
|
||||
|
||||
// MARK: - Conversion Context and Errors
|
||||
|
||||
/// A type that helps the user understanding value conversion errors
|
||||
struct ValueConversionContext {
|
||||
private enum Column {
|
||||
case columnIndex(Int)
|
||||
case columnName(String)
|
||||
}
|
||||
var row: Row?
|
||||
var sql: String?
|
||||
var arguments: StatementArguments?
|
||||
private var column: Column?
|
||||
|
||||
func atColumn(_ columnIndex: Int) -> ValueConversionContext {
|
||||
var result = self
|
||||
result.column = .columnIndex(columnIndex)
|
||||
return result
|
||||
}
|
||||
|
||||
func atColumn(_ columnName: String) -> ValueConversionContext {
|
||||
var result = self
|
||||
result.column = .columnName(columnName)
|
||||
return result
|
||||
}
|
||||
|
||||
var columnIndex: Int? {
|
||||
guard let column = column else { return nil }
|
||||
switch column {
|
||||
case .columnIndex(let index):
|
||||
return index
|
||||
case .columnName(let name):
|
||||
return row?.index(ofColumn: name)
|
||||
}
|
||||
}
|
||||
|
||||
var columnName: String? {
|
||||
guard let column = column else { return nil }
|
||||
switch column {
|
||||
case .columnIndex(let index):
|
||||
guard let row = row else { return nil }
|
||||
return Array(row.columnNames)[index]
|
||||
case .columnName(let name):
|
||||
return name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ValueConversionContext {
|
||||
init(_ statement: SelectStatement) {
|
||||
self.init(
|
||||
row: Row(statement: statement).copy(),
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments,
|
||||
column: nil)
|
||||
}
|
||||
|
||||
init(_ row: Row) {
|
||||
if let statement = row.statement {
|
||||
self.init(
|
||||
row: row.copy(),
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments,
|
||||
column: nil)
|
||||
} else {
|
||||
self.init(
|
||||
row: row.copy(),
|
||||
sql: nil,
|
||||
arguments: nil,
|
||||
column: nil)
|
||||
}
|
||||
}
|
||||
|
||||
init(sql: String, arguments: StatementArguments?) {
|
||||
self.init(
|
||||
row: nil,
|
||||
sql: sql,
|
||||
arguments: arguments,
|
||||
column: nil)
|
||||
}
|
||||
}
|
||||
|
||||
/// The canonical conversion error message
|
||||
///
|
||||
/// - parameter dbValue: nil means "missing column"
|
||||
func conversionErrorMessage<T>(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?) -> String {
|
||||
var message: String
|
||||
var extras: [String] = []
|
||||
|
||||
if let dbValue = dbValue {
|
||||
message = "could not convert database value \(dbValue) to \(T.self)"
|
||||
if let columnName = conversionContext?.columnName {
|
||||
extras.append("column: `\(columnName)`")
|
||||
}
|
||||
if let columnIndex = conversionContext?.columnIndex {
|
||||
extras.append("column index: \(columnIndex)")
|
||||
}
|
||||
} else {
|
||||
message = "could not read \(T.self) from missing column"
|
||||
if let columnName = conversionContext?.columnName {
|
||||
message += " `\(columnName)`"
|
||||
}
|
||||
}
|
||||
|
||||
if let row = conversionContext?.row {
|
||||
extras.append("row: \(row)")
|
||||
}
|
||||
|
||||
if let sql = conversionContext?.sql {
|
||||
extras.append("sql: `\(sql)`")
|
||||
if let arguments = conversionContext?.arguments, arguments.isEmpty == false {
|
||||
extras.append("arguments: \(arguments)")
|
||||
}
|
||||
}
|
||||
|
||||
if extras.isEmpty == false {
|
||||
message += " (" + extras.joined(separator: ", ") + ")"
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
/// The canonical conversion fatal error
|
||||
///
|
||||
/// - parameter dbValue: nil means "missing column", for consistency with (row["missing"] as DatabaseValue? == nil)
|
||||
func fatalConversionError<T>(to: T.Type, from dbValue: DatabaseValue?, conversionContext: ValueConversionContext?, file: StaticString = #file, line: UInt = #line) -> Never {
|
||||
fatalError(conversionErrorMessage(to: T.self, from: dbValue, conversionContext: conversionContext), file: file, line: line)
|
||||
}
|
||||
|
||||
// MARK: - DatabaseValueConvertible
|
||||
|
||||
/// Lossless conversions from database values and rows
|
||||
extension DatabaseValueConvertible {
|
||||
@inline(__always)
|
||||
static func decode(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
|
||||
if let value = fromDatabaseValue(dbValue) {
|
||||
return value
|
||||
} else {
|
||||
fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext())
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func decode(from row: Row, atUncheckedIndex index: Int) -> Self {
|
||||
return decode(
|
||||
from: row.impl.databaseValue(atUncheckedIndex: index),
|
||||
conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func decodeIfPresent(from dbValue: DatabaseValue, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self? {
|
||||
// Use fromDatabaseValue before checking for null: this allows DatabaseValue to convert NULL to .null.
|
||||
if let value = fromDatabaseValue(dbValue) {
|
||||
return value
|
||||
} else if dbValue.isNull {
|
||||
return nil
|
||||
} else {
|
||||
fatalConversionError(to: Self.self, from: dbValue, conversionContext: conversionContext())
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func decodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
|
||||
return decodeIfPresent(
|
||||
from: row.impl.databaseValue(atUncheckedIndex: index),
|
||||
conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DatabaseValueConvertible & StatementColumnConvertible
|
||||
|
||||
/// Lossless conversions from database values and rows
|
||||
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
@inline(__always)
|
||||
static func fastDecode(from sqliteStatement: SQLiteStatement, index: Int32, conversionContext: @autoclosure () -> ValueConversionContext?) -> Self {
|
||||
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
|
||||
fatalConversionError(to: Self.self, from: .null, conversionContext: conversionContext())
|
||||
}
|
||||
return self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func fastDecode(from row: Row, atUncheckedIndex index: Int) -> Self {
|
||||
if let sqliteStatement = row.sqliteStatement {
|
||||
return fastDecode(
|
||||
from: sqliteStatement,
|
||||
index: Int32(index),
|
||||
conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
return row.impl.fastDecode(
|
||||
Self.self,
|
||||
atUncheckedIndex: index,
|
||||
conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func fastDecodeIfPresent(from sqliteStatement: SQLiteStatement, atUncheckedIndex index: Int32) -> Self? {
|
||||
if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
|
||||
return nil
|
||||
}
|
||||
return self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func fastDecodeIfPresent(from row: Row, atUncheckedIndex index: Int) -> Self? {
|
||||
if let sqliteStatement = row.sqliteStatement {
|
||||
return fastDecodeIfPresent(
|
||||
from: sqliteStatement,
|
||||
atUncheckedIndex: Int32(index))
|
||||
}
|
||||
return row.impl.fastDecodeIfPresent(
|
||||
Self.self,
|
||||
atUncheckedIndex: index,
|
||||
conversionContext: ValueConversionContext(row).atColumn(index))
|
||||
}
|
||||
}
|
||||
@@ -55,25 +55,39 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor
|
||||
|
||||
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
self.statement = statement
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
self.columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
} else {
|
||||
self.columnIndex = 0
|
||||
}
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Value? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return dbValue.losslessConvert() as Value
|
||||
return Value.decode(
|
||||
from: dbValue,
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
throw DatabaseError(
|
||||
resultCode: code,
|
||||
message: statement.database.lastErrorMessage,
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,25 +109,39 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>
|
||||
|
||||
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
self.statement = statement
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
self.columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
} else {
|
||||
self.columnIndex = 0
|
||||
}
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Value?? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return dbValue.losslessConvert() as Value?
|
||||
return Value.decodeIfPresent(
|
||||
from: dbValue,
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
throw DatabaseError(
|
||||
resultCode: code,
|
||||
message: statement.database.lastErrorMessage,
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+108
-88
@@ -7,6 +7,8 @@ import Foundation
|
||||
|
||||
/// A database row.
|
||||
public final class Row : Equatable, Hashable, RandomAccessCollection, ExpressibleByDictionaryLiteral, CustomStringConvertible, CustomDebugStringConvertible {
|
||||
// It is not a violation of the Demeter law when another type uses this
|
||||
// property, which is exposed for optimizations.
|
||||
let impl: RowImpl
|
||||
|
||||
/// Unless we are producing a row array, we use a single row when iterating
|
||||
@@ -21,6 +23,9 @@ public final class Row : Equatable, Hashable, RandomAccessCollection, Expressibl
|
||||
/// The statementRef is released in deinit.
|
||||
let statementRef: Unmanaged<SelectStatement>?
|
||||
let sqliteStatement: SQLiteStatement?
|
||||
var statement: SelectStatement? {
|
||||
return statementRef?.takeUnretainedValue()
|
||||
}
|
||||
|
||||
/// The number of columns in the row.
|
||||
public let count: Int
|
||||
@@ -123,7 +128,12 @@ extension Row {
|
||||
///
|
||||
/// This method is case-insensitive.
|
||||
public func hasColumn(_ columnName: String) -> Bool {
|
||||
return impl.index(ofColumn: columnName) != nil
|
||||
return index(ofColumn: columnName) != nil
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func index(ofColumn name: String) -> Int? {
|
||||
return impl.index(ofColumn: name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +141,12 @@ extension Row {
|
||||
|
||||
// MARK: - Extracting Values
|
||||
|
||||
/// Fatal errors if index is out of bounds
|
||||
@inline(__always)
|
||||
private func checkIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range", file: file, line: line)
|
||||
}
|
||||
|
||||
/// Returns true if and only if one column contains a non-null value, or if
|
||||
/// the row was fetched with a row adapter that defines a scoped row that
|
||||
/// contains a non-null value.
|
||||
@@ -143,7 +159,7 @@ extension Row {
|
||||
/// let row = try Row.fetchOne(db, "SELECT NULL, NULL")!
|
||||
/// row.containsNonNullValue // false
|
||||
public var containsNonNullValue: Bool {
|
||||
for i in (0..<count) where !hasNull(atIndex: i) {
|
||||
for i in (0..<count) where !impl.hasNull(atUncheckedIndex: i) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -163,7 +179,7 @@ extension Row {
|
||||
/// in performance-critical code because it can avoid decoding database
|
||||
/// values.
|
||||
public func hasNull(atIndex index: Int) -> Bool {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
checkIndex(index)
|
||||
return impl.hasNull(atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
@@ -173,7 +189,7 @@ extension Row {
|
||||
/// Indexes span from 0 for the leftmost column to (row.count - 1) for the
|
||||
/// righmost column.
|
||||
public subscript(_ index: Int) -> DatabaseValueConvertible? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
checkIndex(index)
|
||||
return impl.databaseValue(atUncheckedIndex: index).storage.value
|
||||
}
|
||||
|
||||
@@ -186,8 +202,8 @@ extension Row {
|
||||
/// value is converted to the requested type `Value`. Should this conversion
|
||||
/// fail, a fatal error is raised.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
return impl.value(atUncheckedIndex: index)
|
||||
checkIndex(index)
|
||||
return Value.decodeIfPresent(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -203,11 +219,8 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
return impl.fastValue(atUncheckedIndex: index)
|
||||
checkIndex(index)
|
||||
return Value.fastDecodeIfPresent(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -218,8 +231,8 @@ extension Row {
|
||||
/// This method crashes if the fetched SQLite value is NULL, or if the
|
||||
/// SQLite value can not be converted to `Value`.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
return impl.value(atUncheckedIndex: index)
|
||||
checkIndex(index)
|
||||
return Value.decode(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given index, converted to the requested type.
|
||||
@@ -234,11 +247,8 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
return impl.fastValue(atUncheckedIndex: index)
|
||||
checkIndex(index)
|
||||
return Value.fastDecode(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns Int64, Double, String, Data or nil, depending on the value
|
||||
@@ -256,7 +266,7 @@ extension Row {
|
||||
// if row["foo"] != nil { ... }
|
||||
//
|
||||
// Without this method, the code above would not compile.
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
return nil
|
||||
}
|
||||
return impl.databaseValue(atUncheckedIndex: index).storage.value
|
||||
@@ -271,10 +281,10 @@ extension Row {
|
||||
/// nil. Otherwise the SQLite value is converted to the requested type
|
||||
/// `Value`. Should this conversion fail, a fatal error is raised.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ columnName: String) -> Value? {
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
return nil
|
||||
}
|
||||
return impl.value(atUncheckedIndex: index)
|
||||
return Value.decodeIfPresent(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -290,13 +300,10 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ columnName: String) -> Value? {
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
return nil
|
||||
}
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
return impl.fastValue(atUncheckedIndex: index)
|
||||
return Value.fastDecodeIfPresent(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -309,11 +316,14 @@ extension Row {
|
||||
/// This method crashes if the fetched SQLite value is NULL, or if the
|
||||
/// SQLite value can not be converted to `Value`.
|
||||
public subscript<Value: DatabaseValueConvertible>(_ columnName: String) -> Value {
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
// Programmer error
|
||||
fatalError("no such column: \(columnName)")
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
// No such column
|
||||
fatalConversionError(
|
||||
to: Value.self,
|
||||
from: nil,
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
return impl.value(atUncheckedIndex: index)
|
||||
return Value.decode(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the value at given column, converted to the requested type.
|
||||
@@ -330,14 +340,14 @@ extension Row {
|
||||
/// StatementColumnConvertible. It *may* trigger SQLite built-in conversions
|
||||
/// (see https://www.sqlite.org/datatype3.html).
|
||||
public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ columnName: String) -> Value {
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
// Programmer error
|
||||
fatalError("no such column: \(columnName)")
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
// No such column
|
||||
fatalConversionError(
|
||||
to: Value.self,
|
||||
from: nil,
|
||||
conversionContext: ValueConversionContext(self).atColumn(columnName))
|
||||
}
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
return impl.fastValue(atUncheckedIndex: index)
|
||||
return Value.fastDecode(from: self, atUncheckedIndex: index)
|
||||
}
|
||||
|
||||
/// Returns Int64, Double, String, NSData or nil, depending on the value
|
||||
@@ -420,8 +430,10 @@ extension Row {
|
||||
/// The returned data does not owns its bytes: it must not be used longer
|
||||
/// than the row's lifetime.
|
||||
public func dataNoCopy(atIndex index: Int) -> Data? {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
return impl.dataNoCopy(atUncheckedIndex: index)
|
||||
checkIndex(index)
|
||||
return impl.dataNoCopy(
|
||||
atUncheckedIndex: index,
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the optional Data at given column.
|
||||
@@ -436,10 +448,12 @@ extension Row {
|
||||
/// The returned data does not owns its bytes: it must not be used longer
|
||||
/// than the row's lifetime.
|
||||
public func dataNoCopy(named columnName: String) -> Data? {
|
||||
guard let index = impl.index(ofColumn: columnName) else {
|
||||
guard let index = index(ofColumn: columnName) else {
|
||||
return nil
|
||||
}
|
||||
return impl.dataNoCopy(atUncheckedIndex: index)
|
||||
return impl.dataNoCopy(
|
||||
atUncheckedIndex: index,
|
||||
conversionContext: ValueConversionContext(self).atColumn(index))
|
||||
}
|
||||
|
||||
/// Returns the optional `NSData` at given column.
|
||||
@@ -595,12 +609,16 @@ public final class RowCursor : Cursor {
|
||||
self.statement = statement
|
||||
self.row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Row? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
@@ -911,7 +929,7 @@ extension Row {
|
||||
/// Accesses the (ColumnName, DatabaseValue) pair at given index.
|
||||
public subscript(position: RowIndex) -> (String, DatabaseValue) {
|
||||
let index = position.index
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
checkIndex(index)
|
||||
return (
|
||||
impl.columnName(atUncheckedIndex: index),
|
||||
impl.databaseValue(atUncheckedIndex: index))
|
||||
@@ -1178,15 +1196,14 @@ protocol RowImpl {
|
||||
var count: Int { get }
|
||||
var isFetched: Bool { get }
|
||||
var scopes: Row.ScopesView { get }
|
||||
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value?
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data?
|
||||
func columnName(atUncheckedIndex index: Int) -> String
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool
|
||||
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ type: Value.Type, atUncheckedIndex index: Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data?
|
||||
|
||||
// This method MUST be case-insensitive, and returns the index of the
|
||||
// leftmost column that matches *name*.
|
||||
/// Returns the index of the leftmost column that matches *name* (case-insensitive)
|
||||
func index(ofColumn name: String) -> Int?
|
||||
|
||||
// row.impl is guaranteed to be self.
|
||||
@@ -1197,42 +1214,55 @@ protocol RowImpl {
|
||||
|
||||
extension RowImpl {
|
||||
func copiedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume immutable row (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
func unscopedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume unadapted row (see AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
func unadaptedRow(_ row: Row) -> Row {
|
||||
// unless customized, assume unadapted row (see AdaptedRowImpl for customization)
|
||||
return row
|
||||
}
|
||||
|
||||
var scopes: Row.ScopesView {
|
||||
// unless customized, assume unuscoped row (see AdaptedRowImpl for customization)
|
||||
return Row.ScopesView()
|
||||
}
|
||||
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool {
|
||||
// unless customized, use slow check (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return databaseValue(atUncheckedIndex: index).isNull
|
||||
}
|
||||
|
||||
func value<Value: DatabaseValueConvertible>(atUncheckedIndex index: Int) -> Value {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
// unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Value.decode(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func value<Value: DatabaseValueConvertible>(atUncheckedIndex index: Int) -> Value? {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
// unless customized, use slow decoding (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Value.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value? {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
// unless customized, copy data (see StatementRowImpl and AdaptedRowImpl for customization)
|
||||
return Data.decodeIfPresent(from: databaseValue(atUncheckedIndex: index), conversionContext: conversionContext)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: merge with StatementCopyRowImpl eventually?
|
||||
/// See Row.init(dictionary:)
|
||||
private struct ArrayRowImpl : RowImpl {
|
||||
let columns: [(String, DatabaseValue)]
|
||||
@@ -1249,10 +1279,6 @@ private struct ArrayRowImpl : RowImpl {
|
||||
return false
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
}
|
||||
|
||||
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue {
|
||||
return columns[index].1
|
||||
}
|
||||
@@ -1261,8 +1287,6 @@ private struct ArrayRowImpl : RowImpl {
|
||||
return columns[index].0
|
||||
}
|
||||
|
||||
// This method MUST be case-insensitive, and returns the index of the
|
||||
// leftmost column that matches *name*.
|
||||
func index(ofColumn name: String) -> Int? {
|
||||
let lowercaseName = name.lowercased()
|
||||
return columns.index { (column, _) in column.lowercased() == lowercaseName }
|
||||
@@ -1270,6 +1294,7 @@ private struct ArrayRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
|
||||
// TODO: merge with ArrayRowImpl eventually?
|
||||
/// See Row.init(copiedFromStatementRef:sqliteStatement:)
|
||||
private struct StatementCopyRowImpl : RowImpl {
|
||||
let dbValues: ContiguousArray<DatabaseValue>
|
||||
@@ -1289,10 +1314,6 @@ private struct StatementCopyRowImpl : RowImpl {
|
||||
return true
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
return databaseValue(atUncheckedIndex: index).losslessConvert()
|
||||
}
|
||||
|
||||
func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue {
|
||||
return dbValues[index]
|
||||
}
|
||||
@@ -1301,8 +1322,6 @@ private struct StatementCopyRowImpl : RowImpl {
|
||||
return columnNames[index]
|
||||
}
|
||||
|
||||
// This method MUST be case-insensitive, and returns the index of the
|
||||
// leftmost column that matches *name*.
|
||||
func index(ofColumn name: String) -> Int? {
|
||||
let lowercaseName = name.lowercased()
|
||||
return columnNames.index { $0.lowercased() == lowercaseName }
|
||||
@@ -1319,7 +1338,7 @@ private struct StatementRowImpl : RowImpl {
|
||||
init(sqliteStatement: SQLiteStatement, statementRef: Unmanaged<SelectStatement>) {
|
||||
self.statementRef = statementRef
|
||||
self.sqliteStatement = sqliteStatement
|
||||
// Optimize row["..."]
|
||||
// Optimize row[columnName]
|
||||
let lowercaseColumnNames = (0..<sqlite3_column_count(sqliteStatement)).map { String(cString: sqlite3_column_name(sqliteStatement, Int32($0))).lowercased() }
|
||||
self.lowercaseColumnIndexes = Dictionary(
|
||||
lowercaseColumnNames
|
||||
@@ -1337,11 +1356,11 @@ private struct StatementRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
func hasNull(atUncheckedIndex index:Int) -> Bool {
|
||||
// Avoid extracting values, because this modifies the statement.
|
||||
// Avoid extracting values, because this modifies the SQLite statement.
|
||||
return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
guard sqlite3_column_type(sqliteStatement, Int32(index)) != SQLITE_NULL else {
|
||||
return nil
|
||||
}
|
||||
@@ -1356,20 +1375,26 @@ private struct StatementRowImpl : RowImpl {
|
||||
return DatabaseValue(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value {
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
return Value.fastDecode(from: sqliteStatement, index: Int32(index), conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value? {
|
||||
return Value.losslessConvert(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
return Value.fastDecodeIfPresent(from: sqliteStatement, atUncheckedIndex: Int32(index))
|
||||
}
|
||||
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
return statementRef.takeUnretainedValue().columnNames[index]
|
||||
}
|
||||
|
||||
// This method MUST be case-insensitive, and returns the index of the
|
||||
// leftmost column that matches *name*.
|
||||
func index(ofColumn name: String) -> Int? {
|
||||
if let index = lowercaseColumnIndexes[name] {
|
||||
return index
|
||||
@@ -1397,11 +1422,6 @@ private struct EmptyRowImpl : RowImpl {
|
||||
fatalError("row index out of range")
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
// Programmer error
|
||||
fatalError("row index out of range")
|
||||
}
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
// Programmer error
|
||||
fatalError("row index out of range")
|
||||
|
||||
@@ -501,19 +501,29 @@ struct AdaptedRowImpl : RowImpl {
|
||||
return base.impl.databaseValue(atUncheckedIndex: mappedIndex)
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value {
|
||||
func fastDecode<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value
|
||||
{
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.fastValue(atUncheckedIndex: mappedIndex)
|
||||
return Value.fastDecode(from: base, atUncheckedIndex: mappedIndex)
|
||||
}
|
||||
|
||||
func fastValue<Value: DatabaseValueConvertible & StatementColumnConvertible>(atUncheckedIndex index: Int) -> Value? {
|
||||
func fastDecodeIfPresent<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
_ type: Value.Type,
|
||||
atUncheckedIndex index: Int,
|
||||
conversionContext: @autoclosure () -> ValueConversionContext?) -> Value?
|
||||
{
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.fastValue(atUncheckedIndex: mappedIndex)
|
||||
return Value.fastDecodeIfPresent(from: base, atUncheckedIndex: mappedIndex)
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int, conversionContext: @autoclosure () -> ValueConversionContext?) -> Data? {
|
||||
let mappedIndex = mapping.baseColumnIndex(atMappingIndex: index)
|
||||
return base.impl.dataNoCopy(atUncheckedIndex: mappedIndex)
|
||||
return base.impl.dataNoCopy(
|
||||
atUncheckedIndex: mappedIndex,
|
||||
conversionContext: conversionContext)
|
||||
}
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
|
||||
@@ -356,12 +356,12 @@ public final class SelectStatement : Statement {
|
||||
/// Creates a cursor over the statement. This cursor does not produce any
|
||||
/// value, and is only intended to give access to the sqlite3_step()
|
||||
/// low-level function.
|
||||
func cursor(arguments: StatementArguments? = nil) -> StatementCursor {
|
||||
func makeCursor(arguments: StatementArguments? = nil) -> StatementCursor {
|
||||
return StatementCursor(statement: self, arguments: arguments)
|
||||
}
|
||||
|
||||
/// Utility function for cursors
|
||||
func cursorReset(arguments: StatementArguments? = nil) {
|
||||
func reset(withArguments arguments: StatementArguments? = nil) {
|
||||
SchedulingWatchdog.preconditionValidQueue(database)
|
||||
prepare(withArguments: arguments)
|
||||
reset()
|
||||
@@ -371,12 +371,13 @@ public final class SelectStatement : Statement {
|
||||
// Hide AuthorizedStatement from Jazzy
|
||||
extension SelectStatement: AuthorizedStatement { }
|
||||
|
||||
// TODO: remove public qualifier, or expose SelectStatement.makeCursor()
|
||||
/// A cursor that iterates a database statement without producing any value.
|
||||
/// For example:
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// let statement = db.makeSelectStatement("SELECT * FROM player")
|
||||
/// let cursor: StatementCursor = statement.cursor()
|
||||
/// let cursor: StatementCursor = statement.makeCursor()
|
||||
/// }
|
||||
public final class StatementCursor: Cursor {
|
||||
public let statement: SelectStatement
|
||||
@@ -387,12 +388,16 @@ public final class StatementCursor: Cursor {
|
||||
fileprivate init(statement: SelectStatement, arguments: StatementArguments? = nil) {
|
||||
self.statement = statement
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Void? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
|
||||
@@ -50,27 +50,6 @@ public protocol StatementColumnConvertible {
|
||||
init(sqliteStatement: SQLiteStatement, index: Int32)
|
||||
}
|
||||
|
||||
extension StatementColumnConvertible {
|
||||
|
||||
@inline(__always)
|
||||
static func losslessConvert(sqliteStatement: SQLiteStatement, index: Int32) -> Self? {
|
||||
guard sqlite3_column_type(sqliteStatement, index) != SQLITE_NULL else {
|
||||
return nil
|
||||
}
|
||||
return self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
static func losslessConvert(sqliteStatement: SQLiteStatement, index: Int32) -> Self {
|
||||
guard sqlite3_column_type(sqliteStatement, index) != SQLITE_NULL else {
|
||||
// Programmer error
|
||||
fatalError("could not convert database value NULL to \(Self.self)")
|
||||
}
|
||||
return self.init(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// A cursor of database values extracted from a single column.
|
||||
/// For example:
|
||||
///
|
||||
@@ -80,7 +59,7 @@ extension StatementColumnConvertible {
|
||||
/// print(name)
|
||||
/// }
|
||||
/// }
|
||||
public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
private let statement: SelectStatement
|
||||
private let sqliteStatement: SQLiteStatement
|
||||
private let columnIndex: Int32
|
||||
@@ -88,28 +67,32 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
|
||||
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
self.statement = statement
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
self.columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
} else {
|
||||
self.columnIndex = 0
|
||||
}
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Value? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
var error = "could not convert database value NULL to \(Value.self) with statement `\(statement.sql)`"
|
||||
if !statement.arguments.isEmpty {
|
||||
error += " arguments \(statement.arguments)"
|
||||
}
|
||||
fatalError(error)
|
||||
}
|
||||
return Value(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return Value.fastDecode(
|
||||
from: sqliteStatement,
|
||||
index: columnIndex,
|
||||
conversionContext: ValueConversionContext(statement).atColumn(Int(columnIndex)))
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
@@ -117,6 +100,9 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "FastDatabaseValueCursor")
|
||||
public typealias ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastDatabaseValueCursor<Value>
|
||||
|
||||
/// A cursor of optional database values extracted from a single column.
|
||||
/// For example:
|
||||
///
|
||||
@@ -126,7 +112,7 @@ public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColum
|
||||
/// print(email ?? "<NULL>")
|
||||
/// }
|
||||
/// }
|
||||
public final class NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
public final class FastNullableDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
private let statement: SelectStatement
|
||||
private let sqliteStatement: SQLiteStatement
|
||||
private let columnIndex: Int32
|
||||
@@ -134,24 +120,29 @@ public final class NullableColumnCursor<Value: DatabaseValueConvertible & Statem
|
||||
|
||||
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
self.statement = statement
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
self.columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement))
|
||||
} else {
|
||||
self.columnIndex = 0
|
||||
}
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Value?? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
return .some(nil)
|
||||
}
|
||||
return Value(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
return Value.fastDecodeIfPresent(from: sqliteStatement, atUncheckedIndex: columnIndex)
|
||||
case let code:
|
||||
statement.database.selectStatementDidFail(statement)
|
||||
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
|
||||
@@ -159,6 +150,9 @@ public final class NullableColumnCursor<Value: DatabaseValueConvertible & Statem
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "FastNullableDatabaseValueCursor")
|
||||
public typealias NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> = FastNullableDatabaseValueCursor<Value>
|
||||
|
||||
/// Types that adopt both DatabaseValueConvertible and
|
||||
/// StatementColumnConvertible can be efficiently initialized from
|
||||
/// database values.
|
||||
@@ -188,8 +182,8 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor<Self> {
|
||||
return try ColumnCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor<Self> {
|
||||
return try FastDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -220,7 +214,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? {
|
||||
// fetchOne returns nil if there is no row, or if there is a row with a null value
|
||||
let cursor = try NullableColumnCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
|
||||
let cursor = try FastNullableDatabaseValueCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
|
||||
return try cursor.next() ?? nil
|
||||
}
|
||||
}
|
||||
@@ -248,7 +242,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> ColumnCursor<Self> {
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor<Self> {
|
||||
return try fetchCursor(db, SQLRequest<Void>(sql, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
@@ -305,7 +299,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// - request: A FetchRequest.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> ColumnCursor<Self> {
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> FastDatabaseValueCursor<Self> {
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
return try fetchCursor(statement, adapter: adapter)
|
||||
}
|
||||
@@ -361,7 +355,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public func fetchCursor(_ db: Database) throws -> ColumnCursor<RowDecoder> {
|
||||
public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor<RowDecoder> {
|
||||
return try RowDecoder.fetchCursor(db, self)
|
||||
}
|
||||
|
||||
@@ -427,8 +421,8 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor<Wrapped> {
|
||||
return try NullableColumnCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
return try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
|
||||
}
|
||||
|
||||
/// Returns an array of optional values fetched from a prepared statement.
|
||||
@@ -470,7 +464,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableColumnCursor<Wrapped> {
|
||||
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
return try fetchCursor(db, SQLRequest<Void>(sql, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
@@ -512,7 +506,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv
|
||||
/// - request: A FetchRequest.
|
||||
/// - returns: A cursor over fetched optional values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> NullableColumnCursor<Wrapped> {
|
||||
public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R) throws -> FastNullableDatabaseValueCursor<Wrapped> {
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
return try fetchCursor(statement, adapter: adapter)
|
||||
}
|
||||
@@ -553,7 +547,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: A cursor over fetched values.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public func fetchCursor(_ db: Database) throws -> NullableColumnCursor<RowDecoder._Wrapped> {
|
||||
public func fetchCursor(_ db: Database) throws -> FastNullableDatabaseValueCursor<RowDecoder._Wrapped> {
|
||||
return try Optional<RowDecoder._Wrapped>.fetchCursor(db, self)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,20 +15,117 @@ private struct DatabaseValueDecodingContainer: SingleValueDecodingContainer {
|
||||
/// - returns: A value of the requested type.
|
||||
/// - throws: `DecodingError.typeMismatch` if the encountered encoded value cannot be converted to the requested type.
|
||||
/// - throws: `DecodingError.valueNotFound` if the encountered encoded value is null.
|
||||
func decode(_ type: Bool.Type) throws -> Bool { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Int.Type) throws -> Int { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Int8.Type) throws -> Int8 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Int16.Type) throws -> Int16 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Int32.Type) throws -> Int32 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Int64.Type) throws -> Int64 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: UInt.Type) throws -> UInt { return dbValue.losslessConvert() }
|
||||
func decode(_ type: UInt8.Type) throws -> UInt8 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: UInt16.Type) throws -> UInt16 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: UInt32.Type) throws -> UInt32 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: UInt64.Type) throws -> UInt64 { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Float.Type) throws -> Float { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Double.Type) throws -> Double { return dbValue.losslessConvert() }
|
||||
func decode(_ type: String.Type) throws -> String { return dbValue.losslessConvert() }
|
||||
func decode(_ type: Bool.Type) throws -> Bool {
|
||||
if let result = Bool.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Int.Type) throws -> Int {
|
||||
if let result = Int.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Int8.Type) throws -> Int8 {
|
||||
if let result = Int8.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Int16.Type) throws -> Int16 {
|
||||
if let result = Int16.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Int32.Type) throws -> Int32 {
|
||||
if let result = Int32.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Int64.Type) throws -> Int64 {
|
||||
if let result = Int64.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: UInt.Type) throws -> UInt {
|
||||
if let result = UInt.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: UInt8.Type) throws -> UInt8 {
|
||||
if let result = UInt8.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: UInt16.Type) throws -> UInt16 {
|
||||
if let result = UInt16.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: UInt32.Type) throws -> UInt32 {
|
||||
if let result = UInt32.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: UInt64.Type) throws -> UInt64 {
|
||||
if let result = UInt64.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Float.Type) throws -> Float {
|
||||
if let result = Float.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: Double.Type) throws -> Double {
|
||||
if let result = Double.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func decode(_ type: String.Type) throws -> String {
|
||||
if let result = String.fromDatabaseValue(dbValue) {
|
||||
return result
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a single value of the given type.
|
||||
///
|
||||
@@ -41,7 +138,11 @@ private struct DatabaseValueDecodingContainer: SingleValueDecodingContainer {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows custom database decoding, such as decoding Date from
|
||||
// String, for example.
|
||||
return type.fromDatabaseValue(dbValue) as! T
|
||||
if let result = type.fromDatabaseValue(dbValue) {
|
||||
return result as! T
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(in: self, debugDescription: "value mismatch")
|
||||
}
|
||||
} else {
|
||||
return try T(from: DatabaseValueDecoder(dbValue: dbValue, codingPath: codingPath))
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
}
|
||||
}
|
||||
try db.makeSelectStatement("SELECT * FROM document WHERE document MATCH ?")
|
||||
.cursor(arguments: [rawPattern])
|
||||
.makeCursor(arguments: [rawPattern])
|
||||
.next() // error on next() for invalid patterns
|
||||
}
|
||||
} catch let error as DatabaseError {
|
||||
|
||||
@@ -70,15 +70,17 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
/// these states can be distinguished with a contains(_:) call.
|
||||
func decodeIfPresent<T>(_ type: T.Type, forKey key: Key) throws -> T? where T : Decodable {
|
||||
let row = decoder.row
|
||||
|
||||
let keyName = key.stringValue
|
||||
|
||||
// Column?
|
||||
if row.hasColumn(key.stringValue) {
|
||||
let dbValue: DatabaseValue = row[key.stringValue]
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.fromDatabaseValue(dbValue) as! T?
|
||||
} else if dbValue.isNull {
|
||||
if let index = row.index(ofColumn: keyName) {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type {
|
||||
return type.fastDecodeIfPresent(from: row, atUncheckedIndex: index) as! T?
|
||||
} else if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
return type.decodeIfPresent(from: row, atUncheckedIndex: index) as! T?
|
||||
} else if row.impl.hasNull(atUncheckedIndex: index) {
|
||||
return nil
|
||||
} else {
|
||||
do {
|
||||
@@ -109,7 +111,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
}
|
||||
|
||||
// Scope?
|
||||
if let scopedRow = row.scopesTree[key.stringValue], scopedRow.containsNonNullValue {
|
||||
if let scopedRow = row.scopesTree[keyName], scopedRow.containsNonNullValue {
|
||||
if let type = T.self as? FetchableRecord.Type {
|
||||
// Prefer FetchableRecord decoding over Decodable.
|
||||
// This allows custom row decoding
|
||||
@@ -132,14 +134,16 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
/// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the given key.
|
||||
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable {
|
||||
let row = decoder.row
|
||||
|
||||
let keyName = key.stringValue
|
||||
|
||||
// Column?
|
||||
if row.hasColumn(key.stringValue) {
|
||||
let dbValue: DatabaseValue = row[key.stringValue]
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.fromDatabaseValue(dbValue) as! T
|
||||
if let index = row.index(ofColumn: keyName) {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type {
|
||||
return type.fastDecode(from: row, atUncheckedIndex: index) as! T
|
||||
} else if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
return type.decode(from: row, atUncheckedIndex: index) as! T
|
||||
} else {
|
||||
do {
|
||||
// This decoding will fail for types that need a keyed container,
|
||||
@@ -167,9 +171,9 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Scope?
|
||||
if let scopedRow = row.scopesTree[key.stringValue] {
|
||||
if let scopedRow = row.scopesTree[keyName] {
|
||||
if let type = T.self as? FetchableRecord.Type {
|
||||
// Prefer FetchableRecord decoding over Decodable.
|
||||
// This allows custom row decoding
|
||||
@@ -275,7 +279,7 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer {
|
||||
if let type = T.self as? DatabaseValueConvertible.Type {
|
||||
// Prefer DatabaseValueConvertible decoding over Decodable.
|
||||
// This allows decoding Date from String, or DatabaseValue from NULL.
|
||||
return type.fromDatabaseValue(row[column.stringValue]) as! T
|
||||
return type.decode(from: row[column.stringValue], conversionContext: ValueConversionContext(row).atColumn(column.stringValue)) as! T
|
||||
} else {
|
||||
return try T(from: RowDecoder(row: row, codingPath: [column]))
|
||||
}
|
||||
|
||||
@@ -48,12 +48,16 @@ public final class RecordCursor<Record: FetchableRecord> : Cursor {
|
||||
self.statement = statement
|
||||
self.row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
statement.cursorReset(arguments: arguments)
|
||||
statement.reset(withArguments: arguments)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func next() throws -> Record? {
|
||||
if done { return nil }
|
||||
if done {
|
||||
// make sure this instance never yields a value again, even if the
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
}
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
done = true
|
||||
|
||||
@@ -46,6 +46,7 @@ extension Optional : _OptionalProtocol {
|
||||
// MARK: - Internal
|
||||
|
||||
/// Reserved for GRDB: do not use.
|
||||
@inline(__always)
|
||||
func GRDBPrecondition(_ condition: @autoclosure() -> Bool, _ message: @autoclosure() -> String = "", file: StaticString = #file, line: UInt = #line) {
|
||||
/// Custom precondition function which aims at solving
|
||||
/// https://bugs.swift.org/browse/SR-905 and
|
||||
|
||||
@@ -185,6 +185,12 @@
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644DE7C20F8C903001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7A20F8C903001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE7D20F8C903001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7A20F8C903001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE8220F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
5644DE8320F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
5644DE8420F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
5644DE8520F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
564E73EF203DA2A2000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F0203DA2A3000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F1203DA2A3000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
@@ -959,6 +965,8 @@
|
||||
5634B1061CF9B970005360B9 /* TransactionObserverSavepointsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverSavepointsTests.swift; sourceTree = "<group>"; };
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRequest.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
5644DE7A20F8C903001FFDDE /* DatabaseValueConversion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversion.swift; sourceTree = "<group>"; };
|
||||
5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversionErrorTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73EB203DA29B000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1609,6 +1617,7 @@
|
||||
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */,
|
||||
566A842F20413DCF00E50BFD /* DatabaseSnapshotTests.swift */,
|
||||
56A238131B9C74A90082EB20 /* DatabaseTests.swift */,
|
||||
5644DE8120F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift */,
|
||||
56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */,
|
||||
56A238191B9C74A90082EB20 /* DatabaseValueConvertible */,
|
||||
56A238181B9C74A90082EB20 /* DatabaseValueTests.swift */,
|
||||
@@ -1705,6 +1714,7 @@
|
||||
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */,
|
||||
566A842720413D6E00E50BFD /* DatabaseSnapshot.swift */,
|
||||
56A238751B9C75030082EB20 /* DatabaseValue.swift */,
|
||||
5644DE7A20F8C903001FFDDE /* DatabaseValueConversion.swift */,
|
||||
560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */,
|
||||
563363C31C942C37000BE133 /* DatabaseWriter.swift */,
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */,
|
||||
@@ -2196,6 +2206,7 @@
|
||||
560FC5311CB003810014AA8E /* TableRecord.swift in Sources */,
|
||||
56DAA2DC1DE9C827006E10C8 /* Cursor.swift in Sources */,
|
||||
5674A6F11F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */,
|
||||
5644DE7C20F8C903001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
560FC5321CB003810014AA8E /* DatabasePool.swift in Sources */,
|
||||
560FC5331CB003810014AA8E /* Migration.swift in Sources */,
|
||||
560FC5341CB003810014AA8E /* QueryInterfaceQuery.swift in Sources */,
|
||||
@@ -2344,6 +2355,7 @@
|
||||
560FC58F1CB00B880014AA8E /* DatabaseReaderTests.swift in Sources */,
|
||||
5653EBD020961FE800F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
560FC5901CB00B880014AA8E /* RecordEditedTests.swift in Sources */,
|
||||
5644DE8220F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
560FC5911CB00B880014AA8E /* RecordPrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
56CC924C201E059100CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
562393311DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */,
|
||||
@@ -2506,6 +2518,7 @@
|
||||
567156501CB16729007DC145 /* RecordPrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
5653EBD120961FE800F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
567156511CB16729007DC145 /* FetchableRecordTests.swift in Sources */,
|
||||
5644DE8320F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
56A8C2441D1918EE0096E9D4 /* FoundationUUIDTests.swift in Sources */,
|
||||
56CC924D201E059100CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
562393321DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */,
|
||||
@@ -2630,6 +2643,7 @@
|
||||
56AFCA061CB1A8BB00F48B96 /* Configuration.swift in Sources */,
|
||||
56DAA2DF1DE9C827006E10C8 /* Cursor.swift in Sources */,
|
||||
5674A6EE1F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */,
|
||||
5644DE7D20F8C903001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
56AFCA071CB1A8BB00F48B96 /* DatabasePool.swift in Sources */,
|
||||
56AFCA081CB1A8BB00F48B96 /* Statement.swift in Sources */,
|
||||
56AFCA091CB1A8BB00F48B96 /* QueryInterfaceQuery.swift in Sources */,
|
||||
@@ -2778,6 +2792,7 @@
|
||||
56A8C2491D1918F10096E9D4 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
5653EBD220961FE800F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
56AFCA5E1CB1AA9900F48B96 /* FetchableRecordTests.swift in Sources */,
|
||||
5644DE8420F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
56AFCA5F1CB1AA9900F48B96 /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
56AFCA601CB1AA9900F48B96 /* RecordPrimaryKeyRowIDTests.swift in Sources */,
|
||||
56CC924E201E059100CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
@@ -2940,6 +2955,7 @@
|
||||
5657AB3C1D108BA9006283EF /* FoundationDataTests.swift in Sources */,
|
||||
5653EBD320961FE800F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
5634B10C1CF9B970005360B9 /* TransactionObserverSavepointsTests.swift in Sources */,
|
||||
5644DE8520F8D1E4001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
56AFCAB51CB1ABC800F48B96 /* DatabaseQueueReadOnlyTests.swift in Sources */,
|
||||
56AFCAB61CB1ABC800F48B96 /* DatabaseReaderTests.swift in Sources */,
|
||||
56CC924F201E059100CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
|
||||
@@ -45,6 +45,10 @@
|
||||
5636E9C11D22574100B9B05F /* FetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5636E9BB1D22574100B9B05F /* FetchRequest.swift */; };
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644DE7820F8C8EA001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7620F8C8E9001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE7920F8C8EA001FFDDE /* DatabaseValueConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7620F8C8E9001FFDDE /* DatabaseValueConversion.swift */; };
|
||||
5644DE7F20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7E20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
5644DE8020F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5644DE7E20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift */; };
|
||||
564E73F3203DA2AC000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73E7203DA278000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F4203DA2AD000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73E7203DA278000C443C /* JoinSupportTests.swift */; };
|
||||
564F9C211F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
@@ -630,6 +634,8 @@
|
||||
5634B1061CF9B970005360B9 /* TransactionObserverSavepointsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverSavepointsTests.swift; sourceTree = "<group>"; };
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRequest.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
5644DE7620F8C8E9001FFDDE /* DatabaseValueConversion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversion.swift; sourceTree = "<group>"; };
|
||||
5644DE7E20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversionErrorTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73E7203DA278000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1253,6 +1259,7 @@
|
||||
56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */,
|
||||
566A843420413DE400E50BFD /* DatabaseSnapshotTests.swift */,
|
||||
56A238131B9C74A90082EB20 /* DatabaseTests.swift */,
|
||||
5644DE7E20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift */,
|
||||
56A2381B1B9C74A90082EB20 /* DatabaseValueConversionTests.swift */,
|
||||
56A238191B9C74A90082EB20 /* DatabaseValueConvertible */,
|
||||
56A238181B9C74A90082EB20 /* DatabaseValueTests.swift */,
|
||||
@@ -1349,6 +1356,7 @@
|
||||
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */,
|
||||
566A842B20413D9A00E50BFD /* DatabaseSnapshot.swift */,
|
||||
56A238751B9C75030082EB20 /* DatabaseValue.swift */,
|
||||
5644DE7620F8C8E9001FFDDE /* DatabaseValueConversion.swift */,
|
||||
560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */,
|
||||
563363C31C942C37000BE133 /* DatabaseWriter.swift */,
|
||||
5636E9BB1D22574100B9B05F /* FetchRequest.swift */,
|
||||
@@ -1869,6 +1877,7 @@
|
||||
F3BA801D1CFB288C003DC1BA /* DatabaseDateComponents.swift in Sources */,
|
||||
5674A6ED1F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */,
|
||||
5657AB141D10899D006283EF /* URL.swift in Sources */,
|
||||
5644DE7920F8C8EA001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
56DAA2E01DE9C827006E10C8 /* Cursor.swift in Sources */,
|
||||
F3BA800C1CFB286F003DC1BA /* DatabaseError.swift in Sources */,
|
||||
566B91301FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */,
|
||||
@@ -2017,6 +2026,7 @@
|
||||
F3BA80D61CFB2FFD003DC1BA /* DatabaseReaderTests.swift in Sources */,
|
||||
5653EB7520961FB200F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
5698AC9D1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */,
|
||||
5644DE8020F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
56A4CDB71D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */,
|
||||
56CC924A201E058900CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
56B964D21DA521450002DA19 /* FTS5RecordTests.swift in Sources */,
|
||||
@@ -2141,6 +2151,7 @@
|
||||
F3BA80791CFB2E61003DC1BA /* DatabaseDateComponents.swift in Sources */,
|
||||
5674A6EC1F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */,
|
||||
5657AB111D10899D006283EF /* URL.swift in Sources */,
|
||||
5644DE7820F8C8EA001FFDDE /* DatabaseValueConversion.swift in Sources */,
|
||||
56DAA2DD1DE9C827006E10C8 /* Cursor.swift in Sources */,
|
||||
F3BA80681CFB2E55003DC1BA /* DatabaseError.swift in Sources */,
|
||||
566B912D1FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */,
|
||||
@@ -2289,6 +2300,7 @@
|
||||
F3BA81321CFB3064003DC1BA /* RecordPrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
5653EB7420961FB200F46237 /* AssociationRowScopeSearchTests.swift in Sources */,
|
||||
5623935A1DEE013C00A6B01F /* FilterCursorTests.swift in Sources */,
|
||||
5644DE7F20F8D1D1001FFDDE /* DatabaseValueConversionErrorTests.swift in Sources */,
|
||||
F3BA80FC1CFB3021003DC1BA /* UpdateStatementTests.swift in Sources */,
|
||||
56CC9249201E058900CB597E /* DropFirstCursorTests.swift in Sources */,
|
||||
5623936C1DEE0CD200A6B01F /* FlattenCursorTests.swift in Sources */,
|
||||
|
||||
@@ -319,10 +319,10 @@ test_performance: Realm FMDB SQLite.swift
|
||||
-scheme GRDBOSXPerformanceComparisonTests \
|
||||
build-for-testing test-without-building
|
||||
|
||||
Realm: Tests/Performance/Realm/build/osx/swift-4.1/RealmSwift.framework
|
||||
Realm: Tests/Performance/Realm/build/osx/swift-4.1.2/RealmSwift.framework
|
||||
|
||||
# Makes sure the Tests/Performance/Realm submodule has been downloaded, and Realm framework has been built.
|
||||
Tests/Performance/Realm/build/osx/swift-4.1/RealmSwift.framework:
|
||||
Tests/Performance/Realm/build/osx/swift-4.1.2/RealmSwift.framework:
|
||||
$(GIT) submodule update --init --recursive Tests/Performance/Realm
|
||||
cd Tests/Performance/Realm && sh build.sh osx-swift
|
||||
|
||||
|
||||
+1
-1
Submodule SQLCipher/src updated: 4f5c99d129...f18dee2e56.
@@ -205,7 +205,7 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
var cursor: ColumnCursor<Int>? = nil
|
||||
var cursor: FastDatabaseValueCursor<Int>? = nil
|
||||
do {
|
||||
try! makeDatabasePool().write { db in
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -168,7 +168,7 @@ class DatabasePoolSchemaCacheTests : GRDBTestCase {
|
||||
_ = s1.wait(timeout: .distantFuture)
|
||||
try! dbPool.read { db in
|
||||
// activate snapshot isolation so that foo table is visible during the whole read. Any read is enough.
|
||||
try db.makeSelectStatement("SELECT * FROM sqlite_master").cursor().next()
|
||||
try db.makeSelectStatement("SELECT * FROM sqlite_master").makeCursor().next()
|
||||
// warm cache
|
||||
_ = try db.primaryKey("foo")
|
||||
// cache contains the primary key
|
||||
|
||||
@@ -120,7 +120,7 @@ class DatabaseQueueReleaseMemoryTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
var cursor: ColumnCursor<Int>? = nil
|
||||
var cursor: FastDatabaseValueCursor<Int>? = nil
|
||||
do {
|
||||
try! makeDatabaseQueue().inDatabase { db in
|
||||
try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -0,0 +1,413 @@
|
||||
import XCTest
|
||||
#if GRDBCIPHER
|
||||
@testable import GRDBCipher
|
||||
#elseif GRDBCUSTOMSQLITE
|
||||
@testable import GRDBCustomSQLite
|
||||
#else
|
||||
@testable import GRDB
|
||||
#endif
|
||||
|
||||
// Those tests are tightly coupled to GRDB decoding code.
|
||||
// Each test comes with the (commented) crashing code snippets that trigger it.
|
||||
class DatabaseValueConversionErrorTests: GRDBTestCase {
|
||||
func testFetchableRecord1() throws {
|
||||
struct Record: FetchableRecord {
|
||||
var name: String
|
||||
|
||||
init(row: Row) {
|
||||
name = row["name"]
|
||||
}
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// conversion error
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS name")
|
||||
statement.arguments = [nil]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL], sql: `SELECT ? AS name`, arguments: [NULL])")
|
||||
}
|
||||
}
|
||||
|
||||
// missing column
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS unused")
|
||||
statement.arguments = ["ignored"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"], sql: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchableRecord2() throws {
|
||||
enum Value: String, DatabaseValueConvertible, Decodable {
|
||||
case valid
|
||||
}
|
||||
|
||||
struct Record: FetchableRecord {
|
||||
var value: Value
|
||||
|
||||
init(row: Row) {
|
||||
value = row["value"]
|
||||
}
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// conversion error
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT 1, ? AS value")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [1:1 value:\"invalid\"], sql: `SELECT 1, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
// missing column
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS unused")
|
||||
statement.arguments = ["ignored"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], sql: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDecodableFetchableRecord1() throws {
|
||||
struct Record: Decodable, FetchableRecord {
|
||||
var name: String
|
||||
var team: String
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// conversion error
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS team")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], sql: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
// missing column
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS unused")
|
||||
statement.arguments = ["ignored"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not read String from missing column `name` (row: [unused:\"ignored\"], sql: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDecodableFetchableRecord2() throws {
|
||||
enum Value: String, DatabaseValueConvertible, Decodable {
|
||||
case valid
|
||||
}
|
||||
|
||||
struct Record: Decodable, FetchableRecord {
|
||||
var value: Value
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// conversion error
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS value")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], sql: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
// missing column
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS unused")
|
||||
statement.arguments = ["ignored"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], sql: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDecodableFetchableRecord3() throws {
|
||||
enum Value: String, Decodable {
|
||||
case valid
|
||||
}
|
||||
|
||||
struct Record: Decodable, FetchableRecord {
|
||||
var value: Value
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// conversion error
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS value")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `value`, column index: 1, row: [name:NULL value:\"invalid\"], sql: `SELECT NULL AS name, ? AS value`, arguments: [\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
// missing column
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT ? AS unused")
|
||||
statement.arguments = ["ignored"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = Record(row: row)
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"])")
|
||||
|
||||
// _ = try Record.fetchOne(statement)
|
||||
try Row.fetchCursor(statement).forEach { row in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["value"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("value")),
|
||||
"could not read \(Value.self) from missing column `value` (row: [unused:\"ignored\"], sql: `SELECT ? AS unused`, arguments: [\"ignored\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementColumnConvertible() throws {
|
||||
// Those tests are tightly coupled to GRDB decoding code.
|
||||
// Each test comes with one or several commented crashing code snippets that trigger it.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS team")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = try String.fetchAll(statement)
|
||||
try statement.makeCursor().forEach {
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: .null,
|
||||
conversionContext: ValueConversionContext(statement).atColumn(0)),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], sql: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
// _ = row["name"] as String
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = row[0] as String
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: String.self,
|
||||
from: row[0],
|
||||
conversionContext: ValueConversionContext(row).atColumn(0)),
|
||||
"could not convert database value NULL to String (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
}
|
||||
}
|
||||
|
||||
func testDecodableDatabaseValueConvertible() throws {
|
||||
enum Value: String, DatabaseValueConvertible, Decodable {
|
||||
case valid
|
||||
}
|
||||
|
||||
// Those tests are tightly coupled to GRDB decoding code.
|
||||
// Each test comes with one or several commented crashing code snippets that trigger it.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
let statement = try db.makeSelectStatement("SELECT NULL AS name, ? AS team")
|
||||
statement.arguments = ["invalid"]
|
||||
let row = try Row.fetchOne(statement)!
|
||||
|
||||
// _ = try Value.fetchAll(statement)
|
||||
try statement.makeCursor().forEach {
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0),
|
||||
conversionContext: ValueConversionContext(statement).atColumn(0)),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"], sql: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
// _ = try Value.fetchOne(statement, adapter: SuffixRowAdapter(fromIndex: 1))
|
||||
let adapter = SuffixRowAdapter(fromIndex: 1)
|
||||
let columnIndex = try adapter.baseColumnIndex(atIndex: 0, layout: statement)
|
||||
try statement.makeCursor().forEach { _ in
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: DatabaseValue(sqliteStatement: statement.sqliteStatement, index: Int32(columnIndex)),
|
||||
conversionContext: ValueConversionContext(statement).atColumn(columnIndex)),
|
||||
"could not convert database value \"invalid\" to \(Value.self) (column: `team`, column index: 1, row: [name:NULL team:\"invalid\"], sql: `SELECT NULL AS name, ? AS team`, arguments: [\"invalid\"])")
|
||||
}
|
||||
|
||||
// _ = row["name"] as Value
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row["name"],
|
||||
conversionContext: ValueConversionContext(row).atColumn("name")),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
|
||||
// _ = row[0] as Value
|
||||
XCTAssertEqual(
|
||||
conversionErrorMessage(
|
||||
to: Value.self,
|
||||
from: row[0],
|
||||
conversionContext: ValueConversionContext(row).atColumn(0)),
|
||||
"could not convert database value NULL to \(Value.self) (column: `name`, column index: 0, row: [name:NULL team:\"invalid\"])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import XCTest
|
||||
#endif
|
||||
|
||||
class DatabaseValueConvertibleDecodableTests: GRDBTestCase {
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromDecodable() {
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromDecodable1() throws {
|
||||
struct Value : Decodable, DatabaseValueConvertible {
|
||||
let string: String
|
||||
|
||||
@@ -17,15 +17,122 @@ class DatabaseValueConvertibleDecodableTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
// static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? { ... }
|
||||
}
|
||||
|
||||
let value = Value.fromDatabaseValue("foo".databaseValue)!
|
||||
XCTAssertEqual(value.string, "foo")
|
||||
do {
|
||||
// Success from DatabaseValue
|
||||
let value = Value.fromDatabaseValue("foo".databaseValue)!
|
||||
XCTAssertEqual(value.string, "foo")
|
||||
}
|
||||
do {
|
||||
// Success from database
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let value = try Value.fetchOne(db, "SELECT 'foo'")!
|
||||
XCTAssertEqual(value.string, "foo")
|
||||
}
|
||||
}
|
||||
do {
|
||||
// Failure from DatabaseValue
|
||||
let value = Value.fromDatabaseValue(1.databaseValue)
|
||||
XCTAssertNil(value)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromDecodable2() throws {
|
||||
struct Value : Decodable, DatabaseValueConvertible {
|
||||
let string: String
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
string = try decoder.singleValueContainer().decode(String.self)
|
||||
}
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
}
|
||||
|
||||
struct ValueWrapper : Decodable, DatabaseValueConvertible {
|
||||
let value: Value
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
value = try decoder.singleValueContainer().decode(Value.self)
|
||||
}
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
// static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? { ... }
|
||||
}
|
||||
|
||||
do {
|
||||
// Success from DatabaseValue
|
||||
let wrapper = ValueWrapper.fromDatabaseValue("foo".databaseValue)!
|
||||
XCTAssertEqual(wrapper.value.string, "foo")
|
||||
}
|
||||
do {
|
||||
// Success from database
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let wrapper = try ValueWrapper.fetchOne(db, "SELECT 'foo'")!
|
||||
XCTAssertEqual(wrapper.value.string, "foo")
|
||||
}
|
||||
}
|
||||
do {
|
||||
// Failure from DatabaseValue
|
||||
let wrapper = ValueWrapper.fromDatabaseValue(1.databaseValue)
|
||||
XCTAssertNil(wrapper)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromDecodable3() throws {
|
||||
struct ValueWrapper : Decodable, DatabaseValueConvertible {
|
||||
struct Nested : Decodable {
|
||||
let string: String
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
string = try decoder.singleValueContainer().decode(String.self)
|
||||
}
|
||||
}
|
||||
let nested: Nested
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
nested = try decoder.singleValueContainer().decode(Nested.self)
|
||||
}
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
// static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? { ... }
|
||||
}
|
||||
|
||||
do {
|
||||
// Success from DatabaseValue
|
||||
let wrapper = ValueWrapper.fromDatabaseValue("foo".databaseValue)!
|
||||
XCTAssertEqual(wrapper.nested.string, "foo")
|
||||
}
|
||||
do {
|
||||
// Success from database
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let wrapper = try ValueWrapper.fetchOne(db, "SELECT 'foo'")!
|
||||
XCTAssertEqual(wrapper.nested.string, "foo")
|
||||
}
|
||||
}
|
||||
do {
|
||||
// Failure from DatabaseValue
|
||||
let wrapper = ValueWrapper.fromDatabaseValue(1.databaseValue)
|
||||
XCTAssertNil(wrapper)
|
||||
}
|
||||
}
|
||||
|
||||
func testCustomDatabaseValueConvertible() throws {
|
||||
@@ -33,7 +140,7 @@ class DatabaseValueConvertibleDecodableTests: GRDBTestCase {
|
||||
let string: String
|
||||
|
||||
var databaseValue: DatabaseValue {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
|
||||
@@ -9,7 +9,7 @@ import Foundation
|
||||
#endif
|
||||
|
||||
class DatabaseValueConvertibleEncodableTests: GRDBTestCase {
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromEncodable() {
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromEncodable1() {
|
||||
struct Value : Encodable, DatabaseValueConvertible {
|
||||
let string: String
|
||||
|
||||
@@ -19,7 +19,7 @@ class DatabaseValueConvertibleEncodableTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
@@ -30,15 +30,78 @@ class DatabaseValueConvertibleEncodableTests: GRDBTestCase {
|
||||
XCTAssertEqual(dbValue.storage.value as! String, "foo")
|
||||
}
|
||||
|
||||
func testEncodableRawRepresentable() {
|
||||
// Test that the rawValue is encoded with DatabaseValueConvertible, not with Encodable
|
||||
struct Value : RawRepresentable, Encodable, DatabaseValueConvertible {
|
||||
let rawValue: Date
|
||||
}
|
||||
|
||||
let dbValue = Value(rawValue: Date()).databaseValue
|
||||
XCTAssertTrue(dbValue.storage.value is String)
|
||||
}
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromEncodable2() {
|
||||
struct Value : Encodable, DatabaseValueConvertible {
|
||||
let string: String
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(string)
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
}
|
||||
|
||||
struct Wrapper : Encodable, DatabaseValueConvertible {
|
||||
let value: Value
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(value)
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Wrapper? {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
// var databaseValue: DatabaseValue { ... }
|
||||
}
|
||||
|
||||
let dbValue = Wrapper(value: Value(string: "foo")).databaseValue
|
||||
XCTAssertEqual(dbValue.storage.value as! String, "foo")
|
||||
}
|
||||
|
||||
func testDatabaseValueConvertibleImplementationDerivedFromEncodable3() {
|
||||
struct Wrapper : Encodable, DatabaseValueConvertible {
|
||||
struct Nested : Encodable {
|
||||
let string: String
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(string)
|
||||
}
|
||||
}
|
||||
let nested: Nested
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(nested)
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Wrapper? {
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
// var databaseValue: DatabaseValue { ... }
|
||||
}
|
||||
|
||||
let dbValue = Wrapper(nested: Wrapper.Nested(string: "foo")).databaseValue
|
||||
XCTAssertEqual(dbValue.storage.value as! String, "foo")
|
||||
}
|
||||
|
||||
func testEncodableRawRepresentable() {
|
||||
// Test that the rawValue is encoded with DatabaseValueConvertible, not with Encodable
|
||||
struct Value : RawRepresentable, Encodable, DatabaseValueConvertible {
|
||||
let rawValue: Date
|
||||
}
|
||||
|
||||
let dbValue = Value(rawValue: Date()).databaseValue
|
||||
XCTAssertTrue(dbValue.storage.value is String)
|
||||
}
|
||||
|
||||
func testEncodableRawRepresentableEnum() {
|
||||
// Make sure this kind of declaration is possible
|
||||
@@ -63,7 +126,7 @@ extension DatabaseValueConvertibleEncodableTests {
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
@@ -92,7 +155,7 @@ extension DatabaseValueConvertibleEncodableTests {
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
@@ -116,7 +179,7 @@ extension DatabaseValueConvertibleEncodableTests {
|
||||
}
|
||||
|
||||
static func fromDatabaseValue(_ databaseValue: DatabaseValue) -> Value? {
|
||||
preconditionFailure("unused")
|
||||
preconditionFailure("not tested")
|
||||
}
|
||||
|
||||
// Infered, tested
|
||||
|
||||
@@ -35,6 +35,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
XCTAssertEqual(try cursor.next()!.int, 1)
|
||||
XCTAssertEqual(try cursor.next()!.int, 2)
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
do {
|
||||
let sql = "SELECT 1 UNION ALL SELECT 2"
|
||||
@@ -370,6 +371,7 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
XCTAssertEqual(try cursor.next()!!.int, 1)
|
||||
XCTAssertTrue(try cursor.next()! == nil)
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
do {
|
||||
let sql = "SELECT 1 UNION ALL SELECT NULL"
|
||||
|
||||
@@ -147,7 +147,7 @@ class GRDBTestCase: XCTestCase {
|
||||
// Compare SQL strings (ignoring leading and trailing white space and semicolons.
|
||||
func assertEqualSQL<Request: FetchRequest>(_ db: Database, _ request: Request, _ sql: String, file: StaticString = #file, line: UInt = #line) throws {
|
||||
let (statement, _) = try request.prepare(db)
|
||||
try statement.cursor().next()
|
||||
try statement.makeCursor().next()
|
||||
assertEqualSQL(lastSQLQuery, sql, file: file, line: line)
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ class GRDBTestCase: XCTestCase {
|
||||
func sql<Request: FetchRequest>(_ databaseReader: DatabaseReader, _ request: Request) -> String {
|
||||
return try! databaseReader.unsafeRead { db in
|
||||
let (statement, _) = try request.prepare(db)
|
||||
try statement.cursor().next()
|
||||
try statement.makeCursor().next()
|
||||
return lastSQLQuery
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ class RowFetchTests: GRDBTestCase {
|
||||
XCTAssertEqual(row["firstName"] as String, "Barbara")
|
||||
XCTAssertEqual(row["lastName"] as String, "Gourde")
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
do {
|
||||
let sql = "SELECT 'Arthur' AS firstName, 'Martin' AS lastName UNION ALL SELECT 'Barbara', 'Gourde'"
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import XCTest
|
||||
#if GRDBCIPHER
|
||||
import GRDBCipher
|
||||
@testable import GRDBCipher
|
||||
#elseif GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
@testable import GRDBCustomSQLite
|
||||
#else
|
||||
#if SWIFT_PACKAGE
|
||||
import CSQLite
|
||||
#else
|
||||
import SQLite3
|
||||
#endif
|
||||
import GRDB
|
||||
@testable import GRDB
|
||||
#endif
|
||||
|
||||
class SelectStatementTests : GRDBTestCase {
|
||||
@@ -32,6 +32,54 @@ class SelectStatementTests : GRDBTestCase {
|
||||
try migrator.migrate(dbWriter)
|
||||
}
|
||||
|
||||
func testStatementCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let sql = "SELECT 'Arthur' AS firstName, 'Martin' AS lastName UNION ALL SELECT 'Barbara', 'Gourde'"
|
||||
let statement = try db.makeSelectStatement(sql)
|
||||
let cursor = statement.makeCursor()
|
||||
|
||||
// Check that StatementCursor gives access to the raw SQLite API
|
||||
XCTAssertEqual(String(cString: sqlite3_column_name(cursor.statement.sqliteStatement, 0)), "firstName")
|
||||
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: StatementCursor) throws {
|
||||
let sql = cursor.statement.sql
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
|
||||
XCTAssertEqual(error.message, "\(customError)")
|
||||
XCTAssertEqual(error.sql!, sql)
|
||||
XCTAssertEqual(error.description, "SQLite error 1 with statement `\(sql)`: \(customError)")
|
||||
}
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_MISUSE)
|
||||
XCTAssertEqual(error.message, "\(customError)")
|
||||
XCTAssertEqual(error.sql!, sql)
|
||||
XCTAssertEqual(error.description, "SQLite error 21 with statement `\(sql)`: \(customError)")
|
||||
}
|
||||
}
|
||||
try test(db.makeSelectStatement("SELECT throw(), NULL").makeCursor())
|
||||
try test(db.makeSelectStatement("SELECT 0, throw(), NULL").makeCursor())
|
||||
}
|
||||
}
|
||||
|
||||
func testArrayStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
@@ -86,7 +86,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testFetchCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: ColumnCursor<Fetched>) throws {
|
||||
func test(_ cursor: FastDatabaseValueCursor<Fetched>) throws {
|
||||
var i = try cursor.next()!
|
||||
XCTAssertEqual(i.int, 1)
|
||||
XCTAssertTrue(i.fast)
|
||||
@@ -94,6 +94,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
XCTAssertEqual(i.int, 2)
|
||||
XCTAssertTrue(i.fast)
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
do {
|
||||
let sql = "SELECT 1 UNION ALL SELECT 2"
|
||||
@@ -120,7 +121,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: ColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: FastDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
@@ -161,7 +162,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testFetchCursorCompilationFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: @autoclosure () throws -> ColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: @autoclosure () throws -> FastDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor()
|
||||
XCTFail()
|
||||
@@ -426,12 +427,13 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testOptionalFetchCursor() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: NullableColumnCursor<Fetched>) throws {
|
||||
func test(_ cursor: FastNullableDatabaseValueCursor<Fetched>) throws {
|
||||
let i = try cursor.next()!
|
||||
XCTAssertEqual(i!.int, 1)
|
||||
XCTAssertTrue(i!.fast)
|
||||
XCTAssertTrue(try cursor.next()! == nil)
|
||||
XCTAssertTrue(try cursor.next() == nil) // end
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
do {
|
||||
let sql = "SELECT 1 UNION ALL SELECT NULL"
|
||||
@@ -456,7 +458,7 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
func testOptionalFetchCursorCompilationFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
func test(_ cursor: @autoclosure () throws -> NullableColumnCursor<Fetched>, sql: String) throws {
|
||||
func test(_ cursor: @autoclosure () throws -> FastNullableDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor()
|
||||
XCTFail()
|
||||
|
||||
@@ -41,7 +41,7 @@ class FetchNamedValuesTests: XCTestCase {
|
||||
#if GRDB_COMPARE
|
||||
func testFMDB() {
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "PerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)
|
||||
|
||||
measure {
|
||||
var count = 0
|
||||
|
||||
@@ -83,7 +83,7 @@ class FetchPositionalValuesTests: XCTestCase {
|
||||
#if GRDB_COMPARE
|
||||
func testFMDB() {
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "PerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)
|
||||
|
||||
measure {
|
||||
var count = 0
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import XCTest
|
||||
import SQLite3
|
||||
import GRDB
|
||||
#if GRDB_COMPARE
|
||||
import SQLite
|
||||
#endif
|
||||
|
||||
private let expectedRowCount = 100_000
|
||||
|
||||
/// Here we test the extraction of models from rows
|
||||
class FetchRecordCodableTests: XCTestCase {
|
||||
|
||||
func testGRDB() throws {
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "PerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = try DatabaseQueue(path: databasePath)
|
||||
|
||||
measure {
|
||||
let items = try! dbQueue.inDatabase { db in
|
||||
try ItemCodable.fetchAll(db, "SELECT * FROM items")
|
||||
}
|
||||
XCTAssertEqual(items.count, expectedRowCount)
|
||||
XCTAssertEqual(items[0].i0, 0)
|
||||
XCTAssertEqual(items[1].i1, 1)
|
||||
XCTAssertEqual(items[expectedRowCount-1].i9, expectedRowCount-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ class FetchRecordStructTests: XCTestCase {
|
||||
// Here we test the loading of an array of Records.
|
||||
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "PerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)
|
||||
|
||||
measure {
|
||||
var items = [ItemStruct]()
|
||||
|
||||
@@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
fetchPositionalValues()
|
||||
fetchNamedValues()
|
||||
fetchStructs()
|
||||
fetchRecords()
|
||||
insertPositionalValues()
|
||||
insertNamedValues()
|
||||
@@ -77,11 +78,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
assert(count == expectedRowCount)
|
||||
}
|
||||
|
||||
func fetchStructs() {
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "ProfilingDatabase", ofType: "sqlite")!
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
let items = dbQueue.inDatabase { db in
|
||||
try! ItemStruct.fetchAll(db)
|
||||
}
|
||||
assert(items.count == expectedRowCount)
|
||||
assert(items[0].i0 == 0)
|
||||
assert(items[1].i1 == 1)
|
||||
assert(items[expectedRowCount-1].i9 == expectedRowCount-1)
|
||||
}
|
||||
|
||||
func fetchRecords() {
|
||||
let databasePath = Bundle(for: type(of: self)).path(forResource: "ProfilingDatabase", ofType: "sqlite")!
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
let items = dbQueue.inDatabase { db in
|
||||
try! Item.fetchAll(db)
|
||||
try! ItemRecord.fetchAll(db)
|
||||
}
|
||||
assert(items.count == expectedRowCount)
|
||||
assert(items[0].i0 == 0)
|
||||
@@ -170,15 +183,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
try! dbQueue.inTransaction { db in
|
||||
for i in 0..<insertedRowCount {
|
||||
try Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
|
||||
try ItemRecord(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
|
||||
}
|
||||
return .commit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Item : Record {
|
||||
class ItemRecord : Record {
|
||||
var i0: Int?
|
||||
var i1: Int?
|
||||
var i2: Int?
|
||||
@@ -235,3 +247,57 @@ class Item : Record {
|
||||
container["i9"] = i9
|
||||
}
|
||||
}
|
||||
|
||||
struct ItemStruct: FetchableRecord, PersistableRecord {
|
||||
var i0: Int?
|
||||
var i1: Int?
|
||||
var i2: Int?
|
||||
var i3: Int?
|
||||
var i4: Int?
|
||||
var i5: Int?
|
||||
var i6: Int?
|
||||
var i7: Int?
|
||||
var i8: Int?
|
||||
var i9: Int?
|
||||
|
||||
init(i0: Int?, i1: Int?, i2: Int?, i3: Int?, i4: Int?, i5: Int?, i6: Int?, i7: Int?, i8: Int?, i9: Int?) {
|
||||
self.i0 = i0
|
||||
self.i1 = i1
|
||||
self.i2 = i2
|
||||
self.i3 = i3
|
||||
self.i4 = i4
|
||||
self.i5 = i5
|
||||
self.i6 = i6
|
||||
self.i7 = i7
|
||||
self.i8 = i8
|
||||
self.i9 = i9
|
||||
}
|
||||
|
||||
static let databaseTableName = "items"
|
||||
|
||||
init(row: GRDB.Row) {
|
||||
i0 = row["i0"]
|
||||
i1 = row["i1"]
|
||||
i2 = row["i2"]
|
||||
i3 = row["i3"]
|
||||
i4 = row["i4"]
|
||||
i5 = row["i5"]
|
||||
i6 = row["i6"]
|
||||
i7 = row["i7"]
|
||||
i8 = row["i8"]
|
||||
i9 = row["i9"]
|
||||
}
|
||||
|
||||
func encode(to container: inout PersistenceContainer) {
|
||||
container["i0"] = i0
|
||||
container["i1"] = i1
|
||||
container["i2"] = i2
|
||||
container["i3"] = i3
|
||||
container["i4"] = i4
|
||||
container["i5"] = i5
|
||||
container["i6"] = i6
|
||||
container["i7"] = i7
|
||||
container["i8"] = i8
|
||||
container["i9"] = i9
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class InsertNamedValuesTests: XCTestCase {
|
||||
measure {
|
||||
_ = try? FileManager.default.removeItem(atPath: databasePath)
|
||||
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)
|
||||
dbQueue.inDatabase { db in
|
||||
db.executeStatements("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class InsertPositionalValuesTests: XCTestCase {
|
||||
measure {
|
||||
_ = try? FileManager.default.removeItem(atPath: databasePath)
|
||||
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)!
|
||||
let dbQueue = FMDatabaseQueue(path: databasePath)
|
||||
dbQueue.inDatabase { db in
|
||||
db.executeStatements("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
private let insertedRowCount = 20_000
|
||||
|
||||
// Here we insert records.
|
||||
class InsertRecordCodableTests: XCTestCase {
|
||||
|
||||
func testGRDB() {
|
||||
let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
|
||||
let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
|
||||
_ = try? FileManager.default.removeItem(atPath: databasePath)
|
||||
defer {
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
try! dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, insertedRowCount)
|
||||
XCTAssertEqual(try Int.fetchOne(db, "SELECT MIN(i0) FROM items")!, 0)
|
||||
XCTAssertEqual(try Int.fetchOne(db, "SELECT MAX(i9) FROM items")!, insertedRowCount - 1)
|
||||
}
|
||||
try! FileManager.default.removeItem(atPath: databasePath)
|
||||
}
|
||||
|
||||
measure {
|
||||
_ = try? FileManager.default.removeItem(atPath: databasePath)
|
||||
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
try! dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE items (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
|
||||
}
|
||||
|
||||
try! dbQueue.inTransaction { db in
|
||||
for i in 0..<insertedRowCount {
|
||||
try ItemCodable(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
|
||||
}
|
||||
return .commit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,21 @@ struct ItemStruct : FetchableRecord, PersistableRecord {
|
||||
}
|
||||
}
|
||||
|
||||
struct ItemCodable : Codable, FetchableRecord, PersistableRecord {
|
||||
var i0: Int
|
||||
var i1: Int
|
||||
var i2: Int
|
||||
var i3: Int
|
||||
var i4: Int
|
||||
var i5: Int
|
||||
var i6: Int
|
||||
var i7: Int
|
||||
var i8: Int
|
||||
var i9: Int
|
||||
|
||||
static let databaseTableName = "items"
|
||||
}
|
||||
|
||||
class ItemClass : Record {
|
||||
var i0: Int
|
||||
var i1: Int
|
||||
|
||||
+1
-1
Submodule Tests/Performance/Realm updated: 13a3cb44c1...d838323410.
Submodule Tests/Performance/SQLite.swift updated: 1d2eef2570...210098546d.
+1
-1
Submodule Tests/Performance/fmdb updated: e0fcde9a0e...d02456a3c9.
@@ -27,8 +27,8 @@ end
|
||||
samples = JSON.parse(STDIN.read)
|
||||
grdb_version = info_plist_version('Support/Info.plist')
|
||||
fmdb_version = info_plist_version('Tests/Performance/fmdb/src/fmdb/Info.plist')
|
||||
sqlite_swift_version = info_plist_version('Tests/Performance/SQLite.swift/Sources/SQLite/Info.plist')
|
||||
realm_version = info_plist_version('Tests/Performance/Realm/build/osx/Realm.framework/Versions/A/Resources/Info.plist')
|
||||
sqlite_swift_version = '0.11.5' # not up-to-date: info_plist_version('Tests/Performance/SQLite.swift/Sources/SQLite/Info.plist')
|
||||
realm_version = '3.7.6' # not up-to-date: info_plist_version('Tests/Performance/Realm/build/osx/Realm.framework/Versions/A/Resources/Info.plist')
|
||||
|
||||
puts <<-REPORT
|
||||
# Comparing the Performances of Swift SQLite libraries
|
||||
@@ -37,7 +37,7 @@ puts <<-REPORT
|
||||
|
||||
Below are performance benchmarks made on for [GRDB #{grdb_version}](https://github.com/groue/GRDB.swift), [FMDB #{fmdb_version}](https://github.com/ccgus/fmdb), and [SQLite.swift #{sqlite_swift_version}](https://github.com/stephencelis/SQLite.swift). They are compared to Core Data, [Realm #{realm_version}](https://realm.io) and the raw use of the SQLite C API from Swift.
|
||||
|
||||
This report was generated on a MacBook Pro (15-inch, Late 2016), with Xcode 9.2, by running the following command:
|
||||
This report was generated on a MacBook Pro (15-inch, Late 2016), with Xcode 9.4.1, by running the following command:
|
||||
|
||||
```sh
|
||||
make test_performance | Tests/parsePerformanceTests.rb | Tests/generatePerformanceReport.rb
|
||||
@@ -48,6 +48,7 @@ All tests use the default settings of each library. For each library, we:
|
||||
- Build and consume database rows with raw SQL and column indexes (aiming at the best performance)
|
||||
- Build and consume database rows with column names (sacrificing performance for maintainability)
|
||||
- Build and consume records values to and from database rows (aiming at the shortest code from database to records)
|
||||
- Build and consume records values to and from database rows, with help from the Codable standard protocol
|
||||
- Build and consume records values to and from database rows, with [change tracking](https://github.com/groue/GRDB.swift/blob/master/README.md#record-comparison) (records know if they have unsaved changes)
|
||||
|
||||
As a bottom line, the raw SQLite C API is used as efficiently as possible, without any error checking.
|
||||
@@ -63,6 +64,9 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho
|
||||
| **Records** | | | | | | |
|
||||
| Fetch | #{formatted_samples(samples, 'FetchRecordStruct').join(" | ")} |
|
||||
| Insert | #{formatted_samples(samples, 'InsertRecordStruct').join(" | ")} |
|
||||
| **Codable Records** | | | | | | |
|
||||
| Fetch | #{formatted_samples(samples, 'FetchRecordCodable').join(" | ")} |
|
||||
| Insert | #{formatted_samples(samples, 'InsertRecordCodable').join(" | ")} |
|
||||
| **Records with change tracking** | | | | | | |
|
||||
| Fetch | #{formatted_samples(samples, 'FetchRecordClass').join(" | ")} |
|
||||
| Insert | #{formatted_samples(samples, 'InsertRecordClass').join(" | ")} |
|
||||
@@ -109,6 +113,18 @@ As a bottom line, the raw SQLite C API is used as efficiently as possible, witho
|
||||
|
||||
This tests inserts 20000 records with the persistence method provided by GRDB's [PersistableRecord](https://github.com/groue/GRDB.swift/blob/master/README.md#persistablerecord-protocol) protocol.
|
||||
|
||||
- **Codable Records**:
|
||||
|
||||
- **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/FetchRecordCodableTests.swift))
|
||||
|
||||
This test fetches an array of 100000 record objects initiated from rows of 10 ints.
|
||||
|
||||
It builds records from GRDB's built-in support for the [Decodable standard protocols](https://github.com/groue/GRDB.swift/blob/master/README.md#codable-records).
|
||||
|
||||
- **Insert** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/InsertRecordCodableTests.swift))
|
||||
|
||||
This tests inserts 20000 records with the persistence method provided by GRDB's built-in support for the [Encodable standard protocols](https://github.com/groue/GRDB.swift/blob/master/README.md#codable-records).
|
||||
|
||||
- **Records with change tracking**:
|
||||
|
||||
- **Fetch** ([source](https://github.com/groue/GRDB.swift/blob/master/Tests/Performance/FetchRecordClassTests.swift))
|
||||
|
||||
Reference in new issue
Block a user