Class: ActiveRecord::ConnectionAdapters::DepartureAdapter

Inherits:
AbstractMysqlAdapter
  • Object
show all
Extended by:
Forwardable
Includes:
ForAlterStatements
Defined in:
lib/active_record/connection_adapters/percona_adapter.rb

Defined Under Namespace

Classes: Column, SchemaCreation

Constant Summary collapse

ADAPTER_NAME =
'Percona'.freeze

Instance Method Summary collapse

Methods included from ForAlterStatements

#add_column_for_alter, #add_index_for_alter, #add_timestamps_for_alter, #bulk_change_table, #change_column_for_alter, included, #remove_column_for_alter, #remove_columns_for_alter, #remove_index_for_alter, #remove_timestamps_for_alter, #rename_column_for_alter

Constructor Details

#initialize(connection, _logger, connection_options, _config) ⇒ DepartureAdapter

Returns a new instance of DepartureAdapter.



74
75
76
77
78
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 74

def initialize(connection, _logger, connection_options, _config)
  @mysql_adapter = connection_options[:mysql_adapter]
  super
  @prepared_statements = false
end

Instance Method Details

#add_index(table_name, column_name, options = {}) ⇒ Object

Adds a new index to the table

Parameters:

  • table_name (String, Symbol)
  • column_name (String, Symbol)
  • options (Hash) (defaults to: {})

    optional



130
131
132
133
134
135
136
137
138
139
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 130

def add_index(table_name, column_name, options = {})
  if ActiveRecord::VERSION::STRING >= '6.1'
    index, algorithm, if_not_exists = add_index_options(table_name, column_name, options)
    create_index = CreateIndexDefinition.new(index, algorithm, if_not_exists)
    execute schema_creation.accept(create_index)
  else
    index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options)
    execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})#{index_options}" # rubocop:disable Metrics/LineLength
  end
end

#change_table(table_name, _options = {}) {|update_table_definition(table_name, recorder)| ... } ⇒ Object

Yields:

  • (update_table_definition(table_name, recorder))


160
161
162
163
164
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 160

def change_table(table_name, _options = {})
  recorder = ActiveRecord::Migration::CommandRecorder.new(self)
  yield update_table_definition(table_name, recorder)
  bulk_change_table(table_name, recorder.commands)
end

#error_number(_exception) ⇒ Object

Returns the MySQL error number from the exception. The AbstractMysqlAdapter requires it to be implemented



168
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 168

def error_number(_exception); end

#exec_delete(sql, name, binds) ⇒ Object Also known as: exec_update



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

def exec_delete(sql, name, binds)
  execute(to_sql(sql, binds), name)
  @connection.affected_rows
end

#exec_insert(sql, name, binds, pk = nil, sequence_name = nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument, Metrics/LineLength



92
93
94
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 92

def exec_insert(sql, name, binds, pk = nil, sequence_name = nil) # rubocop:disable Lint/UnusedMethodArgument, Metrics/LineLength
  execute(to_sql(sql, binds), name)
end

#exec_query(sql, name = 'SQL', _binds = []) ⇒ Object



96
97
98
99
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 96

def exec_query(sql, name = 'SQL', _binds = [])
  result = execute(sql, name)
  ActiveRecord::Result.new(result.fields, result.to_a)
end

#full_versionObject



170
171
172
173
174
175
176
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 170

def full_version
  if ActiveRecord::VERSION::MAJOR < 6
    get_full_version
  else
    schema_cache.database_version.full_version_string
  end
end

#get_full_versionObject

This is a method defined in Rails 6.0, and we have no control over the naming of this method.



180
181
182
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 180

def get_full_version # rubocop:disable Naming/AccessorMethodName
  mysql_adapter.raw_connection.server_info[:version]
end

#new_column(field, default, type_metadata, null, table_name, default_function, collation, comment) ⇒ Object

rubocop:disable Metrics/ParameterLists



120
121
122
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 120

def new_column(field, default, , null, table_name, default_function, collation, comment)
  Column.new(field, default, , null, table_name, default_function, collation, comment)
end

#remove_index(table_name, *args, **options) ⇒ Object

Remove the given index from the table.

Parameters:

  • table_name (String, Symbol)
  • options (Hash)

    optional



145
146
147
148
149
150
151
152
153
154
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 145

def remove_index(table_name, *args, **options)
  column_name = args.first
  if column_name
    return if options[:if_exists] && !index_exists?(table_name, column_name, **options)
    index_name = index_name_for_remove(table_name, column_name, **options)
  else
    index_name = index_name_for_remove(table_name, options)
  end
  execute "ALTER TABLE #{quote_table_name(table_name)} DROP INDEX #{quote_column_name(index_name)}"
end

#schema_creationObject



156
157
158
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 156

def schema_creation
  SchemaCreation.new(self)
end

#select(sql, name = nil, binds = []) ⇒ Object

Executes a SELECT query and returns an array of record hashes with the column names as keys and column values as values.



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

def select(sql, name = nil, binds = [])
  exec_query(sql, name, binds)
end

#select_rows(arel, name = nil, binds = []) ⇒ Object

Executes a SELECT query and returns an array of rows. Each row is an array of field values.



104
105
106
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 104

def select_rows(arel, name = nil, binds = [])
  select_all(arel, name, binds).rows
end

#supports_migrations?Boolean

Returns true, as this adapter supports migrations

Returns:

  • (Boolean)


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

def supports_migrations?
  true
end

#write_query?(sql) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/active_record/connection_adapters/percona_adapter.rb', line 80

def write_query?(sql) # :nodoc:
  !ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
    :desc, :describe, :set, :show, :use
  ).match?(sql)
end