Class: ActiveRecord::ConnectionAdapters::TrilogyAdapter

Inherits:
AbstractMysqlAdapter show all
Includes:
ActiveRecord::ConnectionAdapters::Trilogy::DatabaseStatements
Defined in:
lib/active_record/connection_adapters/trilogy_adapter.rb

Constant Summary collapse

ER_BAD_DB_ERROR =
1049
ER_DBACCESS_DENIED_ERROR =
1044
ER_ACCESS_DENIED_ERROR =
1045
ER_SERVER_SHUTDOWN =
1053
ADAPTER_NAME =
"Trilogy"
SSL_MODES =
{
  SSL_MODE_DISABLED: ::Trilogy::SSL_DISABLED,
  SSL_MODE_PREFERRED: ::Trilogy::SSL_PREFERRED_NOVERIFY,
  SSL_MODE_REQUIRED: ::Trilogy::SSL_REQUIRED_NOVERIFY,
  SSL_MODE_VERIFY_CA: ::Trilogy::SSL_VERIFY_CA,
  SSL_MODE_VERIFY_IDENTITY: ::Trilogy::SSL_VERIFY_IDENTITY
}.freeze
TYPE_MAP =
Type::TypeMap.new.tap { |m| initialize_type_map(m) }

Constants inherited from AbstractMysqlAdapter

AbstractMysqlAdapter::EMULATE_BOOLEANS_TRUE, AbstractMysqlAdapter::EXTENDED_TYPE_MAPS, AbstractMysqlAdapter::NATIVE_DATABASE_TYPES

Constants included from MySQL::Quoting

MySQL::Quoting::QUOTED_COLUMN_NAMES, MySQL::Quoting::QUOTED_TABLE_NAMES

Constants inherited from AbstractAdapter

AbstractAdapter::COMMENT_REGEX, AbstractAdapter::EXTENDED_TYPE_MAPS, AbstractAdapter::SIMPLE_INT

Constants included from QueryCache

QueryCache::DEFAULT_SIZE

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#lock, #logger, #owner, #pool, #visitor

Attributes included from QueryCache

#query_cache, #query_cache_enabled

Attributes included from DatabaseStatements

#transaction_manager

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::Trilogy::DatabaseStatements

#exec_delete, #exec_insert, #internal_exec_query, #select_all

Methods inherited from AbstractMysqlAdapter

#add_index, #add_sql_comment!, #begin_db_transaction, #begin_isolated_db_transaction, #build_change_column_default_definition, #build_change_column_definition, #build_create_index_definition, #build_insert_sql, #case_sensitive_comparison, #change_column, #change_column_comment, #change_column_default, #change_column_null, #change_table_comment, #charset, #check_constraints, #check_version, #collation, #columns_for_distinct, #commit_db_transaction, #create_database, #current_database, dbconsole, #default_index_type?, #disable_referential_integrity, #drop_database, #drop_table, #empty_insert_statement_value, #emulate_booleans, #exec_restart_db_transaction, #exec_rollback_db_transaction, #execute_and_free, extended_type_map, #foreign_keys, #get_advisory_lock, #get_database_version, #index_algorithms, #mariadb?, #native_database_types, #primary_keys, #recreate_database, #release_advisory_lock, #rename_column, #rename_index, #rename_table, #show_variable, #strict_mode?, #supports_advisory_locks?, #supports_bulk_alter?, #supports_check_constraints?, #supports_common_table_expressions?, #supports_datetime_with_precision?, #supports_explain?, #supports_expression_index?, #supports_foreign_keys?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_insert_on_duplicate_skip?, #supports_insert_on_duplicate_update?, #supports_optimizer_hints?, #supports_restart_db_transaction?, #supports_transaction_isolation?, #supports_views?, #supports_virtual_columns?, #table_comment, #table_options

Methods included from MySQL::SchemaStatements

