Class: Momomoto::Procedure

Inherits:
Base
  • Object
show all
Defined in:
lib/momomoto/procedure.rb

Overview

This class implements access to stored procedures. It must not be used directly but you should inherit from this class.

Class Method Summary collapse

Methods inherited from Base

logical_operator=, momomoto_attribute, momomoto_attribute_reader

Class Method Details

.call(params = {}, conditions = {}, options = {}) ⇒ Object

executes the stored procedure



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/momomoto/procedure.rb', line 67

def call( params = {}, conditions = {}, options = {} )
  if columns.length > 0
    sql = "SELECT #{columns.keys.join(',')} FROM "
  else
    sql = "SELECT "
  end
  sql += "#{full_name}(#{compile_parameter(params)})"
  sql += compile_where( conditions )
  sql += compile_order( options[:order] ) if options[:order]
  sql += compile_limit( options[:limit] ) if options[:limit]
  sql += ';'
  data = []
  database.execute( sql ).each do | row |
    data << const_get(:Row).new( row )
  end
  data
end

.compile_parameter(params) ⇒ Object

:nodoc:



55
56
57
58
59
60
61
62
63
64
# File 'lib/momomoto/procedure.rb', line 55

def compile_parameter( params ) # :nodoc:
  args = []
  parameters.each do | parameter |
    field_name, datatype = parameter.to_a.first
    raise Error, "parameter #{field_name} not specified" if not params.member?( field_name )
    raise Error, "Not null fields(#{field_name}) need to be set in #{full_name}" if !params[field_name] && datatype.not_null?
    args << datatype.escape( params[field_name] )
  end
  args.join(',')
end

.construct_procedure_name(classname) ⇒ Object

guesses the procedure name of the procedure this class works on



31
32
33
# File 'lib/momomoto/procedure.rb', line 31

def construct_procedure_name( classname ) # :nodoc:
  classname.to_s.split('::').last.downcase.gsub(/[^a-z_0-9]/, '')
end

.full_nameObject

gets the full name of the procedure including schema if set



36
37
38
# File 'lib/momomoto/procedure.rb', line 36

def full_name # :nodoc:
  "#{ schema_name ? schema_name + '.' : ''}#{procedure_name}"
end

.initializeObject

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/momomoto/procedure.rb', line 17

def initialize # :nodoc:
  return if initialized
  super

  @procedure_name ||= construct_procedure_name( self.name )
  @parameters ||= database.fetch_procedure_parameters( procedure_name )
  @columns ||= database.fetch_procedure_columns( procedure_name )

  const_set( :Row, Class.new( Momomoto::Row ) ) if not const_defined?( :Row )
  initialize_row( const_get( :Row ), self )

end

.parameters(*p) ⇒ Object

gets the parameters this procedure accepts returns an array of hashes with the field names as keys and the datatype as values



49
50
51
52
53
# File 'lib/momomoto/procedure.rb', line 49

def parameters( *p )
  return self.send( :parameters=, *p ) if not p.empty?
  initialize if not initialized
  @parameters
end

.parameters=(*p) ⇒ Object

sets the parameters this procedures accepts example: parameters = :param1=>Momomoto:param1=>Momomoto::Datatype:param1=>Momomoto::Datatype::Text:param1=>Momomoto::Datatype::Text.new



42
43
44
45
# File 'lib/momomoto/procedure.rb', line 42

def parameters=( *p )
  p = p.flatten
  @parameters = p
end