Class: PLSQL::OCIConnection
- Inherits:
-
Connection
- Object
- Connection
- PLSQL::OCIConnection
- Defined in:
- lib/plsql/oci_connection.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Cursor
Instance Attribute Summary
Attributes inherited from Connection
#activerecord_class, #raw_driver
Instance Method Summary collapse
- #autocommit=(value) ⇒ Object
- #autocommit? ⇒ Boolean
- #commit ⇒ Object
- #cursor_from_query(sql, bindvars = [], options = {}) ⇒ Object
- #database_version ⇒ Object
- #describe_synonym(schema_name, synonym_name) ⇒ Object
- #exec(sql, *bindvars) ⇒ Object
- #logoff ⇒ Object
- #ora_value_to_ruby_value(value) ⇒ Object
- #parse(sql) ⇒ Object
- #plsql_to_ruby_data_type(metadata) ⇒ Object
- #prefetch_rows=(value) ⇒ Object
- #rollback ⇒ Object
- #ruby_value_to_ora_value(value, type = nil) ⇒ Object
Methods inherited from Connection
create, #initialize, #jdbc?, #oci?, #raw_connection, #select_all, #select_first, #select_hash_all, #select_hash_first
Constructor Details
This class inherits a constructor from PLSQL::Connection
Instance Method Details
#autocommit=(value) ⇒ Object
36 37 38 |
# File 'lib/plsql/oci_connection.rb', line 36 def autocommit=(value) raw_connection.autocommit = value end |
#autocommit? ⇒ Boolean
32 33 34 |
# File 'lib/plsql/oci_connection.rb', line 32 def autocommit? raw_connection.autocommit? end |
#commit ⇒ Object
24 25 26 |
# File 'lib/plsql/oci_connection.rb', line 24 def commit raw_connection.commit end |
#cursor_from_query(sql, bindvars = [], options = {}) ⇒ Object
121 122 123 |
# File 'lib/plsql/oci_connection.rb', line 121 def cursor_from_query(sql, bindvars=[], ={}) Cursor.new_from_query(self, sql, bindvars, ) end |
#database_version ⇒ Object
274 275 276 |
# File 'lib/plsql/oci_connection.rb', line 274 def database_version @database_version ||= (version = raw_connection.oracle_server_version) && [version.major, version.minor] end |
#describe_synonym(schema_name, synonym_name) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/plsql/oci_connection.rb', line 262 def describe_synonym(schema_name, synonym_name) if schema_name == 'PUBLIC' full_name = synonym_name.to_s else full_name = "#{schema_name}.#{synonym_name}" end = raw_connection.describe_synonym(full_name) [.schema_name, .name] rescue OCIError nil end |
#exec(sql, *bindvars) ⇒ Object
44 45 46 47 |
# File 'lib/plsql/oci_connection.rb', line 44 def exec(sql, *bindvars) raw_connection.exec(sql, *bindvars) true end |
#logoff ⇒ Object
20 21 22 |
# File 'lib/plsql/oci_connection.rb', line 20 def logoff raw_connection.logoff end |
#ora_value_to_ruby_value(value) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/plsql/oci_connection.rb', line 232 def ora_value_to_ruby_value(value) case value when Float, OraNumber, BigDecimal ora_number_to_ruby_number(value) when DateTime, OraDate ora_date_to_ruby_date(value) when OCI8::LOB if value.available? value.rewind value.read else nil end when OCI8::Object::Base tdo = raw_oci_connection.get_tdo_by_class(value.class) if tdo.is_collection? value.to_ary.map{|e| ora_value_to_ruby_value(e)} else # object type tdo.attributes.inject({}) do |hash, attr| hash[attr.name] = ora_value_to_ruby_value(value.instance_variable_get(:@attributes)[attr.name]) hash end end when OCI8::Cursor Cursor.new(self, value) else value end end |
#parse(sql) ⇒ Object
117 118 119 |
# File 'lib/plsql/oci_connection.rb', line 117 def parse(sql) Cursor.new_from_parse(self, sql) end |
#plsql_to_ruby_data_type(metadata) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/plsql/oci_connection.rb', line 125 def plsql_to_ruby_data_type() data_type, data_length = [:data_type], [:data_length] case data_type when "VARCHAR2", "CHAR", "NVARCHAR2", "NCHAR" [String, data_length || 32767] when "CLOB", "NCLOB" [OCI8::CLOB, nil] when "BLOB" [OCI8::BLOB, nil] when "NUMBER", "PLS_INTEGER", "BINARY_INTEGER" [OraNumber, nil] when "DATE" [DateTime, nil] when "TIMESTAMP", "TIMESTAMP WITH TIME ZONE", "TIMESTAMP WITH LOCAL TIME ZONE" [Time, nil] when "TABLE", "VARRAY", "OBJECT" # create Ruby class for collection klass = OCI8::Object::Base.get_class_by_typename([:sql_type_name]) unless klass klass = Class.new(OCI8::Object::Base) klass.set_typename [:sql_type_name] end [klass, nil] when "REF CURSOR" [OCI8::Cursor] else [String, 32767] end end |
#prefetch_rows=(value) ⇒ Object
40 41 42 |
# File 'lib/plsql/oci_connection.rb', line 40 def prefetch_rows=(value) raw_connection.prefetch_rows = value end |
#rollback ⇒ Object
28 29 30 |
# File 'lib/plsql/oci_connection.rb', line 28 def rollback raw_connection.rollback end |
#ruby_value_to_ora_value(value, type = nil) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/plsql/oci_connection.rb', line 155 def ruby_value_to_ora_value(value, type=nil) type ||= value.class case type.to_s.to_sym when :Fixnum, :BigDecimal, :String value when :OraNumber # pass parameters as OraNumber to avoid rounding errors case value when Bignum OraNumber.new(value.to_s) when BigDecimal OraNumber.new(value.to_s('F')) when TrueClass OraNumber.new(1) when FalseClass OraNumber.new(0) else value end when :DateTime case value when Time ::DateTime.civil(value.year, value.month, value.day, value.hour, value.min, value.sec, Rational(value.utc_offset, 86400)) when DateTime value when Date ::DateTime.civil(value.year, value.month, value.day, 0, 0, 0, 0) else value end when :"OCI8::CLOB", :"OCI8::BLOB" # ruby-oci8 cannot create CLOB/BLOB from '' value = nil if value == '' type.new(raw_oci_connection, value) when :"OCI8::Cursor" value && value.raw_cursor else # collections and object types if type.superclass == OCI8::Object::Base return nil if value.nil? tdo = raw_oci_connection.get_tdo_by_class(type) if tdo.is_collection? raise ArgumentError, "You should pass Array value for collection type parameter" unless value.is_a?(Array) elem_list = value.map do |elem| if (attr_tdo = tdo.coll_attr.typeinfo) attr_type, attr_length = plsql_to_ruby_data_type(:data_type => 'OBJECT', :sql_type_name => attr_tdo.typename) else attr_type = elem.class end ruby_value_to_ora_value(elem, attr_type) end # construct collection value # TODO: change setting instance variable to appropriate ruby-oci8 method call when available collection = type.new(raw_oci_connection) collection.instance_variable_set('@attributes', elem_list) collection else # object type raise ArgumentError, "You should pass Hash value for object type parameter" unless value.is_a?(Hash) object_attrs = value.dup object_attrs.keys.each do |key| raise ArgumentError, "Wrong object type field passed to PL/SQL procedure" unless (attr = tdo.attr_getters[key]) case attr.datatype when OCI8::TDO::ATTR_NAMED_TYPE, OCI8::TDO::ATTR_NAMED_COLLECTION # nested object type or collection attr_type, attr_length = plsql_to_ruby_data_type(:data_type => 'OBJECT', :sql_type_name => attr.typeinfo.typename) object_attrs[key] = ruby_value_to_ora_value(object_attrs[key], attr_type) end end type.new(raw_oci_connection, object_attrs) end # all other cases else value end end end |