Skip to content

Commit b2b5c45

Browse files
committed
Allow the SQLServerColumn objects to store the real table name.
1 parent d2a3b4f commit b2b5c45

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def indexes(table_name, name = nil)
4545
def columns(table_name, _name = nil)
4646
return [] if table_name.blank?
4747
column_definitions(table_name).map do |ci|
48-
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :default_function
48+
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :default_function, :table_name
4949
cast_type = lookup_cast_type(ci[:type])
5050
new_column ci[:name], ci[:default_value], cast_type, ci[:type], ci[:null], sqlserver_options
5151
end
@@ -192,8 +192,10 @@ def initialize_native_database_types
192192
end
193193

194194
def column_definitions(table_name)
195-
identifier = SQLServer::Utils.extract_identifiers(table_name)
196-
database = "#{identifier.database_quoted}." if identifier.database_quoted
195+
identifier = SQLServer::Utils.extract_identifiers(table_name)
196+
database = "#{identifier.database_quoted}." if identifier.database_quoted
197+
view_exists = schema_cache.view_exists?(table_name)
198+
view_tblnm = table_name_or_views_table_name(table_name) if view_exists
197199
sql = %{
198200
SELECT DISTINCT
199201
#{lowercase_schema_reflection_sql('columns.TABLE_NAME')} AS table_name,
@@ -247,6 +249,7 @@ def column_definitions(table_name)
247249
results.map do |ci|
248250
ci = ci.symbolize_keys
249251
ci[:_type] = ci[:type]
252+
ci[:table_name] = view_tblnm || table_name
250253
ci[:type] = case ci[:type]
251254
when /^bit|image|text|ntext|datetime$/
252255
ci[:type]
@@ -264,11 +267,11 @@ def column_definitions(table_name)
264267
ci[:default_value],
265268
ci[:default_function] = begin
266269
default = ci[:default_value]
267-
if default.nil? && schema_cache.view_exists?(table_name)
270+
if default.nil? && view_exists
268271
default = select_value "
269272
SELECT c.COLUMN_DEFAULT
270273
FROM #{database}INFORMATION_SCHEMA.COLUMNS c
271-
WHERE c.TABLE_NAME = '#{table_name_or_views_table_name(table_name)}'
274+
WHERE c.TABLE_NAME = '#{view_tblnm}'
272275
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'".squish, 'SCHEMA'
273276
end
274277
case default

lib/active_record/connection_adapters/sqlserver_column.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def sql_type_for_statement
1616
end
1717
end
1818

19+
def table_name
20+
@sqlserver_options[:table_name]
21+
end
22+
1923
def is_identity?
2024
@sqlserver_options[:is_identity]
2125
end

test/cases/column_test_sqlserver.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
class ColumnTestSQLServer < ActiveRecord::TestCase
55

6+
it '#table_name' do
7+
assert SSTestDatatype.columns.all? { |c| c.table_name == 'sst_datatypes' }
8+
assert SSTestCustomersView.columns.all? { |c| c.table_name == 'customers' }
9+
end
10+
611
describe 'ActiveRecord::ConnectionAdapters::SQLServer::Type' do
712

813
let(:obj) { SSTestDatatype.new }

0 commit comments

Comments
 (0)