Class: PLSQL::OCIConnection

Inherits:
Connection show all
Defined in:
lib/plsql/oci_connection.rb

Defined Under Namespace

Classes: Cursor

Instance Attribute Summary

Attributes inherited from Connection

#raw_connection, #raw_driver

Instance Method Summary collapse

Methods inherited from Connection

create, #initialize, #jdbc?, #oci?

Constructor Details

This class inherits a constructor from PLSQL::Connection

Instance Method Details

#autocommit=(value) ⇒ Object



20
21
22
# File 'lib/plsql/oci_connection.rb', line 20

def autocommit=(value)
  raw_connection.autocommit = value
end

#autocommit?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/plsql/oci_connection.rb', line 16

def autocommit?
  raw_connection.autocommit?
end

#commitObject



8
9
10
# File 'lib/plsql/oci_connection.rb', line 8

def commit
  raw_connection.commit
end

#exec(sql, *bindvars) ⇒ Object



54
55
56
57
# File 'lib/plsql/oci_connection.rb', line 54

def exec(sql, *bindvars)
  raw_connection.exec(sql, *bindvars)
  true
end

#logoffObject



4
5
6
# File 'lib/plsql/oci_connection.rb', line 4

def logoff
  raw_connection.logoff
end

#ora_value_to_ruby_value(val) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/plsql/oci_connection.rb', line 121

def ora_value_to_ruby_value(val)
  case val
  when Float, OraNumber
    ora_number_to_ruby_number(val)
  when DateTime, OraDate
    ora_date_to_ruby_date(val)
  when OCI8::CLOB
    val.rewind
    val.read
  else
    val
  end
end

#parse(sql) ⇒ Object



83
84
85
# File 'lib/plsql/oci_connection.rb', line 83

def parse(sql)
  Cursor.new(raw_connection.parse(sql))
end

#plsql_to_ruby_data_type(data_type, data_length) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/plsql/oci_connection.rb', line 88

def plsql_to_ruby_data_type(data_type, data_length)
  case data_type
  when "VARCHAR2"
    [String, data_length || 4000]
  when "CLOB"
    [OCI8::CLOB, nil]
  when "NUMBER"
    [OraNumber, nil]
  when "DATE"
    [DateTime, nil]
  when "TIMESTAMP"
    [Time, nil]
  # CLOB
  # BLOB
  else
    [String, 4000]
  end
end

#rollbackObject



12
13
14
# File 'lib/plsql/oci_connection.rb', line 12

def rollback
  raw_connection.rollback
end

#ruby_value_to_ora_value(val, type) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/plsql/oci_connection.rb', line 107

def ruby_value_to_ora_value(val, type)
  if type == OraNumber
    val.nil? || val.is_a?(Fixnum) ? val : val.to_f
  elsif type == DateTime
    val ? val.to_datetime : nil
  elsif type == OCI8::CLOB
    # ruby-oci8 cannot create CLOB from ''
    val = nil if val == ''
    OCI8::CLOB.new(raw_oci_connection, val)
  else
    val
  end
end

#select_all(sql, *bindvars, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/plsql/oci_connection.rb', line 36

def select_all(sql, *bindvars, &block)
  cursor = raw_connection.exec(sql, *bindvars)
  results = []
  row_count = 0
  while row = cursor.fetch
    row_with_typecast = row.map {|val| ora_value_to_ruby_value(val) }
    if block_given?
      yield(row_with_typecast)
      row_count += 1
    else
      results << row_with_typecast
    end
  end
  block_given? ? row_count : results
ensure
  cursor.close rescue nil
end

#select_first(sql, *bindvars) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/plsql/oci_connection.rb', line 24

def select_first(sql, *bindvars)
  cursor = raw_connection.exec(sql, *bindvars)
  result = cursor.fetch
  if result
    result.map { |val| ora_value_to_ruby_value(val) }
  else
    nil
  end
ensure
  cursor.close rescue nil
end