Parent

Files

Class/Module Index [+]

Quicksearch

ActiveRecord::ConnectionAdapters::JdbcAdapter

Attributes

config[R]

Public Class Methods

arel2_visitors(config) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 117
def self.arel2_visitors(config)
  { 'jdbc' => ::Arel::Visitors::ToSql }
end
new(connection, logger, config) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 25
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
visitor_for(pool) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 106
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

Protected Class Methods

insert?(sql) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 432
def self.insert?(sql)
  JdbcConnection::insert?(sql)
end
select?(sql) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 428
def self.select?(sql)
  JdbcConnection::select?(sql)
end
update?(sql) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 436
def self.update?(sql)
  ! select?(sql) && ! insert?(sql)
end

Public Instance Methods

active?() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 193
def active?
  @connection.active?
end
adapter_spec(config) click to toggle source

Locate specialized adapter specification if one exists based on config data

# File lib/arjdbc/jdbc/adapter.rb, line 70
def adapter_spec(config)
  dialect = (config[:dialect] || config[:driver]).to_s
  ::ArJdbc.constants.sort.each do |constant|
    constant = ::ArJdbc.const_get(constant) # e.g. ArJdbc::MySQL

    if constant.respond_to?(:adapter_matcher)
      spec = constant.adapter_matcher(dialect, config)
      return spec if spec
    end
  end

  if config[:jndi] && ! config[:dialect]
    begin
      data_source = Java::JavaxNaming::InitialContext.new.lookup(config[:jndi])
      connection = data_source.getConnection
      config[:dialect] = connection.getMetaData.getDatabaseProductName
    rescue Java::JavaSql::SQLException => e
      warn "failed to set database :dialect from connection meda-data (#{e})"
    else
      return adapter_spec(config) # re-try matching a spec with set config[:dialect]
    ensure
      connection.close if connection  # return to the pool
    end
  end

  nil
end
begin_db_transaction() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 341
def begin_db_transaction
  @connection.begin
end
commit_db_transaction() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 345
def commit_db_transaction
  @connection.commit
end
configure_arel2_visitors(config) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 121
def configure_arel2_visitors(config)
  if defined?(::Arel::Visitors::VISITORS)
    visitors = ::Arel::Visitors::VISITORS
    visitor = nil
    adapter_spec = [config[:adapter_spec], self.class].detect {|a| a && a.respond_to?(:arel2_visitors) }
    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
disconnect!() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 203
def disconnect!
  @connection.disconnect!
end
exec_delete(sql, name, binds) click to toggle source

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.

# File lib/arjdbc/jdbc/adapter.rb, line 255
def exec_delete(sql, name, binds)
  exec_query(sql, name, binds)
end
exec_insert(sql, name, binds) click to toggle source

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.

# File lib/arjdbc/jdbc/adapter.rb, line 248
def exec_insert(sql, name, binds)
  exec_query(sql, name, binds)
end
exec_query(sql, name = 'SQL', binds = []) click to toggle source

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

# File lib/arjdbc/jdbc/adapter.rb, line 241
def exec_query(sql, name = 'SQL', binds = [])
  execute(sql, name, binds)
end
exec_update(sql, name, binds) click to toggle source

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.

# File lib/arjdbc/jdbc/adapter.rb, line 262
def exec_update(sql, name, binds)
  exec_query(sql, name, binds)
end
execute(sql, name = nil, binds = []) click to toggle source

Executes the SQL statement in the context of this connection.

# File lib/arjdbc/jdbc/adapter.rb, line 271
def execute(sql, name = nil, binds = [])
  sql = to_sql(sql, binds)
  if name == :skip_logging
    _execute(sql, name)
  else
    log(sql, name) { _execute(sql, name ||= "SQL") }
  end
end
indexes(table_name, name = nil, schema_name = nil) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 337
def indexes(table_name, name = nil, schema_name = nil)
  @connection.indexes(table_name, name, schema_name)
