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]

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
# 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 =~ /Microsoft SQL Azure/i
                       @sqlserver_azure = true
                       @database_version.match(/\s(\d{4})\s/)[1].to_i
                     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 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)


274
275
276
277
278
279
280
281
282
283
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 274

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 ========================================== #



229
230
231
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 229

def adapter_name
  ADAPTER_NAME
end

#auto_connectObject



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

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

#auto_connect_durationObject



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

def auto_connect_duration
  @@auto_connect_duration ||= 10
end

#cs_equality_operatorObject



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

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

#disable_referential_integrityObject



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

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



291
292
293
294
295
296
297
298
299
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 291

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

#inspectObject



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

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



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

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

#native_date_database_typeObject



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

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

#native_string_database_typeObject



363
364
365
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 363

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

#native_text_database_typeObject



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

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

#native_time_database_typeObject



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

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

#pk_and_sequence_for(table_name) ⇒ Object

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



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

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

#primary_key(table_name) ⇒ Object



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

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

#reconnect!Object



285
286
287
288
289
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 285

def reconnect!
  disconnect!
  connect
  active?
end

#reset!Object



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

def reset!
  remove_database_connections_and_rollback { }
end

#retry_deadlock_victimObject Also known as: retry_deadlock_victim?



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

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

#sqlserver?Boolean

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

Returns:

  • (Boolean)


318
319
320
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 318

def sqlserver?
  true
end

#sqlserver_2005?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2005?
  @database_year == 2005
end

#sqlserver_2008?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2008?
  @database_year == 2008
end

#sqlserver_2011?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2011?
  @database_year == 2011
end

#sqlserver_2012?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2012?
  @database_year == 2012
end

#sqlserver_azure?Boolean

Returns:

  • (Boolean)


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

def sqlserver_azure?
  @sqlserver_azure
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)


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

def supports_bulk_alter?
  false
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


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

def supports_count_distinct?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


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

def supports_ddl_transactions?
  true
end

#supports_explain?Boolean

Returns:

  • (Boolean)


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

def supports_explain?
  true
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)


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

def supports_index_sort_order?
  true
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


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

def supports_migrations?
  true
end

#supports_primary_key?Boolean

Returns:

  • (Boolean)


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

def supports_primary_key?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


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

def supports_savepoints?
  true
end

#versionObject



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

def version
  self.class::VERSION
end