diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index f844636d5..79ab6bfea 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -576,6 +576,14 @@ 56A238A51B9C753B0082EB20 /* Record.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238A11B9C753B0082EB20 /* Record.swift */; }; 56A238B61B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238B51B9CA2590082EB20 /* DatabaseTimestampTests.swift */; }; 56A238B71B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238B51B9CA2590082EB20 /* DatabaseTimestampTests.swift */; }; + 56A4CDB01D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB11D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB21D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB31D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB41D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB51D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB61D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; + 56A4CDB71D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; }; 56A5E4091BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A5E4081BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift */; }; 56A5E40A1BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A5E4081BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift */; }; 56A8C2301D1914540096E9D4 /* UUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A8C22F1D1914540096E9D4 /* UUID.swift */; }; @@ -1466,6 +1474,7 @@ 56A238921B9C750B0082EB20 /* DatabaseMigrator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseMigrator.swift; sourceTree = ""; }; 56A238A11B9C753B0082EB20 /* Record.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Record.swift; sourceTree = ""; }; 56A238B51B9CA2590082EB20 /* DatabaseTimestampTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTimestampTests.swift; sourceTree = ""; }; + 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLExpressionLiteralTests.swift; sourceTree = ""; }; 56A5E4081BA2BCF900707640 /* RecordWithColumnNameManglingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecordWithColumnNameManglingTests.swift; sourceTree = ""; }; 56A8C21E1D1914110096E9D4 /* UUIDTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UUIDTests.swift; sourceTree = ""; }; 56A8C22F1D1914540096E9D4 /* UUID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UUID.swift; sourceTree = ""; }; @@ -1912,6 +1921,7 @@ 56AF746A1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift */, 56EB0AB11BCD787300A3DC55 /* DataMemoryTests.swift */, 56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */, + 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */, 5605F1861C69111300235C62 /* StatementInformationTests.swift */, ); path = Private; @@ -3180,6 +3190,7 @@ 567156171CB142AA007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, 56AF74631D41FB89005E9FF3 /* TableIndexTests.swift in Sources */, 560FC58D1CB00B880014AA8E /* Row+FoundationTests.swift in Sources */, + 56A4CDB11D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 567156141CB141D0007DC145 /* InMemoryDatabaseTests.swift in Sources */, 560FC58E1CB00B880014AA8E /* PrimaryKeyRowIDTests.swift in Sources */, EE5075861D00E611005D9C5B /* DatabaseValue+FoundationTests.swift in Sources */, @@ -3297,6 +3308,7 @@ 56A8C2431D1918EE0096E9D4 /* NSUUIDTests.swift in Sources */, 56AF74641D41FB89005E9FF3 /* TableIndexTests.swift in Sources */, 5671564C1CB16729007DC145 /* InMemoryDatabaseTests.swift in Sources */, + 56A4CDB21D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 5671564D1CB16729007DC145 /* PrimaryKeyRowIDTests.swift in Sources */, 5671564E1CB16729007DC145 /* DatabaseReaderTests.swift in Sources */, 5671564F1CB16729007DC145 /* RecordEditedTests.swift in Sources */, @@ -3452,6 +3464,7 @@ 56AFCA581CB1AA9900F48B96 /* Row+FoundationTests.swift in Sources */, 569178411CED8E0C00E179EA /* FetchedRecordsControllerTests.swift in Sources */, 56AFCA5A1CB1AA9900F48B96 /* RecordSubClassTests.swift in Sources */, + 56A4CDB51D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 56AFCA5B1CB1AA9900F48B96 /* DatabaseValueTests.swift in Sources */, 56AFCA5C1CB1AA9900F48B96 /* ReadOnlyDatabaseTests.swift in Sources */, 56AFCA5D1CB1AA9900F48B96 /* DatabaseReaderTests.swift in Sources */, @@ -3554,6 +3567,7 @@ 56AFCAAF1CB1ABC800F48B96 /* DatabasePoolConcurrencyTests.swift in Sources */, 56AFCAB01CB1ABC800F48B96 /* TransactionObserverTests.swift in Sources */, 56AFCAB11CB1ABC800F48B96 /* Row+FoundationTests.swift in Sources */, + 56A4CDB61D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 5657AB4C1D108BA9006283EF /* NSNullTests.swift in Sources */, 56AFCAB31CB1ABC800F48B96 /* RecordSubClassTests.swift in Sources */, 56AFCAB41CB1ABC800F48B96 /* DatabaseValueTests.swift in Sources */, @@ -3716,6 +3730,7 @@ 56A8C2471D1918F00096E9D4 /* NSUUIDTests.swift in Sources */, 56AF74661D41FB89005E9FF3 /* TableIndexTests.swift in Sources */, 565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */, + 56A4CDB41D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */, 56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */, 56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */, @@ -3848,6 +3863,7 @@ 56E5D8041B4D424400430942 /* GRDBTestCase.swift in Sources */, 56AF746B1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */, 569178461CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */, + 56A4CDB01D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 563363DC1C95891E000BE133 /* DatabaseQueueFileAttributesTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -4031,6 +4047,7 @@ 56A8C24D1D1918F30096E9D4 /* NSUUIDTests.swift in Sources */, 56AF74691D41FB89005E9FF3 /* TableIndexTests.swift in Sources */, F3BA80D61CFB2FFD003DC1BA /* DatabaseReaderTests.swift in Sources */, + 56A4CDB71D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, F3BA80F91CFB3021003DC1BA /* UpdateStatementTests.swift in Sources */, F3BA80DA1CFB300E003DC1BA /* DatabaseTimestampTests.swift in Sources */, F3BA81201CFB3063003DC1BA /* PrimaryKeyMultipleTests.swift in Sources */, @@ -4220,6 +4237,7 @@ F3BA80C91CFB2FD8003DC1BA /* DatabaseQueueBackupTests.swift in Sources */, 56AF746E1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */, F3BA80B51CFB2FCA003DC1BA /* InMemoryDatabaseTests.swift in Sources */, + 56A4CDB31D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, F3BA80DF1CFB300F003DC1BA /* RawRepresentableTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index ebbd40e2d..0015a3455 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -24,10 +24,10 @@ extension QueryInterfaceRequest : FetchRequest { /// - throws: A DatabaseError whenever SQLite could not parse the sql query. public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { // TODO: split statement generation from arguments building - var arguments = StatementArguments() + var arguments: StatementArguments? = StatementArguments() let sql = try query.sql(db, &arguments) let statement = try db.makeSelectStatement(sql) - try statement.setArgumentsWithValidation(arguments) + try statement.setArgumentsWithValidation(arguments!) return (statement, nil) } } diff --git a/GRDB/QueryInterface/SQLCollation.swift b/GRDB/QueryInterface/SQLCollation.swift index 8423e0b62..ecfa088fa 100644 --- a/GRDB/QueryInterface/SQLCollation.swift +++ b/GRDB/QueryInterface/SQLCollation.swift @@ -46,7 +46,7 @@ extension _SQLCollatedExpression : _SQLOrdering { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface - public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { return try sqlExpression.orderingSQL(db, &arguments) } } diff --git a/GRDB/QueryInterface/SQLSelectQuery.swift b/GRDB/QueryInterface/SQLSelectQuery.swift index 48c75cb34..62e6371d8 100644 --- a/GRDB/QueryInterface/SQLSelectQuery.swift +++ b/GRDB/QueryInterface/SQLSelectQuery.swift @@ -50,7 +50,7 @@ public struct _SQLSelectQuery { self.limit = limit } - func sql(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + func sql(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { var sql = "SELECT" if isDistinct { @@ -200,7 +200,7 @@ indirect enum _SQLSource { case table(name: String, alias: String?) case query(query: _SQLSelectQuery, alias: String?) - func sql(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + func sql(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { switch self { case .table(let table, let alias): if let alias = alias { @@ -227,7 +227,7 @@ indirect enum _SQLSource { /// See https://github.com/groue/GRDB.swift/#the-query-interface public protocol _SQLOrdering { var reversedOrdering: _SQLOrderingExpression { get } - func orderingSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String + func orderingSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String } /// This type is an implementation detail of the query interface. @@ -258,7 +258,7 @@ extension _SQLOrderingExpression : _SQLOrdering { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface - public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { switch self { case .asc(let expression): return try expression.sql(db, &arguments) + " ASC" @@ -347,7 +347,7 @@ extension _SpecificSQLExpressible where Self: _SQLOrdering { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface - public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + public func orderingSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { return try sqlExpression.sql(db, &arguments) } } @@ -358,7 +358,7 @@ extension _SpecificSQLExpressible where Self: _SQLSelectable { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface - public func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + public func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { return try sqlExpression.sql(db, &arguments) } @@ -366,7 +366,7 @@ extension _SpecificSQLExpressible where Self: _SQLSelectable { /// Do not use it directly. /// /// See https://github.com/groue/GRDB.swift/#the-query-interface - public func countedSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + public func countedSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { return try sqlExpression.sql(db, &arguments) } @@ -464,7 +464,7 @@ public indirect enum _SQLExpression { case countDistinct(_SQLExpression) /// - func sql(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + func sql(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { // NOTE: this method *was* slow to compile // https://medium.com/swift-programming/speeding-up-slow-swift-build-times-922feeba5780#.s77wmh4h0 // 10746.4ms /Users/groue/Documents/git/groue/GRDB.swift/GRDB/FetchRequest/SQLSelectQuery.swift:439:10 func sql(db: Database, _ arguments: inout StatementArguments) throws -> String @@ -473,12 +473,15 @@ public indirect enum _SQLExpression { switch self { case .SQLLiteral(let sql, let literalArguments): if let literalArguments = literalArguments { - arguments.values.append(contentsOf: literalArguments.values) + guard arguments != nil else { + fatalError("Not implemented") + } + arguments!.values.append(contentsOf: literalArguments.values) for (name, value) in literalArguments.namedValues { - if arguments.namedValues[name] != nil { + if arguments!.namedValues[name] != nil { throw DatabaseError(code: SQLITE_MISUSE, message: "argument \(String(reflecting: name)) can't be reused") } - arguments.namedValues[name] = value + arguments!.namedValues[name] = value } } return sql @@ -487,8 +490,12 @@ public indirect enum _SQLExpression { guard let value = value else { return "NULL" } - arguments.values.append(value) - return "?" + if arguments == nil { + return value.sqlLiteral + } else { + arguments!.values.append(value) + return "?" + } case .identifier(let identifier, let sourceName): if let sourceName = sourceName { @@ -650,8 +657,8 @@ extension _SQLExpression : _SQLOrdering {} /// /// See https://github.com/groue/GRDB.swift/#the-query-interface public protocol _SQLSelectable { - func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String - func countedSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String + func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String + func countedSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String var sqlSelectableKind: _SQLSelectableKind { get } } @@ -671,7 +678,7 @@ enum _SQLResultColumn { extension _SQLResultColumn : _SQLSelectable { - func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + func resultColumnSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { switch self { case .star(let sourceName): if let sourceName = sourceName { @@ -684,7 +691,7 @@ extension _SQLResultColumn : _SQLSelectable { } } - func countedSQL(_ db: Database, _ arguments: inout StatementArguments) throws -> String { + func countedSQL(_ db: Database, _ arguments: inout StatementArguments?) throws -> String { switch self { case .star: return "*" diff --git a/TODO.md b/TODO.md index e91274af4..6d362d224 100644 --- a/TODO.md +++ b/TODO.md @@ -22,6 +22,7 @@ Not sure +- [ ] Remove DatabaseValue.value() - [ ] Move Database Events filtering to the TransactionObserverType protocol - [ ] Support for NSColor/UIColor. Beware UIColor components can go beyond [0, 1.0] in iOS10. diff --git a/Tests/Private/SQLExpressionLiteralTests.swift b/Tests/Private/SQLExpressionLiteralTests.swift new file mode 100644 index 000000000..48e257fd3 --- /dev/null +++ b/Tests/Private/SQLExpressionLiteralTests.swift @@ -0,0 +1,40 @@ +import XCTest + +#if USING_SQLCIPHER + @testable import GRDBCipher +#elseif USING_CUSTOMSQLITE + @testable import GRDBCustomSQLite +#else + @testable import GRDB +#endif + +class SQLExpressionLiteralTests: GRDBTestCase { + + func testWithArguments() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let expression = SQLColumn("foo").collating("NOCASE") == "'bar'" && SQLColumn("baz") >= 1 + var arguments: StatementArguments? = StatementArguments() + let sql = try dbQueue.inDatabase { db -> String in + try expression.sql(db, &arguments) + } + XCTAssertEqual(sql, "((\"foo\" = ? COLLATE NOCASE) AND (\"baz\" >= ?))") + let values = arguments!.values + XCTAssertEqual(values.count, 2) + XCTAssertEqual((values[0] as! String), "'bar'") + XCTAssertEqual((values[1] as! Int), 1) + } + } + + func testWithoutArguments() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + let expression = SQLColumn("foo").collating("NOCASE") == "'bar'" && SQLColumn("baz") >= 1 + let sql = try dbQueue.inDatabase { db -> String in + var arguments: StatementArguments? = nil + return try expression.sql(db, &arguments) + } + XCTAssertEqual(sql, "((\"foo\" = '''bar''' COLLATE NOCASE) AND (\"baz\" >= 1))") + } + } +}