Skip to content

Commit

Permalink
Fix queries with date and date-time placeholder conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Oct 15, 2024
1 parent 818e50f commit f7c7714
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
- [#1215](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1215) Fix mismatched foreign key errors
- [#1228](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1228) Enable identity insert on view's base table
- [#1238](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1238) Allow INSERT statements with SELECT notation
- [#1246](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1246) Fix queries with date and date-time placeholder conditions

Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,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 @@ -344,7 +344,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 @@ -354,15 +354,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?(Date) ||
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 @@ -602,4 +602,18 @@ def setup
@conn.execute("ALTER TABLE engines DROP COLUMN old_car_id") rescue nil
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 f7c7714

Please sign in to comment.