Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure raw_connection has a @raw_connection set (2) #2368

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DatabaseStatements
def execute(sql, name = nil, async: false)
sql = transform_query(sql)

log(sql, name, async: async) { @raw_connection.exec(sql) }
log(sql, name, async: async) { _connection.exec(sql) }
end

def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
Expand All @@ -25,10 +25,10 @@ def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
cached = false
with_retry do
if without_prepared_statement?(binds)
cursor = @raw_connection.prepare(sql)
cursor = _connection.prepare(sql)
else
unless @statements.key? sql
@statements[sql] = @raw_connection.prepare(sql)
@statements[sql] = _connection.prepare(sql)
end

cursor = @statements[sql]
Expand Down Expand Up @@ -101,10 +101,10 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
returning_id_col = returning_id_index = nil
with_retry do
if without_prepared_statement?(binds)
cursor = @raw_connection.prepare(sql)
cursor = _connection.prepare(sql)
else
unless @statements.key?(sql)
@statements[sql] = @raw_connection.prepare(sql)
@statements[sql] = _connection.prepare(sql)
end

cursor = @statements[sql]
Expand Down Expand Up @@ -141,12 +141,12 @@ def exec_update(sql, name = nil, binds = [])
with_retry do
cached = false
if without_prepared_statement?(binds)
cursor = @raw_connection.prepare(sql)
cursor = _connection.prepare(sql)
else
if @statements.key?(sql)
cursor = @statements[sql]
else
cursor = @statements[sql] = @raw_connection.prepare(sql)
cursor = @statements[sql] = _connection.prepare(sql)
end

cursor.bind_params(type_casted_binds)
Expand All @@ -164,7 +164,7 @@ def exec_update(sql, name = nil, binds = [])
alias :exec_delete :exec_update

def begin_db_transaction # :nodoc:
@raw_connection.autocommit = false
_connection.autocommit = false
end

def transaction_isolation_levels
Expand All @@ -183,15 +183,15 @@ def begin_isolated_db_transaction(isolation)
end

def commit_db_transaction # :nodoc:
@raw_connection.commit
_connection.commit
ensure
@raw_connection.autocommit = true
_connection.autocommit = true
end

def exec_rollback_db_transaction # :nodoc:
@raw_connection.rollback
_connection.rollback
ensure
@raw_connection.autocommit = true
_connection.autocommit = true
end

def create_savepoint(name = current_savepoint_name) # :nodoc:
Expand Down Expand Up @@ -265,14 +265,14 @@ def write_lobs(table_name, klass, attributes, columns) # :nodoc:
raise ActiveRecord::RecordNotFound, "statement #{sql} returned no rows"
end
lob = lob_record[col.name]
@raw_connection.write_lob(lob, value.to_s, col.type == :binary)
_connection.write_lob(lob, value.to_s, col.type == :binary)
end
end
end

private
def with_retry
@raw_connection.with_retry do
_connection.with_retry do
yield
rescue
@statements.clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ def type_cast(value)
when ActiveModel::Type::Binary::Data
lob_value = value == "" ? " " : value
bind_type = OCI8::BLOB
ora_value = bind_type.new(@raw_connection.raw_oci_connection, lob_value)
ora_value = bind_type.new(_connection.raw_oci_connection, lob_value)
ora_value.size = 0 if value == ""
ora_value
when Type::OracleEnhanced::Text::Data
lob_value = value.to_s == "" ? " " : value.to_s
bind_type = OCI8::CLOB
ora_value = bind_type.new(@raw_connection.raw_oci_connection, lob_value)
ora_value = bind_type.new(_connection.raw_oci_connection, lob_value)
ora_value.size = 0 if value.to_s == ""
ora_value
when Type::OracleEnhanced::NationalCharacterText::Data
lob_value = value.to_s == "" ? " " : value.to_s
bind_type = OCI8::NCLOB
ora_value = bind_type.new(@raw_connection.raw_oci_connection, lob_value)
ora_value = bind_type.new(_connection.raw_oci_connection, lob_value)
ora_value.size = 0 if value.to_s == ""
ora_value
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def table_exists?(table_name)
end

