Class: ActiveRecord::StoredProcedure

Inherits:
Base
  • Object
show all
Extended by:
Spare::Attributes, Spare::Core, Spare::ModelSchema
Defined in:
lib/spare/stored_procedure.rb

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

Methods included from Spare::Core

inspect

Methods included from Spare::ModelSchema

schema, schema=, stored_procedure, stored_procedure_name, stored_procedure_name=

Methods included from Spare::Attributes

columns

Instance Attribute Details

#call_resultsObject

Returns the value of attribute call_results.



17
18
19
# File 'lib/spare/stored_procedure.rb', line 17

def call_results
  @call_results
end

Instance Method Details

#call_sqlObject



56
57
58
# File 'lib/spare/stored_procedure.rb', line 56

def call_sql
  "CALL #{self.class.stored_procedure[:db]}.#{self.class.stored_procedure[:specific_name]}(#{in_params.join(',')});"
end

#executeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spare/stored_procedure.rb', line 69

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_paramsObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spare/stored_procedure.rb', line 23

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 # OUT
      prms << "@#{param.name}"
    end
  end
  prms
end

#in_paramsObject



19
20
21
# File 'lib/spare/stored_procedure.rb', line 19

def in_params
  @in_params ||= in_fetch_params
end

#inout_paramsObject



39
40
41
# File 'lib/spare/stored_procedure.rb', line 39

def inout_params
  @inout_params ||= self.class.stored_procedure[:param_list].select{|param| param.param_type.to_s =~ /inout/i}
end

#inout_sqlObject

In MySQL even with multi-statements flag set variables must be set 1 at a time, so return an array



48
49
50
51
52
53
54
# File 'lib/spare/stored_procedure.rb', line 48

def inout_sql
  sql = []
  inout_params.each do |param|
    sql << "SET @#{param.name} = #{connection.quote(send(param.name))}"
  end
  sql
end

#out_paramsObject



35
36
37
# File 'lib/spare/stored_procedure.rb', line 35

def out_params
  @out_params ||= self.class.stored_procedure[:param_list].select{|param| param.param_type.to_s =~ /out/i}
end

#out_sqlObject



43
44
45
# File 'lib/spare/stored_procedure.rb', line 43

def out_sql
  "SELECT #{out_params.collect{|param| "@#{param.name}"}.join(',')};"
end

#to_sql(skip_valid = false) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/spare/stored_procedure.rb', line 60

def to_sql(skip_valid=false)
  if skip_valid || valid?
    # sql = (inout_sql.blank? ? "" : inout_sql)
    sql = call_sql
    sql << out_sql unless out_params.blank?
    sql
  end
end