diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ab0c698d..1a424b00c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,7 @@ All notable changes to this project will be documented in this file. GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library. - #### 4.x Releases @@ -55,9 +53,12 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: - [0.110.0](#01100), ... - + +**Fixed** + +- [#592](https://github.com/groue/GRDB.swift/pull/592): Fix Data interpolation + ## 4.2.0 diff --git a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift index 6d7d71382..0ec52db3c 100644 --- a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift @@ -75,6 +75,15 @@ extension SQLInterpolation { appendInterpolation(Column(codingKey.stringValue)) } + // When a value is both an expression and a sequence of expressions, + // favor the expression side. Use case: Foundation.Data interpolation. + /// :nodoc: + public mutating func appendInterpolation(_ expressible: T) + where T: Sequence, T.Element: SQLExpressible, T: SQLExpressible + { + sql += expressible.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + } + /// Appends a sequence of expressions, wrapped in parentheses. /// /// // SELECT * FROM player WHERE id IN (?,?,?) diff --git a/Tests/GRDBTests/SQLInterpolationTests.swift b/Tests/GRDBTests/SQLInterpolationTests.swift index bce0ba8e2..100a1f555 100644 --- a/Tests/GRDBTests/SQLInterpolationTests.swift +++ b/Tests/GRDBTests/SQLInterpolationTests.swift @@ -104,6 +104,16 @@ class SQLInterpolationTests: GRDBTestCase { test(value: V(), isInterpolatedAs: "V".databaseValue) } + func testDataInterpolation() { + // This test makes sure the Sequence conformance of Data does not + // kick in. + let data = "SQLite".data(using: .utf8)! + var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) + sql.appendInterpolation(data) + XCTAssertEqual(sql.sql, "?") + XCTAssertEqual(sql.arguments, [data]) + } + func testQualifiedExpressionInterpolation() { var sql = SQLInterpolation(literalCapacity: 0, interpolationCount: 1) diff --git a/Tests/GRDBTests/SQLLiteralTests.swift b/Tests/GRDBTests/SQLLiteralTests.swift index a649545c4..bae20c007 100644 --- a/Tests/GRDBTests/SQLLiteralTests.swift +++ b/Tests/GRDBTests/SQLLiteralTests.swift @@ -234,6 +234,15 @@ extension SQLLiteralTests { test(value: V(), isInterpolatedAs: "V".databaseValue) } + func testDataInterpolation() { + // This test makes sure the Sequence conformance of Data does not + // kick in. + let data = "SQLite".data(using: .utf8)! + let query: SQLLiteral = "SELECT \(data)" + XCTAssertEqual(query.sql, "SELECT ?") + XCTAssertEqual(query.arguments, [data]) + } + func testQualifiedExpressionInterpolation() { let query: SQLLiteral = """ SELECT \(Column("name").aliased("foo"))