Class: ActiveRecord::StoredProcedure
Overview
TODO - Refactor using only those modules necessary for things to work, should be a lot easier when updated to support only Rails 4. Also, move any methods here to their own module
Instance Attribute Summary collapse
Instance Method Summary
collapse
inspect
schema, schema=, stored_procedure, stored_procedure_name, stored_procedure_name=
columns
Instance Attribute Details
#call_results ⇒ Object
Returns the value of attribute call_results.
16
17
18
|
# File 'lib/spare/stored_procedure.rb', line 16
def call_results
@call_results
end
|
Instance Method Details
#call_sql ⇒ Object
55
56
57
|
# File 'lib/spare/stored_procedure.rb', line 55
def call_sql
"CALL #{self.class.stored_procedure[:db]}.#{self.class.stored_procedure[:specific_name]}(#{in_params.join(',')});"
end
|
#execute ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/spare/stored_procedure.rb', line 68
def execute
if valid?
conn = self.class.connection
unless inout_params.blank?
self.inout_sql.each do |inout_to_set|
conn.execute(inout_to_set)
end
end
self.call_results = conn.execute(self.to_sql(true))
if out_params.length != 0
clnt = conn.instance_variable_get(:@connection)
while clnt.next_result
result_array = clnt.store_result.to_a[0]
out_params.each_with_index do |param,i|
send "#{param.name}=", result_array[i]
end
end
end
end
valid?
end
|
#in_fetch_params ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/spare/stored_procedure.rb', line 22
def in_fetch_params
prms = []
self.class.stored_procedure[:param_list].each do |param|
if param.param_type == "IN"
prms << self.class.connection.quote(self.send(param.name.to_sym))
else
prms << "@#{param.name}"
end
end
prms
end
|
#in_params ⇒ Object
18
19
20
|
# File 'lib/spare/stored_procedure.rb', line 18
def in_params
@in_params ||= in_fetch_params
end
|
#inout_params ⇒ Object
38
39
40
|
# File 'lib/spare/stored_procedure.rb', line 38
def inout_params
@inout_params ||= self.class.stored_procedure[:param_list].select{|param| param.param_type.to_s =~ /inout/i}
end
|
#inout_sql ⇒ Object
In MySQL even with multi-statements flag set variables must be set 1 at a time, so return an array
47
48
49
50
51
52
53
|
# File 'lib/spare/stored_procedure.rb', line 47
def inout_sql
sql = []
inout_params.each do |param|
sql << "SET @#{param.name} = #{self.class.connection.quote(send(param.name))}"
end
sql
end
|
#out_params ⇒ Object
34
35
36
|
# File 'lib/spare/stored_procedure.rb', line 34
def out_params
@out_params ||= self.class.stored_procedure[:param_list].select{|param| param.param_type.to_s =~ /out/i}
end
|
#out_sql ⇒ Object
42
43
44
|
# File 'lib/spare/stored_procedure.rb', line 42
def out_sql
"SELECT #{out_params.collect{|param| "@#{param.name}"}.join(',')};"
end
|
#to_sql(skip_valid = false) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/spare/stored_procedure.rb', line 59
def to_sql(skip_valid=false)
if skip_valid || valid?
sql = call_sql
sql << out_sql unless out_params.blank?
sql
end
end
|