Class: Db2Query::Query

Inherits:
Object
  • Object
show all
Includes:
SqlStatement
Defined in:
lib/db2_query/query.rb

Defined Under Namespace

Classes: Bind

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlStatement

#db2_spec_sql, #delete_sql?, #insert_sql?, #iud_sql?, #table_name

Constructor Details

#initialize(query_name = nil) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
14
15
# File 'lib/db2_query/query.rb', line 9

def initialize(query_name = nil)
  @columns = {}
  @query_name = query_name
  @sql_statement = nil
  @argument_types = {}
  @types = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
# File 'lib/db2_query/query.rb', line 29

def method_missing(method_name, *args, &block)
  map_column(method_name, args)
end

Instance Attribute Details

#argument_typesObject (readonly)

Returns the value of attribute argument_types.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def argument_types
  @argument_types
end

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def columns
  @columns
end

#keysObject (readonly)

Returns the value of attribute keys.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def keys
  @keys
end

#query_nameObject (readonly)

Returns the value of attribute query_name.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def query_name
  @query_name
end

#sqlObject (readonly)

Returns the value of attribute sql.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def sql
  @sql
end

#typesObject (readonly)

Returns the value of attribute types.



5
6
7
# File 'lib/db2_query/query.rb', line 5

def types
  @types
end

Instance Method Details

#argument_keysObject



46
47
48
49
50
51
# File 'lib/db2_query/query.rb', line 46

def argument_keys
  keys.map do |key|
    arg_key = "#{key}".split(".").last
    arg_key.to_sym unless arg_key.nil?  
  end
end

#argument_type(key) ⇒ Object



40
41
42
43
44
# File 'lib/db2_query/query.rb', line 40

def argument_type(key)
  argument_types.fetch(key) || data_type(key)
rescue
  raise Db2Query::Error, "No argument #{key} type found at query: #{query_name}"
end

#column_idObject



77
78
79
# File 'lib/db2_query/query.rb', line 77

def column_id
  columns.fetch(:id, nil)
end

#data_type(key) ⇒ Object



33
34
35
36
37
38
# File 'lib/db2_query/query.rb', line 33

def data_type(key)
  column = column_from_key(key)
  types.fetch(column.to_sym)
rescue
  raise Db2Query::Error, "No column #{column} found at query: #{query_name} definitions"
end

#define_args(args) ⇒ Object



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

def define_args(args)
  class_eval { attr_accessor "args" }
  send("args=", raw_query_args(args))
end

#define_sql(raw_sql) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/db2_query/query.rb', line 17

def define_sql(raw_sql)
  @keys = []
  @sql = raw_sql.gsub(/:\w+/) do |match|
    @keys << match[1..]
    '?'
  end
end

#exec_query_arguments(args) ⇒ Object



88
89
90
# File 'lib/db2_query/query.rb', line 88

def exec_query_arguments(args)
  [db2_spec_sql, binds(args), validated_args(args)]
end

#lengthObject



53
54
55
# File 'lib/db2_query/query.rb', line 53

def length
  columns.length
end

#map_column(name, args) ⇒ Object



25
26
27
# File 'lib/db2_query/query.rb', line 25

def map_column(name, args)
  @columns[name] = args
end

#raw_query_args(args) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/db2_query/query.rb', line 57

def raw_query_args(args)
  case args
  when Array, Hash
    validated_args(args)
  else
    args
  end
end

#sorted_args(args) ⇒ Object



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

def sorted_args(args)
  argument_keys.map.with_index do |key, index|
    serialized_arg(args.is_a?(Hash) ? args[key] : args[index], key)
  end
end

#validate_result_columns(result_columns) ⇒ Object



81
82
83
84
85
86
# File 'lib/db2_query/query.rb', line 81

def validate_result_columns(result_columns)
  res_cols, def_cols = [result_columns.length, length]
  if res_cols != def_cols
    raise Db2Query::ColumnError.new(def_cols, res_cols)
  end
end

#validate_select_queryObject



92
93
94
95
96
# File 'lib/db2_query/query.rb', line 92

def validate_select_query
  if iud_sql?
    raise Db2Query::Error, "Fetch queries are used for select statement query only."
  end
end