Skip to content

Commit

Permalink
Fix postgres SQL syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed May 24, 2024
1 parent 458025b commit 0b05a3d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion spec/marten/db/query/set_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ describe Marten::DB::Query::Set do
tag_3 = Tag.create!(name: "coding", is_active: true)
tag_4 = Tag.create!(name: "programming", is_active: true)

qset = Marten::DB::Query::Set(Tag).new.filter("name!=:name", name: "crystal")
qset = Marten::DB::Query::Set(Tag).new.filter("name!=:name", name: "crystal").order(:id)

qset.to_a.should eq [tag_1, tag_3, tag_4]
end
Expand Down
12 changes: 2 additions & 10 deletions src/marten/db/query/sql/raw_predicate_node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,12 @@ module Marten
raise Errors::UnmetQuerySetCondition.new("Wrong number of parameters provided for query: #{@statement}")
end

parameter_offset = 0
sanitized_query = @statement.gsub(POSITIONAL_PARAMETER_RE) do
parameter_offset += 1
connection.parameter_id_for_ordered_argument(parameter_offset)
end

{sanitized_query, params.as(Array(::DB::Any))}
{@statement.gsub(POSITIONAL_PARAMETER_RE, "%s"), params.as(Array(::DB::Any))}
end

private def sanitize_named_parameters(connection)
sanitized_params = [] of ::DB::Any

parameter_offset = 0
sanitized_query = @statement.gsub(NAMED_PARAMETER_RE) do |match|
# Specifically handle PostgreSQL's cast syntax (::).
next match if $1 == ":"
Expand All @@ -89,9 +82,8 @@ module Marten
)
end

parameter_offset += 1
sanitized_params << params.as(Hash(String, ::DB::Any))[parameter_match]
connection.parameter_id_for_ordered_argument(parameter_offset)
"%s"
end

{sanitized_query, sanitized_params}
Expand Down

0 comments on commit 0b05a3d

Please sign in to comment.