From 67eb1ba1e56a68068ca21ef364c174ec7f4ddd29 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 21 Apr 2025 14:56:16 -0700 Subject: [PATCH] More == overloads. --- Sources/StructuredQueriesCore/Operators.swift | 185 +++++++++++------- .../SQLMacroTests.swift | 12 -- .../StructuredQueriesTests/UpdateTests.swift | 11 +- 3 files changed, 117 insertions(+), 91 deletions(-) diff --git a/Sources/StructuredQueriesCore/Operators.swift b/Sources/StructuredQueriesCore/Operators.swift index 05450fdc..458dda94 100644 --- a/Sources/StructuredQueriesCore/Operators.swift +++ b/Sources/StructuredQueriesCore/Operators.swift @@ -14,7 +14,8 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func == ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.eq(rhs) } @@ -34,27 +35,12 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func != ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.neq(rhs) } - @_disfavoredOverload - @_documentation(visibility: private) - public static func == ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS" : "=", rhs: rhs) - } - - @_disfavoredOverload - @_documentation(visibility: private) - public static func != ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS NOT" : "<>", rhs: rhs) - } - /// Returns a predicate expression indicating whether two query expressions are equal. /// /// ```swift @@ -119,34 +105,6 @@ private func isNull(_ expression: some QueryExpression) -> Bool { } extension QueryExpression where QueryValue: QueryBindable & _OptionalProtocol { - @_documentation(visibility: private) - public static func == ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS" : "=", rhs: rhs) - } - - @_documentation(visibility: private) - public static func != ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS NOT" : "<>", rhs: rhs) - } - - @_documentation(visibility: private) - public static func == ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS" : "=", rhs: rhs) - } - - @_documentation(visibility: private) - public static func != ( - lhs: Self, rhs: some QueryExpression - ) -> some QueryExpression { - BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS NOT" : "<>", rhs: rhs) - } - @_documentation(visibility: private) public func eq(_ other: some QueryExpression) -> some QueryExpression { BinaryOperator(lhs: self, operator: "=", rhs: other) @@ -183,16 +141,6 @@ extension QueryExpression where QueryValue: QueryBindable & _OptionalProtocol { } extension QueryExpression where QueryValue: QueryBindable { - @_documentation(visibility: private) - public static func == (lhs: Self, rhs: _Null) -> some QueryExpression { - lhs.is(rhs) - } - - @_documentation(visibility: private) - public static func != (lhs: Self, rhs: _Null) -> some QueryExpression { - lhs.isNot(rhs) - } - @_documentation(visibility: private) public func `is`( _ other: _Null @@ -217,6 +165,80 @@ extension _Null: ExpressibleByNilLiteral { public init(nilLiteral: ()) {} } +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_disfavoredOverload +@_documentation(visibility: private) +public func == ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS" : "=", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_disfavoredOverload +@_documentation(visibility: private) +public func != ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(rhs) ? "IS NOT" : "<>", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func == ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS" : "=", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func != ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(lhs) ? "IS NOT" : "<>", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func == ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS" : "=", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func != ( + lhs: any QueryExpression, + rhs: some QueryExpression +) -> some QueryExpression { + BinaryOperator(lhs: lhs, operator: isNull(lhs) || isNull(rhs) ? "IS NOT" : "<>", rhs: rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func == ( + lhs: any QueryExpression, + rhs: _Null +) -> some QueryExpression { + SQLQueryExpression(lhs).is(rhs) +} + +// NB: This overload is required due to an overload resolution bug of 'Updates[dynamicMember:]'. +@_documentation(visibility: private) +public func != ( + lhs: any QueryExpression, + rhs: _Null +) -> some QueryExpression { + SQLQueryExpression(lhs).isNot(rhs) +} + extension QueryExpression where QueryValue: QueryBindable { /// Returns a predicate expression indicating whether the value of the first expression is less /// than that of the second expression. @@ -229,7 +251,8 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func < ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.lt(rhs) } @@ -245,7 +268,8 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func > ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.gt(rhs) } @@ -261,7 +285,8 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func <= ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.lte(rhs) } @@ -277,7 +302,8 @@ extension QueryExpression where QueryValue: QueryBindable { /// - rhs: Another expression to compare. /// - Returns: A predicate expression. public static func >= ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.gte(rhs) } @@ -338,7 +364,8 @@ extension QueryExpression where QueryValue == Bool { /// - rhs: The right-hand side of the operation. /// - Returns: A predicate expression. public static func && ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.and(rhs) } @@ -353,7 +380,8 @@ extension QueryExpression where QueryValue == Bool { /// - rhs: The right-hand side of the operation. /// - Returns: A predicate expression. public static func || ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { lhs.or(rhs) } @@ -415,7 +443,8 @@ extension QueryExpression where QueryValue: Numeric { /// - rhs: The second expression to add. /// - Returns: A sum expression. public static func + ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "+", rhs: rhs) } @@ -427,7 +456,8 @@ extension QueryExpression where QueryValue: Numeric { /// - rhs: The second expression to subtract. /// - Returns: A difference expression. public static func - ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "-", rhs: rhs) } @@ -439,7 +469,8 @@ extension QueryExpression where QueryValue: Numeric { /// - rhs: The second expression to multiply. /// - Returns: A product expression. public static func * ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "*", rhs: rhs) } @@ -451,7 +482,8 @@ extension QueryExpression where QueryValue: Numeric { /// - rhs: The second expression to divide. /// - Returns: A quotient expression. public static func / ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "/", rhs: rhs) } @@ -546,7 +578,8 @@ extension QueryExpression where QueryValue: BinaryInteger { /// - rhs: The value to divide `lhs` by. /// - Returns: An expression representing the remainder, or `NULL` if `rhs` is zero. public static func % ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "%", rhs: rhs) } @@ -558,7 +591,8 @@ extension QueryExpression where QueryValue: BinaryInteger { /// - rhs: Another integer expression. /// - Returns: An expression representing a bitwise AND operation on the two given expressions. public static func & ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "&", rhs: rhs) } @@ -570,7 +604,8 @@ extension QueryExpression where QueryValue: BinaryInteger { /// - rhs: Another integer expression. /// - Returns: An expression representing a bitwise OR operation on the two given expressions. public static func | ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "|", rhs: rhs) } @@ -583,7 +618,8 @@ extension QueryExpression where QueryValue: BinaryInteger { /// - rhs: Another integer expression. /// - Returns: An expression representing a left bitshift operation on the two given expressions. public static func << ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "<<", rhs: rhs) } @@ -596,7 +632,8 @@ extension QueryExpression where QueryValue: BinaryInteger { /// - rhs: Another integer expression. /// - Returns: An expression representing a right bitshift operation on the two given expressions. public static func >> ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: ">>", rhs: rhs) } @@ -647,7 +684,8 @@ extension QueryExpression where QueryValue == String { /// - rhs: The second string expression. /// - Returns: An expression concatenating the first expression with the second. public static func + ( - lhs: Self, rhs: some QueryExpression + lhs: Self, + rhs: some QueryExpression ) -> some QueryExpression { BinaryOperator(lhs: lhs, operator: "||", rhs: rhs) } @@ -762,7 +800,8 @@ extension SQLQueryExpression { /// - lhs: The column to append. /// - rhs: The appended text. public static func += ( - lhs: inout Self, rhs: some QueryExpression + lhs: inout Self, + rhs: some QueryExpression ) { lhs = Self(lhs + rhs) } diff --git a/Tests/StructuredQueriesTests/SQLMacroTests.swift b/Tests/StructuredQueriesTests/SQLMacroTests.swift index d64f6342..3729716c 100644 --- a/Tests/StructuredQueriesTests/SQLMacroTests.swift +++ b/Tests/StructuredQueriesTests/SQLMacroTests.swift @@ -173,18 +173,6 @@ extension SnapshotTests { """ } } - - func foo() { - let searchText = "get" - #sql( - """ - SELECT \(Reminder.columns) - FROM \(Reminder.self) - WHERE \(Reminder.title) COLLATE NOCASE LIKE \(searchText) - """, - as: Reminder.self - ) - } } } diff --git a/Tests/StructuredQueriesTests/UpdateTests.swift b/Tests/StructuredQueriesTests/UpdateTests.swift index 4942a1b7..f7c8df05 100644 --- a/Tests/StructuredQueriesTests/UpdateTests.swift +++ b/Tests/StructuredQueriesTests/UpdateTests.swift @@ -352,8 +352,7 @@ extension SnapshotTests { .find(1) .update { $0.dueDate = Case() - .when($0.dueDate.is(nil), then: #sql("'2018-01-29 00:08:00.000'")) - .else(#sql("NULL")) + .when($0.dueDate == nil, then: #sql("'2018-01-29 00:08:00.000'")) } assertQuery( @@ -362,11 +361,11 @@ extension SnapshotTests { ) { """ UPDATE "reminders" - SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' ELSE NULL END + SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' END WHERE ("reminders"."id" = 1) RETURNING "dueDate" """ - } results: { + }results: { """ ┌─────┐ │ nil │ @@ -380,11 +379,11 @@ extension SnapshotTests { ) { """ UPDATE "reminders" - SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' ELSE NULL END + SET "dueDate" = CASE WHEN ("reminders"."dueDate" IS NULL) THEN '2018-01-29 00:08:00.000' END WHERE ("reminders"."id" = 1) RETURNING "dueDate" """ - } results: { + }results: { """ ┌────────────────────────────────┐ │ Date(2018-01-29T00:08:00.000Z) │