Skip to content

Commit 928febf

Browse files
committed
[Rails5] Support unprepared_statement blocks & ValueTooLong excp.
1 parent b485c2a commit 928febf

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,15 @@ def column_definitions(table_name)
329329
INNER JOIN #{database}.sys.columns AS c
330330
ON o.object_id = c.object_id
331331
AND c.name = columns.COLUMN_NAME
332-
WHERE columns.TABLE_NAME = @0
333-
AND columns.TABLE_SCHEMA = #{identifier.schema.blank? ? 'schema_name()' : '@1'}
332+
WHERE columns.TABLE_NAME = #{prepared_statements ? '@0' : quote(identifier.object)}
333+
AND columns.TABLE_SCHEMA = #{identifier.schema.blank? ? 'schema_name()' : (prepared_statements ? '@1' : quote(identifier.schema))}
334334
ORDER BY columns.ordinal_position
335335
}.gsub(/[ \t\r\n]+/, ' ').strip
336336
binds = []
337337
nv128 = SQLServer::Type::UnicodeVarchar.new limit: 128
338338
binds << Relation::QueryAttribute.new('TABLE_NAME', identifier.object, nv128)
339339
binds << Relation::QueryAttribute.new('TABLE_SCHEMA', identifier.schema, nv128) unless identifier.schema.blank?
340-
results = sp_executesql(sql, 'SCHEMA', binds, prepare: true)
340+
results = sp_executesql(sql, 'SCHEMA', binds)
341341
results.map do |ci|
342342
ci = ci.symbolize_keys
343343
ci[:_type] = ci[:type]

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ def translate_exception(e, message)
316316
DeadlockVictim.new(message)
317317
when /database .* does not exist/i
318318
NoDatabaseError.new(message)
319+
when /data would be truncated/
320+
ValueTooLong.new(message)
319321
else
320322
super
321323
end

test/cases/coerced_tests.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22

33

44

5+
require 'models/event'
56
module ActiveRecord
67
class AdapterTest < ActiveRecord::TestCase
78
# As far as I can tell, SQL Server does not support null bytes in strings.
89
coerce_tests! :test_update_prepared_statement
10+
11+
# So sp_executesql swallows this exception. Run without prpared to see it.
12+
coerce_tests! :test_value_limit_violations_are_translated_to_specific_exception
13+
def test_value_limit_violations_are_translated_to_specific_exception_coerced
14+
connection.unprepared_statement do
15+
error = assert_raises(ActiveRecord::ValueTooLong) do
16+
Event.create(title: 'abcdefgh')
17+
end
18+
assert_not_nil error.cause
19+
end
20+
end
921
end
1022
end
1123

0 commit comments

Comments
 (0)