Class: ActiveRecord::ConnectionAdapters::OracleEnhancedJDBCConnection
- Inherits:
-
OracleEnhancedConnection
- Object
- OracleEnhancedConnection
- ActiveRecord::ConnectionAdapters::OracleEnhancedJDBCConnection
- Defined in:
- lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb
Overview
JDBC database interface for JRuby
Instance Attribute Summary collapse
-
#active ⇒ Object
(also: #active?)
Returns the value of attribute active.
-
#auto_retry ⇒ Object
(also: #auto_retry?)
Returns the value of attribute auto_retry.
Attributes inherited from OracleEnhancedConnection
Instance Method Summary collapse
- #autocommit=(value) ⇒ Object
- #autocommit? ⇒ Boolean
- #commit ⇒ Object
- #describe(name) ⇒ Object
- #exec(sql) ⇒ Object
- #exec_no_retry(sql) ⇒ Object
-
#initialize(config) ⇒ OracleEnhancedJDBCConnection
constructor
A new instance of OracleEnhancedJDBCConnection.
- #logoff ⇒ Object
- #new_connection(config) ⇒ Object
-
#ping ⇒ Object
Checks connection, returns true if active.
-
#reset! ⇒ Object
Resets connection, by logging off and creating a new connection.
- #rollback ⇒ Object
- #select(sql, name = nil, return_column_names = false) ⇒ Object
- #select_no_retry(sql, name = nil, return_column_names = false) ⇒ Object
-
#with_retry(&block) ⇒ Object
mark connection as dead if connection lost.
- #write_lob(lob, value, is_binary = false) ⇒ Object
Methods inherited from OracleEnhancedConnection
Constructor Details
#initialize(config) ⇒ OracleEnhancedJDBCConnection
Returns a new instance of OracleEnhancedJDBCConnection.
44 45 46 47 48 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 44 def initialize(config) @active = true @config = config new_connection(@config) end |
Instance Attribute Details
#active ⇒ Object Also known as: active?
Returns the value of attribute active.
37 38 39 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 37 def active @active end |
#auto_retry ⇒ Object Also known as: auto_retry?
Returns the value of attribute auto_retry.
40 41 42 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 40 def auto_retry @auto_retry end |
Instance Method Details
#autocommit=(value) ⇒ Object
102 103 104 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 102 def autocommit=(value) @raw_connection.setAutoCommit(value) end |
#autocommit? ⇒ Boolean
98 99 100 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 98 def autocommit? @raw_connection.getAutoCommit end |
#commit ⇒ Object
90 91 92 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 90 def commit @raw_connection.commit end |
#describe(name) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 244 def describe(name) real_name = OracleEnhancedAdapter.valid_table_name?(name) ? name.to_s.upcase : name.to_s if real_name.include?('.') table_owner, table_name = real_name.split('.') else table_owner, table_name = @owner, real_name end sql = <<-SQL SELECT owner, table_name, 'TABLE' name_type FROM all_tables WHERE owner = '#{table_owner}' AND table_name = '#{table_name}' UNION ALL SELECT owner, view_name table_name, 'VIEW' name_type FROM all_views WHERE owner = '#{table_owner}' AND view_name = '#{table_name}' UNION ALL SELECT table_owner, table_name, 'SYNONYM' name_type FROM all_synonyms WHERE owner = '#{table_owner}' AND synonym_name = '#{table_name}' UNION ALL SELECT table_owner, table_name, 'SYNONYM' name_type FROM all_synonyms WHERE owner = 'PUBLIC' AND synonym_name = '#{real_name}' SQL if result = select_one(sql) case result['name_type'] when 'SYNONYM' describe("#{result['owner']}.#{result['table_name']}") else [result['owner'], result['table_name']] end else raise OracleEnhancedConnectionException, %Q{"DESC #{name}" failed; does it exist?} end end |
#exec(sql) ⇒ Object
152 153 154 155 156 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 152 def exec(sql) with_retry do exec_no_retry(sql) end end |
#exec_no_retry(sql) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 158 def exec_no_retry(sql) cs = prepare_call(sql) case sql when /\A\s*UPDATE/i, /\A\s*INSERT/i, /\A\s*DELETE/i cs.executeUpdate else cs.execute true end ensure cs.close rescue nil end |
#logoff ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 82 def logoff @active = false @raw_connection.close true rescue false end |
#new_connection(config) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 50 def new_connection(config) username, password, database = config[:username].to_s, config[:password].to_s, config[:database].to_s privilege = config[:privilege] && config[:privilege].to_s host, port = config[:host], config[:port] url = config[:url] || "jdbc:oracle:thin:@#{host || 'localhost'}:#{port || 1521}:#{database || 'XE'}" prefetch_rows = config[:prefetch_rows] || 100 cursor_sharing = config[:cursor_sharing] || 'similar' properties = java.util.Properties.new properties.put("user", username) properties.put("password", password) properties.put("defaultRowPrefetch", "#{prefetch_rows}") if prefetch_rows properties.put("internal_logon", privilege) if privilege @raw_connection = java.sql.DriverManager.getConnection(url, properties) exec %q{alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'} exec %q{alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS'} # rescue nil exec "alter session set cursor_sharing = #{cursor_sharing}" # rescue nil self.autocommit = true # Set session time zone to current time zone @raw_connection.setSessionTimeZone(java.util.TimeZone.default.getID) # default schema owner @owner = username.upcase @raw_connection end |
#ping ⇒ Object
Checks connection, returns true if active. Note that ping actively checks the connection, while #active? simply returns the last known state.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 109 def ping exec_no_retry("select 1 from dual") @active = true rescue NativeException => e @active = false if e. =~ /^java\.sql\.SQLException/ raise OracleEnhancedConnectionException, e. else raise end end |
#reset! ⇒ Object
Resets connection, by logging off and creating a new connection.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 122 def reset! logoff rescue nil begin new_connection(@config) @active = true rescue NativeException => e @active = false if e. =~ /^java\.sql\.SQLException/ raise OracleEnhancedConnectionException, e. else raise end end end |
#rollback ⇒ Object
94 95 96 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 94 def rollback @raw_connection.rollback end |
#select(sql, name = nil, return_column_names = false) ⇒ Object
171 172 173 174 175 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 171 def select(sql, name = nil, return_column_names = false) with_retry do select_no_retry(sql, name, return_column_names) end end |
#select_no_retry(sql, name = nil, return_column_names = false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 177 def select_no_retry(sql, name = nil, return_column_names = false) stmt = prepare_statement(sql) rset = stmt.executeQuery = rset.getMetaData column_count = .getColumnCount cols = (1..column_count).map do |i| oracle_downcase(.getColumnName(i)) end col_types = (1..column_count).map do |i| .getColumnTypeName(i) end rows = [] while rset.next hash = Hash.new cols.each_with_index do |col, i0| i = i0 + 1 hash[col] = case column_type = col_types[i0] when /CLOB/ name == 'Writable Large Object' ? rset.getClob(i) : get_ruby_value_from_result_set(rset, i, column_type) when /BLOB/ name == 'Writable Large Object' ? rset.getBlob(i) : get_ruby_value_from_result_set(rset, i, column_type) when 'DATE' t = get_ruby_value_from_result_set(rset, i, column_type) # RSI: added emulate_dates_by_column_name functionality # if emulate_dates_by_column_name && self.class.is_date_column?(col) # d.to_date # elsif if t && OracleEnhancedAdapter.emulate_dates && (t.hour == 0 && t.min == 0 && t.sec == 0) t.to_date else # JRuby Time supports time before year 1900 therefore now need to fall back to DateTime t end # RSI: added emulate_integers_by_column_name functionality when "NUMBER" n = get_ruby_value_from_result_set(rset, i, column_type) if n && n.is_a?(Float) && OracleEnhancedAdapter.emulate_integers_by_column_name && OracleEnhancedAdapter.is_integer_column?(col) n.to_i else n end else get_ruby_value_from_result_set(rset, i, column_type) end unless col == 'raw_rnum_' end rows << hash end return_column_names ? [rows, cols] : rows ensure rset.close rescue nil stmt.close rescue nil end |
#with_retry(&block) ⇒ Object
mark connection as dead if connection lost
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 138 def with_retry(&block) should_retry = auto_retry? && autocommit? begin yield if block_given? rescue NativeException => e raise unless e. =~ /^java\.sql\.SQLException: (Closed Connection|Io exception:|No more data to read from socket)/ @active = false raise unless should_retry should_retry = false reset! rescue nil retry end end |
#write_lob(lob, value, is_binary = false) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 236 def write_lob(lob, value, is_binary = false) if is_binary lob.setBytes(1, value.to_java_bytes) else lob.setString(1,value) end end |