Class: PLSQL::OCIConnection

Inherits:
Connection show all
Defined in:
lib/plsql/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



85
86
87
# File 'lib/plsql/connection.rb', line 85

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

#autocommit?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/plsql/connection.rb', line 81

def autocommit?
  raw_connection.autocommit?
end

#commitObject



73
74
75
# File 'lib/plsql/connection.rb', line 73

def commit
  raw_connection.commit
end

#exec(sql, *bindvars) ⇒ Object



119
120
121
# File 'lib/plsql/connection.rb', line 119

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

#logoffObject



69
70
71
# File 'lib/plsql/connection.rb', line 69

def logoff
  raw_connection.logoff
end

#ora_value_to_ruby_value(val) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/plsql/connection.rb', line 179

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)
  else
    val
  end
end

#parse(sql) ⇒ Object



147
148
149
# File 'lib/plsql/connection.rb', line 147

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

#plsql_to_ruby_data_type(data_type, data_length) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/plsql/connection.rb', line 152

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

#rollbackObject



77
78
79
# File 'lib/plsql/connection.rb', line 77

def rollback
  raw_connection.rollback
end

#ruby_value_to_ora_value(val, type) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/plsql/connection.rb', line 169

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
  else
    val
  end
end

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/plsql/connection.rb', line 101

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/plsql/connection.rb', line 89

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