# File lib/arjdbc/jdbc/adapter.rb, line 362 def primary_key(table) primary_keys(table).first end
AbstractAdapter
# File lib/arjdbc/jdbc/adapter.rb, line 117 def self.arel2_visitors(config) { 'jdbc' => ::Arel::Visitors::ToSql } end
# 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
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 432 def self.insert?(sql) JdbcConnection::insert?(sql) end
# File lib/arjdbc/jdbc/adapter.rb, line 193 def active? @connection.active? end
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
# File lib/arjdbc/jdbc/adapter.rb, line 341 def begin_db_transaction @connection.begin end
# File lib/arjdbc/jdbc/adapter.rb, line 345 def commit_db_transaction @connection.commit end
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 203 def disconnect! @connection.disconnect! end
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
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
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
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
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
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 48 def jdbc_column_class ActiveRecord::ConnectionAdapters::JdbcColumn end
# File lib/arjdbc/jdbc/adapter.rb, line 233 def jdbc_columns(table_name, name = nil) @connection.columns(table_name.to_s) 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 (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
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 98 def modify_types(types) types end
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 357 def pk_and_sequence_for(table) key = primary_key(table) [key, nil] if key end
# File lib/arjdbc/jdbc/adapter.rb, line 362 def primary_key(table) primary_keys(table).first end
# File lib/arjdbc/jdbc/adapter.rb, line 366 def primary_keys(table) @connection.primary_keys(table) end
# File lib/arjdbc/jdbc/adapter.rb, line 197 def reconnect! @connection.reconnect! configure_connection if respond_to?(:configure_connection) @connection end
# File lib/arjdbc/jdbc/adapter.rb, line 349 def rollback_db_transaction @connection.rollback end
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
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
# 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
# File lib/arjdbc/jdbc/adapter.rb, line 146 def supports_migrations? true end
# File lib/arjdbc/jdbc/adapter.rb, line 333 def table_exists?(name) jdbc_columns(name) rescue nil end
# File lib/arjdbc/jdbc/adapter.rb, line 329 def tables(name = nil) @connection.tables end
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
Generated with the Darkfish Rdoc Generator 2.