Module: ActiveRecord::Jdbc::PLSql

Defined in:
lib/activerecord/jdbc/plsql.rb,
lib/activerecord/jdbc/plsql/version.rb

Constant Summary collapse

SQL_TYPES =
{
    binary: java.sql.Types::BINARY,
    boolean: java.sql.Types::BOOLEAN,
    date: java.sql.Types::DATE,
    datetime: java.sql.Types::TIMESTAMP,
    decimal: java.sql.Types::DECIMAL,
    float: java.sql.Types::FLOAT,
    integer: java.sql.Types::INTEGER,
    string: java.sql.Types::VARCHAR,
    text: java.sql.Types::VARCHAR,
    time: java.sql.Types::TIME,
    timestamp: java.sql.Types::TIMESTAMP
}
VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#call_procedure(name, *binds) ⇒ Object

Entry point to call procedure



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/activerecord/jdbc/plsql.rb', line 114

def call_procedure(name, *binds)
  parameters = parse_parameters(binds)

  request_string = create_request_string(name, parameters)
  statement = raw_connection.with_connection_retry_guard do |jdbc_connection|
    jdbc_connection.prepareCall(request_string)
  end

  output_parameters = parse_output_parameters(parameters)
  input_parameters = parse_input_parameters(parameters)

  register_parameters(statement, output_parameters, input_parameters)

  logger.info 'Call procedure: ' + request_string
  statement.execute

  get_results(statement, output_parameters)
end

#create_request_string(name, attributes) ⇒ Object



49
50
51
# File 'lib/activerecord/jdbc/plsql.rb', line 49

def create_request_string(name, attributes)
  "{CALL #{name}(#{Array.new(attributes.size, '?').join(', ')})}"
end

#get_result(statement, key, type) ⇒ Object

Choose the method to call with type



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/activerecord/jdbc/plsql.rb', line 84

def get_result(statement, key, type)
  case type
    when :binary
      statement.getString(key)
    when :boolean
      statement.getBoolean(key)
    when :date
      statement.getDate(key)
    when :datetime
      statement.getTimestamp(key)
    when :decimal
      statement.getDouble(key)
    when :float
      statement.getFloat(key)
    when :integer
      statement.getInt(key)
    when :string
      statement.getString(key)
    when :text
      statement.getString(key)
    when :time
      statement.getTime(key)
    when :timestamp
      statement.getTimestamp(key)
    else
      statement.getObject(key)
  end
end

#get_results(statement, output_parameters) ⇒ Object

Store results in hash



74
75
76
77
78
79
80
81
# File 'lib/activerecord/jdbc/plsql.rb', line 74

def get_results(statement, output_parameters)
  results = Hash.new
  output_parameters.each do |k, v|
    key = k.is_a?(Symbol) ? k.to_s : k
    results[k] = get_result(statement, key, v)
  end
  results
end

#parse_input_parameters(parameters) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/activerecord/jdbc/plsql.rb', line 29

def parse_input_parameters(parameters)
  input_parameters = Hash.new
  if parameters.is_a? Hash
    parameters.each { |k, v| input_parameters[k] = v unless SQL_TYPES.include? v}
  else
    parameters.each_with_index { |v, i| input_parameters[i + 1] = v unless SQL_TYPES.include? v }
  end
  input_parameters
end

#parse_output_parameters(parameters) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/activerecord/jdbc/plsql.rb', line 39

def parse_output_parameters(parameters)
  output_parameters = Hash.new
  if parameters.is_a? Hash
    parameters.each { |k, v| output_parameters[k] = v if SQL_TYPES.include? v}
  else
    parameters.each_with_index { |v, i| output_parameters[i + 1] = v if SQL_TYPES.include? v }
  end
  output_parameters
end

#parse_parameters(parameters) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/activerecord/jdbc/plsql.rb', line 21

def parse_parameters(parameters)
  if parameters.size == 1 and parameters[0].is_a? Hash
    parameters[0]
  else
    parameters
  end
end

#register_input_parameters(statement, input_parameters) ⇒ Object



53
54
55
56
57
58
# File 'lib/activerecord/jdbc/plsql.rb', line 53

def register_input_parameters(statement, input_parameters)
  input_parameters.each do |k, v|
    key = k.is_a?(Symbol) ? k.to_s : k
    statement.setObject(key, v)
  end
end

#register_output_parameters(statement, output_parameters) ⇒ Object



60
61
62
63
64
65
# File 'lib/activerecord/jdbc/plsql.rb', line 60

def register_output_parameters(statement, output_parameters)
  output_parameters.each do |k, v|
    key = k.is_a?(Symbol) ? k.to_s : k
    statement.registerOutParameter(key, SQL_TYPES[v])
  end
end

#register_parameters(statement, output_parameters, input_parameters) ⇒ Object



67
68
69
70
71
# File 'lib/activerecord/jdbc/plsql.rb', line 67

def register_parameters(statement, output_parameters, input_parameters)
  # Order is important for INOUT parameters
  register_output_parameters(statement, output_parameters)
  register_input_parameters(statement, input_parameters)
end