#create_schema_dumper, #create_table, #indexes, #internal_string_options_for_primary_key, #remove_column, #schema_creation, #table_alias_length, #type_to_sql, #update_table_definition

Methods included from MySQL::Quoting

#cast_bound_value, #column_name_matcher, #column_name_with_order_matcher, #quote_column_name, #quote_table_name, #quoted_binary, #quoted_date, #type_cast, #unquote_identifier, #unquoted_false, #unquoted_true

Methods included from MySQL::DatabaseStatements

#build_explain_clause, #explain, #high_precision_current_timestamp, #write_query?

Methods inherited from AbstractAdapter

#adapter_name, #add_enum_value, #advisory_locks_enabled?, #all_foreign_keys_valid?, #async_enabled?, #build_insert_sql, build_read_query_regexp, #case_insensitive_comparison, #case_sensitive_comparison, #check_all_foreign_keys_valid!, #check_if_write_query, #check_version, #clean!, #clear_cache!, #close, #connect!, #connection_class, #connection_retries, #create_enum, #database_exists?, database_exists?, #database_version, dbconsole, #default_index_type?, #default_timezone, #default_uniqueness_comparison, #disable_extension, #disable_referential_integrity, #drop_enum, #enable_extension, #expire, extended_type_map, #extensions, find_cmd_and_exec, #get_advisory_lock, #get_database_version, #index_algorithms, #internal_metadata, #lease, #lock_thread=, #migration_context, #migrations_paths, #prefetch_primary_key?, #prepared_statements?, #prepared_statements_disabled_cache, #preventing_writes?, #raw_connection, #reconnect!, register_class_with_precision, #release_advisory_lock, #rename_enum, #rename_enum_value, #replica?, #requires_reloading?, #retry_deadline, #return_value_after_insert?, #role, #schema_cache, #schema_migration, #schema_version, #seconds_idle, #shard, #steal!, #supports_advisory_locks?, #supports_bulk_alter?, #supports_check_constraints?, #supports_common_table_expressions?, #supports_concurrent_connections?, #supports_datetime_with_precision?, #supports_ddl_transactions?, #supports_deferrable_constraints?, #supports_exclusion_constraints?, #supports_explain?, #supports_expression_index?, #supports_extensions?, #supports_foreign_keys?, #supports_foreign_tables?, #supports_index_include?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_insert_conflict_target?, #supports_insert_on_duplicate_skip?, #supports_insert_on_duplicate_update?, #supports_insert_returning?, #supports_materialized_views?, #supports_nulls_not_distinct?, #supports_optimizer_hints?, #supports_partial_index?, #supports_partitioned_indexes?, #supports_restart_db_transaction?, #supports_transaction_isolation?, #supports_unique_constraints?, #supports_validate_constraints?, #supports_views?, #supports_virtual_columns?, #throw_away!, type_cast_config_to_boolean, type_cast_config_to_integer, #unprepared_statement, #use_metadata_table?, #valid_type?, validate_default_timezone, #verify!, #with_instrumenter

Methods included from Savepoints

#create_savepoint, #current_savepoint_name, #exec_rollback_to_savepoint, #release_savepoint

Methods included from QueryCache

#cache, #clear_query_cache, dirties_query_cache, #disable_query_cache!, #enable_query_cache!, included, #select_all, #uncached

Methods included from DatabaseLimits

#index_name_length, #max_identifier_length, #table_alias_length, #table_name_length

Methods included from Quoting

#cast_bound_value, #column_name_matcher, #column_name_with_order_matcher, #lookup_cast_type_from_column, #quote, #quote_bound_value, #quote_column_name, #quote_default_expression, #quote_table_name, #quote_table_name_for_assignment, #quoted_binary, #quoted_date, #quoted_false, #quoted_time, #quoted_true, #sanitize_as_sql_comment, #type_cast, #unquoted_false, #unquoted_true

Methods included from DatabaseStatements

