Class: ActiveRecord::ConnectionAdapters::SQLServerAdapter

Overview

class SQLServerColumn

Constant Summary collapse

ADAPTER_NAME =
'SQLServer'.freeze
DATABASE_VERSION_REGEXP =
/Microsoft SQL Server\s+"?(\d{4}|\w+)"?/
SUPPORTED_VERSIONS =
[2005,2008,2010,2011].freeze

Constants included from ActiveRecord::ConnectionAdapters::Sqlserver::Version

ActiveRecord::ConnectionAdapters::Sqlserver::Version::VERSION

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::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::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.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 190

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
  @connection_options = config
  connect
  @database_version = info_schema_query { select_value('SELECT @@version') }
  @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    = info_schema_query { select_value("SELECT CAST(SERVERPROPERTY('productlevel') AS VARCHAR(128))") }
  @product_version  = info_schema_query { select_value("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR(128))") }
  @edition          = info_schema_query { select_value("SELECT CAST(SERVERPROPERTY('edition') AS VARCHAR(128))") }
  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.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def database_version
  @database_version
end

#database_yearObject (readonly)

Returns the value of attribute database_year.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def database_year
  @database_year
end

#editionObject (readonly)

Returns the value of attribute edition.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def edition
  @edition
end

#product_levelObject (readonly)

Returns the value of attribute product_level.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def product_level
  @product_level
end

#product_versionObject (readonly)

Returns the value of attribute product_version.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def product_version
  @product_version
end

#spidObject (readonly)

Returns the value of attribute spid.



181
182
183
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 181

def spid
  @spid
end

Instance Method Details

#active?Boolean

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

Returns:

  • (Boolean)


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

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



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

def adapter_name
  ADAPTER_NAME
end

#auto_connectObject



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

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

#auto_connect_durationObject



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

def auto_connect_duration
  @@auto_connect_duration ||= 10
end

#cs_equality_operatorObject



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

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

#disable_referential_integrityObject



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

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



272
273
274
275
276
277
278
279
280
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 272

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

#inspectObject



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

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



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

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

#native_date_database_typeObject



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

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

#native_string_database_typeObject



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

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

#native_text_database_typeObject



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

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

#native_time_database_typeObject



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

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

#pk_and_sequence_for(table_name) ⇒ Object

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



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

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

#primary_key(table_name) ⇒ Object



293
294
295
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 293

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

#reconnect!Object



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

def reconnect!
  disconnect!
  connect
  active?
end

#reset!Object



282
283
284
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 282

def reset!
  remove_database_connections_and_rollback { }
end

#retry_deadlock_victimObject Also known as: retry_deadlock_victim?



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

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

#sqlserver?Boolean

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

Returns:

  • (Boolean)


299
300
301
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 299

def sqlserver?
  true
end

#sqlserver_2005?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2005?
  @database_year == 2005
end

#sqlserver_2008?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2008?
  @database_year == 2008
end

#sqlserver_2011?Boolean

Returns:

  • (Boolean)


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

def sqlserver_2011?
  @database_year == 2011
end

#sqlserver_azure?Boolean

Returns:

  • (Boolean)


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

def sqlserver_azure?
  @sqlserver_azure
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


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

def supports_count_distinct?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


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

def supports_ddl_transactions?
  true
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


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

def supports_migrations?
  true
end

#supports_primary_key?Boolean

Returns:

  • (Boolean)


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

def supports_primary_key?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


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

def supports_savepoints?
  true
end

#versionObject



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

def version
  self.class::VERSION
end