Skip to content

Commit 2a76eea

Browse files
committed
Added a log_info_schema_queries class attribute and make all queries to INFORMATION_SCHEMA silent by default.
1 parent e576fcc commit 2a76eea

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

CHANGELOG

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

22
MASTER
33

4+
* Added a log_info_schema_queries class attribute and make all queries to INFORMATION_SCHEMA silent by
5+
default. [Ken Collins]
6+
47
* Fix millisecond support in datetime columns. ODBC::Timestamp incorrectly takes SQL Server milliseconds
58
and applies them as nanoseconds. We cope with this at the DBI layer by using SQLServerDBI::Type::SqlserverTimestamp
69
class to parse the before type cast value appropriately. Also update the adapters #quoted_date method

README.rdoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ To pass the ActiveRecord tests we had to implement an class accessor for the nat
5858
* SQL Server 2000 is 'text'
5959
* SQL Server 2005 is 'varchar(max)'
6060

61-
During testing this type is set to 'varchar(8000)' for both versions. The reason is that rails expects to be able to use SQL = operators on text data types and this is not possible with a native 'text' data type in SQL Server. The default 'varchar(max)' for SQL Server 2005 can be queried using the SQL = operator and has plenty of storage space which is why we made it the default for 2005. If for some reason you want to change the data type created during migrations for any SQL Server version, you can configure this line to your liking in a config/initializer file.
61+
During testing this type is set to 'varchar(8000)' for both versions. The reason is that rails expects to be able to use SQL = operators on text data types and this is not possible with a native 'text' data type in SQL Server. The default 'varchar(max)' for SQL Server 2005 can be queried using the SQL = operator and has plenty of storage space which is why we made it the default for 2005. If for some reason you want to change the data type created during migrations for any SQL Server version, you can configure this line to your liking in a config/initializers file.
6262

6363
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
6464

@@ -68,6 +68,13 @@ By default any :binary column created by migrations will create these native typ
6868
* SQL Server 2005 is 'varbinary(max)'
6969

7070

71+
== Schema Information Logging
72+
73+
By default all queries to the INFORMATION_SCHEMA table is silenced. If you think logging these queries are useful, you can enable it by adding this like to a config/initializers file.
74+
75+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.log_info_schema_queries = true
76+
77+
7178
== Versions
7279

7380
It is our goal to match the adapter version with each version of rails. However we will track our own tiny version independent of ActiveRecord. For example, an adapter version of 2.2.x will work on any 2.2.x version of ActiveRecord. This convention will be used in both the Git tags as well as the Rubygems versioning.

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class SQLServerAdapter < AbstractAdapter
155155
SUPPORTED_VERSIONS = [2000,2005].freeze
156156
LIMITABLE_TYPES = ['string','integer','float','char','nchar','varchar','nvarchar'].freeze
157157

158-
cattr_accessor :native_text_database_type, :native_binary_database_type
158+
cattr_accessor :native_text_database_type, :native_binary_database_type, :log_info_schema_queries
159159

160160
class << self
161161

@@ -189,7 +189,7 @@ def supports_ddl_transactions?
189189
end
190190

191191
def database_version
192-
@database_version ||= select_value('SELECT @@version')
192+
@database_version ||= info_schema_query { select_value('SELECT @@version') }
193193
end
194194

195195
def database_year
@@ -452,18 +452,20 @@ def table_alias_length
452452
end
453453

454454
def tables(name = nil)
455-
select_values "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'"
455+
info_schema_query do
456+
select_values "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'"
457+
end
456458
end
457459

458460
def views(name = nil)
459461
@sqlserver_views_cache ||=
460-
select_values "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME NOT IN ('sysconstraints','syssegments')"
462+
info_schema_query { select_values("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME NOT IN ('sysconstraints','syssegments')") }
461463
end
462464

463465
def view_information(table_name)
464466
table_name = unqualify_table_name(table_name)
465467
@sqlserver_view_information_cache[table_name] ||=
466-
select_one "SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '#{table_name}'"
468+
info_schema_query { select_one("SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '#{table_name}'") }
467469
end
468470

469471
def view_table_name(table_name)
@@ -671,6 +673,10 @@ def update_sql(sql, name = nil)
671673
select_value('SELECT @@ROWCOUNT AS AffectedRows')
672674
end
673675

676+
def info_schema_query
677+
log_info_schema_queries ? yield : ActiveRecord::Base.silence{ yield }
678+
end
679+
674680
def raw_execute(sql, name = nil, &block)
675681
log(sql, name) do
676682
if block_given?
@@ -736,7 +742,7 @@ def add_limit_offset_for_association_limiting!(sql, options)
736742
# SCHEMA STATEMENTS ========================================#
737743

738744
def remove_check_constraints(table_name, column_name)
739-
constraints = select_values("SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{quote_string(table_name)}' and COLUMN_NAME = '#{quote_string(column_name)}'")
745+
constraints = info_schema_query { select_values("SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{quote_string(table_name)}' and COLUMN_NAME = '#{quote_string(column_name)}'") }
740746
constraints.each do |constraint|
741747
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_column_name(constraint)}"
742748
end
@@ -892,7 +898,7 @@ def column_definitions(table_name)
892898
WHERE columns.TABLE_NAME = '#{table_name}'
893899
ORDER BY columns.ordinal_position
894900
}.gsub(/[ \t\r\n]+/,' ')
895-
results = without_type_conversion { select(sql,nil,true) }
901+
results = info_schema_query { without_type_conversion{ select(sql,nil,true) } }
896902
results.collect do |ci|
897903
ci.symbolize_keys!
898904
ci[:type] = case ci[:type]
@@ -909,7 +915,7 @@ def column_definitions(table_name)
909915
real_table_name = table_name_or_views_table_name(table_name)
910916
real_column_name = views_real_column_name(table_name,ci[:name])
911917
col_default_sql = "SELECT c.COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = '#{real_table_name}' AND c.COLUMN_NAME = '#{real_column_name}'"
912-
ci[:default_value] = without_type_conversion { select_value(col_default_sql) }
918+
ci[:default_value] = info_schema_query { without_type_conversion{ select_value(col_default_sql) } }
913919
end
914920
ci[:default_value] = case ci[:default_value]
915921
when nil, '(null)', '(NULL)'

test/cases/adapter_test_sqlserver.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,6 @@ def setup
473473
end
474474

475475
should 'find default values' do
476-
# col_default_sql = "SELECT c.COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = 'string_defaults' AND c.COLUMN_NAME = 'string_with_pretend_null_one'"
477-
# raise @connection.select_value(col_default_sql).inspect
478-
# raise @connection.without_type_conversion{ @connection.select_value(col_default_sql) }.inspect
479-
480476
assert_equal 'null', StringDefaultsView.new.pretend_null,
481477
StringDefaultsView.columns_hash['pretend_null'].inspect
482478
end

0 commit comments

Comments
 (0)