Class: PLSQL::JDBCConnection
- Inherits:
-
Connection
- Object
- Connection
- PLSQL::JDBCConnection
- Defined in:
- lib/plsql/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
232 233 234 |
# File 'lib/plsql/connection.rb', line 232 def autocommit=(value) raw_connection.setAutoCommit(value) end |
#autocommit? ⇒ Boolean
228 229 230 |
# File 'lib/plsql/connection.rb', line 228 def autocommit? raw_connection.getAutoCommit end |
#commit ⇒ Object
220 221 222 |
# File 'lib/plsql/connection.rb', line 220 def commit raw_connection.commit end |
#exec(sql, *bindvars) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/plsql/connection.rb', line 277 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
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/plsql/connection.rb', line 389 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 '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
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/plsql/connection.rb', line 346 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 '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
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/plsql/connection.rb', line 407 def get_ruby_value_from_result_set(rset, i, type_name) case type_name when "CHAR", "VARCHAR2" rset.getString(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
213 214 215 216 217 218 |
# File 'lib/plsql/connection.rb', line 213 def logoff raw_connection.close true rescue false end |
#ora_value_to_ruby_value(val) ⇒ Object
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/plsql/connection.rb', line 454 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) else val end end |
#parse(sql) ⇒ Object
326 327 328 |
# File 'lib/plsql/connection.rb', line 326 def parse(sql) Cursor.new(sql, self) end |
#plsql_to_ruby_data_type(data_type, data_length) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/plsql/connection.rb', line 427 def plsql_to_ruby_data_type(data_type, data_length) case data_type when "VARCHAR2" [String, data_length || 4000] when "NUMBER" [BigDecimal, nil] when "DATE" [Time, nil] when "TIMESTAMP" [Time, nil] # CLOB # BLOB else [String, 4000] end end |
#prepare_call(sql, *bindvars) ⇒ Object
338 339 340 341 342 343 344 |
# File 'lib/plsql/connection.rb', line 338 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
330 331 332 333 334 335 336 |
# File 'lib/plsql/connection.rb', line 330 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
224 225 226 |
# File 'lib/plsql/connection.rb', line 224 def rollback raw_connection.rollback end |
#ruby_value_to_ora_value(val, type) ⇒ Object
444 445 446 447 448 449 450 451 452 |
# File 'lib/plsql/connection.rb', line 444 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) else val end end |
#select_all(sql, *bindvars, &block) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/plsql/connection.rb', line 253 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/plsql/connection.rb', line 236 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
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/plsql/connection.rb', line 367 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 '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 |