Class: ActiveRecord::ConnectionAdapters::JdbcAdapter

Inherits:
AbstractAdapter
  • Object
show all
Extended by:
ShadowCoreMethods
Includes:
CompatibilityMethods, JdbcConnectionPoolCallbacks
Defined in:
lib/arjdbc/jdbc/adapter.rb

Direct Known Subclasses

MysqlAdapter, PostgreSQLAdapter, SQLite3Adapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShadowCoreMethods

alias_chained_method

Methods included from JdbcConnectionPoolCallbacks

included, needed?, #on_checkin, #on_checkout

Methods included from CompatibilityMethods

needed?, #quote_table_name

Constructor Details

#initialize(connection, logger, config) ⇒ JdbcAdapter

Returns a new instance of JdbcAdapter.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arjdbc/jdbc/adapter.rb', line 27

def initialize(connection, logger, config)
  @config = config
  spec = config[:adapter_spec] || adapter_spec(config)
  config[:adapter_spec] ||= spec
  unless connection
    connection_class = jdbc_connection_class spec
    connection = connection_class.new config
  end
  super(connection, logger)
  if spec && (config[:adapter_class].nil? || config[:adapter_class] == JdbcAdapter)
    extend spec
  end
  configure_arel2_visitors(config)
  connection.adapter = self
  JndiConnectionPoolCallbacks.prepare(self, connection)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/arjdbc/jdbc/adapter.rb', line 25

def config
  @config
end

Class Method Details

.arel2_visitors(config) ⇒ Object



107
108
109
# File 'lib/arjdbc/jdbc/adapter.rb', line 107

def self.arel2_visitors(config)
  { 'jdbc' => ::Arel::Visitors::ToSql }
end

.visitor_for(pool) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/arjdbc/jdbc/adapter.rb', line 96

def self.visitor_for(pool)
  config = pool.spec.config
  adapter = config[:adapter]
  adapter_spec = config[:adapter_spec] || self
  if adapter =~ /^(jdbc|jndi)$/
    adapter_spec.arel2_visitors(config).values.first.new(pool)
  else
    adapter_spec.arel2_visitors(config)[adapter].new(pool)
  end
end

Instance Method Details

#_execute(sql, name = nil) ⇒ Object

we need to do it this way, to allow Rails stupid tests to always work even if we define a new execute method. Instead of mixing in a new execute, an _execute should be mixed in.



218
219
220
# File 'lib/arjdbc/jdbc/adapter.rb', line 218

def _execute(sql, name = nil)
  @connection.execute(sql)
end

#active?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/arjdbc/jdbc/adapter.rb', line 183

def active?
  @connection.active?
end

#adapter_nameObject

:nodoc:



92
93
94
# File 'lib/arjdbc/jdbc/adapter.rb', line 92

def adapter_name #:nodoc:
  'JDBC'
end

#adapter_spec(config) ⇒ Object

Locate specialized adapter specification if one exists based on config data



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/arjdbc/jdbc/adapter.rb', line 60

def adapter_spec(config)
  2.times do
    dialect = (config[:dialect] || config[:driver]).to_s
    ::ArJdbc.constants.map { |name| ::ArJdbc.const_get name }.each do |constant|
      if constant.respond_to? :adapter_matcher
        spec = constant.adapter_matcher(dialect, config)
        return spec if spec
      end
    end

    # If nothing matches and we're using jndi, try to automatically detect the database.
    break unless config[:jndi] and !config[:dialect]
    begin
      conn = Java::javax.naming.InitialContext.new.lookup(config[:jndi]).getConnection
      config[:dialect] = conn..getDatabaseProductName

      # Derby-specific hack
      if ::ArJdbc::Derby.adapter_matcher(config[:dialect], config)
        # Needed to set the correct database schema name
        config[:username] ||= conn..getUserName
      end
    rescue
      conn.close if conn
    end
  end
  nil
end

#begin_db_transactionObject



307
308
309
# File 'lib/arjdbc/jdbc/adapter.rb', line 307

def begin_db_transaction
  @connection.begin
end

#commit_db_transactionObject



311
312
313
# File 'lib/arjdbc/jdbc/adapter.rb', line 311

def commit_db_transaction
  @connection.commit
end

#configure_arel2_visitors(config) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/arjdbc/jdbc/adapter.rb', line 111

