Skip to content

Commit 1a22735

Browse files
authored
Merge pull request #285 from yahonda/add-spec-sql-logger
Add optional SQL logging spec helper
2 parents af9183c + 48cf4b4 commit 1a22735

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pkg
88
log
99
tmp
1010
sqlnet.log
11+
debug.log
1112
Gemfile.lock
1213
*.zip
1314
.idea

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ end
1414
group :test, :development do
1515
gem "rake", ">= 10.0"
1616
gem "rspec", "~> 3.1"
17+
gem "logger"
1718

1819
unless ENV["NO_ACTIVERECORD"]
1920
gem "activerecord", github: "rails/rails", branch: "main"

spec/support/sql_logger.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Always write SQL executed during the spec run to debug.log.
2+
# Logger args mirror activerecord-oracle_enhanced-adapter's spec logger
3+
# (shift_age, shift_size) so debug.log behaves the same way.
4+
require "logger"
5+
6+
PLSQL_DEBUG_LOGGER = Logger.new("debug.log", 0, 100 * 1024 * 1024)
7+
PLSQL_DEBUG_LOGGER.formatter = ->(_sev, time, _prog, msg) {
8+
"#{time.iso8601(6)} #{msg}\n"
9+
}
10+
11+
module PLSQLSQLLogger
12+
def exec(sql, *bindvars)
13+
PLSQL_DEBUG_LOGGER.info("EXEC #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}")
14+
super
15+
end
16+
17+
def cursor_from_query(sql, bindvars = [], options = {})
18+
PLSQL_DEBUG_LOGGER.info("QUERY #{sql.strip}#{bindvars.empty? ? '' : " BINDS=#{bindvars.inspect}"}")
19+
super
20+
end
21+
22+
def parse(sql)
23+
PLSQL_DEBUG_LOGGER.info("PARSE #{sql.strip}")
24+
super
25+
end
26+
end
27+
28+
PLSQL::OCIConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::OCIConnection)
29+
PLSQL::JDBCConnection.prepend(PLSQLSQLLogger) if defined?(PLSQL::JDBCConnection)
30+
31+
if defined?(ActiveRecord::Base)
32+
ActiveRecord::Base.logger = PLSQL_DEBUG_LOGGER
33+
end

0 commit comments

Comments
 (0)