class ActiveRecord::ConnectionAdapters::TrilogyAdapter

Constants

ADAPTER_NAME
ER_ACCESS_DENIED_ERROR
ER_BAD_DB_ERROR
ER_DBACCESS_DENIED_ERROR
SSL_MODES
TYPE_MAP

Public Class Methods

new(config, *) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 75
def initialize(config, *)
  config = config.dup

  # Trilogy ignores `socket` if `host is set. We want the opposite to allow
  # configuring UNIX domain sockets via `DATABASE_URL`.
  config.delete(:host) if config[:socket]

  # Set FOUND_ROWS capability on the connection so UPDATE queries returns number of rows
  # matched rather than number of rows updated.
  config[:found_rows] = true

  if config[:prepared_statements]
    raise ArgumentError, "Trilogy currently doesn't support prepared statements. Remove `prepared_statements: true` from your database configuration."
  end

  super
end
new_client(config) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 30
def new_client(config)
  config[:ssl_mode] = parse_ssl_mode(config[:ssl_mode]) if config[:ssl_mode]
  ::Trilogy.new(config)
rescue ::Trilogy::Error => error
  raise translate_connect_error(config, error)
end
parse_ssl_mode(mode) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 37
def parse_ssl_mode(mode)
  return mode if mode.is_a? Integer

  m = mode.to_s.upcase
  m = "SSL_MODE_#{m}" unless m.start_with? "SSL_MODE_"

  SSL_MODES.fetch(m.to_sym, mode)
end
translate_connect_error(config, error) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 46
def translate_connect_error(config, error)
  case error.error_code
  when ER_DBACCESS_DENIED_ERROR, ER_BAD_DB_ERROR
    ActiveRecord::NoDatabaseError.db_error(config[:database])
  when ER_ACCESS_DENIED_ERROR
    ActiveRecord::DatabaseConnectionError.username_error(config[:username])
  else
    if error.message.include?("TRILOGY_DNS_ERROR")
      ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
    else
      ActiveRecord::ConnectionNotEstablished.new(error.message)
    end
  end
end

Private Class Methods

initialize_type_map(m) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 62
def initialize_type_map(m)
  super

  m.register_type(%r(char)i) do |sql_type|
    limit = extract_limit(sql_type)
    Type.lookup(:string, adapter: :trilogy, limit: limit)
  end

  m.register_type %r(^enum)i, Type.lookup(:string, adapter: :trilogy)
  m.register_type %r(^set)i,  Type.lookup(:string, adapter: :trilogy)
end

Public Instance Methods

active?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 123
def active?
  connected? && @lock.synchronize { @raw_connection&.ping } || false
rescue ::Trilogy::Error
  false
end
connected?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 119
def connected?
  !(@raw_connection.nil? || @raw_connection.closed?)
end
discard!() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 139
def discard!
  @lock.synchronize do
    super
    @raw_connection&.discard!
    @raw_connection = nil
  end
end
disconnect!() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 131
def disconnect!
  @lock.synchronize do
    super
    @raw_connection&.close
    @raw_connection = nil
  end
end
savepoint_errors_invalidate_transactions?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 111
def savepoint_errors_invalidate_transactions?
  true
end
supports_comments?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 99
def supports_comments?
  true
end
supports_comments_in_create?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 103
def supports_comments_in_create?
  true
end
supports_json?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 95
def supports_json?
  !mariadb? && database_version >= "5.7.8"
end
supports_lazy_transactions?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 115
def supports_lazy_transactions?
  true
end
supports_savepoints?() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 107
def supports_savepoints?
  true
end

Private Instance Methods

connect() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 156
def connect
  @raw_connection = self.class.new_client(@config)
rescue ConnectionNotEstablished => ex
  raise ex.set_pool(@pool)
end
default_prepared_statements() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 195
def default_prepared_statements
  false
end
error_number(exception) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 152
def error_number(exception)
  exception.error_code if exception.respond_to?(:error_code)
end
full_version() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 168
def full_version
  database_version.full_version_string
end
get_full_version() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 172
def get_full_version
  with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
    conn.server_info[:version]
  end
end
reconnect() click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 162
def reconnect
  @raw_connection&.close
  @raw_connection = nil
  connect
end
text_type?(type) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 148
def text_type?(type)
  TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
end
translate_exception(exception, message:, sql:, binds:) click to toggle source
# File lib/active_record/connection_adapters/trilogy_adapter.rb, line 178
def translate_exception(exception, message:, sql:, binds:)
  if exception.is_a?(::Trilogy::TimeoutError) && !exception.error_code
    return ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds, connection_pool: @pool)
  end

  case exception
  when ::Trilogy::ConnectionClosed, ::Trilogy::EOFError
    return ConnectionFailed.new(message, connection_pool: @pool)
  when ::Trilogy::Error
    if exception.is_a?(SystemCallError) || exception.message.include?("TRILOGY_INVALID_SEQUENCE_ID")
      return ConnectionFailed.new(message, connection_pool: @pool)
    end
  end

  super
end