Class: OracleRaw

Inherits:
Object
  • Object
show all
Defined in:
lib/oracle_raw.rb

Overview

This is a Ruby library for interfacing with an Oracle Database using pooled OCI8 raw connections (ruby-oci8.rubyforge.org/en/). It uses ActiveRecord Oracle Enhanced adapter (github.com/rsim/oracle-enhanced) for connection pooling.

Installation

gem install oracle_raw

Usage

Consult the README.

Additional information

ActiveRecord and ConnectionPool documentation:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tnsnames, schema, password, pool_size = 1, global_options = {}) ⇒ OracleRaw

Establishes a connection with the given connection parameters, and sets global options.

tnsnames = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SID = TEST)))'



59
60
61
62
63
64
# File 'lib/oracle_raw.rb', line 59

def initialize(tnsnames, schema, password, pool_size = 1, global_options = {})
  ActiveRecord::Base.establish_connection(:adapter  => "oracle_enhanced", 
            :username => schema, :password => password, 
            :database => tnsnames, :pool => pool_size)
  @global_options = global_options 
end

Instance Attribute Details

#global_optionsObject

Global options are readable and writable.



53
54
55
# File 'lib/oracle_raw.rb', line 53

def global_options
  @global_options
end

Instance Method Details

#closeObject

Closes all connections in the connection pool.



68
69
70
# File 'lib/oracle_raw.rb', line 68

def close
  ActiveRecord::Base.connection_pool.disconnect!
end

#query(sqlquery, parameters = [], options = {}) ⇒ Object

Depending whether the :metadata option is :none or :all, returns either a plain result set or a map of the following kind:

{ :count => rowcount, :columns => colnames, :data => data, :date => date, :duration => duration }

Exception are propagated to the caller.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/oracle_raw.rb', line 86

def query(sqlquery, parameters = [], options = {})

  with_connection { |conn|

    starttime = Time.new; data = []

    cursor = conn.parse(sqlquery)
    cursor.bind_parameters(parameters) if parameters
    cursor.exec_with_prefetch(5000)

    case options[:item_format] || @global_options[:item_format]

      when :hash then

        case options[:amount] || @global_options[:amount]

          when :first_row then data = cursor.fetch_hash()
          else while r = cursor.fetch_hash(); data << r;  end 
        end
      else 
        case options[:amount] || @global_options[:amount]

          when :single_value then temp = cursor.fetch(); data = (temp ? temp[0] : nil)
          when :first_row then data = cursor.fetch()
          else while r = cursor.fetch(); data << r; end
        end
    end

    case options[:metadata] || @global_options[:metadata]

      when :all then
        colnames = cursor.get_col_names.each do |n| n.downcase! end 
        rowcount = cursor.row_count
        cursor.close
        {:count => rowcount, :columns => colnames, :data => data, :date => starttime, :duration => Time.new - starttime}
      when :plain then
        cursor.close
        data
      else 
        cursor.close
        {:data => data}
    end
  }
end

#with_connectionObject

Yields a raw connection to the block argument. Example:

db.with_connection { |c| c.exec("insert into students (last_name, first_name) values ('Kruskal-Wallis', 'Lucy')") }



76
77
78
# File 'lib/oracle_raw.rb', line 76

def with_connection
  ActiveRecord::Base.connection_pool.with_connection { |conn| yield conn.raw_connection }
end