Parent

Files

Class/Module Index [+]

Quicksearch

Object

Public Instance Methods

as400_connection(config) click to toggle source

@note Assumes AS400 driver (jt400.jar) is on class-path.

# File lib/arjdbc/db2/connection_methods.rb, line 23
def as400_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::AS400

  return jndi_connection(config) if config[:jndi]

  config[:url] ||= begin
    # jdbc:as400://[host]
    url = 'jdbc:as400://'
    url << config[:host] if config[:host]
    # jdbc:as400://myiSeries;database name=IASP1
    url << ";database name=#{config[:database]}" if config[:database]
    # jdbc:as400://[host];proxy server=HODServerName:proxyServerPort
    url << ";proxy server=#{config[:proxy]}" if config[:proxy]
    url
  end
  require 'arjdbc/db2/as400'
  config[:driver] ||= ::ArJdbc::AS400::DRIVER_NAME
  config[:connection_alive_sql] ||= 'SELECT 1 FROM sysibm.tables FETCH FIRST 1 ROWS ONLY'
  jdbc_connection(config)
end
db2_connection(config) click to toggle source

@note Assumes DB2 driver (db2jcc.jar) is on class-path.

# File lib/arjdbc/db2/connection_methods.rb, line 3
def db2_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::DB2

  return jndi_connection(config) if jndi_config?(config)

  config[:url] ||= begin
    if config[:host] # Type 4 URL: jdbc:db2://server:port/database
      config[:port] ||= 50000
      "jdbc:db2://#{config[:host]}:#{config[:port]}/#{config[:database]}"
    else # Type 2 URL: jdbc:db2:database
      "jdbc:db2:#{config[:database]}"
    end
  end
  config[:driver] ||= ::ArJdbc::DB2::DRIVER_NAME
  config[:connection_alive_sql] ||= 'SELECT 1 FROM syscat.tables FETCH FIRST 1 ROWS ONLY'
  jdbc_connection(config)
