Module: ActiveRecord::Jdbc::PLSql::ClassMethods

Defined in:
lib/activerecord/jdbc/plsql.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
}

Instance Method Summary collapse

Instance Method Details

#call_procedure(name, attributes = nil) ⇒ Object

Entry point to call procedure



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/activerecord/jdbc/plsql.rb', line 107

def call_procedure(name, attributes = nil)
  request_string = create_request_string(name, attributes)
  statement = connection.raw_connection.with_connection_retry_guard do |jdbc_connection|
    jdbc_connection.prepareCall(request_string)
  end

  register_parameters(statement, attributes)

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

  parse_results(statement, attributes)
end

#create_request_string(name, attributes) ⇒ Object

Create request string



34
35
36
# File 'lib/activerecord/jdbc/plsql.rb', line 34

def create_request_string(name, attributes)
  "{CALL #{name}(#{stringify_parameters(attributes)})}"
end

#parse_result(statement, key, type) ⇒ Object

Choose the method to call with type



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/activerecord/jdbc/plsql.rb', line 77

def parse_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

#parse_results(statement, attributes) ⇒ Object

Store results in hash



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/activerecord/jdbc/plsql.rb', line 60

def parse_results(statement, attributes)
  result = Hash.new

  if attributes.is_a?(Hash)
    attributes.each { |k, v|
      result[k] = parse_result(statement, k.to_s, v) if v.is_a? Symbol
    }
  elsif attributes.is_a?(Array)
    attributes.each_with_index { |v, i|
      result[i] = parse_result(statement, i, :string) if v.nil?
    }
  end

  result
end

#register_parameter(statement, key, value) ⇒ Object

Choose the best way to register parameter (IN or OUT)



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

def register_parameter(statement, key, value)
  case value
    when Symbol
      statement.registerOutParameter(key.to_s, SQL_TYPES[value])
    else
      statement.setString(key.to_s, value.to_s)
  end
end

#register_parameters(statement, attributes) ⇒ Object

Register parameters for callable statement



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

def register_parameters(statement, attributes)
  if attributes.is_a?(Hash)
    attributes.each { |k, v| register_parameter(statement, k, v) }
  elsif attributes.is_a?(Array)
    attributes.each_with_index { |v, i| register_parameter(statement, i, v) }
  elsif !attributes.nil?
    statement.setString(1, attributes.to_s)
  end
end

#stringify_parameters(attributes) ⇒ Object

Create parameter part of call string



25
26
27
28
29
30
31
# File 'lib/activerecord/jdbc/plsql.rb', line 25

def stringify_parameters(attributes)
  if attributes.is_a?(Hash) || attributes.is_a?(Array)
    Array.new(attributes.size, '?').join(', ')
  elsif !attributes.nil?
    '?'
  end
end