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



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

def call_procedure(name, attributes = nil)
  request_string = create_request_string(name, attributes)
  statement = connection.jdbc_connection.prepareCall(request_string)

  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



36
37
38
# File 'lib/activerecord/jdbc/plsql.rb', line 36

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



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
105
106
# File 'lib/activerecord/jdbc/plsql.rb', line 79

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



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

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)



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

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



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

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
32
33
# File 'lib/activerecord/jdbc/plsql.rb', line 25

def stringify_parameters(attributes)
  if attributes.is_a?(Hash)
    stringify_parameters(attributes.values)
  elsif attributes.is_a?(Array)
    attributes.map { |k| stringify_parameters(k) }.join(',')
  elsif !attributes.nil?
    '?'
  end
end