Method: SimpleOracleJDBC::Binding#bind_value

Defined in:
lib/simple_oracle_jdbc/bindings.rb

#bind_value(obj, v, i) ⇒ Object

Given a JDBC prepared call or prepared statement, a value and a bind index, the value will be bound to JDBC statement.

If value is a single value, ie not an array in is considered an IN parameter.

If value is an array, then it should have either 2 or 3 elements.

  • 2 elements indictes the value is an IN parameter, element 0 indicates the type of the bind variable, and element 1 is the value, eg:

    [String, "Some_value"]

  • 3 elements indicates the value is an OUT or an IN OUT parameter (useful only when using stored procedures), eg:

    [String, "Some_value", :out] [:refcursor, nil, :out]

When binding values, Ruby types are mapped to Java / JDBC types based on the type of the passed in Ruby object. The mapping is as follows:

RUBY_TO_JDBC_TYPES = {
 Date       => OracleTypes::DATE,
 Time       => OracleTypes::TIMESTAMP,
 String     => OracleTypes::VARCHAR,
 Fixnum     => OracleTypes::INTEGER,
 Integer    => OracleTypes::INTEGER,
 Bignum     => OracleTypes::NUMERIC,
 Float      => OracleTypes::NUMERIC,
 :refcursor => OracleTypes::CURSOR,
 :raw       => OracleTypes::RAW
}

Note that to bind a ref_cursor, there is no natural Ruby class, so it can only be bound using the array form for values.

Also note that in this version, it is not possible to bind a ref_cursor into a procedure - it can only be retrieved.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simple_oracle_jdbc/bindings.rb', line 60

def bind_value(obj, v, i)
  type  = v.class
  value = v
  if v.is_a? Array
    # class is being overriden from the input
    type = v[0]
    value = v[1]

    if v.length == 3
      bind_out_parameter(obj, i, type, value)
    end
  end

  if type == Date
    bind_date(obj, value, i)
  elsif type == Time
    bind_time(obj, value, i)
  elsif type == String
    bind_string(obj, value, i)
  elsif type == Fixnum or type == Integer
    bind_int(obj, value, i)
  elsif type == Float
    bind_number(obj, value, i)
  elsif type == :refcursor
    bind_refcursor(obj, value, i)
  elsif type == :raw
    bind_raw(obj, value, i)
  elsif type == SimpleOracleJDBC::OraArray
    value.bind_to_call(@connection, obj, i)
  elsif type == SimpleOracleJDBC::OraRecord
    value.bind_to_call(@connection, obj, i)
  else
    raise UnknownBindType, type.to_s
  end
end