0
-# Author/Maintainer: Maik Schmidt <contact@maik-schmidt.de>
0
-require 'active_record/connection_adapters/abstract_adapter'
0
- require 'db2/db2cli' unless self.class.const_defined?(:DB2CLI)
0
- require 'active_record/vendor/db2'
0
- # Establishes a connection to the database that's used by
0
- # all Active Record objects
0
- def self.db2_connection(config) # :nodoc:
0
- config = config.symbolize_keys
0
- usr = config[:username]
0
- pwd = config[:password]
0
- schema = config[:schema]
0
- if config.has_key?(:database)
0
- database = config[:database]
0
- raise ArgumentError, 'No database specified. Missing argument: database.'
0
- connection = DB2::Connection.new(DB2::Environment.new)
0
- connection.connect(database, usr, pwd)
0
- ConnectionAdapters::DB2Adapter.new(connection, logger, :schema => schema)
0
- module ConnectionAdapters
0
- # The DB2 adapter works with the C-based CLI driver (http://rubyforge.org/projects/ruby-dbi/)
0
- # * <tt>:username</tt> -- Defaults to nothing
0
- # * <tt>:password</tt> -- Defaults to nothing
0
- # * <tt>:database</tt> -- The name of the database. No default, must be provided.
0
- # * <tt>:schema</tt> -- Database schema to be set initially.
0
- class DB2Adapter < AbstractAdapter
0
- def initialize(connection, logger, connection_options)
0
- super(connection, logger)
0
- @connection_options = connection_options
0
- if schema = @connection_options[:schema]
0
- with_statement do |stmt|
0
- stmt.exec_direct("SET SCHEMA=#{schema}")
0
- def select_rows(sql, name = nil)
0
- stmt = DB2::Statement.new(@connection)
0
- stmt.exec_direct("#{sql.gsub(/=\s*null/i, 'IS NULL')} with ur")
0
- while row = stmt.fetch
0
- def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
0
- super || last_insert_id
0
- def execute(sql, name = nil)
0
- with_statement do |stmt|
0
- rows_affected = stmt.row_count
0
- def begin_db_transaction
0
- @connection.set_auto_commit_off
0
- def commit_db_transaction
0
- @connection.set_auto_commit_on
0
- def rollback_db_transaction
0
- @connection.set_auto_commit_on
0
- def quote_column_name(column_name)
0
- def quote_string(string)
0
- string.gsub(/'/, "''") # ' (for ruby-mode)
0
- def add_limit_offset!(sql, options)
0
- if limit = options[:limit]
0
- offset = options[:offset] || 0
0
- # The following trick was added by andrea+rails@webcom.it.
0
- sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')
0
- sql << ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}"
0
- def tables(name = nil)
0
- schema = @connection_options[:schema] || '%'
0
- with_statement do |stmt|
0
- stmt.tables(schema).each { |t| result << t[2].downcase }
0
- def indexes(table_name, name = nil)
0
- schema = @connection_options[:schema] || ''
0
- with_statement do |stmt|
0
- stmt.indexes(table_name, schema).each do |t|
0
- next if t[4] == 'SYSIBM' # Skip system indexes.
0
- idx_name = t[5].downcase
0
- col_name = t[8].downcase
0
- if tmp.has_key?(idx_name)
0
- tmp[idx_name].columns << col_name
0
- tmp[idx_name] = IndexDefinition.new(table_name, idx_name, is_unique, [col_name])
0
- def columns(table_name, name = nil)
0
- schema = @connection_options[:schema] || '%'
0
- with_statement do |stmt|
0
- stmt.columns(table_name, schema).each do |c|
0
- c_name = c[3].downcase
0
- c_default = c[12] == 'NULL' ? nil : c[12]
0
- c_default.gsub!(/^'(.*)'$/, '\1') if !c_default.nil?
0
- c_type = c[5].downcase
0
- c_type += "(#{c[6]})" if !c[6].nil? && c[6] != ''
0
- result << Column.new(c_name, c_default, c_type, c[17] == 'YES')
0
- def native_database_types
0
- :primary_key => 'int generated by default as identity (start with 42) primary key',
0
- :string => { :name => 'varchar', :limit => 255 },
0
- :text => { :name => 'clob', :limit => 32768 },
0
- :integer => { :name => 'int' },
0
- :float => { :name => 'float' },
0
- :decimal => { :name => 'decimal' },
0
- :datetime => { :name => 'timestamp' },
0
- :timestamp => { :name => 'timestamp' },
0
- :time => { :name => 'time' },
0
- :date => { :name => 'date' },
0
- :binary => { :name => 'blob', :limit => 32768 },
0
- :boolean => { :name => 'decimal', :limit => 1 }
0
- @connection.select_one 'select 1 from ibm.sysdummy1'
0
- def table_alias_length
0
- stmt = DB2::Statement.new(@connection)
0
- row = select_one(<<-GETID.strip)
0
- with temp(id) as (values (identity_val_local())) select * from temp
0
- def select(sql, name = nil)
0
- with_statement do |stmt|
0
- stmt.exec_direct("#{sql.gsub(/=\s*null/i, 'IS NULL')} with ur")
0
- while row = stmt.fetch_as_hash
0
- row.delete('internal$rownum')
0
- # DB2 driver is unavailable.
0
- module ActiveRecord # :nodoc:
0
- def self.db2_connection(config) # :nodoc:
0
- # Set up a reasonable error message
0
- raise LoadError, "DB2 Libraries could not be loaded."
Comments
No one has commented yet.