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.
-
#set_time_zone(time_zone = nil) ⇒ Object
Set time zone (default taken from TZ environment variable).
-
#time_zone ⇒ Object
Returns session time zone.
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.ancestors.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 36 37 |
# File 'lib/plsql/connection.rb', line 26 def self.create_new(params) #:nodoc: conn = case driver_type when :oci OCIConnection.create_raw(params) when :jdbc JDBCConnection.create_raw(params) else raise ArgumentError, "Unknown raw driver" end conn.set_time_zone(params[:time_zone]) conn end |
.driver_type ⇒ Object
:nodoc:
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/plsql/connection.rb', line 39 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)
91 92 93 |
# File 'lib/plsql/connection.rb', line 91 def autocommit=(value) raise NoMethodError, "Not implemented for this raw driver" end |
#autocommit? ⇒ Boolean
Current autocommit mode (true or false)
86 87 88 |
# File 'lib/plsql/connection.rb', line 86 def autocommit? raise NoMethodError, "Not implemented for this raw driver" end |
#commit ⇒ Object
:nodoc:
77 78 79 |
# File 'lib/plsql/connection.rb', line 77 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])
191 192 193 |
# File 'lib/plsql/connection.rb', line 191 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
184 185 186 187 188 |
# File 'lib/plsql/connection.rb', line 184 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
214 215 216 217 218 219 220 |
# File 'lib/plsql/connection.rb', line 214 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
223 224 225 226 227 228 229 |
# File 'lib/plsql/connection.rb', line 223 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:
149 150 151 |
# File 'lib/plsql/connection.rb', line 149 def exec(sql, *bindvars) #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#jdbc? ⇒ Boolean
Is it JDBC connection
66 67 68 |
# File 'lib/plsql/connection.rb', line 66 def jdbc? @raw_driver == :jdbc end |
#logoff ⇒ Object
:nodoc:
70 71 72 73 74 75 |
# File 'lib/plsql/connection.rb', line 70 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
61 62 63 |
# File 'lib/plsql/connection.rb', line 61 def oci? @raw_driver == :oci end |
#parse(sql) ⇒ Object
:nodoc:
153 154 155 |
# File 'lib/plsql/connection.rb', line 153 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)
97 98 99 |
# File 'lib/plsql/connection.rb', line 97 def prefetch_rows=(value) raise NoMethodError, "Not implemented for this raw driver" end |
#raw_connection ⇒ Object
Returns OCI8 or JDBC connection
52 53 54 55 56 57 58 |
# File 'lib/plsql/connection.rb', line 52 def raw_connection if @activerecord_class @activerecord_class.connection.raw_connection else @raw_connection end end |
#rollback ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/plsql/connection.rb', line 81 def rollback #:nodoc: raise NoMethodError, "Not implemented for this raw driver" end |
#select_all(sql, *bindvars, &block) ⇒ Object
:nodoc:
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/plsql/connection.rb', line 115 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:
101 102 103 104 105 106 |
# File 'lib/plsql/connection.rb', line 101 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:
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/plsql/connection.rb', line 132 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:
108 109 110 111 112 113 |
# File 'lib/plsql/connection.rb', line 108 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
196 197 198 |
# File 'lib/plsql/connection.rb', line 196 def session_id @session_id ||= select_first("SELECT TO_NUMBER(USERENV('SESSIONID')) FROM dual")[0] end |
#set_time_zone(time_zone = nil) ⇒ Object
Set time zone (default taken from TZ environment variable)
201 202 203 204 |
# File 'lib/plsql/connection.rb', line 201 def set_time_zone(time_zone=nil) time_zone ||= ENV['TZ'] exec("alter session set time_zone = '#{time_zone}'") if time_zone end |
#time_zone ⇒ Object
Returns session time zone
207 208 209 |
# File 'lib/plsql/connection.rb', line 207 def time_zone select_first("SELECT SESSIONTIMEZONE FROM dual")[0] end |