end
derby_connection(config) click to toggle source
# File lib/arjdbc/derby/connection_methods.rb, line 2
def derby_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::Derby

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/derby'
    ::Jdbc::Derby.load_driver(:require) if defined?(::Jdbc::Derby.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  config[:url] ||= "jdbc:derby:#{config[:database]};create=true"
  config[:driver] ||= defined?(::Jdbc::Derby.driver_name) ?
    ::Jdbc::Derby.driver_name : 'org.apache.derby.jdbc.EmbeddedDriver'

  embedded_driver(config)
end
firebird_connection(config) click to toggle source
# File lib/arjdbc/firebird/connection_methods.rb, line 2
def firebird_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::Firebird

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/firebird'
    ::Jdbc::Firebird.load_driver(:require)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  config[:host] ||= 'localhost'
  config[:port] ||= 3050
  config[:url] ||= begin
    "jdbc:firebirdsql://#{config[:host]}:#{config[:port]}/#{config[:database]}"
  end
  config[:driver] ||= ::Jdbc::Firebird.driver_name

  jdbc_connection(config)
end
h2_connection(config) click to toggle source
# File lib/arjdbc/h2/connection_methods.rb, line 2
def h2_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::H2
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::H2Adapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/h2'
    ::Jdbc::H2.load_driver(:require) if defined?(::Jdbc::H2.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  config[:url] ||= begin
    db = config[:database]
    if db[0, 4] == 'mem:' || db[0, 5] == 'file:' || db[0, 5] == 'hsql:'
      "jdbc:h2:#{db}"
    else
      "jdbc:h2:file:#{File.expand_path(db)}"
    end
  end
  config[:driver] ||= defined?(::Jdbc::H2.driver_name) ? ::Jdbc::H2.driver_name : 'org.h2.Driver'

  embedded_driver(config)
end
hsqldb_connection(config) click to toggle source
# File lib/arjdbc/hsqldb/connection_methods.rb, line 2
def hsqldb_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::HSQLDB
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::HsqldbAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/hsqldb'
    ::Jdbc::HSQLDB.load_driver(:require) if defined?(::Jdbc::HSQLDB.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  config[:url] ||= begin
    db = config[:database]
    if db[0, 4] == 'mem:' || db[0, 5] == 'file:' || db[0, 5] == 'hsql:'
      "jdbc:hsqldb:#{db}"
    else
      "jdbc:hsqldb:file:#{db}"
    end
  end
  config[:driver] ||= defined?(::Jdbc::HSQLDB.driver_name) ? ::Jdbc::HSQLDB.driver_name : 'org.hsqldb.jdbcDriver'
  config[:connection_alive_sql] ||= 'CALL PI()' # does not like 'SELECT 1'

  embedded_driver(config)
end
informix_connection(config) click to toggle source
# File lib/arjdbc/informix/connection_methods.rb, line 2
def informix_connection(config)
  config[:port] ||= 9088
  config[:url] ||= "jdbc:informix-sqli://#{config[:host]}:#{config[:port]}/#{config[:database]}:INFORMIXSERVER=#{config[:servername]}"
  config[:driver] = 'com.informix.jdbc.IfxDriver'
  config[:adapter_spec] = ::ArJdbc::Informix
  jdbc_connection(config)
end
mariadb_connection(config) click to toggle source
# File lib/arjdbc/mysql/connection_methods.rb, line 74
def mariadb_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::MySQL
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::MysqlAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/mariadb'
    ::Jdbc::MariaDB.load_driver(:require) if defined?(::Jdbc::MariaDB.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  config[:driver] ||= 'org.mariadb.jdbc.Driver'

  mysql_connection(config)
end
mssql_connection(config) click to toggle source

Default connection method for MS-SQL adapter (`adapter: mssql`), uses the (open-source) jTDS driver. If you’d like to use the “official” MS’s SQL-JDBC driver, it’s preferable to use the {sqlserver_connection} method (set `adapter: sqlserver`).

# File lib/arjdbc/mssql/connection_methods.rb, line 7
def mssql_connection(config)
  # NOTE: this detection ain't perfect and is only meant as a temporary hack
  # users will get a deprecation eventually to use `adapter: sqlserver` ...
  if config[:driver] =~ /SQLServerDriver$/ || config[:url] =~ /^jdbc:sqlserver:/
    return sqlserver_connection(config)
  end

  config[:adapter_spec] ||= ::ArJdbc::MSSQL
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::MSSQLAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/jtds'
    # NOTE: the adapter has only support for working with the
    # open-source jTDS driver (won't work with MS's driver) !
    ::Jdbc::JTDS.load_driver(:require) if defined?(::Jdbc::JTDS.load_driver)
  rescue LoadError => e # assuming driver.jar is on the class-path
    raise e unless e.message.to_s.index('no such file to load')
  end

  config[:host] ||= 'localhost'
  config[:port] ||= 1433
  config[:driver] ||= defined?(::Jdbc::JTDS.driver_name) ? ::Jdbc::JTDS.driver_name : 'net.sourceforge.jtds.jdbc.Driver'
  config[:connection_alive_sql] ||= 'SELECT 1'

  config[:url] ||= begin
    url = "jdbc:jtds:sqlserver://#{config[:host]}:#{config[:port]}/#{config[:database]}"
    # Instance is often a preferrable alternative to port when dynamic ports are used.
    # If instance is specified then port is essentially ignored.
    url << ";instance=#{config[:instance]}" if config[:instance]
    # This will enable windows domain-based authentication and will require the JTDS native libraries be available.
    url << ";domain=#{config[:domain]}" if config[:domain]
    # AppName is shown in sql server as additional information against the connection.
    url << ";appname=#{config[:appname]}" if config[:appname]
    url
  end

  unless config[:domain]
    config[:username] ||= 'sa'
    config[:password] ||= ''
  end
  jdbc_connection(config)
end
mysql_connection(config) click to toggle source
# File lib/arjdbc/mysql/connection_methods.rb, line 2
def mysql_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::MySQL
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::MysqlAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  driver = config[:driver] ||=
    defined?(::Jdbc::MySQL.driver_name) ? ::Jdbc::MySQL.driver_name : 'com.mysql.jdbc.Driver'

  begin
    require 'jdbc/mysql'
    ::Jdbc::MySQL.load_driver(:require) if defined?(::Jdbc::MySQL.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end if mysql_driver = driver[0, 10] == 'com.mysql.'

  config[:username] = 'root' unless config.key?(:username)
  # jdbc:mysql://[host][,failoverhost...][:port]/[database]
  # - if the host name is not specified, it defaults to 127.0.0.1
  # - if the port is not specified, it defaults to 3306
  # - alternate fail-over syntax: [host:port],[host:port]/[database]
  unless config[:url]
    host = config[:host]; host = host.join(',') if host.respond_to?(:join)
    url = "jdbc:mysql://#{host}"
    url << ":#{config[:port]}" if config[:port]
    url << "/#{config[:database]}"
    config[:url] = url
  end

  mariadb_driver = ! mysql_driver && driver[0, 12] == 'org.mariadb.' # org.mariadb.jdbc.Driver

  properties = ( config[:properties] ||= {} )
  if mysql_driver
    properties['zeroDateTimeBehavior'] ||= 'convertToNull'
    properties['jdbcCompliantTruncation'] ||= 'false'
    properties['useUnicode'] = 'true' unless properties.key?('useUnicode') # otherwise platform default
    encoding = config.key?(:encoding) ? config[:encoding] : 'utf8'
    properties['characterEncoding'] = encoding if encoding
    if ! ( reconnect = config[:reconnect] ).nil?
      properties['autoReconnect'] ||= reconnect.to_s
      # properties['maxReconnects'] ||= '3'
      # with reconnect fail-over sets connection read-only (by default)
      # properties['failOverReadOnly'] ||= 'false'
    end
  end
  if config[:sslkey] || sslcert = config[:sslcert] # || config[:use_ssl]
    properties['useSSL'] ||= true # supported by MariaDB as well
    if mysql_driver
      properties['requireSSL'] ||= true if mysql_driver
      properties['clientCertificateKeyStoreUrl'] ||= begin
        java.io.File.new(sslcert).to_url.to_s
      end if sslcert
      if sslca = config[:sslca]
        properties['trustCertificateKeyStoreUrl'] ||= begin
          java.io.File.new(sslca).to_url.to_s
        end
      else
        properties['verifyServerCertificate'] ||= false if mysql_driver
      end
    end
    if mariadb_driver
      properties['verifyServerCertificate'] ||= false
    end
  end
  if socket = config[:socket]
    properties['localSocket'] ||= socket if mariadb_driver
  end

  jdbc_connection(config)
end
oracle_connection(config) click to toggle source

Unless a connection URL (`url: jdbc:oracle:…`) is specified we’ll use the thin method to connect to the Oracle DB. @note Oracle’s JDBC driver should be on the class-path.

# File lib/arjdbc/oracle/connection_methods.rb, line 5
def oracle_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::Oracle
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::OracleAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  config[:port] ||= 1521
  config[:url] ||= "jdbc:oracle:thin:@#{config[:host]}:#{config[:port]}:#{config[:database]}"
  config[:driver] ||= "oracle.jdbc.driver.OracleDriver"
  config[:connection_alive_sql] ||= 'SELECT 1 FROM DUAL'
  jdbc_connection(config)
end
parse_sqlite3_config!(config) click to toggle source
# File lib/arjdbc/sqlite3/connection_methods.rb, line 30
def parse_sqlite3_config!(config)
  database = ( config[:database] ||= config[:dbfile] ) # allow Rails relative path :
  if database != ':memory:' && defined?(Rails.root) || Object.const_defined?(:RAILS_ROOT)
    rails_root = defined?(Rails.root) ? Rails.root : RAILS_ROOT
    config[:database] = File.expand_path(database, rails_root.to_s)
    dirname = File.dirname(config[:database])
    Dir.mkdir(dirname) unless File.directory?(dirname)
  end
end
postgresql_connection(config) click to toggle source
# File lib/arjdbc/postgresql/connection_methods.rb, line 2
def postgresql_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::PostgreSQL
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/postgres'
    ::Jdbc::Postgres.load_driver(:require) if defined?(::Jdbc::Postgres.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end
  config[:driver] ||= defined?(::Jdbc::Postgres.driver_name) ? ::Jdbc::Postgres.driver_name : 'org.postgresql.Driver'

  host = config[:host] ||= ( config[:hostaddr] || ENV['PGHOST'] || 'localhost' )
  port = config[:port] ||= ( ENV['PGPORT'] || 5432 )
  database = config[:database] || config[:dbname] || ENV['PGDATABASE']

  config[:url] ||= "jdbc:postgresql://#{host}:#{port}/#{database}"
  config[:url] << config[:pg_params] if config[:pg_params] # should go away

  config[:username] ||= ( config[:user] || ENV['PGUSER'] || ENV_JAVA['user.name'] )
  config[:password] ||= ENV['PGPASSWORD'] unless config.key?(:password)
  properties = ( config[:properties] ||= {} )
  # PG :connect_timeout - maximum time to wait for connection to succeed
  if connect_timeout = ( config[:connect_timeout] || ENV['PGCONNECT_TIMEOUT'] )
    properties['socketTimeout'] ||= connect_timeout
  end
  if login_timeout = config[:login_timeout]
    properties['loginTimeout'] ||= login_timeout
  end
  sslmode = config.key?(:sslmode) ? config[:sslmode] : config[:requiressl]
  # NOTE: makes not much sense since this needs some JVM options :
  # sslmode = ENV['PGSSLMODE'] || ENV['PGREQUIRESSL'] if sslmode.nil?
  unless sslmode.nil? # PG :sslmode - disable|allow|prefer|require
    # JRuby/JVM needs to be started with :
    #  -Djavax.net.ssl.trustStore=mystore -Djavax.net.ssl.trustStorePassword=...
    # or a non-validating connection might be used (for testing) :
    #  :sslfactory = 'org.postgresql.ssl.NonValidatingFactory'
    properties['ssl'] ||= 'true' if sslmode == true || sslmode.to_s == 'require'
  end
  properties['tcpKeepAlive'] ||= config[:keepalives] if config.key?(:keepalives)
  properties['kerberosServerName'] ||= config[:krbsrvname] if config[:krbsrvname]

  jdbc_connection(config)
end
select_limited_ids_list(options, join_dependency) click to toggle source
# File lib/arjdbc/derby/active_record_patch.rb, line 6
def select_limited_ids_list(options, join_dependency)
  return super unless connection.is_a?(ArJdbc::Derby)
  connection.select_all(
    construct_finder_sql_for_association_limiting(options, join_dependency),
    "#{name} Load IDs For Limited Eager Loading"
  ).collect { |row| connection.quote(row[primary_key], columns_hash[primary_key]) }.join(", ")
end
sqlite3_connection(config) click to toggle source
# File lib/arjdbc/sqlite3/connection_methods.rb, line 2
def sqlite3_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::SQLite3
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::SQLite3Adapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  begin
    require 'jdbc/sqlite3'
    ::Jdbc::SQLite3.load_driver(:require) if defined?(::Jdbc::SQLite3.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  parse_sqlite3_config!(config)
  database = config[:database] # NOTE: "jdbc:sqlite::memory:" syntax is supported
  config[:url] ||= "jdbc:sqlite:#{database == ':memory:' ? '' : database}"
  config[:driver] ||= defined?(::Jdbc::SQLite3.driver_name) ? ::Jdbc::SQLite3.driver_name : 'org.sqlite.JDBC'
  config[:connection_alive_sql] ||= 'SELECT 1'

  options = ( config[:properties] ||= {} )
  # NOTE: configuring from JDBC properties not supported on 3.7.2 :
  options['busy_timeout'] ||= config[:timeout] if config.key?(:timeout)

  jdbc_connection(config)
end
sqlserver_connection(config) click to toggle source

@note Assumes SQLServer SQL-JDBC driver on the class-path.

# File lib/arjdbc/mssql/connection_methods.rb, line 54
def sqlserver_connection(config)
  config[:adapter_spec] ||= ::ArJdbc::MSSQL
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::MSSQLAdapter unless config.key?(:adapter_class)

  return jndi_connection(config) if jndi_config?(config)

  config[:host] ||= 'localhost'
  config[:driver] ||= 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
  config[:connection_alive_sql] ||= 'SELECT 1'

  config[:url] ||= begin
    url = "jdbc:sqlserver://#{config[:host]}"
    url << ( config[:port] ? ":#{config[:port]};" : ';' )
    url << "databaseName=#{config[:database]};" if config[:database]
    url << "instanceName=#{config[:instance]};" if config[:instance]
    app = config[:appname] || config[:application]
    url << "applicationName=#{app};" if app
    isc = config[:integrated_security] # Win only - needs sqljdbc_auth.dll
    url << "integratedSecurity=#{isc};" unless isc.nil?
    url
  end
  jdbc_connection(config)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.