Module: Db2Query::Core::ClassMethods

Defined in:
lib/db2_query/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/db2_query/core.rb', line 100

def method_missing(method_name, *args, &block)
  if sql_query_method?(method_name)
    define_sql_query(method_name)
    method(method_name).call(*args)
  elsif method_name == :exec_query
    sql, args = [args.shift, args.first]
    query = definitions.lookup_query(args, sql)
    exec_query_result(query, args)
  else
    super
  end
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



10
11
12
# File 'lib/db2_query/core.rb', line 10

def definitions
  @definitions
end

Instance Method Details

#define_query_definitionsObject



18
19
20
# File 'lib/db2_query/core.rb', line 18

def define_query_definitions
  @definitions = new_definitions
end

#exec_query_result(query, args) ⇒ Object



22
23
24
25
# File 'lib/db2_query/core.rb', line 22

def exec_query_result(query, args)
  reset_id_when_required(query)
  connection.exec_query(query, args)
end

#fetch(sql, args = []) ⇒ Object



66
67
68
69
70
# File 'lib/db2_query/core.rb', line 66

def fetch(sql, args = [])
  query = definitions.lookup_query(args, sql)
  query.validate_select_query
  connection.exec_query(query, args)
end

#fetch_list(sql, args) ⇒ Object



72
73
74
75
# File 'lib/db2_query/core.rb', line 72

def fetch_list(sql, args)
  list = args.first
  fetch(sql_with_list(sql, list), args.drop(1))
end

#initiation {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



14
15
16
# File 'lib/db2_query/core.rb', line 14

def initiation
  yield(self) if block_given?
end

#query(*query_args) ⇒ Object Also known as: define



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/db2_query/core.rb', line 27

def query(*query_args)
  if query_args.first.is_a?(Symbol)
    query_name, body = query_args

    body_lambda = if body.is_a?(Proc)
      -> args { body.call(args << { query_name: query_name }) }
    elsif body.is_a?(String)
      definition = definitions.lookup_query(query_name, body.strip)
      -> args { exec_query_result(definition, args) }
    else
      raise Db2Query::QueryMethodError.new
    end

    singleton_class.define_method(query_name) do |*args|
      body_lambda.call(args)
    end
  elsif query_args.first.is_a?(String)
    sql, args = [query_args.first.strip, query_args.drop(1)]

    definition = Query.new.tap do |d|
      d.define_sql(sql)
      d.define_args(args)
    end

    connection.raw_query(definition.db2_spec_sql, definition.args)
  else
    raise Db2Query::Error, "Wrong query implementation"
  end
end

#query_arguments(query_name, argument_types) ⇒ Object



62
63
64
# File 'lib/db2_query/core.rb', line 62

def query_arguments(query_name, argument_types)
  query_arguments_map[query_name] = argument_types
end

#query_arguments_mapObject



58
59
60
# File 'lib/db2_query/core.rb', line 58

def query_arguments_map
  @query_arguments_map ||= {}
end