Class: ActiveRecord::ConnectionAdapters::SQLServerAdapter

Overview

class SQLServerColumn

Constant Summary collapse

VERSION =
File.read(File.expand_path("../../../../VERSION",__FILE__)).strip
ADAPTER_NAME =
'SQLServer'.freeze
DATABASE_VERSION_REGEXP =
/Microsoft SQL Server\s+"?(\d{4}|\w+)"?/
SUPPORTED_VERSIONS =
[2005,2008,2010,2011,2012,2014,2016,2017]

Constants included from ActiveRecord::ConnectionAdapters::Sqlserver::Errors

ActiveRecord::ConnectionAdapters::Sqlserver::Errors::LOST_CONNECTION_EXCEPTIONS, ActiveRecord::ConnectionAdapters::Sqlserver::Errors::LOST_CONNECTION_MESSAGES

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

Constants included from ActiveRecord::ConnectionAdapters::Sqlserver::Quoting

ActiveRecord::ConnectionAdapters::Sqlserver::Quoting::QUOTED_STRING_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::Errors

#lost_connection_exceptions, #lost_connection_messages

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::DatabaseLimits

#column_name_length, #columns_per_multicolumn_index, #columns_per_table, #in_clause_length, #index_name_length, #indexes_per_table, #joins_per_query, #sql_query_length, #table_alias_length, #table_name_length

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::SchemaStatements

#change_column, #change_column_default, #change_column_null, #columns, #indexes, #native_database_types, #remove_column, #remove_index!, #rename_column, #rename_table, #table_exists?, #tables, #type_to_sql, #views

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::Showplan

#explain

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::DatabaseStatements

#activity_stats, #add_limit_offset!, #begin_db_transaction, #case_sensitive_modifier, #charset, #commit_db_transaction, #create_database, #create_savepoint, #current_database, #drop_database, #empty_insert_statement_value, #exec_delete, #exec_insert, #exec_query, #exec_update, #execute, #execute_procedure, #newid_function, #newsequentialid_function, #outside_transaction?, #recreate_database, #recreate_database!, #release_savepoint, #rollback_db_transaction, #rollback_to_savepoint, #run_with_isolation_level, #select_rows, #supports_statement_cache?, #transaction, #use_database, #user_options, #user_options_dateformat, #user_options_isolation_level, #user_options_language

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::CoreExt::DatabaseStatements

#transaction_with_retry_deadlock_victim

Methods included from ActiveRecord::ConnectionAdapters::Sqlserver::Quoting

#quote, #quote_column_name, #quote_string, #quote_table_name, #quoted_date, #quoted_datetime, #quoted_false, #quoted_full_iso8601, #quoted_string_prefix, #quoted_true, #substitute_at

Constructor Details

#initialize(connection, logger, pool, config) ⇒ SQLServerAdapter

Returns a new instance of SQLServerAdapter.


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 196

def initialize(connection, logger, pool, config)
  super(connection, logger, pool)
  # AbstractAdapter Responsibility
  @schema_cache = Sqlserver::SchemaCache.new self
  @visitor = Arel::Visitors::SQLServer.new self
  # Our Responsibility
  @config = config
  @connection_options = config
  connect
  @database_version = select_value 'SELECT @@version', 'SCHEMA'
  @database_year = begin
                     if @database_version =~ /Azure/i
                       @sqlserver_azure = true
                       @database_version.match(/\s-\s([0-9.]+)/)[1]
                       year = 2016
                     elsif @database_version =~ /vNext/i
                       year = 2016
                     else
                       year = DATABASE_VERSION_REGEXP.match(@database_version)[1]
                       year == "Denali" ? 2011 : year.to_i
                     end
                   rescue
                     0
                   end
  @product_level    = select_value "SELECT CAST(SERVERPROPERTY('productlevel') AS VARCHAR(128))", 'SCHEMA'
  @product_version  = select_value "SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR(128))", 'SCHEMA'
  @edition          = select_value "SELECT CAST(SERVERPROPERTY('edition') AS VARCHAR(128))", 'SCHEMA'
  initialize_dateformatter
  use_database
  unless (@sqlserver_azure == true || SUPPORTED_VERSIONS.include?(@database_year))
    raise NotImplementedError, "Currently, only #{SUPPORTED_VERSIONS.to_sentence} are supported. We got back #{@database_version}."
  end
end

Instance Attribute Details

#database_versionObject (readonly)

Returns the value of attribute database_version


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

def database_version
  @database_version
end

#database_yearObject (readonly)

Returns the value of attribute database_year


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

def database_year
  @database_year
end

#editionObject (readonly)

Returns the value of attribute edition


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

def edition
  @edition
end

#product_levelObject (readonly)

Returns the value of attribute product_level


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

def product_level
  @product_level
end

#product_versionObject (readonly)

Returns the value of attribute product_version


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

def product_version
  @product_version
