diff --git a/.gitignore b/.gitignore index 1d6ff5e..8aeed57 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ pkg log tmp sqlnet.log +debug.log Gemfile.lock *.zip .idea diff --git a/Gemfile b/Gemfile index 85d1363..58fe003 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ end group :test, :development do gem "rake", ">= 10.0" gem "rspec", "~> 3.1" + gem "logger" unless ENV["NO_ACTIVERECORD"] gem "activerecord", github: "rails/rails", branch: "main" diff --git a/spec/support/sql_logger.rb b/spec/support/sql_logger.rb new file mode 100644 index 0000000..59bbbaf --- /dev/null +++ b/spec/support/sql_logger.rb @@ -0,0 +1,33 @@ +# Always write SQL executed during the spec run to debug.log. +# Logger args mirror activerecord-oracle_enhanced-adapter's spec logger +# (shift_age, shift_size) so debug.log behaves the same way. +require "logger" + +PLSQL_DEBUG_LOGGER = Logger.new("debug.log", 0, 100 * 1024 * 1024) +PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) { + "#{time.iso8601(6)} #{msg}\n" +} + +module PLSQLSQLLogger + def exec(sql, *bindvars) + PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end + + def cursor_from_query(sql, bindvars = [], options = {}) + PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}") + super + end + + def parse(sql) + PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}") + super + end +end + +PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection) +PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection) + +if defined?(ActiveRecord::Base) + ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER +end