Skip to content

Commit

Permalink
Fix queries with date and date-time placeholder conditions (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored Oct 16, 2024
1 parent 7fcf20d commit 63b6854
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
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

0 comments on commit 63b6854

Please sign in to comment.