def data_source_exists?(table_name)
(_owner, _table_name) = @raw_connection.describe(table_name)
(_owner, _table_name) = _connection.describe(table_name)
true
rescue
false
Expand Down Expand Up @@ -87,7 +87,7 @@ def synonyms
end

def indexes(table_name) # :nodoc:
(_owner, table_name) = @raw_connection.describe(table_name)
(_owner, table_name) = _connection.describe(table_name)
default_tablespace_name = default_tablespace

result = select_all(<<~SQL.squish, "SCHEMA", [bind_string("table_name", table_name)])
Expand Down Expand Up @@ -368,7 +368,7 @@ def index_name(table_name, options) # :nodoc:
#
# Will always query database and not index cache.
def index_name_exists?(table_name, index_name)
(_owner, table_name) = @raw_connection.describe(table_name)
(_owner, table_name) = _connection.describe(table_name)
result = select_value(<<~SQL.squish, "SCHEMA", [bind_string("table_name", table_name), bind_string("index_name", index_name.to_s.upcase)])
SELECT 1 FROM all_indexes i
WHERE i.owner = SYS_CONTEXT('userenv', 'current_schema')
Expand Down Expand Up @@ -511,7 +511,7 @@ def change_column_comment(table_name, column_name, comment_or_changes)

def table_comment(table_name) # :nodoc:
# TODO
(_owner, table_name) = @raw_connection.describe(table_name)
(_owner, table_name) = _connection.describe(table_name)
select_value(<<~SQL.squish, "SCHEMA", [bind_string("table_name", table_name)])
SELECT comments FROM all_tab_comments
WHERE owner = SYS_CONTEXT('userenv', 'current_schema')
Expand All @@ -527,7 +527,7 @@ def table_options(table_name) # :nodoc:

def column_comment(table_name, column_name) # :nodoc:
# TODO: it does not exist in Abstract adapter
(_owner, table_name) = @raw_connection.describe(table_name)
(_owner, table_name) = _connection.describe(table_name)
select_value(<<~SQL.squish, "SCHEMA", [bind_string("table_name", table_name), bind_string("column_name", column_name.upcase)])
SELECT comments FROM all_col_comments
WHERE owner = SYS_CONTEXT('userenv', 'current_schema')
Expand Down Expand Up @@ -555,7 +555,7 @@ def tablespace(table_name)

# get table foreign keys for schema dump
def foreign_keys(table_name) # :nodoc:
(_owner, desc_table_name) = @raw_connection.describe(table_name)
(_owner, desc_table_name) = _connection.describe(table_name)

fk_info = select_all(<<~SQL.squish, "SCHEMA", [bind_string("desc_table_name", desc_table_name)])
SELECT r.table_name to_table
Expand Down
31 changes: 18 additions & 13 deletions lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,13 @@ def native_database_types # :nodoc:

def auto_retry=(value) # :nodoc:
@auto_retry = value
@raw_connection.auto_retry = value if @raw_connection
_connection.auto_retry = value if _connection
end

# return raw OCI8 or JDBC connection
def raw_connection
@raw_connection.raw_connection
verify!
_connection.raw_connection
end

# Returns true if the connection is active.
Expand All @@ -446,21 +447,21 @@ def active? # :nodoc:
# #active? method is also available, but that simply returns the
# last known state, which isn't good enough if the connection has
# gone stale since the last use.
@raw_connection.ping
_connection.ping
rescue OracleEnhanced::ConnectionException
false
end

def reconnect
@raw_connection.reset # tentative
_connection.reset # tentative
rescue OracleEnhanced::ConnectionException
connect
end

