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) }

Constants included from ActiveRecord::ConnectionAdapters::SQLServer::Version

ActiveRecord::ConnectionAdapters::SQLServer::Version::VERSION

Constants included from ActiveRecord::ConnectionAdapters::SQLServer::Quoting

ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_COLUMN_NAMES, ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_TABLE_NAMES

Constants included from ActiveRecord::ConnectionAdapters::SQLServer::Showplan

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

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::Quoting

#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

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::DatabaseStatements

#begin_db_transaction, #begin_isolated_db_transaction, #build_insert_sql, #case_sensitive_comparison, #commit_db_transaction, #exec_delete, #exec_rollback_db_transaction, #exec_update, #execute_procedure, #insert_fixtures_set, #internal_exec_query, #internal_exec_sql_query, #newid_function, #newsequentialid_function, #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?

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::Showplan

#explain

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::SchemaStatements

#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

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::DatabaseLimits

#index_name_length, #table_alias_length, #table_name_length

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::DatabaseTasks

#charset, #collation, #create_database, #current_database, #drop_database

Methods included from ActiveRecord::ConnectionAdapters::SQLServer::Savepoints

#create_savepoint, #current_savepoint_name, #exec_rollback_to_savepoint, #release_savepoint

Constructor Details

#initializeSQLServerAdapter

Returns a new instance of SQLServerAdapter.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 101

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

#spidObject (readonly)

Returns the value of attribute spid.



54
55
56
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 54

def spid
  @spid
end

Class Method Details

.dbconsole(config, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 67

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



84
85
86
87
88
89
90
91
92
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 84

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_nameObject



94
95
96
97
98
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 94

def rails_application_name
  Rails.application.class.name.split("::").first
rescue
  nil # Might not be in a Rails context so we fallback to `nil`.
end

Instance Method Details

#active?Boolean

Abstract Adapter (Connection Management) ================== #

Returns:

  • (Boolean)


241
242
243
244
245
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 241

def active?
  @raw_connection&.active?
rescue *connection_errors
  false
end

#arel_visitorObject

Abstract Adapter ========================================== #



115
116
117
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 115

def arel_visitor
  Arel::Visitors::SQLServer.new(self)
end

#check_versionObject

:nodoc:



339
340
341
342
343
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 339

def check_version # :nodoc:
  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



265
266
267
268
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 265

def clear_cache!(...)
  @view_information = nil
  super
end

#combine_bind_parameters(from_clause: [], join_clause: [], where_clause: [], having_clause: [], limit: nil, offset: nil) ⇒ Object



328
329
330
331
332
333
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 328

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_prefixObject



312
313
314
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 312

def database_prefix
  @connection_parameters[:database_prefix]
end

#database_prefix_identifier(name) ⇒ Object



316
317
318
319
320
321
322
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 316

def database_prefix_identifier(name)
  if database_prefix_remote_server?
    SQLServer::Utils.extract_identifiers("#{database_prefix}#{name}")
  else
    SQLServer::Utils.extract_identifiers(name)
  end
end

#database_prefix_remote_server?Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 305

def database_prefix_remote_server?
  return false if database_prefix.blank?

  name = SQLServer::Utils.extract_identifiers(database_prefix)
  name.fully_qualified? && name.object.blank?
end

#disable_referential_integrityObject



231
232
233
234
235
236
237
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 231

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



256
257
258
259
260
261
262
263
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 256

def disconnect!
  super

  @raw_connection&.close rescue nil
  @raw_connection = nil
  @spid = nil
  @collation = nil
end

#get_database_versionObject

:nodoc:



335
336
337
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 335

def get_database_version # :nodoc:
  version_year
end

#pk_and_sequence_for(table_name) ⇒ Object



290
291
292
293
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 290

def pk_and_sequence_for(table_name)
  pk = primary_key(table_name)
  pk ? [pk, nil] : nil
end

#reconnectObject



247
248
249
250
251
252
253
254
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 247

def reconnect
  @raw_connection&.close rescue nil
  @raw_connection = nil
  @spid = nil
  @collation = nil

  connect
end

#reset!Object



270
271
272
273
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 270

def reset!
  reset_transaction
  execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
end

#return_value_after_insert?(column) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


227
228
229
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 227

def return_value_after_insert?(column) # :nodoc:
  column.is_primary? || column.is_identity?
end

#schema_creationObject



123
124
125
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 123

def schema_creation
  SQLServer::SchemaCreation.new(self)
end

#sqlserver?Boolean

SQLServer Specific (DB Reflection) ======================== #

Returns:

  • (Boolean)


297
298
299
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 297

def sqlserver?
  true
end

#sqlserver_azure?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 301

def sqlserver_azure?
  !!(sqlserver_version =~ /Azure/i)
end

#supports_advisory_locks?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 135

def supports_advisory_locks?
  false
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 131

def supports_bulk_alter?
  false
end

#supports_check_constraints?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 175

def supports_check_constraints?
  true
end

#supports_comments?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 183

def supports_comments?
  false
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 187

def supports_comments_in_create?
  false
end

#supports_common_table_expressions?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 199

def supports_common_table_expressions?
  true
end

#supports_datetime_with_precision?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 171

def supports_datetime_with_precision?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 127

def supports_ddl_transactions?
  true
end

#supports_explain?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 151

def supports_explain?
  true
end

#supports_expression_index?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 147

def supports_expression_index?
  false
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 163

def supports_foreign_keys?
  true
end

#supports_in_memory_oltp?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 207

def supports_in_memory_oltp?
  version_year >= 2014
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 139

def supports_index_sort_order?
  true
end

#supports_indexes_in_create?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 159

def supports_indexes_in_create?
  false
end

#supports_insert_conflict_target?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 223

def supports_insert_conflict_target?
  false
end

#supports_insert_on_duplicate_skip?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 215

def supports_insert_on_duplicate_skip?
  false
end

#supports_insert_on_duplicate_update?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 219

def supports_insert_on_duplicate_update?
  false
end

#supports_insert_returning?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 211

def supports_insert_returning?
  true
end

#supports_json?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 179

def supports_json?
  version_year >= 2016
end

#supports_lazy_transactions?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 203

def supports_lazy_transactions?
  true
end

#supports_optimizer_hints?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 195

def supports_optimizer_hints?
  true
end

#supports_partial_index?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 143

def supports_partial_index?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 191

def supports_savepoints?
  true
end

#supports_transaction_isolation?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 155

def supports_transaction_isolation?
  true
end

#supports_views?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 167

def supports_views?
  true
end

#tables_with_referential_integrityObject

Abstract Adapter (Misc Support) =========================== #



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 277

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 119

def valid_type?(type)
  !native_database_types[type].nil?
end

#versionObject



324
325
326
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 324

def version
  self.class::VERSION
end