Class: ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/spare/mysql_abstract_adapter.rb

Defined Under Namespace

Classes: Column

Instance Method Summary collapse

Instance Method Details

#stored_procedure(name) ⇒ Object

Returns hash describing the stored procedure.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spare/mysql_abstract_adapter.rb', line 12

def stored_procedure(name)#:nodoc:
  sp_name = name.split('.').reverse

  sql = "SELECT db,specific_name,param_list,db_collation FROM mysql.proc WHERE specific_name = #{quote(sp_name[0])}"
  sql << " AND db = #{quote(sp_name[1])}" if sp_name[1]

  result = execute(sql)
  keys = result.fields.collect{|k| k.to_sym}
  values = result.to_a[0]
  raise ActiveRecord::StoredProcedureNotFound, "#{name} was not found" unless values
  sp = Hash[keys.zip(values)]
  sp[:param_list] = stored_procedure_params(sp[:param_list], sp[:db_collation])
  sp
end

#stored_procedure_params(param_list, collation) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spare/mysql_abstract_adapter.rb', line 32

def stored_procedure_params(param_list,collation)
  params = []
  param_list = param_list.to_s.split("\n").collect{ |r| r.gsub(/\s+/, ' ').strip.split(" ")}
  param_list.delete([])
  param_list.each do |param|
    param_type = param[0].upcase
    field_name = param[1].to_s.underscore #set_field_encoding(param[1])
    sql_type = param[2]

    if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR > 1
      cast_type = lookup_cast_type(sql_type)
      column = new_column(field_name, nil, cast_type, sql_type, true, collation, nil)
    else
      column = new_column(field_name, nil, sql_type, false, collation)
    end
    
    column.param_type = param_type
    params << column
  end
  params
end