Class: PLSQL::Connection
- Inherits:
-
Object
- Object
- PLSQL::Connection
- Defined in:
- lib/plsql/connection.rb
Direct Known Subclasses
Defined Under Namespace
Modules: CursorCommon
Constant Summary collapse
- RUBY_TEMP_TABLE_PREFIX =
'ruby_'
Instance Attribute Summary collapse
-
#activerecord_class ⇒ Object
readonly
Returns the value of attribute activerecord_class.
-
#raw_driver ⇒ Object
readonly
Returns the value of attribute raw_driver.
Class Method Summary collapse
-
.create(raw_conn, ar_class = nil) ⇒ Object
:nodoc:.
-
.create_new(params) ⇒ Object
:nodoc:.
-
.driver_type ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#autocommit=(value) ⇒ Object
Set autocommit mode (true or false).
-
#autocommit? ⇒ Boolean
Current autocommit mode (true or false).
-
#commit ⇒ Object
:nodoc:.
-
#database_version ⇒ Object
Returns array with major and minor version of database (e.g. [10, 2]).
-
#describe_synonym(schema_name, synonym_name) ⇒ Object
all_synonyms view is quite slow therefore this implementation is overriden in OCI connection with faster native OCI method.
-
#drop_all_ruby_temporary_tables ⇒ Object
Drop all ruby temporary tables that are used for calling packages with table parameter types defined in packages.
-
#drop_session_ruby_temporary_tables ⇒ Object
Drop ruby temporary tables created in current session that are used for calling packages with table parameter types defined in packages.
-
#exec(sql, *bindvars) ⇒ Object
:nodoc:.
-
#initialize(raw_conn, ar_class = nil) ⇒ Connection
constructor
:nodoc:.
-
#jdbc? ⇒ Boolean
Is it JDBC connection.
-
#logoff ⇒ Object
:nodoc:.
-
#oci? ⇒ Boolean
Is it OCI8 connection.
-
#parse(sql) ⇒ Object
:nodoc:.
-
#prefetch_rows=(value) ⇒ Object
Set number of rows to be prefetched.
-
#raw_connection ⇒ Object
Returns OCI8 or JDBC connection.
-
#rollback ⇒ Object
:nodoc:.
-
#select_all(sql, *bindvars, &block) ⇒ Object
:nodoc:.
-
#select_first(sql, *bindvars) ⇒ Object
:nodoc:.
-
#select_hash_all(sql, *bindvars, &block) ⇒ Object
:nodoc:.
-
#select_hash_first(sql, *bindvars) ⇒ Object
:nodoc:.
-
#session_id ⇒ Object
Returns session ID.
Constructor Details
#initialize(raw_conn, ar_class = nil) ⇒ Connection
:nodoc:
6 7 8 9 10 |
# File 'lib/plsql/connection.rb', line 6 def initialize(raw_conn, ar_class = nil) #:nodoc: @raw_driver = self.class.driver_type @raw_connection = raw_conn @activerecord_class = ar_class end |
Instance Attribute Details
#activerecord_class ⇒ Object (readonly)
Returns the value of attribute activerecord_class.
4 5 6 |
# File 'lib/plsql/connection.rb', line 4 def activerecord_class @activerecord_class end |
#raw_driver ⇒ Object (readonly)
Returns the value of attribute raw_driver.
3 4 5 |
# File 'lib/plsql/connection.rb', line 3 def raw_driver @raw_driver end |
Class Method Details
.create(raw_conn, ar_class = nil) ⇒ Object
:nodoc:
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/plsql/connection.rb', line 12 def self.create(raw_conn, ar_class = nil) #:nodoc: if ar_class && !(defined?(::ActiveRecord) && [ar_class, ar_class.superclass].include?(::ActiveRecord::Base)) raise ArgumentError, "Wrong ActiveRecord class" end case driver_type when :oci OCIConnection.new(raw_conn, ar_class) when :jdbc JDBCConnection.new(raw_conn, ar_class) else raise ArgumentError, "Unknown raw driver" end end |
.create_new(params) ⇒ Object
:nodoc:
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/plsql/connection.rb', line 26 def self.create_new(params) #:nodoc: case driver_type when :oci OCIConnection.create_raw(params) when :jdbc JDBCConnection.create_raw(params) else raise ArgumentError, "Unknown raw driver" end end |
.driver_type ⇒ Object
:nodoc:
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/plsql/connection.rb', line 37 def self.driver_type #:nodoc: # MRI 1.8.6 or YARV 1.9.1 @driver_type ||= if (!defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby") && defined?(OCI8) :oci # JRuby elsif (defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby") :jdbc else nil end end |
Instance Method Details
#autocommit=(value) ⇒ Object
Set autocommit mode (true or false)
89 90 91 |
# File 'lib/plsql/connection.rb', line 89 def autocommit=(value) raise NoMethodError, "Not implemented for this raw driver" end |
#autocommit? ⇒ Boolean
Current autocommit mode (true or false)
84 85 86 |
# File 'lib/plsql/connection.rb', line 84 def autocommit? raise NoMethodError, "Not implemented for this raw driver" end |
#commit ⇒ Object
:nodoc:
75 76 77 |
# File 'lib/plsql/connection.rb', line 75 def commit #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#database_version ⇒ Object
Returns array with major and minor version of database (e.g. [10, 2])
189 190 191 |
# File 'lib/plsql/connection.rb', line 189 def database_version raise NoMethodError, "Not implemented for this raw driver" end |
#describe_synonym(schema_name, synonym_name) ⇒ Object
all_synonyms view is quite slow therefore this implementation is overriden in OCI connection with faster native OCI method
182 183 184 185 186 |
# File 'lib/plsql/connection.rb', line 182 def describe_synonym(schema_name, synonym_name) #:nodoc: select_first( "SELECT table_owner, table_name FROM all_synonyms WHERE owner = :owner AND synonym_name = :synonym_name", schema_name.to_s.upcase, synonym_name.to_s.upcase) end |
#drop_all_ruby_temporary_tables ⇒ Object
Drop all ruby temporary tables that are used for calling packages with table parameter types defined in packages
201 202 203 204 205 206 207 |
# File 'lib/plsql/connection.rb', line 201 def drop_all_ruby_temporary_tables select_all("SELECT table_name FROM user_tables WHERE temporary='Y' AND table_name LIKE :table_name", RUBY_TEMP_TABLE_PREFIX.upcase+'%').each do |row| exec "TRUNCATE TABLE #{row[0]}" exec "DROP TABLE #{row[0]}" end end |
#drop_session_ruby_temporary_tables ⇒ Object
Drop ruby temporary tables created in current session that are used for calling packages with table parameter types defined in packages
210 211 212 213 214 215 216 |
# File 'lib/plsql/connection.rb', line 210 def drop_session_ruby_temporary_tables select_all("SELECT table_name FROM user_tables WHERE temporary='Y' AND table_name LIKE :table_name", RUBY_TEMP_TABLE_PREFIX.upcase+"#{session_id}_%").each do |row| exec "TRUNCATE TABLE #{row[0]}" exec "DROP TABLE #{row[0]}" end end |
#exec(sql, *bindvars) ⇒ Object
:nodoc:
147 148 149 |
# File 'lib/plsql/connection.rb', line 147 def exec(sql, *bindvars) #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#jdbc? ⇒ Boolean
Is it JDBC connection
64 65 66 |
# File 'lib/plsql/connection.rb', line 64 def jdbc? @raw_driver == :jdbc end |
#logoff ⇒ Object
:nodoc:
68 69 70 71 72 73 |
# File 'lib/plsql/connection.rb', line 68 def logoff #:nodoc: # Rollback any uncommited transactions rollback # Common cleanup activities before logoff, should be called from particular driver method drop_session_ruby_temporary_tables end |
#oci? ⇒ Boolean
Is it OCI8 connection
59 60 61 |
# File 'lib/plsql/connection.rb', line 59 def oci? @raw_driver == :oci end |
#parse(sql) ⇒ Object
:nodoc:
151 152 153 |
# File 'lib/plsql/connection.rb', line 151 def parse(sql) #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#prefetch_rows=(value) ⇒ Object
Set number of rows to be prefetched. This can reduce the number of network round trips when fetching many rows. The default value is one. (If ActiveRecord oracle_enhanced connection is used then default is 100)
95 96 97 |
# File 'lib/plsql/connection.rb', line 95 def prefetch_rows=(value) raise NoMethodError, "Not implemented for this raw driver" end |
#raw_connection ⇒ Object
Returns OCI8 or JDBC connection
50 51 52 53 54 55 56 |
# File 'lib/plsql/connection.rb', line 50 def raw_connection if @activerecord_class @activerecord_class.connection.raw_connection else @raw_connection end end |
#rollback ⇒ Object
:nodoc:
79 80 81 |
# File 'lib/plsql/connection.rb', line 79 def rollback #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#select_all(sql, *bindvars, &block) ⇒ Object
:nodoc:
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/plsql/connection.rb', line 113 def select_all(sql, *bindvars, &block) #:nodoc: cursor = cursor_from_query(sql, bindvars) results = [] row_count = 0 while row = cursor.fetch if block_given? yield(row) row_count += 1 else results << row end end block_given? ? row_count : results ensure cursor.close rescue nil end |
#select_first(sql, *bindvars) ⇒ Object
:nodoc:
99 100 101 102 103 104 |
# File 'lib/plsql/connection.rb', line 99 def select_first(sql, *bindvars) #:nodoc: cursor = cursor_from_query(sql, bindvars, :prefetch_rows => 1) cursor.fetch ensure cursor.close rescue nil end |
#select_hash_all(sql, *bindvars, &block) ⇒ Object
:nodoc:
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/plsql/connection.rb', line 130 def select_hash_all(sql, *bindvars, &block) #:nodoc: cursor = cursor_from_query(sql, bindvars) results = [] row_count = 0 while row = cursor.fetch_hash if block_given? yield(row) row_count += 1 else results << row end end block_given? ? row_count : results ensure cursor.close rescue nil end |
#select_hash_first(sql, *bindvars) ⇒ Object
:nodoc:
106 107 108 109 110 111 |
# File 'lib/plsql/connection.rb', line 106 def select_hash_first(sql, *bindvars) #:nodoc: cursor = cursor_from_query(sql, bindvars, :prefetch_rows => 1) cursor.fetch_hash ensure cursor.close rescue nil end |
#session_id ⇒ Object
Returns session ID
194 195 196 |
# File 'lib/plsql/connection.rb', line 194 def session_id @session_id ||= select_first("SELECT TO_NUMBER(USERENV('SESSIONID')) FROM dual")[0] end |