end
jdbc_column_class() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 48
def jdbc_column_class
  ActiveRecord::ConnectionAdapters::JdbcColumn
end
jdbc_columns(table_name, name = nil) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 233
def jdbc_columns(table_name, name = nil)
  @connection.columns(table_name.to_s)
end
jdbc_connection(unwrap = nil) click to toggle source

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 (SQL) object.

# File lib/arjdbc/jdbc/adapter.rb, line 55
def jdbc_connection(unwrap = nil)
  java_connection = raw_connection.connection
  return java_connection unless unwrap
  connection_class = java.sql.Connection.java_class
  if java_connection.wrapper_for?(connection_class)
    java_connection.unwrap(connection_class) # java.sql.Wrapper.unwrap
  elsif java_connection.respond_to?(:connection)
    # e.g. org.apache.tomcat.jdbc.pool.PooledConnection
    java_connection.connection # getConnection
  else
    java_connection
  end
end
jdbc_connection_class(spec) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 42
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
modify_types(types) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 98
def modify_types(types)
  types
end
native_sql_to_type(type) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 158
def native_sql_to_type(type)
  if /^(.*?)\(([0-9]+)\)/ =~ type
    tname, limit = $1, $2.to_i
    ntypes = native_database_types
    if ntypes[:primary_key] == type
      return :primary_key, nil
    else
      ntypes.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 /^(.*?)/ =~ type
    tname = $1
    ntypes = native_database_types
    if ntypes[:primary_key] == type
      return :primary_key, nil
    else
      ntypes.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) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 357
def pk_and_sequence_for(table)
  key = primary_key(table)
  [key, nil] if key
end
primary_key(table) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 362
def primary_key(table)
  primary_keys(table).first
end
primary_keys(table) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 366
def primary_keys(table)
  @connection.primary_keys(table)
end
reconnect!() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 197
def reconnect!
  @connection.reconnect!
  configure_connection if respond_to?(:configure_connection)
  @connection
end
rollback_db_transaction() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 349
def rollback_db_transaction
  @connection.rollback
end
select(*args) click to toggle source

Returns an array of record hashes with the column names as keys and column values as values. @note on AR-3.2 expects "only" 2 arguments `select(sql, name = nil)`

we accept 3 arguments as well `select(sql, name = nil, binds = [])`
# File lib/arjdbc/jdbc/adapter.rb, line 313
def select(*args)
  execute(*args)
end
select_one(sql, name = nil) click to toggle source

Do we need this? Not in AR 3.

# File lib/arjdbc/jdbc/adapter.rb, line 227
def select_one(sql, name = nil)
  select(sql, name).first
end
select_rows(sql, name = nil) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 317
def select_rows(sql, name = nil)
  rows = []
  select(sql, name).each {|row| rows << row.values }
  rows
end
supports_migrations?() click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 146
def supports_migrations?
  true
end
table_exists?(name) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 333
def table_exists?(name)
  jdbc_columns(name) rescue nil
end
tables(name = nil) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 329
def tables(name = nil)
  @connection.tables
end
to_sql(arel, binds = []) click to toggle source

Converts an arel AST to SQL

# File lib/arjdbc/jdbc/adapter.rb, line 373
def to_sql(arel, binds = [])
  if arel.respond_to?(:ast)
    visitor.accept(arel.ast) do
      quote(*binds.shift.reverse)
    end
  else # for backwards compatibility :
    sql = arel.respond_to?(:to_sql) ? arel.send(:to_sql) : arel
    return sql if binds.blank?
    sql.gsub('?') { quote(*binds.shift.reverse) }
  end
end
write_large_object(*args) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 353
def write_large_object(*args)
  @connection.write_large_object(*args)
end

Protected Instance Methods

last_inserted_id(result) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 404
def last_inserted_id(result)
  result
end
translate_exception(e, message) click to toggle source
# File lib/arjdbc/jdbc/adapter.rb, line 399
def translate_exception(e, message)
  puts e.backtrace if $DEBUG || ENV['DEBUG']
  super
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.