Correct way to combine where clauses? #324

Closed
opened 2025-12-29 18:25:40 +01:00 by adam · 2 comments
Owner

Originally created by @zachwaugh on GitHub (Mar 26, 2020).

I'm wondering the best way to build a query with conditionals. It looks something like this where we optional want to limit the query based on some state.

var query = From<Post>()

if unseen {
  query = query.where(\.unread == true)
}

if let date = updatedSince {
  query = query.where(\.updatedAt > date)
}

That will do an OR, but I want it to be an AND. The closest I've found is doing this:

var query = From<Post>()
var filter = Where<Post>()

if unseen {
   filter = filter && Where(\.unread == true)
}

if let date = updatedSince {
  filter = filter && Where(\.updatedAt > date)
}

query.where(filter)

That seems to work, but feels like maybe I'm missing a simpler way? Also, this is a contrived example, but I'd like to be able to pass in an existing FetchChainBuilder<Post> from elsewhere and append Where clauses using AND as well. Ideally, we'd have an API like:

func filter(query: FetchChainBuilder<Post>) -> FetchChainBuilder<Post> {
  var filters: [Where<Post>()] = []

  if unseen {
    filters.append(Where(\.unread == true))
  }

  if let date = updatedSince {
    filters.append(Where(\.updatedAt > date))
  }

  return query.where(filters)
}

Thanks!

Originally created by @zachwaugh on GitHub (Mar 26, 2020). I'm wondering the best way to build a query with conditionals. It looks something like this where we optional want to limit the query based on some state. ```swift var query = From<Post>() if unseen { query = query.where(\.unread == true) } if let date = updatedSince { query = query.where(\.updatedAt > date) } ``` That will do an `OR`, but I want it to be an `AND`. The closest I've found is doing this: ```swift var query = From<Post>() var filter = Where<Post>() if unseen { filter = filter && Where(\.unread == true) } if let date = updatedSince { filter = filter && Where(\.updatedAt > date) } query.where(filter) ``` That seems to work, but feels like maybe I'm missing a simpler way? Also, this is a contrived example, but I'd like to be able to pass in an existing `FetchChainBuilder<Post>` from elsewhere and append `Where` clauses using `AND` as well. Ideally, we'd have an API like: ```swift func filter(query: FetchChainBuilder<Post>) -> FetchChainBuilder<Post> { var filters: [Where<Post>()] = [] if unseen { filters.append(Where(\.unread == true)) } if let date = updatedSince { filters.append(Where(\.updatedAt > date)) } return query.where(filters) } ``` Thanks!
adam added the question label 2025-12-29 18:25:40 +01:00
adam closed this issue 2025-12-29 18:25:40 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Mar 27, 2020):

You can use an array of Where clauses:

var filters: [Where<Post>] = []
if unseen {
    filters.append(\.unread == true)
}
if let date = updatedSince {
    filters.append(\.updatedAt > date)
}
return query.where(fitlers.combinedByAnd())
@JohnEstropia commented on GitHub (Mar 27, 2020): You can use an array of `Where` clauses: ```swift var filters: [Where<Post>] = [] if unseen { filters.append(\.unread == true) } if let date = updatedSince { filters.append(\.updatedAt > date) } return query.where(fitlers.combinedByAnd()) ```
Author
Owner

@zachwaugh commented on GitHub (Mar 27, 2020):

Wow, that’s exactly what I was looking for, not sure I how I missed it. Thank you!

@zachwaugh commented on GitHub (Mar 27, 2020): Wow, that’s exactly what I was looking for, not sure I how I missed it. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#324