From 87ee00c2c5c4d17a39dbf8342d189891e2d1d38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 9 Feb 2019 15:35:34 +0100 Subject: [PATCH] SQLInterpolation --- GRDB.xcodeproj/project.pbxproj | 18 +++- GRDB/QueryInterface/SQLInterpolation.swift | 83 ++++++++++++++ GRDBCipher.xcodeproj/project.pbxproj | 20 +++- GRDBCustom.xcodeproj/project.pbxproj | 16 ++- Tests/GRDBTests/SQLInterpolationTests.swift | 113 ++++++++++++++++++++ 5 files changed, 244 insertions(+), 6 deletions(-) create mode 100644 GRDB/QueryInterface/SQLInterpolation.swift create mode 100644 Tests/GRDBTests/SQLInterpolationTests.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index c96b40907..315648f6c 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -438,6 +438,8 @@ 567DAF221EAB61ED00FC0928 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 567DAF141EAB61ED00FC0928 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; }; 567DAF351EAB789800FC0928 /* DatabaseLogErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */; }; 567DAF391EAB789800FC0928 /* DatabaseLogErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */; }; + 567F0B2D220F0E2E00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B2C220F0E2E00D111FB /* SQLInterpolationTests.swift */; }; + 567F0B2E220F0E2E00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B2C220F0E2E00D111FB /* SQLInterpolationTests.swift */; }; 567F45A81F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 567F45AC1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 568068311EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; }; @@ -504,6 +506,9 @@ 5698AD381DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */; }; 5698AD3B1DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */; }; 569C1EB51CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */; }; + 569D6DDE220EF9E100A058A9 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */; }; + 569D6DDF220EF9E100A058A9 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */; }; + 569D6DE0220EF9E100A058A9 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */; }; 569EF0E2200D2D8400A9FA45 /* DatabaseRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569EF0E1200D2D8400A9FA45 /* DatabaseRegion.swift */; }; 569EF0E3200D2D8400A9FA45 /* DatabaseRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569EF0E1200D2D8400A9FA45 /* DatabaseRegion.swift */; }; 569EF0E4200D2D8400A9FA45 /* DatabaseRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569EF0E1200D2D8400A9FA45 /* DatabaseRegion.swift */; }; @@ -1053,6 +1058,7 @@ 567A80521D41350C00C7DCEC /* IndexInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexInfoTests.swift; sourceTree = ""; }; 567DAF141EAB61ED00FC0928 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = grdb_config.h; sourceTree = ""; }; 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLogErrorTests.swift; sourceTree = ""; }; + 567F0B2C220F0E2E00D111FB /* SQLInterpolationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLInterpolationTests.swift; sourceTree = ""; }; 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TruncateOptimizationTests.swift; sourceTree = ""; }; 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = ""; }; 5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = ""; }; @@ -1093,6 +1099,7 @@ 5698AD201DABAEFA0056AF8C /* FTS5WrapperTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5WrapperTokenizer.swift; sourceTree = ""; }; 5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5CustomTokenizer.swift; sourceTree = ""; }; 569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SchedulingWatchdogTests.swift; sourceTree = ""; }; + 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLInterpolation.swift; sourceTree = ""; }; 569EF0E1200D2D8400A9FA45 /* DatabaseRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseRegion.swift; sourceTree = ""; }; 56A238131B9C74A90082EB20 /* DatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTests.swift; sourceTree = ""; }; 56A238141B9C74A90082EB20 /* DatabaseQueueInMemoryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueInMemoryTests.swift; sourceTree = ""; }; @@ -1485,6 +1492,7 @@ 563EF45221631E21007DAACD /* QueryInterfacePromiseTests.swift */, 56300B5D1C53C38F005A543B /* QueryInterfaceRequestTests.swift */, 56300B841C54DC95005A543B /* Record+QueryInterfaceRequestTests.swift */, + 567F0B2C220F0E2E00D111FB /* SQLInterpolationTests.swift */, 566AD8C51D531BE4002EC1A8 /* TableDefinitionTests.swift */, 56300B6A1C53D3E8005A543B /* TableRecord+QueryInterfaceRequestTests.swift */, 5698AC7F1DA380A20056AF8C /* VirtualTableModuleTests.swift */, @@ -1509,6 +1517,7 @@ 56CEB5421EAA359A00BFAF62 /* SQLExpression.swift */, 56FC98771D969DEF00E3C842 /* SQLExpression+QueryInterface.swift */, 5653EC0B2098738B00F46237 /* SQLGenerationContext.swift */, + 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */, 56CEB5431EAA359A00BFAF62 /* SQLOrdering.swift */, 56D91AA12205E03700770D8D /* SQLRelation.swift */, 56CEB5441EAA359A00BFAF62 /* SQLSelectable.swift */, @@ -2497,6 +2506,7 @@ 5698AC3D1D9E5A590056AF8C /* FTS3Pattern.swift in Sources */, 563EF45D2163309F007DAACD /* Inflections.swift in Sources */, 56D7E453221596180052464B /* GRDB-4.0.swift in Sources */, + 569D6DE0220EF9E100A058A9 /* SQLInterpolation.swift in Sources */, 565490BF1D5AE236005622CB /* DatabaseValueConvertible.swift in Sources */, 566B91191FA4C3F50012D5B0 /* DatabaseCollation.swift in Sources */, 5613ED4E21A95C4300DC7A68 /* ValueObservation+FetchableRecord.swift in Sources */, @@ -2669,6 +2679,7 @@ 563B06AC217EF0CC00B38F35 /* ValueObservation.swift in Sources */, 56D91AAA2205F2F100770D8D /* DatabasePromise.swift in Sources */, 5698AC7B1DA37DCB0056AF8C /* VirtualTableModule.swift in Sources */, + 569D6DDF220EF9E100A058A9 /* SQLInterpolation.swift in Sources */, 56A238821B9C75030082EB20 /* DatabaseError.swift in Sources */, 5653EB0D20944C7C00F46237 /* HasManyAssociation.swift in Sources */, 56D7E452221596180052464B /* GRDB-4.0.swift in Sources */, @@ -2741,6 +2752,7 @@ 56EA63C6209C7CE3009715B8 /* DerivableRequestTests.swift in Sources */, 5674A71A1F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 5698AC071D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift in Sources */, + 567F0B2E220F0E2E00D111FB /* SQLInterpolationTests.swift in Sources */, 56C3F7561CF9F12400F6A361 /* DatabaseSavepointTests.swift in Sources */, 56B021CD1D8C0D3900B239BB /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, 563B0706218627F800B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -2921,6 +2933,7 @@ 562756431E963AAC0035B653 /* DatabaseWriterTests.swift in Sources */, 56EA63C5209C7CE3009715B8 /* DerivableRequestTests.swift in Sources */, 56D496BF1D8135D4008276D7 /* TableDefinitionTests.swift in Sources */, + 567F0B2D220F0E2E00D111FB /* SQLInterpolationTests.swift in Sources */, 5674A7171F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 56D496801D813131008276D7 /* StatementColumnConvertibleFetchTests.swift in Sources */, 563B0705218627F800B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -3150,6 +3163,7 @@ 56FC98781D969DEF00E3C842 /* SQLExpression+QueryInterface.swift in Sources */, 56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */, 5695311F1C907A8C00CF1A2B /* DatabaseSchemaCache.swift in Sources */, + 569D6DDE220EF9E100A058A9 /* SQLInterpolation.swift in Sources */, 568D131F2207213E00674B58 /* SQLSelectQueryGenerator.swift in Sources */, 5605F15D1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */, 56D7E451221596180052464B /* GRDB-4.0.swift in Sources */, @@ -3566,7 +3580,7 @@ MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -3613,7 +3627,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; MTL_ENABLE_DEBUG_INFO = NO; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/GRDB/QueryInterface/SQLInterpolation.swift b/GRDB/QueryInterface/SQLInterpolation.swift new file mode 100644 index 000000000..68cb18301 --- /dev/null +++ b/GRDB/QueryInterface/SQLInterpolation.swift @@ -0,0 +1,83 @@ +#if swift(>=5.0) +public struct SQLInterpolation: StringInterpolationProtocol { + var sql: String + var arguments: StatementArguments { return context.arguments! } + private var context = SQLGenerationContext.literalGenerationContext(withArguments: true) + + public init(literalCapacity: Int, interpolationCount: Int) { + sql = "" + sql.reserveCapacity(literalCapacity + interpolationCount) + } + + /// "SELECT * FROM player" + public mutating func appendLiteral(_ literal: String) { + sql += literal + } + + /// "SELECT * FROM \(Player.self)" + public mutating func appendInterpolation(_ table: T.Type) { + sql += table.databaseTableName.quotedDatabaseIdentifier + } + + /// "SELECT \(AllColumns()) FROM player" + public mutating func appendInterpolation(_ selection: SQLSelectable) { + sql += selection.resultColumnSQL(&context) + } + + /// "SELECT score + \(bonus) FROM player" + /// "SELECT \(Column("name")) FROM player" + public mutating func appendInterpolation(_ expressible: SQLExpressible & SQLSelectable & SQLOrderingTerm) { + sql += expressible.sqlExpression.expressionSQL(&context) + } + + /// "SELECT \(Column("name")) FROM player" + /// "SELECT score + \(bonus) FROM player" + public mutating func appendInterpolation(_ expressible: T?) { + if let expressible = expressible { + sql += expressible.sqlExpression.expressionSQL(&context) + } else { + sql += "NULL" + } + } + + /// "SELECT \(CodingKey.name) FROM player" + public mutating func appendInterpolation(_ codingKey: CodingKey) { + appendInterpolation(Column(codingKey.stringValue)) + } + + /// "SELECT * FROM player WHERE id IN \([1, 2, 3])" + public mutating func appendInterpolation(_ sequence: S) where S: Sequence, S.Element: SQLExpressible { + sql += "(" + var first = true + for element in sequence { + if first { + first = false + } else { + sql += "," + } + appendInterpolation(element) + } + sql += ")" + } + + /// "SELECT * FROM player WHERE id IN \([Column("a"), Column("b" + 1)])" + public mutating func appendInterpolation(_ sequence: S) where S: Sequence, S.Element == SQLExpression { + sql += "(" + var first = true + for element in sequence { + if first { + first = false + } else { + sql += "," + } + appendInterpolation(element) + } + sql += ")" + } + + /// "SELECT * FROM player ORDER BY \(Column("name").desc)" + public mutating func appendInterpolation(_ ordering: SQLOrderingTerm) { + sql += ordering.orderingTermSQL(&context) + } +} +#endif diff --git a/GRDBCipher.xcodeproj/project.pbxproj b/GRDBCipher.xcodeproj/project.pbxproj index 9a57ba33b..96ae4324d 100755 --- a/GRDBCipher.xcodeproj/project.pbxproj +++ b/GRDBCipher.xcodeproj/project.pbxproj @@ -577,6 +577,12 @@ 567E55EE1D2BDD3F00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; 567E55F31D2BDDFE00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; 567E55F41D2BDDFF00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; + 567F0B2A220F0DDF00D111FB /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B28220F0DDF00D111FB /* SQLInterpolation.swift */; }; + 567F0B2B220F0DDF00D111FB /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B28220F0DDF00D111FB /* SQLInterpolation.swift */; }; + 567F0B33220F196F00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */; }; + 567F0B34220F196F00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */; }; + 567F0B35220F196F00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */; }; + 567F0B36220F196F00D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */; }; 567F45A91F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 567F45AA1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 567F45AD1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; @@ -1171,6 +1177,8 @@ 567A80521D41350C00C7DCEC /* IndexInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexInfoTests.swift; sourceTree = ""; }; 567DAF141EAB61ED00FC0928 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = grdb_config.h; sourceTree = ""; }; 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLogErrorTests.swift; sourceTree = ""; }; + 567F0B28220F0DDF00D111FB /* SQLInterpolation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLInterpolation.swift; sourceTree = ""; }; + 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLInterpolationTests.swift; sourceTree = ""; }; 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TruncateOptimizationTests.swift; sourceTree = ""; }; 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = ""; }; 5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = ""; }; @@ -1581,6 +1589,7 @@ 563EF45821631E51007DAACD /* QueryInterfacePromiseTests.swift */, 56300B5D1C53C38F005A543B /* QueryInterfaceRequestTests.swift */, 56300B841C54DC95005A543B /* Record+QueryInterfaceRequestTests.swift */, + 567F0B32220F196F00D111FB /* SQLInterpolationTests.swift */, 566AD8C51D531BE4002EC1A8 /* TableDefinitionTests.swift */, 56300B6A1C53D3E8005A543B /* TableRecord+QueryInterfaceRequestTests.swift */, 5698AC7F1DA380A20056AF8C /* VirtualTableModuleTests.swift */, @@ -1605,6 +1614,7 @@ 56CEB5421EAA359A00BFAF62 /* SQLExpression.swift */, 5653EC15209873A400F46237 /* SQLExpression+QueryInterface.swift */, 5653EC19209873E600F46237 /* SQLGenerationContext.swift */, + 567F0B28220F0DDF00D111FB /* SQLInterpolation.swift */, 56CEB5431EAA359A00BFAF62 /* SQLOrdering.swift */, 56D91A9B2205DD5200770D8D /* SQLRelation.swift */, 56CEB5441EAA359A00BFAF62 /* SQLSelectable.swift */, @@ -2322,6 +2332,7 @@ 5653EC17209873A400F46237 /* SQLExpression+QueryInterface.swift in Sources */, 564CE52621B312B500652B19 /* ValueObservation+MapReducer.swift in Sources */, 5659F4A11EA8D997004A4992 /* Result.swift in Sources */, + 567F0B2A220F0DDF00D111FB /* SQLInterpolation.swift in Sources */, 560FC5201CB003810014AA8E /* Date.swift in Sources */, 560FC5221CB003810014AA8E /* DatabaseValueConvertible+RawRepresentable.swift in Sources */, 563EF424215F8A88007DAACD /* OrderedDictionary.swift in Sources */, @@ -2445,6 +2456,7 @@ 56EA63CC209C7F31009715B8 /* DerivableRequestTests.swift in Sources */, 560FC5701CB00B880014AA8E /* DatabasePoolCollationTests.swift in Sources */, 560FC5711CB00B880014AA8E /* RecordPrimaryKeySingleTests.swift in Sources */, + 567F0B33220F196F00D111FB /* SQLInterpolationTests.swift in Sources */, 5674A7191F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 56B021CA1D8C0D3900B239BB /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, 563B07082186280600B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -2625,6 +2637,7 @@ 56C3F7551CF9F12400F6A361 /* DatabaseSavepointTests.swift in Sources */, 5672DE5B1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56B021CB1D8C0D3900B239BB /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, + 567F0B34220F196F00D111FB /* SQLInterpolationTests.swift in Sources */, 5674A71B1F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 56B14E811D4DAE54000BF4A3 /* RowFromDictionaryLiteralTests.swift in Sources */, 563B07092186280600B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -2803,6 +2816,7 @@ 5653EC18209873A400F46237 /* SQLExpression+QueryInterface.swift in Sources */, 564CE52721B312B700652B19 /* ValueObservation+MapReducer.swift in Sources */, 5659F4A41EA8D997004A4992 /* Result.swift in Sources */, + 567F0B2B220F0DDF00D111FB /* SQLInterpolation.swift in Sources */, 56AFC9F41CB1A8BB00F48B96 /* CGFloat.swift in Sources */, 56AFC9F51CB1A8BB00F48B96 /* Date.swift in Sources */, 563EF425215F8A88007DAACD /* OrderedDictionary.swift in Sources */, @@ -2926,6 +2940,7 @@ 56EA63CE209C7F31009715B8 /* DerivableRequestTests.swift in Sources */, 56AFCA3F1CB1AA9900F48B96 /* RecordPrimaryKeySingleTests.swift in Sources */, 56AFCA421CB1AA9900F48B96 /* DatabasePoolFunctionTests.swift in Sources */, + 567F0B35220F196F00D111FB /* SQLInterpolationTests.swift in Sources */, 5674A7181F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 56AFCA441CB1AA9900F48B96 /* RowCopiedFromStatementTests.swift in Sources */, 563B070A2186280600B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -3106,6 +3121,7 @@ 56EA63CF209C7F31009715B8 /* DerivableRequestTests.swift in Sources */, 5657AB641D108BA9006283EF /* FoundationNSURLTests.swift in Sources */, 56FF455C1D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */, + 567F0B36220F196F00D111FB /* SQLInterpolationTests.swift in Sources */, 56AFCA971CB1ABC800F48B96 /* DatabasePoolCollationTests.swift in Sources */, 5674A7151F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 563B070B2186280600B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -3571,7 +3587,7 @@ MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -3618,7 +3634,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; MTL_ENABLE_DEBUG_INFO = NO; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 05af70e47..6095720e5 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -259,6 +259,10 @@ 567DAF211EAB61ED00FC0928 /* grdb_config.h in Headers */ = {isa = PBXBuildFile; fileRef = 567DAF141EAB61ED00FC0928 /* grdb_config.h */; settings = {ATTRIBUTES = (Private, ); }; }; 567DAF381EAB789800FC0928 /* DatabaseLogErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */; }; 567DAF3C1EAB789800FC0928 /* DatabaseLogErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */; }; + 567F0B26220F0DD400D111FB /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B24220F0DD400D111FB /* SQLInterpolation.swift */; }; + 567F0B27220F0DD400D111FB /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B24220F0DD400D111FB /* SQLInterpolation.swift */; }; + 567F0B30220F196400D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B2F220F196300D111FB /* SQLInterpolationTests.swift */; }; + 567F0B31220F196400D111FB /* SQLInterpolationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F0B2F220F196300D111FB /* SQLInterpolationTests.swift */; }; 567F45AB1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 567F45AF1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; }; 568068341EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; }; @@ -805,6 +809,8 @@ 567A80521D41350C00C7DCEC /* IndexInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexInfoTests.swift; sourceTree = ""; }; 567DAF141EAB61ED00FC0928 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = grdb_config.h; sourceTree = ""; }; 567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLogErrorTests.swift; sourceTree = ""; }; + 567F0B24220F0DD400D111FB /* SQLInterpolation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLInterpolation.swift; sourceTree = ""; }; + 567F0B2F220F196300D111FB /* SQLInterpolationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLInterpolationTests.swift; sourceTree = ""; }; 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TruncateOptimizationTests.swift; sourceTree = ""; }; 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = ""; }; 5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = ""; }; @@ -1189,6 +1195,7 @@ 563EF45521631E3E007DAACD /* QueryInterfacePromiseTests.swift */, 56300B5D1C53C38F005A543B /* QueryInterfaceRequestTests.swift */, 56300B841C54DC95005A543B /* Record+QueryInterfaceRequestTests.swift */, + 567F0B2F220F196300D111FB /* SQLInterpolationTests.swift */, 566AD8C51D531BE4002EC1A8 /* TableDefinitionTests.swift */, 56300B6A1C53D3E8005A543B /* TableRecord+QueryInterfaceRequestTests.swift */, 5698AC7F1DA380A20056AF8C /* VirtualTableModuleTests.swift */, @@ -1213,6 +1220,7 @@ 56CEB5421EAA359A00BFAF62 /* SQLExpression.swift */, 56FC98771D969DEF00E3C842 /* SQLExpression+QueryInterface.swift */, 5653EC072098737400F46237 /* SQLGenerationContext.swift */, + 567F0B24220F0DD400D111FB /* SQLInterpolation.swift */, 56CEB5431EAA359A00BFAF62 /* SQLOrdering.swift */, 56D91A9E2205E02300770D8D /* SQLRelation.swift */, 56CEB5441EAA359A00BFAF62 /* SQLSelectable.swift */, @@ -1959,6 +1967,7 @@ F3BA800D1CFB2871003DC1BA /* DatabasePool.swift in Sources */, F3BA800B1CFB286D003DC1BA /* Database.swift in Sources */, F3BA80341CFB28A4003DC1BA /* Record.swift in Sources */, + 567F0B27220F0DD400D111FB /* SQLInterpolation.swift in Sources */, 563EF421215F8A76007DAACD /* OrderedDictionary.swift in Sources */, F3BA80161CFB2876003DC1BA /* Row.swift in Sources */, 569EF0E7200D37FD00A9FA45 /* DatabaseRegion.swift in Sources */, @@ -2082,6 +2091,7 @@ 56EA63CA209C7F1E009715B8 /* DerivableRequestTests.swift in Sources */, F3BA80E91CFB3016003DC1BA /* RowAdapterTests.swift in Sources */, 562393371DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */, + 567F0B31220F196400D111FB /* SQLInterpolationTests.swift in Sources */, 5674A7161F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 56B021D01D8C0D3900B239BB /* MutablePersistableRecordPersistenceConflictPolicyTests.swift in Sources */, 563B0703218627DF00B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -2260,6 +2270,7 @@ F3BA80691CFB2E55003DC1BA /* DatabasePool.swift in Sources */, F3BA80671CFB2E55003DC1BA /* Database.swift in Sources */, F3BA80901CFB2E7A003DC1BA /* Record.swift in Sources */, + 567F0B26220F0DD400D111FB /* SQLInterpolation.swift in Sources */, 563EF420215F8A76007DAACD /* OrderedDictionary.swift in Sources */, F3BA80721CFB2E55003DC1BA /* Row.swift in Sources */, 569EF0E6200D37FD00A9FA45 /* DatabaseRegion.swift in Sources */, @@ -2383,6 +2394,7 @@ 56EA63C9209C7F1E009715B8 /* DerivableRequestTests.swift in Sources */, F3BA811D1CFB305F003DC1BA /* TableRecord+QueryInterfaceRequestTests.swift in Sources */, F3BA80AD1CFB2FA6003DC1BA /* DataMemoryTests.swift in Sources */, + 567F0B30220F196400D111FB /* SQLInterpolationTests.swift in Sources */, 5698ACB91DA6285E0056AF8C /* FTS3TokenizerTests.swift in Sources */, 5674A7141F3087710095F066 /* DatabaseValueConvertibleDecodableTests.swift in Sources */, 563B0702218627DF00B38F35 /* ValueObservationRowTests.swift in Sources */, @@ -2601,7 +2613,7 @@ MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -2648,7 +2660,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; MTL_ENABLE_DEBUG_INFO = NO; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/Tests/GRDBTests/SQLInterpolationTests.swift b/Tests/GRDBTests/SQLInterpolationTests.swift new file mode 100644 index 000000000..5d13165d2 --- /dev/null +++ b/Tests/GRDBTests/SQLInterpolationTests.swift @@ -0,0 +1,113 @@ +#if swift(>=5.0) +import XCTest +#if GRDBCIPHER + @testable import GRDBCipher +#elseif GRDBCUSTOMSQLITE + @testable import GRDBCustomSQLite +#else + @testable import GRDB +#endif + +class SQLInterpolationTests: GRDBTestCase { + func testSelectableInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 2) + + // Non-existential + sql.appendInterpolation(AllColumns()); sql.appendLiteral("\n") + // Existential + sql.appendInterpolation(AllColumns() as SQLSelectable) + + XCTAssertEqual(sql.sql, """ + * + * + """) + XCTAssert(sql.arguments.isEmpty) + } + + func testTableInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) + + struct Player: TableRecord { } + sql.appendInterpolation(Player.self) + + XCTAssertEqual(sql.sql, #""" + "player" + """#) + XCTAssert(sql.arguments.isEmpty) + } + + func testExpressibleInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 7) + + let a = Column("a") + let b = Column("b") + let integer: Int = 1 + let optionalInteger: Int? = 2 + let nilInteger: Int? = nil + sql.appendInterpolation(a); sql.appendLiteral("\n") + sql.appendInterpolation(a + 1); sql.appendLiteral("\n") + sql.appendInterpolation(a < b); sql.appendLiteral("\n") + sql.appendInterpolation(integer); sql.appendLiteral("\n") + sql.appendInterpolation(optionalInteger); sql.appendLiteral("\n") + sql.appendInterpolation(nilInteger); sql.appendLiteral("\n") + sql.appendInterpolation(a == nilInteger) + + XCTAssertEqual(sql.sql, #""" + "a" + ("a" + ?) + ("a" < "b") + ? + ? + NULL + ("a" IS NULL) + """#) + XCTAssertEqual(sql.arguments.values, [1.databaseValue, 1.databaseValue, 2.databaseValue]) + XCTAssert(sql.arguments.namedValues.isEmpty) + } + + func testCodingKeyInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) + + enum CodingKeys: String, CodingKey { + case name + } + sql.appendInterpolation(CodingKeys.name) + + XCTAssertEqual(sql.sql, #""" + "name" + """#) + XCTAssert(sql.arguments.isEmpty) + } + + func testExpressibleSequenceInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) + + let set: Set = [1] + let array = ["foo", "bar", "baz"] + let expressions = [Column("a"), Column("b") + 2] + sql.appendInterpolation(set); sql.appendLiteral("\n") + sql.appendInterpolation(array); sql.appendLiteral("\n") + sql.appendInterpolation(expressions) + + XCTAssertEqual(sql.sql, #""" + (?) + (?,?,?) + ("a",("b" + ?)) + """#) + XCTAssertEqual(sql.arguments.values, [1.databaseValue, "foo".databaseValue, "bar".databaseValue, "baz".databaseValue, 2.databaseValue]) + XCTAssert(sql.arguments.namedValues.isEmpty) + } + + func testOrderingTermInterpolation() { + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) + + sql.appendInterpolation(Column("name").desc) + + XCTAssertEqual(sql.sql, #""" + "name" DESC + """#) + XCTAssert(sql.arguments.isEmpty) + } +} +#endif +