From 4adc8a3cfb29fd49829d276737f2769e081a77e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Wed, 20 Jun 2018 13:13:29 -0400 Subject: [PATCH] Fixed the case that the rhs is nil and we want it as a literal then. --- Sources/SQL/SQLBinaryOperator.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/SQL/SQLBinaryOperator.swift b/Sources/SQL/SQLBinaryOperator.swift index ca08e32b..4e7337a2 100644 --- a/Sources/SQL/SQLBinaryOperator.swift +++ b/Sources/SQL/SQLBinaryOperator.swift @@ -180,12 +180,18 @@ public enum GenericSQLBinaryOperator: SQLBinaryOperator, Equatable { public func == (_ lhs: KeyPath, _ rhs: V) -> E where T: SQLTable, V: Encodable, E: SQLExpression { + if rhs.isNil { + return E.binary(.column(.keyPath(lhs)), .equal, .literal(.null)) + } return E.binary(.column(.keyPath(lhs)), .equal, .bind(.encodable(rhs))) } public func != (_ lhs: KeyPath, _ rhs: V) -> E where T: SQLTable, V: Encodable, E: SQLExpression { + if rhs.isNil { + return E.binary(.column(.keyPath(lhs)), .notEqual, .literal(.null)) + } return E.binary(.column(.keyPath(lhs)), .notEqual, .bind(.encodable(rhs))) } @@ -195,3 +201,13 @@ public func == (_ lhs: KeyPath, _ rhs: KeyPath) -> E { return E.binary(.column(.keyPath(lhs)), .equal, .column(.keyPath(rhs))) } + +internal extension Encodable { + /// Returns `true` if this `Encodable` is `nil`. + var isNil: Bool { + guard let optional = self as? AnyOptionalType, optional.anyWrapped == nil else { + return false + } + return true + } +}