×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Ruby
Posted by: Yaroslav Sidlovsky
Added: Oct 19, 2017 2:59 PM
Modified: Oct 19, 2017 3:17 PM
Views: 5
Tags: oci8 oracle ruby
Class OCI8 is the main class for the gem `ruby-oci8`
  1. require 'oci8'
  2.  
  3. OCI8.class_eval do
  4.   def exec_scalar(sql, *args)
  5.     cur = self.exec(sql, *args)
  6.     begin
  7.       if (row = cur.fetch)
  8.         return row[0]
  9.       end
  10.       nil
  11.     ensure
  12.       cur.close rescue nil
  13.     end
  14.   end
  15. end
  16.  
  17. OCI8::Cursor.class_eval do
  18.   def exec_scalar(*bindvars)
  19.     self.exec(*bindvars)
  20.     if (row = self.fetch)
  21.       return row[0]
  22.     end
  23.     nil
  24.   end
  25. end
  26.  
  27. # Usage:
  28.  
  29. conn = OCI8.new('scott', 'tiger')
  30. conn.exec_scalar('select 1 from dual')
  31. # => #<BigDecimal:7f0631547c78,'0.1E1',9(18)>
  32.  
  33. cur = conn.parse('select 1 from dual')
  34. cur.exec_scalar
  35. # => #<BigDecimal:7f063154c408,'0.1E1',9(18)>
  36.  
  37. cur.close
  38. conn.logoff