Skip to content

Commit 22959f5

Browse files
authored
Add buildArray support for QueryFragmentBuilder<Bool> (#125)
Support for more dynamic queries based off arrays: ```swift Reminder.where { for searchTerm in searchTerms { $0.title.contains(searchTerm) || $0.notes.contains(searchTerm) } } ```
1 parent 6b3f436 commit 22959f5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Sources/StructuredQueriesCore/QueryFragmentBuilder.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public enum QueryFragmentBuilder<Clause> {
2222
}
2323

2424
extension QueryFragmentBuilder<Bool> {
25+
public static func buildArray(_ components: [[QueryFragment]]) -> [QueryFragment] {
26+
components.map { $0.joined(separator: " AND ") }
27+
}
28+
2529
public static func buildExpression(
2630
_ expression: some QueryExpression<Bool>
2731
) -> [QueryFragment] {

Tests/StructuredQueriesTests/WhereTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,22 @@ extension SnapshotTests {
233233
"""
234234
}
235235
}
236+
237+
@Test func buildArray() {
238+
let terms = ["daily", "monthly"]
239+
assertQuery(
240+
RemindersList.where {
241+
for term in terms {
242+
$0.title.contains(term)
243+
}
244+
}
245+
) {
246+
"""
247+
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."title", "remindersLists"."position"
248+
FROM "remindersLists"
249+
WHERE ("remindersLists"."title" LIKE '%daily%') AND ("remindersLists"."title" LIKE '%monthly%')
250+
"""
251+
}
252+
}
236253
}
237254
}

0 commit comments

Comments
 (0)