Class: SimpleOracleJDBC::OraArray
- Inherits:
-
Object
- Object
- SimpleOracleJDBC::OraArray
- Includes:
- TypeMap
- Defined in:
- lib/simple_oracle_jdbc/ora_array.rb
Instance Attribute Summary collapse
-
#ora_type ⇒ Object
readonly
Returns the value of attribute ora_type.
Instance Method Summary collapse
-
#bind_to_call(conn, stmt, index) ⇒ Object
Given a database connection, a prepared statement and a bind index, this method will bind the array of values (set at object initialization time or by the values= method) to the statement.
-
#initialize(ora_type, values) ⇒ OraArray
constructor
This must be initialized with the name of the Oracle array type as defined on the database, ie the t_name is table of varchar2(10);.
-
#register_as_out_parameter(conn, stmt, index) ⇒ Object
Given a database connection, a prepared statement and a bind index, register the bind at that index as an out or inout parameter.
-
#retrieve_out_value(conn, stmt, index) ⇒ Object
After executing a statement, retrieve the resultant array from Oracle returning a Ruby array of Ruby objects.
-
#values=(value_array) ⇒ Object
Values must be a Ruby array of objects or nil.
Methods included from TypeMap
#java_date_as_date, #java_date_as_time, #java_integer_as_integer, #java_number_as_float, #java_string_as_string, #oracle_raw_as_string, #ruby_any_date_as_jdbc_date, #ruby_date_as_jdbc_date, #ruby_number_as_jdbc_number, #ruby_raw_string_as_jdbc_raw, #ruby_time_as_jdbc_timestamp
Constructor Details
#initialize(ora_type, values) ⇒ OraArray
This must be initialized with the name of the Oracle array type as defined on the database, ie the t_name is table of varchar2(10);
Values must be an array of Ruby objects, or nil. The values in the array will be cast into the appropriate Oracle type depending on the definition of the array defined in Oracle.
14 15 16 17 18 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 14 def initialize(ora_type, values) @ora_type = ora_type.upcase self.values = values @descriptor = nil end |
Instance Attribute Details
#ora_type ⇒ Object (readonly)
Returns the value of attribute ora_type.
6 7 8 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 6 def ora_type @ora_type end |
Instance Method Details
#bind_to_call(conn, stmt, index) ⇒ Object
Given a database connection, a prepared statement and a bind index, this method will bind the array of values (set at object initialization time or by the values= method) to the statement.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 37 def bind_to_call(conn, stmt, index) # First thing that is need is a descriptor for the given type set_descriptor(conn) base_type = @descriptor.get_base_name jarray = nil if base_type == 'VARCHAR' or base_type == 'CHAR' jarray = @values.to_java elsif base_type == 'RAW' jarray = @values.map{|i| ruby_raw_string_as_jdbc_raw(i) }.to_java elsif base_type == 'NUMBER' or base_type == 'INTEGER' jarray = @values.map{|i| ruby_number_as_jdbc_number(i) }.to_java elsif base_type == 'DATE' or base_type == 'TIMESTAMP' jarray = @values.map{|i| ruby_any_date_as_jdbc_date(i) }.to_java elsif @values.first.is_a? Array or @values.first.nil? # If the value is an array, assume we are dealing with an array # of records. Also need the nil check as we could be binding # an empty array or a return value. jarray = create_struct_array(conn, base_type,@values) else raise "#{base_type}: Unimplemented Array Type" end ora_array = ARRAY.new(@descriptor, conn, jarray) stmt.set_object(index, ora_array) end |
#register_as_out_parameter(conn, stmt, index) ⇒ Object
Given a database connection, a prepared statement and a bind index, register the bind at that index as an out or inout parameter.
66 67 68 69 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 66 def register_as_out_parameter(conn, stmt, index) set_descriptor(conn) stmt.register_out_parameter(index, OracleTypes::ARRAY, @ora_type) end |
#retrieve_out_value(conn, stmt, index) ⇒ Object
After executing a statement, retrieve the resultant array from Oracle returning a Ruby array of Ruby objects.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 73 def retrieve_out_value(conn, stmt, index) set_descriptor(conn) ora_array = stmt.get_array(index) if ora_array.nil? return nil end base_type = ora_array.get_base_type_name if base_type == 'VARCHAR' or base_type == 'CHAR' retrieve_as_string(ora_array) elsif base_type == 'RAW' retrieve_as_raw(ora_array) elsif base_type == 'NUMBER' or base_type == 'INTEGER' retrieve_as_number(ora_array) elsif base_type == 'DATE' or base_type == 'TIMESTAMP' retrieve_as_date(ora_array) else retrieve_oracle_record(conn, base_type, ora_array) end end |
#values=(value_array) ⇒ Object
Values must be a Ruby array of objects or nil.
While the values can be set in upon object initialization, this method allows them to be changed. The one advantage is that it allows the array descriptor to be reused across many database calls. As this must be queried from the database, it requires 1 database round trip for each new object, but is cached inside the object once it is initialized.
27 28 29 30 31 32 |
# File 'lib/simple_oracle_jdbc/ora_array.rb', line 27 def values=(value_array) if value_array and !value_array.is_a? Array raise "The values must be a Ruby array, not #{value_array.class}" end @values = value_array || Array.new end |