end

#spidObject (readonly)

Returns the value of attribute spid


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

def spid
  @spid
end

Instance Method Details

#active?Boolean

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

Returns:

  • (Boolean)

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

def active?
  case @connection_options[:mode]
  when :dblib
    return @connection.active?
  end
  raw_connection_do("SELECT 1")
  true
rescue *lost_connection_exceptions
  false
end

#adapter_nameObject

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


232
233
234
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 232

def adapter_name
  ADAPTER_NAME
end

#auto_connectObject


353
354
355
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 353

def auto_connect
  @@auto_connect.is_a?(FalseClass) ? false : true
end

#auto_connect_durationObject


357
358
359
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 357

def auto_connect_duration
  @@auto_connect_duration ||= 10
end

#cs_equality_operatorObject


386
387
388
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 386

def cs_equality_operator
  @@cs_equality_operator || 'COLLATE Latin1_General_CS_AS_WS'
end

#disable_referential_integrityObject


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

def disable_referential_integrity
  do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
  yield
ensure
  do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
end

#disconnect!Object


294
295
296
297
298
299
300
301
302
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 294

def disconnect!
  @spid = nil
  case @connection_options[:mode]
  when :dblib
    @connection.close rescue nil
  when :odbc
    @connection.disconnect rescue nil
  end
end

#inspectObject


349
350
351
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 349

def inspect
  "#<#{self.class} version: #{version}, year: #{@database_year}, product_level: #{@product_level.inspect}, product_version: #{@product_version.inspect}, edition: #{@edition.inspect}, connection_options: #{@connection_options.inspect}>"
end

#native_binary_database_typeObject


382
383
384
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 382

def native_binary_database_type
  @@native_binary_database_type || 'varbinary(max)'
end

#native_date_database_typeObject


378
379
380
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 378

def native_date_database_type
  sqlserver_2005? ? 'datetime' : 'date'
end

#native_string_database_typeObject


366
367
368
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 366

def native_string_database_type
  @@native_string_database_type || (enable_default_unicode_types ? 'nvarchar' : 'varchar')
end

#native_text_database_typeObject


370
371
372
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 370

def native_text_database_type
  @@native_text_database_type || enable_default_unicode_types ? 'nvarchar(max)' : 'varchar(max)'
end

#native_time_database_typeObject


374
375
376
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 374

def native_time_database_type
  sqlserver_2005? ? 'datetime' : 'time'
end

#pk_and_sequence_for(table_name) ⇒ Object

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


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

def pk_and_sequence_for(table_name)
  idcol = identity_column(table_name)
  idcol ? [idcol.name,nil] : nil
end

#primary_key(table_name) ⇒ Object


315
316
317
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 315

def primary_key(table_name)
  identity_column(table_name).try(:name) || schema_cache.columns[table_name].detect(&:is_primary?).try(:name)
end

#reconnect!Object


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

def reconnect!
  disconnect!
  connect
  active?
end

#reset!Object


304
305
306
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 304

def reset!
  remove_database_connections_and_rollback { }
end

#retry_deadlock_victimObject Also known as: retry_deadlock_victim?


361
362
363
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 361

def retry_deadlock_victim
  @@retry_deadlock_victim.is_a?(FalseClass) ? false : true
end

#sqlserver?Boolean

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

Returns:

  • (Boolean)

321
322
323
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 321

def sqlserver?
  true
end

#sqlserver_2005?Boolean

Returns:

  • (Boolean)

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

def sqlserver_2005?
  @database_year == 2005
end

#sqlserver_2008?Boolean

Returns:

  • (Boolean)

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

def sqlserver_2008?
  @database_year == 2008
end

#sqlserver_2011?Boolean

Returns:

  • (Boolean)

333
334
335
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 333

def sqlserver_2011?
  @database_year == 2011
end

#sqlserver_2012?Boolean

Returns:

  • (Boolean)

337
338
339
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 337

def sqlserver_2012?
  @database_year == 2012
end

#sqlserver_azure?Boolean

Returns:

  • (Boolean)

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

def sqlserver_azure?
  @sqlserver_azure
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)

252
253
254
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 252

def supports_bulk_alter?
  false
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)

244
245
246
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 244

def supports_count_distinct?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)

248
249
250
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 248

def supports_ddl_transactions?
  true
end

#supports_explain?Boolean

Returns:

  • (Boolean)

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

def supports_explain?
  true
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)

260
261
262
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 260

def supports_index_sort_order?
  true
end

#supports_migrations?Boolean

Returns:

  • (Boolean)

236
237
238
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 236

def supports_migrations?
  true
end

#supports_primary_key?Boolean

Returns:

  • (Boolean)

240
241
242
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 240

def supports_primary_key?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)

256
257
258
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 256

def supports_savepoints?
  true
end

#versionObject


345
346
347
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 345

def version
  self.class::VERSION
end