def configure_arel2_visitors(config)
  if defined?(::Arel::Visitors::VISITORS)
    visitors = ::Arel::Visitors::VISITORS
    visitor = nil
    adapter_spec = config[:adapter_spec] || self.class
    adapter_spec.arel2_visitors(config).each do |k,v|
      visitor = v
      visitors[k] = v
    end
    if visitor && config[:adapter] =~ /^(jdbc|jndi)$/
      visitors[config[:adapter]] = visitor
    end
    @visitor = visitors[config[:adapter]].new(self)
  end
end

#database_nameObject

:nodoc:



144
145
146
# File 'lib/arjdbc/jdbc/adapter.rb', line 144

def database_name #:nodoc:
  @connection.database_name
end

#disconnect!Object



192
193
194
# File 'lib/arjdbc/jdbc/adapter.rb', line 192

def disconnect!
  @connection.disconnect!
end

#exec_delete(sql, name, binds) ⇒ Object

Executes delete sql statement in the context of this connection using binds as the bind substitutes. name is the logged along with the executed sql statement.



279
280
281
# File 'lib/arjdbc/jdbc/adapter.rb', line 279

def exec_delete(sql, name, binds)
  exec_query(sql, name, binds)
end

#exec_insert(sql, name, binds) ⇒ Object

Executes insert sql statement in the context of this connection using binds as the bind substitutes. name is the logged along with the executed sql statement.



272
273
274
# File 'lib/arjdbc/jdbc/adapter.rb', line 272

def exec_insert(sql, name, binds)
  exec_query(sql, name, binds)
end

#exec_query(sql, name = 'SQL', binds = []) ⇒ Object

Executes sql statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed sql statement.



265
266
267
# File 'lib/arjdbc/jdbc/adapter.rb', line 265

def exec_query(sql, name = 'SQL', binds = [])
  execute(sql, name, binds)
end

#exec_update(sql, name, binds) ⇒ Object

Executes update sql statement in the context of this connection using binds as the bind substitutes. name is the logged along with the executed sql statement.



286
287
288
# File 'lib/arjdbc/jdbc/adapter.rb', line 286

def exec_update(sql, name, binds)
  exec_query(sql, name, binds)
end

#execute(sql, name = nil, binds = []) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/arjdbc/jdbc/adapter.rb', line 206

def execute(sql, name = nil, binds = [])
  sql = substitute_binds(sql, binds)
  if name == :skip_logging
    _execute(sql)
  else
    log(sql, name) { _execute(sql) }
  end
end

#indexes(table_name, name = nil, schema_name = nil) ⇒ Object



303
304
305
# File 'lib/arjdbc/jdbc/adapter.rb', line 303

def indexes(table_name, name = nil, schema_name = nil)
  @connection.indexes(table_name, name, schema_name)
end

#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) ⇒ Object



255
256
257
258
# File 'lib/arjdbc/jdbc/adapter.rb', line 255

def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
  id = execute(sql, name = nil, binds)
  id_value || id
end

#is_a?(klass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
# File 'lib/arjdbc/jdbc/adapter.rb', line 127

def is_a?(klass)          # :nodoc:
  # This is to fake out current_adapter? conditional logic in AR tests
  if Class === klass && klass.name =~ /#{adapter_name}Adapter$/i
    true
  else
    super
  end
end

#jdbc_column_classObject



50
51
52
# File 'lib/arjdbc/jdbc/adapter.rb', line 50

def jdbc_column_class
  ActiveRecord::ConnectionAdapters::JdbcColumn
end

#jdbc_columns(table_name, name = nil) ⇒ Object



290
291
292
# File 'lib/arjdbc/jdbc/adapter.rb', line 290

def jdbc_columns(table_name, name = nil)
  @connection.columns(table_name.to_s)
end

#jdbc_connectionObject

Retrieve the raw java.sql.Connection object.



55
56
57
# File 'lib/arjdbc/jdbc/adapter.rb', line 55

def jdbc_connection
  raw_connection.connection
end

#jdbc_connection_class(spec) ⇒ Object



44
45
46
47
48
# File 'lib/arjdbc/jdbc/adapter.rb', line 44

def jdbc_connection_class(spec)
  connection_class = spec.jdbc_connection_class if spec && spec.respond_to?(:jdbc_connection_class)
  connection_class = ::ActiveRecord::ConnectionAdapters::JdbcConnection unless connection_class
  connection_class
end

#jdbc_insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) ⇒ Object



222
223
224
# File 'lib/arjdbc/jdbc/adapter.rb', line 222

def jdbc_insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
  insert_sql(sql, name, pk, id_value, sequence_name, binds)
end

#jdbc_select_all(sql, name = nil, binds = []) ⇒ Object



