Class: PLSQL::JDBCConnection
- Inherits:
-
Connection
- Object
- Connection
- PLSQL::JDBCConnection
- Defined in:
- lib/plsql/jdbc_connection.rb
Defined Under Namespace
Classes: Cursor
Instance Attribute Summary
Attributes inherited from Connection
Instance Method Summary collapse
- #autocommit=(value) ⇒ Object
- #autocommit? ⇒ Boolean
- #commit ⇒ Object
- #exec(sql, *bindvars) ⇒ Object
- #get_bind_variable(stmt, i, type) ⇒ Object
- #get_java_sql_type(value, type) ⇒ Object
- #get_ruby_value_from_result_set(rset, i, type_name) ⇒ Object
- #logoff ⇒ Object
- #ora_value_to_ruby_value(val) ⇒ Object
- #parse(sql) ⇒ Object
- #plsql_to_ruby_data_type(data_type, data_length) ⇒ Object
- #prepare_call(sql, *bindvars) ⇒ Object
- #prepare_statement(sql, *bindvars) ⇒ Object
- #rollback ⇒ Object
- #ruby_value_to_ora_value(val, type) ⇒ Object
- #select_all(sql, *bindvars, &block) ⇒ Object
- #select_first(sql, *bindvars) ⇒ Object
- #set_bind_variable(stmt, i, value, type = nil, length = nil) ⇒ Object
Methods inherited from Connection
create, #initialize, #jdbc?, #oci?
Constructor Details
This class inherits a constructor from PLSQL::Connection
Instance Method Details
#autocommit=(value) ⇒ Object
22 23 24 |
# File 'lib/plsql/jdbc_connection.rb', line 22 def autocommit=(value) raw_connection.setAutoCommit(value) end |
#autocommit? ⇒ Boolean
18 19 20 |
# File 'lib/plsql/jdbc_connection.rb', line 18 def autocommit? raw_connection.getAutoCommit end |
#commit ⇒ Object
10 11 12 |
# File 'lib/plsql/jdbc_connection.rb', line 10 def commit raw_connection.commit end |
#exec(sql, *bindvars) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/plsql/jdbc_connection.rb', line 67 def exec(sql, *bindvars) cs = prepare_call(sql, *bindvars) cs.execute true ensure cs.close rescue nil end |
#get_bind_variable(stmt, i, type) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/plsql/jdbc_connection.rb', line 183 def get_bind_variable(stmt, i, type) case type.to_s when 'Fixnum', 'Bignum', 'Integer' stmt.getInt(i) when 'Float' stmt.getFloat(i) when 'BigDecimal' bd = stmt.getBigDecimal(i) bd && BigDecimal.new(bd.to_s) when 'String' stmt.getString(i) when 'Java::OracleSql::CLOB' stmt.getClob(i) when 'Date','Time','DateTime' ts = stmt.getTimestamp(i) # ts && Time.parse(Time.at(ts.getTime/1000).iso8601) ts && Time.local(1900+ts.year, ts.month+1, ts.date, ts.hours, ts.minutes, ts.seconds) end end |
#get_java_sql_type(value, type) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/plsql/jdbc_connection.rb', line 136 def get_java_sql_type(value, type) case type ? type.to_s : value.class.to_s when 'Fixnum', 'Bignum', 'Integer' java.sql.Types::INTEGER when 'Float' java.sql.Types::FLOAT when 'BigDecimal' java.sql.Types::NUMERIC when 'String' java.sql.Types::VARCHAR when 'Java::OracleSql::CLOB' Java::oracle.jdbc.OracleTypes::CLOB when 'Date' java.sql.Types::DATE when 'Time' java.sql.Types::DATE when 'DateTime' java.sql.Types::DATE else java.sql.Types::VARCHAR end end |
#get_ruby_value_from_result_set(rset, i, type_name) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/plsql/jdbc_connection.rb', line 203 def get_ruby_value_from_result_set(rset, i, type_name) case type_name when "CHAR", "VARCHAR2" rset.getString(i) when "CLOB" ora_value_to_ruby_value(rset.getClob(i)) when "NUMBER" d = rset.getBigDecimal(i) if d.nil? nil elsif d.scale == 0 d.longValue else d.doubleValue end when "DATE", "TIMESTAMP" Time.at(rset.getTimestamp(i).getTime/1000) else nil end end |
#logoff ⇒ Object
3 4 5 6 7 8 |
# File 'lib/plsql/jdbc_connection.rb', line 3 def logoff raw_connection.close true rescue false end |
#ora_value_to_ruby_value(val) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/plsql/jdbc_connection.rb', line 262 def ora_value_to_ruby_value(val) case val when Float, BigDecimal ora_number_to_ruby_number(val) # when OraDate # ora_date_to_ruby_date(val) when Java::OracleSql::CLOB if val.isEmptyLob nil else val.getSubString(1, val.length) end else val end end |
#parse(sql) ⇒ Object
116 117 118 |
# File 'lib/plsql/jdbc_connection.rb', line 116 def parse(sql) Cursor.new(sql, self) end |
#plsql_to_ruby_data_type(data_type, data_length) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/plsql/jdbc_connection.rb', line 225 def plsql_to_ruby_data_type(data_type, data_length) case data_type when "VARCHAR2" [String, data_length || 4000] when "CLOB" [Java::OracleSql::CLOB, nil] when "NUMBER" [BigDecimal, nil] when "DATE" [Time, nil] when "TIMESTAMP" [Time, nil] # CLOB # BLOB else [String, 4000] end end |
#prepare_call(sql, *bindvars) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/plsql/jdbc_connection.rb', line 128 def prepare_call(sql, *bindvars) stmt = raw_connection.prepareCall(sql) bindvars.each_with_index do |bv, i| set_bind_variable(stmt, i+1, bv) end stmt end |
#prepare_statement(sql, *bindvars) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/plsql/jdbc_connection.rb', line 120 def prepare_statement(sql, *bindvars) stmt = raw_connection.prepareStatement(sql) bindvars.each_with_index do |bv, i| set_bind_variable(stmt, i+1, bv) end stmt end |
#rollback ⇒ Object
14 15 16 |
# File 'lib/plsql/jdbc_connection.rb', line 14 def rollback raw_connection.rollback end |
#ruby_value_to_ora_value(val, type) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/plsql/jdbc_connection.rb', line 244 def ruby_value_to_ora_value(val, type) if type == BigDecimal val.nil? || val.is_a?(Fixnum) ? val : val.to_f elsif type == Time date_to_time(val) elsif type == Java::OracleSql::CLOB if val clob = Java::OracleSql::CLOB.createTemporary(raw_connection, false, Java::OracleSql::CLOB::DURATION_SESSION) clob.setString(1,val) clob else Java::OracleSql::CLOB.getEmptyCLOB end else val end end |
#select_all(sql, *bindvars, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/plsql/jdbc_connection.rb', line 43 def select_all(sql, *bindvars, &block) stmt = prepare_statement(sql, *bindvars) results = [] row_count = 0 rset = stmt.executeQuery = rset.getMetaData column_count = .getColumnCount while rset.next row_with_typecast = (1..column_count).map do |i| get_ruby_value_from_result_set(rset,i,.getColumnTypeName(i)) end if block_given? yield(row_with_typecast) row_count += 1 else results << row_with_typecast end end block_given? ? row_count : results ensure rset.close rescue nil stmt.close rescue nil end |
#select_first(sql, *bindvars) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/plsql/jdbc_connection.rb', line 26 def select_first(sql, *bindvars) stmt = prepare_statement(sql, *bindvars) rset = stmt.executeQuery = rset.getMetaData column_count = .getColumnCount if rset.next (1..column_count).map do |i| get_ruby_value_from_result_set(rset,i,.getColumnTypeName(i)) end else nil end ensure rset.close rescue nil stmt.close rescue nil end |
#set_bind_variable(stmt, i, value, type = nil, length = nil) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/plsql/jdbc_connection.rb', line 159 def set_bind_variable(stmt, i, value, type=nil, length=nil) key = i.kind_of?(Integer) ? nil : i.to_s.gsub(':','') case !value.nil? && type ? type.to_s : value.class.to_s when 'Fixnum', 'Bignum', 'Integer' stmt.send("setInt#{key && "AtName"}", key || i, value) when 'Float' stmt.send("setFloat#{key && "AtName"}", key || i, value) when 'BigDecimal' stmt.send("setBigDecimal#{key && "AtName"}", key || i, java.math.BigDecimal.new(value.to_s)) when 'String' stmt.send("setString#{key && "AtName"}", key || i, value) when 'Java::OracleSql::CLOB' stmt.send("setClob#{key && "AtName"}", key || i, value) when 'Date' stmt.send("setDate#{key && "AtName"}", key || i, java.sql.Date.new(Time.parse(value.to_s).to_i*1000)) when 'Time' stmt.send("setTime#{key && "AtName"}", key || i, java.sql.Time.new(value.to_i*1000)) when 'DateTime' stmt.send("setTime#{key && "AtName"}", key || i, java.sql.Time.new(Time.parse(value.strftime("%c")).to_i*1000)) when 'NilClass' stmt.send("setNull#{key && "AtName"}", key || i, get_java_sql_type(value, type)) end end |