# Reconnects to the database.
def reconnect! # :nodoc:
super
@raw_connection.reset!
_connection.reset!
rescue OracleEnhanced::ConnectionException => e
@logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}" if @logger
end
Expand All @@ -478,12 +479,12 @@ def reset!
# Disconnects from the database.
def disconnect! # :nodoc:
super
@raw_connection.logoff rescue nil
_connection.logoff rescue nil
end

def discard!
super
@raw_connection = nil
_connection = nil
end

# use in set_sequence_name to avoid fetching primary key value from sequence
Expand All @@ -508,7 +509,7 @@ def prefetch_primary_key?(table_name = nil)
table_name = table_name.to_s
do_not_prefetch = @do_not_prefetch_primary_key[table_name]
if do_not_prefetch.nil?
owner, desc_table_name = @raw_connection.describe(table_name)
owner, desc_table_name = _connection.describe(table_name)
@do_not_prefetch_primary_key[table_name] = do_not_prefetch = !has_primary_key?(table_name, owner, desc_table_name)
end
!do_not_prefetch
Expand Down Expand Up @@ -577,7 +578,7 @@ def default_tablespace
end

def column_definitions(table_name)
(owner, desc_table_name) = @raw_connection.describe(table_name)
(owner, desc_table_name) = _connection.describe(table_name)

select_all(<<~SQL.squish, "SCHEMA", [bind_string("owner", owner), bind_string("table_name", desc_table_name)])
SELECT cols.column_name AS name, cols.data_type AS sql_type,
Expand Down Expand Up @@ -609,7 +610,7 @@ def clear_table_columns_cache(table_name)
# Find a table's primary key and sequence.
# *Note*: Only primary key is implemented - sequence will be nil.
def pk_and_sequence_for(table_name, owner = nil, desc_table_name = nil) # :nodoc:
(owner, desc_table_name) = @raw_connection.describe(table_name)
(owner, desc_table_name) = _connection.describe(table_name)

seqs = select_values_forcing_binds(<<~SQL.squish, "SCHEMA", [bind_string("owner", owner), bind_string("sequence_name", default_sequence_name(desc_table_name))])
select us.sequence_name
Expand Down Expand Up @@ -651,7 +652,7 @@ def has_primary_key?(table_name, owner = nil, desc_table_name = nil) # :nodoc:
end

def primary_keys(table_name) # :nodoc:
(_owner, desc_table_name) = @raw_connection.describe(table_name)
(_owner, desc_table_name) = _connection.describe(table_name)

pks = select_values_forcing_binds(<<~SQL.squish, "SCHEMA", [bind_string("table_name", desc_table_name)])
SELECT cc.column_name
Expand Down Expand Up @@ -696,7 +697,7 @@ def max_identifier_length
alias index_name_length max_identifier_length

def get_database_version
@raw_connection.database_version
_connection.database_version
end

def check_version
Expand All @@ -707,6 +708,10 @@ def check_version
end
end

private def _connection
@unconfigured_connection || @raw_connection
end

class << self
def type_map
@type_map ||= Type::TypeMap.new.tap { |m| initialize_type_map(m) }
Expand Down Expand Up @@ -773,7 +778,7 @@ def extract_limit(sql_type) # :nodoc:
end

def translate_exception(exception, message:, sql:, binds:) # :nodoc:
case @raw_connection.error_code(exception)
case _connection.error_code(exception)
when 1
RecordNotUnique.new(message, sql: sql, binds: binds)
when 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class ::Post < ActiveRecord::Base
end

after(:all) do
Object.send(:remove_const, "Post")
Object.send(:remove_const, "Post") if defined?(Post)
ActiveRecord::Base.clear_cache!
end

Expand Down Expand Up @@ -428,7 +428,7 @@ def lookup(path)

before(:all) do
ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
@conn = ActiveRecord::Base.connection.instance_variable_get("@raw_connection")
@conn = ActiveRecord::Base.connection.send(:_connection)
@sys_conn = ActiveRecord::ConnectionAdapters::OracleEnhanced::Connection.create(SYS_CONNECTION_PARAMS)
schema_define do
create_table :posts, force: true
Expand Down