Skip to content

Commit

Permalink
Ensure #filter can be called with raw predicates from model classes d…
Browse files Browse the repository at this point in the history
…irectly
  • Loading branch information
ellmetha committed Jun 18, 2024
1 parent 1bb1066 commit 91c06f7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spec/marten/db/model/querying_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ describe Marten::DB::Model::Querying do
results.size.should eq 1
results[0].should eq Tag.get!(name: "crystal")
end

it "allows filtering records by providing a single raw predicate" do
TestUser.filter("username = 'jd1'").to_a.should eq [TestUser.get!(username: "jd1")]
end

it "allows filtering records by providing a raw predicate and positional parameters" do
TestUser.filter("username = ?", "jd1").to_a.should eq [TestUser.get!(username: "jd1")]
end

it "allows filtering records by providing a raw predicate and named parameters" do
TestUser.filter("username = :username", username: "jd1").to_a.should eq [TestUser.get!(username: "jd1")]
end

it "allows filtering records by providing a raw predicate and an array of positional parameters" do
TestUser.filter("username = ?", ["jd1"]).to_a.should eq [TestUser.get!(username: "jd1")]
end

it "allows filtering records by providing a raw predicate and a hash of named parameters" do
TestUser.filter("username = :username", {"username" => "jd1"}).to_a.should eq [TestUser.get!(username: "jd1")]
end

it "allows filtering records by providing a raw predicate and a named tuple of named parameters" do
TestUser.filter("username = :username", {username: "jd1"}).to_a.should eq [TestUser.get!(username: "jd1")]
end
end

describe "::first" do
Expand Down
75 changes: 75 additions & 0 deletions src/marten/db/model/querying.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,81 @@ module Marten
default_queryset.filter(**kwargs)
end

# Returns a query set whose records match the given raw predicate and named parameters.
#
# This method enables filtering based on raw SQL conditions, offering greater flexibility than standard field
# predicates. It returns a modified `Marten::DB::Query::Set`.
#
# Example:
#
# ```
# query_set = Post.all
# query_set.filter("is_published = true")
# ```
def filter(raw_predicate : String)
default_queryset.filter(raw_predicate)
end

# Returns a query set whose records match the given raw predicate and named parameters.
#
# This method enables filtering based on raw SQL conditions, offering greater flexibility than standard field
# predicates. It returns a modified `Marten::DB::Query::Set`.
#
# Example:
#
# ```
# query_set = Post.all
# query_set.filter("is_published = ?", true)
# ```
def filter(raw_predicate : String, *args)
default_queryset.filter(raw_predicate, *args)
end

# Returns a query set whose records match the given raw predicate and named parameters.
#
# This method enables filtering based on raw SQL conditions, offering greater flexibility than standard field
# predicates. It returns a modified `Marten::DB::Query::Set`.
#
# Example:
#
# ```
# query_set = Post.all
# query_set.filter("is_published = :published", published: true)
# ```
def filter(raw_predicate : String, **kwargs)
default_queryset.filter(raw_predicate, **kwargs)
end

# Returns a query set whose records match the given raw predicate and named parameters.
#
# This method enables filtering based on raw SQL conditions, offering greater
# flexibility than standard field predicates. It returns a modified `Marten::DB::Query::Set`.
#
# Example:
#
# ```
# query_set = Post.all
# query_set.filter("is_published = ?", [true])
# ```
def filter(raw_predicate : String, params : Array)
default_queryset.filter(raw_predicate, params)
end

# Returns a query set whose records match the given raw predicate and named parameters.
#
# This method enables filtering based on raw SQL conditions, offering greater
# flexibility than standard field predicates. It returns a modified `Marten::DB::Query::Set`.
#
# Example:
#
# ```
# query_set = Post.all
# query_set.filter("is_published = :published", {published: true})
# ```
def filter(raw_predicate : String, params : Hash | NamedTuple)
default_queryset.filter(raw_predicate, params)
end

# Returns a queryset matching a specific set of advanced filters.
#
# This method returns a `Marten::DB::Query::Set` object and allows to define complex database queries
Expand Down

0 comments on commit 91c06f7

Please sign in to comment.