#add_transaction_record, #begin_db_transaction, #begin_isolated_db_transaction, #cacheable_query, #commit_db_transaction, #default_sequence_name, #delete, #empty_insert_statement_value, #exec_delete, #exec_insert, #exec_insert_all, #exec_query, #exec_restart_db_transaction, #exec_rollback_db_transaction, #exec_update, #execute, #explain, #high_precision_current_timestamp, #insert, #insert_fixture, #insert_fixtures_set, #internal_exec_query, #mark_transaction_written_if_write, #query, #query_value, #query_values, #reset_sequence!, #reset_transaction, #restart_db_transaction, #rollback_db_transaction, #rollback_to_savepoint, #sanitize_limit, #select_all, #select_one, #select_rows, #select_value, #select_values, #to_sql, #transaction, #transaction_isolation_levels, #transaction_open?, #truncate, #truncate_tables, #update, #with_yaml_fallback, #write_query?

Methods included from SchemaStatements

#add_check_constraint, #add_column, #add_columns, #add_foreign_key, #add_index, #add_index_options, #add_reference, #add_timestamps, #assume_migrated_upto_version, #build_add_column_definition, #build_change_column_default_definition, #build_create_index_definition, #build_create_join_table_definition, #build_create_table_definition, #bulk_change_table, #change_column, #change_column_comment, #change_column_default, #change_column_null, #change_table, #change_table_comment, #check_constraint_exists?, #check_constraint_options, #check_constraints, #column_exists?, #columns, #columns_for_distinct, #create_join_table, #create_schema_dumper, #create_table, #data_source_exists?, #data_sources, #distinct_relation_for_primary_key, #drop_join_table, #drop_table, #dump_schema_information, #foreign_key_column_for, #foreign_key_exists?, #foreign_key_options, #foreign_keys, #index_algorithm, #index_exists?, #index_name, #index_name_exists?, #indexes, #internal_string_options_for_primary_key, #max_index_name_size, #native_database_types, #options_include_default?, #primary_key, #quoted_columns_for_index, #remove_check_constraint, #remove_column, #remove_columns, #remove_foreign_key, #remove_index, #remove_reference, #remove_timestamps, #rename_column, #rename_index, #rename_table, #schema_creation, #table_alias_for, #table_comment, #table_exists?, #table_options, #tables, #type_to_sql, #update_table_definition, #use_foreign_keys?, #valid_column_definition_options, #valid_primary_key_options, #valid_table_definition_options, #view_exists?, #views

Constructor Details

#initializeTrilogyAdapter

Returns a new instance of TrilogyAdapter.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 102

def initialize(...)
  super

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

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

Class Method Details

.new_client(config) ⇒ Object



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

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) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 64

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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 73

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  !(@raw_connection.nil? || @raw_connection.closed?) && @lock.synchronize { @raw_connection&.ping } || false
rescue ::Trilogy::Error
  false
end

#discard!Object



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

def discard!
  @lock.synchronize do
    super
    @raw_connection&.discard!
    @raw_connection = nil
  end
end

#disconnect!Object



154
155
156
157
158
159
160
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 154

def disconnect!
  @lock.synchronize do
    super
    @raw_connection&.close
    @raw_connection = nil
  end
end

#quote_string(string) ⇒ Object



140
141
142
143
144
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 140

def quote_string(string)
  with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
    conn.escape(string)
  end
end

#savepoint_errors_invalidate_transactions?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 132

def savepoint_errors_invalidate_transactions?
  true
end

#supports_comments?Boolean

Returns:

  • (Boolean)


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

def supports_comments?
  true
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 124

def supports_comments_in_create?
  true
end

#supports_json?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 116

def supports_json?
  !mariadb? && database_version >= "5.7.8"
end

#supports_lazy_transactions?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 136

def supports_lazy_transactions?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/active_record/connection_adapters/trilogy_adapter.rb', line 128

def supports_savepoints?
  true
end