Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/arjdbc/jdbc/base_ext.rb,
lib/arjdbc/h2/connection_methods.rb,
lib/arjdbc/db2/connection_methods.rb,
lib/arjdbc/jdbc/connection_methods.rb,
lib/arjdbc/derby/connection_methods.rb,
lib/arjdbc/mssql/connection_methods.rb,
lib/arjdbc/mysql/connection_methods.rb,
lib/arjdbc/hsqldb/connection_methods.rb,
lib/arjdbc/oracle/connection_methods.rb,
lib/arjdbc/sqlite3/connection_methods.rb,
lib/arjdbc/informix/connection_methods.rb,
lib/arjdbc/postgresql/connection_methods.rb

Overview

reopen

Class Method Summary collapse

Class Method Details

.db2_connection(config) ⇒ Object Also known as: jdbcdb2_connection



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/arjdbc/db2/connection_methods.rb', line 3

def db2_connection(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[:adapter_spec] = ::ArJdbc::DB2
  jdbc_connection(config)
end

.derby_connection(config) ⇒ Object Also known as: jdbcderby_connection



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/arjdbc/derby/connection_methods.rb', line 4

def derby_connection(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'
  config[:adapter_spec] = ::ArJdbc::Derby
  conn = embedded_driver(config)
  md = conn.jdbc_connection.
  if md.database_major_version < 10 || (md.database_major_version == 10 && md.database_minor_version < 5)
    raise ::ActiveRecord::ConnectionFailed, "Derby adapter requires Derby 10.5 or later"
  end
  conn
end

.embedded_driver(config) ⇒ Object



10
11
12
13
14
# File 'lib/arjdbc/jdbc/connection_methods.rb', line 10

def embedded_driver(config)
  config[:username] ||= "sa"
  config[:password] ||= ""
  jdbc_connection(config)
end

.h2_connection(config) ⇒ Object Also known as: jdbch2_connection



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/arjdbc/h2/connection_methods.rb', line 4

def h2_connection(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] ||= "jdbc:h2:#{config[:database]}"
  config[:driver] ||= defined?(::Jdbc::H2.driver_name) ? ::Jdbc::H2.driver_name : 'org.h2.Driver'
  config[:adapter_spec] = ::ArJdbc::H2
  embedded_driver(config)
end

.hsqldb_connection(config) ⇒ Object Also known as: jdbchsqldb_connection



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/arjdbc/hsqldb/connection_methods.rb', line 4

def hsqldb_connection(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] ||= "jdbc:hsqldb:#{config[:database]}"
  config[:driver] ||= defined?(::Jdbc::HSQLDB.driver_name) ? ::Jdbc::HSQLDB.driver_name : 'org.hsqldb.jdbcDriver'
  config[:adapter_spec] = ::ArJdbc::HSQLDB
  embedded_driver(config)
end

.informix_connection(config) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/arjdbc/informix/connection_methods.rb', line 3

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

.jdbc_connection(config) ⇒ Object Also known as: jndi_connection



3
4
5
6
7
# File 'lib/arjdbc/jdbc/connection_methods.rb', line 3

def jdbc_connection(config)
  adapter_class = config[:adapter_class]
  adapter_class ||= ::ActiveRecord::ConnectionAdapters::JdbcAdapter
  adapter_class.new(nil, logger, config)
end

.mssql_connection(config) ⇒ Object Also known as: jdbcmssql_connection



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
# File 'lib/arjdbc/mssql/connection_methods.rb', line 3

def mssql_connection(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[:adapter_spec] = ::ArJdbc::MsSQL

  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) ⇒ Object Also known as: jdbcmysql_connection, mysql2_connection



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/arjdbc/mysql/connection_methods.rb', line 3

def mysql_connection(config)
  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

  config[:port] ||= 3306
  config[:url] ||= "jdbc:mysql://#{config[:host]}:#{config[:port]}/#{config[:database]}"
  config[:driver] ||= defined?(::Jdbc::MySQL.driver_name) ? ::Jdbc::MySQL.driver_name : 'com.mysql.jdbc.Driver'
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::MysqlAdapter
  config[:adapter_spec] = ::ArJdbc::MySQL
  options = (config[:options] ||= {})
  options['zeroDateTimeBehavior'] ||= 'convertToNull'
  options['jdbcCompliantTruncation'] ||= 'false'
  options['useUnicode'] ||= 'true'
  options['characterEncoding'] = config[:encoding] || 'utf8'
  connection = jdbc_connection(config)
  ::ArJdbc::MySQL.kill_cancel_timer(connection.raw_connection)
  connection
end

.oracle_connection(config) ⇒ Object Also known as: jdbcoracle_connection



3
4
5
6
7
8
9
# File 'lib/arjdbc/oracle/connection_methods.rb', line 3

def oracle_connection(config)
  config[:port] ||= 1521
  config[:url] ||= "jdbc:oracle:thin:@#{config[:host]}:#{config[:port]}:#{config[:database]}"
  config[:driver] ||= "oracle.jdbc.driver.OracleDriver"
  config[:adapter_spec] = ::ArJdbc::Oracle
  jdbc_connection(config)
end

.parse_sqlite3_config!(config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/arjdbc/sqlite3/connection_methods.rb', line 21

def parse_sqlite3_config!(config)
  config[:database] ||= config[:dbfile]

  # Allow database path relative to RAILS_ROOT, but only if
  # the database path is not the special path that tells
  # Sqlite to build a database only in memory.
  rails_root_defined = defined?(Rails.root) || Object.const_defined?(:RAILS_ROOT)
  if rails_root_defined && ':memory:' != config[:database]
    rails_root = defined?(Rails.root) ? Rails.root : RAILS_ROOT
    config[:database] = File.expand_path(config[:database], rails_root)
  end
end

.postgresql_connection(config) ⇒ Object Also known as: jdbcpostgresql_connection



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/arjdbc/postgresql/connection_methods.rb', line 3

def postgresql_connection(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[:username] ||= Java::JavaLang::System.get_property("user.name")
  config[:host] ||= "localhost"
  config[:port] ||= 5432
  config[:url] ||= "jdbc:postgresql://#{config[:host]}:#{config[:port]}/#{config[:database]}"
  config[:url] << config[:pg_params] if config[:pg_params]
  config[:driver] ||= defined?(::Jdbc::Postgres.driver_name) ? ::Jdbc::Postgres.driver_name : 'org.postgresql.Driver'
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  config[:adapter_spec] = ::ArJdbc::PostgreSQL
  conn = jdbc_connection(config)
  conn.execute("SET SEARCH_PATH TO #{config[:schema_search_path]}") if config[:schema_search_path]
  conn
end

.reset_column_information_with_arjdbc_base_extObject

Allow adapters to provide their own reset_column_information methods

NOTE: This only affects the current thread’s connection.



7
8
9
10
11
# File 'lib/arjdbc/jdbc/base_ext.rb', line 7

def reset_column_information_with_arjdbc_base_ext
  # Invoke the adapter-specific reset_column_information method
  connection.reset_column_information if connection.respond_to?(:reset_column_information)
  reset_column_information_without_arjdbc_base_ext
end

.sqlite3_connection(config) ⇒ Object Also known as: jdbcsqlite3_connection



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/arjdbc/sqlite3/connection_methods.rb', line 3

def sqlite3_connection(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]
  database = '' if database == ':memory:'
  config[:url] ||= "jdbc:sqlite:#{database}"
  config[:driver] ||= defined?(::Jdbc::SQLite3.driver_name) ? ::Jdbc::SQLite3.driver_name : 'org.sqlite.JDBC'
  config[:adapter_class] = ActiveRecord::ConnectionAdapters::SQLite3Adapter
  config[:adapter_spec] = ::ArJdbc::SQLite3
  jdbc_connection(config)
end