229
230
231
# File 'lib/arjdbc/jdbc/adapter.rb', line 229

def jdbc_select_all(sql, name = nil, binds = [])
  select(sql, name, binds)
end

#jdbc_update(sql, name = nil, binds = []) ⇒ Object

:nodoc:



226
227
228
# File 'lib/arjdbc/jdbc/adapter.rb', line 226

def jdbc_update(sql, name = nil, binds = []) #:nodoc:
  execute(sql, name, binds)
end

#last_inserted_id(result) ⇒ Object



245
246
247
# File 'lib/arjdbc/jdbc/adapter.rb', line 245

def last_inserted_id(result)
  result
end

#modify_types(tp) ⇒ Object



88
89
90
# File 'lib/arjdbc/jdbc/adapter.rb', line 88

def modify_types(tp)
  tp
end

#native_database_typesObject

:nodoc:



140
141
142
# File 'lib/arjdbc/jdbc/adapter.rb', line 140

def native_database_types #:nodoc:
  @connection.native_database_types
end

#native_sql_to_type(tp) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/arjdbc/jdbc/adapter.rb', line 148

def native_sql_to_type(tp)
  if /^(.*?)\(([0-9]+)\)/ =~ tp
    tname = $1
    limit = $2.to_i
    ntype = native_database_types
    if ntype[:primary_key] == tp
      return :primary_key,nil
    else
      ntype.each do |name,val|
        if name == :primary_key
          next
        end
        if val[:name].downcase == tname.downcase && (val[:limit].nil? || val[:limit].to_i == limit)
          return name,limit
        end
      end
    end
  elsif /^(.*?)/ =~ tp
    tname = $1
    ntype = native_database_types
    if ntype[:primary_key] == tp
      return :primary_key,nil
    else
      ntype.each do |name,val|
        if val[:name].downcase == tname.downcase && val[:limit].nil?
          return name,nil
        end
      end
    end
  else
    return :string,255
  end
  return nil,nil
end

#pk_and_sequence_for(table) ⇒ Object



323
324
325
326
# File 'lib/arjdbc/jdbc/adapter.rb', line 323

def pk_and_sequence_for(table)
  key = primary_key(table)
  [key, nil] if key
end

#primary_key(table) ⇒ Object



328
329
330
# File 'lib/arjdbc/jdbc/adapter.rb', line 328

def primary_key(table)
  primary_keys(table).first
end

#primary_keys(table) ⇒ Object



332
333
334
# File 'lib/arjdbc/jdbc/adapter.rb', line 332

def primary_keys(table)
  @connection.primary_keys(table)
end

#reconnect!Object



187
188
189
190
# File 'lib/arjdbc/jdbc/adapter.rb', line 187

def reconnect!
  @connection.reconnect!
  @connection
end

#rollback_db_transactionObject



315
316
317
# File 'lib/arjdbc/jdbc/adapter.rb', line 315

def rollback_db_transaction
  @connection.rollback
end

#select(*args) ⇒ Object



336
337
338
# File 'lib/arjdbc/jdbc/adapter.rb', line 336

def select(*args)
  execute(*args)
end

#select_one(sql, name = nil) ⇒ Object

Do we need this? Not in AR 3.



240
241
242
# File 'lib/arjdbc/jdbc/adapter.rb', line 240

def select_one(sql, name = nil)
  select(sql, name).first
end

#select_rows(sql, name = nil) ⇒ Object



249
250
251
252
253
# File 'lib/arjdbc/jdbc/adapter.rb', line 249

def select_rows(sql, name = nil)
  rows = []
  select(sql, name).each {|row| rows << row.values }
  rows
end

#substitute_binds(manager, binds = []) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/arjdbc/jdbc/adapter.rb', line 196

def substitute_binds(manager, binds = [])
  sql = extract_sql(manager)
  if binds.empty?
    sql
  else
    copy = binds.dup
    sql.gsub('?') { quote(*copy.shift.reverse) }
  end
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/arjdbc/jdbc/adapter.rb', line 136

def supports_migrations?
  true
end

#table_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/arjdbc/jdbc/adapter.rb', line 299

def table_exists?(name)
  jdbc_columns(name) rescue nil
end

#tables(name = nil) ⇒ Object



295
296
297
# File 'lib/arjdbc/jdbc/adapter.rb', line 295

def tables(name = nil)
  @connection.tables
end

#write_large_object(*args) ⇒ Object



319
320
321
# File 'lib/arjdbc/jdbc/adapter.rb', line 319

def write_large_object(*args)
  @connection.write_large_object(*args)
end