Class: ActiveRecord::ConnectionAdapters::SQLServerAdapter

Constant Summary collapse

ADAPTER_NAME =
'SQLServer'.freeze

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

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

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

ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_FALSE, ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_STRING_PREFIX, ActiveRecord::ConnectionAdapters::SQLServer::Quoting::QUOTED_TRUE

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

Instance Method Summary collapse

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

#fetch_type_metadata, #quote_column_name, #quote_default_expression, #quote_string, #quote_string_single, #quote_string_single_national, #quoted_date, #quoted_false, #quoted_true, #unquoted_false, #unquoted_true

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

#begin_db_transaction, #begin_isolated_db_transaction, #case_sensitive_comparison, #commit_db_transaction, #create_savepoint, #exec_delete, #exec_insert, #exec_query, #exec_rollback_db_transaction, #exec_rollback_to_savepoint, #exec_update, #execute, #execute_procedure, #insert_fixtures_set, #newid_function, #newsequentialid_function, #release_savepoint, #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

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

#explain

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

#change_column, #change_column_default, #change_column_null, #columns, #columns_for_distinct, #create_schema_dumper, #create_table, #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, #type_to_sql, #update_table_definition

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

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

Constructor Details

#initialize(connection, logger = nil, config = {}) ⇒ SQLServerAdapter

Returns a new instance of SQLServerAdapter.


55
56
57
58
59
60
61
62
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 55

def initialize(connection, logger = nil, config = {})
  super(connection, logger, config)  # Our Responsibility

  @connection_options = config
  connect
  initialize_dateformatter
  use_database
end

Instance Attribute Details

#spidObject (readonly)

Returns the value of attribute spid


43
44
45
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 43

def spid
  @spid
end

Instance Method Details

#active?Boolean

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

Returns:

  • (Boolean)

156
157
158
159
160
161
162
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 156

def active?
  return false unless @connection
  raw_connection_do 'SELECT 1'
  true
rescue *connection_errors
  false
end

#arel_visitorObject

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


66
67
68
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 66

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

#clear_cache!Object


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

def clear_cache!
  @view_information = nil
  super
end

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


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

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


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

def database_prefix
  @connection_options[:database_prefix]
end

#database_prefix_identifier(name) ⇒ Object


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

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)

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

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


146
147
148
149
150
151
152
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 146

def disable_referential_integrity
  tables = tables_with_referential_integrity
  tables.each { |t| do_execute "ALTER TABLE #{quote_table_name(t)} NOCHECK CONSTRAINT ALL" }
  yield
ensure
  tables.each { |t| do_execute "ALTER TABLE #{quote_table_name(t)} CHECK CONSTRAINT ALL" }
end

#disconnect!Object


170
171
172
173
174
175
176
177
178
179
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 170

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

#inspectObject


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

def inspect
  "#<#{self.class} version: #{version}, mode: #{@connection_options[:mode]}, azure: #{sqlserver_azure?.inspect}>"
end

#pk_and_sequence_for(table_name) ⇒ Object


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

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

#reconnect!Object


164
165
166
167
168
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 164

def reconnect!
  super
  disconnect!
  connect
end

#reset!Object


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

def reset!
  reset_transaction
  do_execute 'IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION'
end

#schema_creationObject


74
75
76
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 74

def schema_creation
  SQLServer::SchemaCreation.new self
end

#sqlserver?Boolean

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

Returns:

  • (Boolean)

213
214
215
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 213

def sqlserver?
  true
end

#sqlserver_azure?Boolean

Returns:

  • (Boolean)

217
218
219
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 217

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

#supports_advisory_locks?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 86

def supports_advisory_locks?
  false
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)

82
83
84
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 82

def supports_bulk_alter?
  false
end

#supports_comments?Boolean

Returns:

  • (Boolean)

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

def supports_comments?
  false
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)

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

def supports_comments_in_create?
  false
end

#supports_datetime_with_precision?Boolean

Returns:

  • (Boolean)

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

def supports_datetime_with_precision?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 78

def supports_ddl_transactions?
  true
end

#supports_explain?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 102

def supports_explain?
  true
end

#supports_expression_index?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 98

def supports_expression_index?
  false
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)

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

def supports_foreign_keys?
  true
end

#supports_in_memory_oltp?Boolean

Returns:

  • (Boolean)

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

def supports_in_memory_oltp?
  @version_year >= 2014
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 90

def supports_index_sort_order?
  true
end

#supports_indexes_in_create?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 110

def supports_indexes_in_create?
  false
end

#supports_json?Boolean

Returns:

  • (Boolean)

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

def supports_json?
  @version_year >= 2016
end

#supports_partial_index?Boolean

Returns:

  • (Boolean)

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

def supports_partial_index?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)

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

def supports_savepoints?
  true
end

#supports_transaction_isolation?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 106

def supports_transaction_isolation?
  true
end

#supports_views?Boolean

Returns:

  • (Boolean)

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

def supports_views?
  true
end

#tables_with_referential_integrityObject

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


193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 193

def tables_with_referential_integrity
  schemas_and_tables = select_rows <<-SQL.strip_heredoc
    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)

70
71
72
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 70

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

#versionObject


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

def version
  self.class::VERSION
end