Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix queries with date and date-time placeholder conditions #1247

Merged
merged 2 commits into from
Oct 16, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Fixed

- [#1244](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1244) Allow INSERT statements with SELECT notation
- [#1247](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1247) Fix queries with date and date-time placeholder conditions

## v7.2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def sp_executesql_sql_type(attr)
end
end

value = basic_attribute_type?(attr) ? attr : attr.value_for_database
value = active_model_attribute?(attr) ? attr.value_for_database : attr

if value.is_a?(Numeric)
value > 2_147_483_647 ? "bigint".freeze : "int".freeze
Expand All @@ -349,7 +349,7 @@ def sp_executesql_sql_type(attr)
end

def sp_executesql_sql_param(attr)
return quote(attr) if basic_attribute_type?(attr)
return quote(attr) unless active_model_attribute?(attr)

case value = attr.value_for_database
when Type::Binary::Data, ActiveRecord::Type::SQLServer::Data
Expand All @@ -359,14 +359,8 @@ def sp_executesql_sql_param(attr)
end
end

def basic_attribute_type?(type)
type.is_a?(Symbol) ||
type.is_a?(String) ||
type.is_a?(Numeric) ||
type.is_a?(Time) ||
type.is_a?(TrueClass) ||
type.is_a?(FalseClass) ||
type.is_a?(NilClass)
def active_model_attribute?(type)
type.is_a?(::ActiveModel::Attribute)
end

def sp_executesql_sql(sql, types, params, name)
Expand Down
14 changes: 14 additions & 0 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,18 @@ def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
end
end
end

describe "placeholder conditions" do
it 'using time placeholder' do
assert_equal Task.where("starting < ?", Time.now).count, 1
end

it 'using date placeholder' do
assert_equal Task.where("starting < ?", Date.today).count, 1
end

it 'using date-time placeholder' do
assert_equal Task.where("starting < ?", DateTime.current).count, 1
end
end
end
Loading