Skip to content

Commit 0b20ec1

Browse files
committed
Add support for view defaults by making column_definitions use real table name for schema info.
1 parent 4770248 commit 0b20ec1

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

CHANGELOG

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11

2-
*MASTER* ()
2+
MASTER
3+
4+
*
5+
6+
7+
2.2.2 (December 2nd, 2008)
8+
9+
* Add support for view defaults by making column_definitions use real table name for schema info.
310

411
* Include version in connection method and inspection. [Ken Collins]
512

613

7-
*2.2.1* (November 25th, 2008)
14+
2.2.1 (November 25th, 2008)
815

916
* Add identity insert support for views. Cache #views so that identity #table_name_or_views_table_name
1017
will run quickly. [Ken Collins]
@@ -13,7 +20,7 @@
1320
#table_exists? will not fall back to checking views too. [Ken Collins]
1421

1522

16-
*2.2.0* (November 21st, 2008)
23+
2.2.0 (November 21st, 2008)
1724

1825
* Release for rails 2.2.2. Many many changes. [Ken Collins], [Murray Steele], [Shawn Balestracci], [Joe Rafaniello]
1926

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def default_name(table_name, column_name)
756756
# IDENTITY INSERTS =========================================#
757757

758758
def with_identity_insert_enabled(table_name, &block)
759-
table_name = table_name_or_views_table_name(table_name)
759+
table_name = quote_table_name(table_name_or_views_table_name(table_name))
760760
set_identity_insert(table_name, true)
761761
yield
762762
ensure
@@ -786,7 +786,7 @@ def identity_column(table_name)
786786

787787
def table_name_or_views_table_name(table_name)
788788
unquoted_table_name = unqualify_table_name(table_name)
789-
views.include?(unquoted_table_name) ? view_table_name(unquoted_table_name) : table_name
789+
views.include?(unquoted_table_name) ? view_table_name(unquoted_table_name) : unquoted_table_name
790790
end
791791

792792
# HELPER METHODS ===========================================#
@@ -854,7 +854,8 @@ def initialize_sqlserver_caches(reset_columns=true)
854854

855855
def column_definitions(table_name)
856856
db_name = unqualify_db_name(table_name)
857-
table_name = unqualify_table_name(table_name)
857+
real_table_name = table_name_or_views_table_name(table_name)
858+
passed_table_name = unqualify_table_name(table_name)
858859
sql = %{
859860
SELECT
860861
columns.TABLE_NAME as table_name,
@@ -879,12 +880,13 @@ def column_definitions(table_name)
879880
ELSE 1
880881
END as is_identity
881882
FROM #{db_name}INFORMATION_SCHEMA.COLUMNS columns
882-
WHERE columns.TABLE_NAME = '#{table_name}'
883+
WHERE columns.TABLE_NAME = '#{real_table_name}'
883884
ORDER BY columns.ordinal_position
884885
}.gsub(/[ \t\r\n]+/,' ')
885886
results = without_type_conversion { select(sql,nil,true) }
886887
results.collect do |ci|
887888
ci.symbolize_keys!
889+
ci[:table_name] = passed_table_name
888890
ci[:type] = case ci[:type]
889891
when /^bit|image|text|ntext|datetime$/
890892
ci[:type]

test/cases/adapter_test_sqlserver.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,14 @@ def setup
402402
assert CustomersView.columns_hash['id'].is_identity?
403403
end
404404

405+
should 'pick up on default values from table' do
406+
assert_equal 0, CustomersView.new.balance
407+
assert_equal 'null', StringDefaultsView.new.string_with_pretend_null_one
408+
end
409+
405410
should 'yield all column objects having the correct table name' do
406-
assert CustomersView.columns.all?{ |c| c.table_name == 'customers_view' }
411+
assert CustomersView.columns.all?{ |c| c.table_name == 'customers_view' },
412+
CustomersView.columns.map(&:table_name).inspect
407413
end
408414

409415
should 'respond true to table_exists?' do

test/cases/sqlserver_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class FkTestHasFk < ActiveRecord::Base ; end
2020
class FkTestHasPk < ActiveRecord::Base ; end
2121
class NumericData < ActiveRecord::Base ; self.table_name = 'numeric_data' ; end
2222
class CustomersView < ActiveRecord::Base ; self.table_name = 'customers_view' ; end
23+
class StringDefaultsView < ActiveRecord::Base ; self.table_name = 'string_defaults_view' ; end
2324
class SqlServerUnicode < ActiveRecord::Base ; end
2425
class SqlServerString < ActiveRecord::Base ; end
2526
class SqlServerChronic < ActiveRecord::Base

test/schema/sqlserver_specific_schema.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
SELECT id, name, balance
6868
FROM customers
6969
CUSTOMERSVIEW
70-
70+
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'string_defaults_view') DROP VIEW string_defaults_view"
71+
execute <<-STRINGDEFAULTSVIEW
72+
CREATE VIEW string_defaults_view AS
73+
SELECT id, string_with_pretend_null_one
74+
FROM string_defaults
75+
STRINGDEFAULTSVIEW
7176

7277
end

0 commit comments

Comments
 (0)