Class: ActiveRecord::ConnectionAdapters::SQLServerAdapter
Constant Summary
collapse
- ADAPTER_NAME =
"SQLServer".freeze
- DEFAULT_TIME_PRECISION =
7
- TYPE_MAP =
Type::TypeMap.new.tap { |m| initialize_type_map(m) }
ActiveRecord::ConnectionAdapters::SQLServer::Version::VERSION
ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_COLUMN_NAMES, ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_TABLE_NAMES
ActiveRecord::ConnectionAdapters::SQLServer::Showplan::OPTIONS, ActiveRecord::ConnectionAdapters::SQLServer::Showplan::OPTION_ALL, ActiveRecord::ConnectionAdapters::SQLServer::Showplan::OPTION_TEXT, ActiveRecord::ConnectionAdapters::SQLServer::Showplan::OPTION_XML
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#fetch_type_metadata, #quote, #quote_default_expression, #quote_string, #quote_string_single, #quote_string_single_national, #quoted_date, #quoted_false, #quoted_true, #type_cast, #unquoted_false, #unquoted_true
#affected_rows, #begin_db_transaction, #begin_isolated_db_transaction, #build_insert_sql, #case_sensitive_comparison, #cast_result, #commit_db_transaction, #exec_delete, #exec_rollback_db_transaction, #exec_update, #execute_procedure, #insert_fixtures_set, #internal_exec_sql_query, #newid_function, #newsequentialid_function, #perform_query, #raw_execute, #set_transaction_isolation_level, #transaction_isolation_levels, #use_database, #user_options, #user_options_dateformat, #user_options_isolation_level, #user_options_language, #with_identity_insert_enabled, #write_query?
#explain
#add_timestamps, #build_change_column_default_definition, #build_change_column_definition, #change_column, #change_column_default, #change_column_null, #change_table_schema, #check_constraints, #columns, #columns_for_distinct, #create_schema, #create_schema_dumper, #create_table, #drop_schema, #drop_table, #extract_foreign_key_action, #foreign_keys, #indexes, #native_database_types, #new_column, #primary_keys, #primary_keys_select, #remove_column, #remove_index!, #rename_column, #rename_index, #rename_table, #schema_names, #type_to_sql, #update_table_definition
#index_name_length, #table_alias_length, #table_name_length
#charset, #collation, #create_database, #current_database, #drop_database
#create_savepoint, #current_savepoint_name, #exec_rollback_to_savepoint, #release_savepoint
Constructor Details
Returns a new instance of SQLServerAdapter.
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 99
def initialize(...)
super
@config[:tds_version] = "7.3" unless @config[:tds_version]
@config[:appname] = self.class.rails_application_name unless @config[:appname]
@config[:login_timeout] = @config[:login_timeout].present? ? @config[:login_timeout].to_i : nil
@config[:timeout] = @config[:timeout].present? ? @config[:timeout].to_i / 1000 : nil
@config[:encoding] = @config[:encoding].present? ? @config[:encoding] : nil
@connection_parameters ||= @config
end
|
Instance Attribute Details
#spid ⇒ Object
Returns the value of attribute spid.
52
53
54
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 52
def spid
@spid
end
|
Class Method Details
.dbconsole(config, options = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 65
def dbconsole(config, options = {})
sqlserver_config = config.configuration_hash
args = []
args += ["-d", "#{config.database}"] if config.database
args += ["-U", "#{sqlserver_config[:username]}"] if sqlserver_config[:username]
args += ["-P", "#{sqlserver_config[:password]}"] if sqlserver_config[:password]
if sqlserver_config[:host]
host_arg = +"tcp:#{sqlserver_config[:host]}"
host_arg << ",#{sqlserver_config[:port]}" if sqlserver_config[:port]
args += ["-S", host_arg]
end
find_cmd_and_exec("sqlcmd", *args)
end
|
.new_client(config) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 82
def new_client(config)
TinyTds::Client.new(config)
rescue TinyTds::Error => error
if error.message.match(/database .* does not exist/i)
raise ActiveRecord::NoDatabaseError
else
raise
end
end
|
.rails_application_name ⇒ Object
92
93
94
95
96
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 92
def rails_application_name
Rails.application.class.name.split("::").first
rescue
nil end
|
Instance Method Details
#active? ⇒ Boolean
Abstract Adapter (Connection Management) ================== #
239
240
241
242
243
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 239
def active?
@raw_connection&.active?
rescue *connection_errors
false
end
|
#arel_visitor ⇒ Object
Abstract Adapter ========================================== #
113
114
115
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 113
def arel_visitor
Arel::Visitors::SQLServer.new(self)
end
|
#check_version ⇒ Object
337
338
339
340
341
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 337
def check_version if schema_cache.database_version < 2012
raise "Your version of SQL Server (#{database_version}) is too old. SQL Server Active Record supports 2012 or higher."
end
end
|
#clear_cache! ⇒ Object
263
264
265
266
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 263
def clear_cache!(...)
@view_information = nil
super
end
|
#combine_bind_parameters(from_clause: [], join_clause: [], where_clause: [], having_clause: [], limit: nil, offset: nil) ⇒ Object
326
327
328
329
330
331
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 326
def combine_bind_parameters(from_clause: [], join_clause: [], where_clause: [], having_clause: [], limit: nil, offset: nil)
result = from_clause + join_clause + where_clause + having_clause
result << offset if offset
result << limit if limit
result
end
|
#database_prefix ⇒ Object
310
311
312
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 310
def database_prefix
@connection_parameters[:database_prefix]
end
|
#database_prefix_identifier(name) ⇒ Object
314
315
316
317
318
319
320
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 314
def database_prefix_identifier(name)
if database_prefix_remote_server?
SQLServer::Utils.("#{database_prefix}#{name}")
else
SQLServer::Utils.(name)
end
end
|
#database_prefix_remote_server? ⇒ Boolean
303
304
305
306
307
308
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 303
def database_prefix_remote_server?
return false if database_prefix.blank?
name = SQLServer::Utils.(database_prefix)
name.fully_qualified? && name.object.blank?
end
|
#disable_referential_integrity ⇒ Object
229
230
231
232
233
234
235
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 229
def disable_referential_integrity
tables = tables_with_referential_integrity
tables.each { |t| execute "ALTER TABLE #{quote_table_name(t)} NOCHECK CONSTRAINT ALL" }
yield
ensure
tables.each { |t| execute "ALTER TABLE #{quote_table_name(t)} CHECK CONSTRAINT ALL" }
end
|
#disconnect! ⇒ Object
254
255
256
257
258
259
260
261
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 254
def disconnect!
super
@raw_connection&.close rescue nil
@raw_connection = nil
@spid = nil
@collation = nil
end
|
#get_database_version ⇒ Object
333
334
335
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 333
def get_database_version version_year
end
|
#pk_and_sequence_for(table_name) ⇒ Object
288
289
290
291
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 288
def pk_and_sequence_for(table_name)
pk = primary_key(table_name)
pk ? [pk, nil] : nil
end
|
#reconnect ⇒ Object
245
246
247
248
249
250
251
252
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 245
def reconnect
@raw_connection&.close rescue nil
@raw_connection = nil
@spid = nil
@collation = nil
connect
end
|
#reset! ⇒ Object
268
269
270
271
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 268
def reset!
reset_transaction
execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
end
|
#return_value_after_insert?(column) ⇒ Boolean
225
226
227
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 225
def return_value_after_insert?(column) column.is_primary? || column.is_identity?
end
|
#schema_creation ⇒ Object
121
122
123
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 121
def schema_creation
SQLServer::SchemaCreation.new(self)
end
|
#sqlserver? ⇒ Boolean
SQLServer Specific (DB Reflection) ======================== #
295
296
297
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 295
def sqlserver?
true
end
|
#sqlserver_azure? ⇒ Boolean
299
300
301
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 299
def sqlserver_azure?
!!(sqlserver_version =~ /Azure/i)
end
|
#supports_advisory_locks? ⇒ Boolean
133
134
135
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 133
def supports_advisory_locks?
false
end
|
#supports_bulk_alter? ⇒ Boolean
129
130
131
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 129
def supports_bulk_alter?
false
end
|
#supports_check_constraints? ⇒ Boolean
173
174
175
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 173
def supports_check_constraints?
true
end
|
181
182
183
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181
def
false
end
|
185
186
187
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 185
def
false
end
|
#supports_common_table_expressions? ⇒ Boolean
197
198
199
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 197
def supports_common_table_expressions?
true
end
|
#supports_datetime_with_precision? ⇒ Boolean
169
170
171
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 169
def supports_datetime_with_precision?
true
end
|
#supports_ddl_transactions? ⇒ Boolean
125
126
127
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 125
def supports_ddl_transactions?
true
end
|
#supports_explain? ⇒ Boolean
149
150
151
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 149
def supports_explain?
true
end
|
#supports_expression_index? ⇒ Boolean
145
146
147
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 145
def supports_expression_index?
false
end
|
#supports_foreign_keys? ⇒ Boolean
161
162
163
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 161
def supports_foreign_keys?
true
end
|
#supports_in_memory_oltp? ⇒ Boolean
205
206
207
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 205
def supports_in_memory_oltp?
version_year >= 2014
end
|
#supports_index_sort_order? ⇒ Boolean
137
138
139
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 137
def supports_index_sort_order?
true
end
|
#supports_indexes_in_create? ⇒ Boolean
157
158
159
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 157
def supports_indexes_in_create?
false
end
|
#supports_insert_conflict_target? ⇒ Boolean
221
222
223
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 221
def supports_insert_conflict_target?
false
end
|
#supports_insert_on_duplicate_skip? ⇒ Boolean
213
214
215
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 213
def supports_insert_on_duplicate_skip?
false
end
|
#supports_insert_on_duplicate_update? ⇒ Boolean
217
218
219
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 217
def supports_insert_on_duplicate_update?
false
end
|
#supports_insert_returning? ⇒ Boolean
209
210
211
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 209
def supports_insert_returning?
true
end
|
#supports_json? ⇒ Boolean
177
178
179
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 177
def supports_json?
version_year >= 2016
end
|
#supports_lazy_transactions? ⇒ Boolean
201
202
203
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 201
def supports_lazy_transactions?
true
end
|
#supports_optimizer_hints? ⇒ Boolean
193
194
195
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 193
def supports_optimizer_hints?
true
end
|
#supports_partial_index? ⇒ Boolean
141
142
143
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 141
def supports_partial_index?
true
end
|
#supports_savepoints? ⇒ Boolean
189
190
191
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 189
def supports_savepoints?
true
end
|
#supports_transaction_isolation? ⇒ Boolean
153
154
155
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 153
def supports_transaction_isolation?
true
end
|
#supports_views? ⇒ Boolean
165
166
167
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 165
def supports_views?
true
end
|
#tables_with_referential_integrity ⇒ Object
Abstract Adapter (Misc Support) =========================== #
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 275
def tables_with_referential_integrity
schemas_and_tables = select_rows <<~SQL.squish
SELECT DISTINCT s.name, o.name
FROM sys.foreign_keys i
INNER JOIN sys.objects o ON i.parent_object_id = o.OBJECT_ID
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
SQL
schemas_and_tables.map do |schema_table|
schema, table = schema_table
"#{SQLServer::Utils.quoted_raw(schema)}.#{SQLServer::Utils.quoted_raw(table)}"
end
end
|
#valid_type?(type) ⇒ Boolean
117
118
119
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 117
def valid_type?(type)
!native_database_types[type].nil?
end
|
#version ⇒ Object
322
323
324
|
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 322
def version
self.class::VERSION
end
|