Skip to content

Commit

Permalink
Merge pull request #14 from skelpo/master
Browse files Browse the repository at this point in the history
Fixed the case that the rhs is nil and we want it as a literal then.
  • Loading branch information
tanner0101 authored Jun 21, 2018
2 parents 9c8c19b + 4adc8a3 commit 27fafe9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/SQL/SQLBinaryOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,18 @@ public enum GenericSQLBinaryOperator: SQLBinaryOperator, Equatable {
public func == <T,V,E>(_ lhs: KeyPath<T, V>, _ 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 != <T,V,E>(_ lhs: KeyPath<T, V>, _ 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)))
}

Expand All @@ -195,3 +201,13 @@ public func == <A, B, C, D, E>(_ lhs: KeyPath<A, B>, _ rhs: KeyPath<C, D>) -> 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
}
}

0 comments on commit 27fafe9

Please sign in to comment.