Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/CaseStudies/ObservableModelDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftUI

struct ObservableModelDemo: SwiftUICaseStudy {
let readMe = """
This demonstrates how to use the `@FetchAll` and `@FetchOne` tools in an @Observable model. \
This demonstrates how to use the `@FetchAll` and `@FetchOne` tools in an `@Observable` model. \
In SwiftUI, the `@Query` macro only works when installed directly in a SwiftUI view, and \
cannot be used outside of views.

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 37 additions & 27 deletions Examples/Reminders/SearchReminders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class SearchRemindersModel {
withErrorReporting {
try database.write { db in
try Reminder
.where { $0.isCompleted && $0.id.in(baseQuery.select { $1.id }) }
.where {
$0.isCompleted && $0.id.in(
baseQuery(searchText: searchText, searchTokens: searchTokens).select { $1.id }
)
}
.where {
if let monthsAgo {
#sql("\($0.dueDate) < date('now', '-\(raw: monthsAgo) months')")
Expand All @@ -78,29 +82,6 @@ class SearchRemindersModel {
}
}

private var baseQuery: SelectOf<ReminderText, Reminder> {
let searchText = searchText.quoted()

return
ReminderText
.where {
if !searchText.isEmpty {
$0.match(searchText)
}
}
.where {
for token in searchTokens {
switch token.kind {
case .near:
$0.match("NEAR(\(token.rawValue.quoted()))")
case .tag:
$0.tags.match(token.rawValue)
}
}
}
.join(Reminder.all) { $0.rowid.eq($1.rowid) }
}

private func updateQuery(debounce: Bool = true) async throws {
if debounce {
try await clock.sleep(for: .seconds(0.3))
Expand All @@ -120,7 +101,8 @@ class SearchRemindersModel {
} else {
try await $searchResults.load(
SearchRequest(
baseQuery: baseQuery,
searchText: searchText,
searchTokens: searchTokens,
showCompletedInSearchResults: showCompletedInSearchResults
),
animation: .default
Expand All @@ -145,10 +127,12 @@ class SearchRemindersModel {
var completedCount = 0
var rows: [Row] = []
}
let baseQuery: SelectOf<ReminderText, Reminder>
let searchText: String
let searchTokens: [Token]
let showCompletedInSearchResults: Bool
func fetch(_ db: Database) throws -> Value {
try Value(
let baseQuery = baseQuery(searchText: searchText, searchTokens: searchTokens)
return try Value(
completedCount:
baseQuery
.where { $1.isCompleted }
Expand Down Expand Up @@ -280,6 +264,32 @@ struct SearchRemindersView: View {
}
}

fileprivate func baseQuery(
searchText: String,
searchTokens: [SearchRemindersModel.Token]
) -> SelectOf<ReminderText, Reminder> {
let searchText = searchText.quoted()

return
ReminderText
.where {
if !searchText.isEmpty {
$0.match(searchText)
}
}
.where {
for token in searchTokens {
switch token.kind {
case .near:
$0.match("NEAR(\(token.rawValue.quoted()))")
case .tag:
$0.tags.match(token.rawValue)
}
}
}
.join(Reminder.all) { $0.rowid.eq($1.rowid) }
}

extension String {
fileprivate func quoted() -> String {
split(separator: " ")
Expand Down
13 changes: 2 additions & 11 deletions Sources/SQLiteData/Internal/StatementKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,10 @@ protocol StatementKeyRequest<QueryValue>: FetchKeyRequest {

extension StatementKeyRequest {
static func == (lhs: Self, rhs: Self) -> Bool {
// NB: A Swift 6.1 regression prevents this from compiling:
// https://github.com/swiftlang/swift/issues/79623
// return AnyHashable(lhs.statement) == AnyHashable(rhs.statement)
let lhs = lhs.statement
let rhs = rhs.statement
return AnyHashable(lhs) == AnyHashable(rhs)
lhs.statement.query == rhs.statement.query
}

func hash(into hasher: inout Hasher) {
// NB: A Swift 6.1 regression prevents this from compiling:
// https://github.com/swiftlang/swift/issues/79623
// hasher.combine(statement)
let statement = statement
hasher.combine(statement)
hasher.combine(statement.query)
}
}