-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathcore.rb
80 lines (62 loc) · 2.86 KB
/
core.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
module ArJdbc
module Abstract
# This is minimum amount of code needed from base JDBC Adapter class to make common adapters
# work. This replaces using jdbc/adapter as a base class for all adapters.
module Core
attr_reader :config
def initialize(connection, logger = nil, config = {})
@config = config
if self.class.equal? ActiveRecord::ConnectionAdapters::JdbcAdapter
spec = @config.key?(:adapter_spec) ? @config[:adapter_spec] :
( @config[:adapter_spec] = adapter_spec(@config) ) # due resolving visitor
extend spec if spec
end
connection ||= jdbc_connection_class(config[:adapter_spec]).new(config, self)
super(connection, logger, config) # AbstractAdapter
connection.configure_connection # will call us (maybe)
end
# Retrieve the raw `java.sql.Connection` object.
# The unwrap parameter is useful if an attempt to unwrap a pooled (JNDI)
# connection should be made - to really return the 'native' JDBC object.
# @param unwrap [true, false] whether to unwrap the connection object
# @return [Java::JavaSql::Connection] the JDBC connection
def jdbc_connection(unwrap = nil)
raw_connection.jdbc_connection(unwrap)
end
protected
def translate_exception_class(e, sql, binds)
message = "#{e.class.name}: #{e.message}"
exception = translate_exception(
e, message: message, sql: sql, binds: binds
)
exception.set_backtrace e.backtrace
exception
end
def translate_exception(exception, message:, sql:, binds:)
# override in derived class
# we shall not translate native "Java" exceptions as they might
# swallow an ArJdbc / driver bug into an AR::StatementInvalid !
return exception if exception.is_a?(Java::JavaLang::Throwable)
case exception
when SystemExit, SignalException, NoMemoryError then exception
when ActiveModel::RangeError, TypeError, RuntimeError then exception
when ActiveRecord::ConnectionNotEstablished then exception
else super
end
end
def extract_raw_bind_values(binds)
binds.map { |b| b.respond_to?(:value_for_database) ? b.value_for_database : b.to_s }
end
# this version of log() automatically fills type_casted_binds from binds if necessary
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil)
if binds.any? && (type_casted_binds.nil? || type_casted_binds.empty?)
type_casted_binds = ->{ extract_raw_bind_values(binds) }
end
super
end
end
end
JDBC_GEM_ROOT = File.expand_path("../../../..", __FILE__) + "/"
ActiveRecord::LogSubscriber.backtrace_cleaner.add_silencer { |line| line.start_with?(JDBC_GEM_ROOT) }
end