Skip to content

Commit bb12b34

Browse files
authored
Generalize search operations to StringProtocol (#126)
1 parent 22959f5 commit bb12b34

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ extension QueryExpression where QueryValue == String {
710710
///
711711
/// - Parameter pattern: A string expression describing the `GLOB` pattern.
712712
/// - Returns: A predicate expression.
713-
public func glob(_ pattern: QueryValue) -> some QueryExpression<Bool> {
714-
BinaryOperator(lhs: self, operator: "GLOB", rhs: pattern)
713+
public func glob(_ pattern: some StringProtocol) -> some QueryExpression<Bool> {
714+
BinaryOperator(lhs: self, operator: "GLOB", rhs: "\(pattern)")
715715
}
716716

717717
/// A predicate expression from this string expression matched against another _via_ the `LIKE`
@@ -726,8 +726,11 @@ extension QueryExpression where QueryValue == String {
726726
/// - pattern: A string expression describing the `LIKE` pattern.
727727
/// - escape: An optional character for the `ESCAPE` clause.
728728
/// - Returns: A predicate expression.
729-
public func like(_ pattern: QueryValue, escape: Character? = nil) -> some QueryExpression<Bool> {
730-
LikeOperator(string: self, pattern: pattern, escape: escape)
729+
public func like(
730+
_ pattern: some StringProtocol,
731+
escape: Character? = nil
732+
) -> some QueryExpression<Bool> {
733+
LikeOperator(string: self, pattern: "\(pattern)", escape: escape)
731734
}
732735

733736
/// A predicate expression from this string expression matched against another _via_ the `MATCH`
@@ -740,8 +743,12 @@ extension QueryExpression where QueryValue == String {
740743
///
741744
/// - Parameter pattern: A string expression describing the `MATCH` pattern.
742745
/// - Returns: A predicate expression.
743-
public func match(_ pattern: QueryValue) -> some QueryExpression<Bool> {
744-
BinaryOperator(lhs: self, operator: "MATCH", rhs: pattern)
746+
public func match(_ pattern: some StringProtocol) -> some QueryExpression<Bool> {
747+
BinaryOperator(
748+
lhs: self,
749+
operator: "MATCH",
750+
rhs: "\(pattern)"
751+
)
745752
}
746753

747754
/// A predicate expression from this string expression matched against another _via_ the `LIKE`
@@ -754,7 +761,7 @@ extension QueryExpression where QueryValue == String {
754761
///
755762
/// - Parameter other: A string expression describing the prefix.
756763
/// - Returns: A predicate expression.
757-
public func hasPrefix(_ other: QueryValue) -> some QueryExpression<Bool> {
764+
public func hasPrefix(_ other: some StringProtocol) -> some QueryExpression<Bool> {
758765
like("\(other)%")
759766
}
760767

@@ -768,7 +775,7 @@ extension QueryExpression where QueryValue == String {
768775
///
769776
/// - Parameter other: A string expression describing the suffix.
770777
/// - Returns: A predicate expression.
771-
public func hasSuffix(_ other: QueryValue) -> some QueryExpression<Bool> {
778+
public func hasSuffix(_ other: some StringProtocol) -> some QueryExpression<Bool> {
772779
like("%\(other)")
773780
}
774781

@@ -783,7 +790,7 @@ extension QueryExpression where QueryValue == String {
783790
/// - Parameter other: A string expression describing the infix.
784791
/// - Returns: A predicate expression.
785792
@_disfavoredOverload
786-
public func contains(_ other: QueryValue) -> some QueryExpression<Bool> {
793+
public func contains(_ other: some StringProtocol) -> some QueryExpression<Bool> {
787794
like("%\(other)%")
788795
}
789796
}

Tests/StructuredQueriesTests/OperatorsTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ extension SnapshotTests {
342342
("rows"."string" LIKE '%a%')
343343
"""
344344
}
345+
assertInlineSnapshot(of: Row.columns.string.contains("a"[...]), as: .sql) {
346+
"""
347+
("rows"."string" LIKE '%a%')
348+
"""
349+
}
345350
assertInlineSnapshot(of: Row.update { $0.string += "!" }, as: .sql) {
346351
"""
347352
UPDATE "rows"

0 commit comments

Comments
 (0)