Class: Db2Query::Definitions

Inherits:
Object
  • Object
show all
Defined in:
lib/db2_query/definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_arguments_map, field_types_map) ⇒ Definitions

Returns a new instance of Definitions.



8
9
10
11
12
13
# File 'lib/db2_query/definitions.rb', line 8

def initialize(query_arguments_map, field_types_map)
  @arguments_map = query_arguments_map
  @types_map = field_types_map
  describe
  initialize_types
end

Instance Attribute Details

#arguments_mapObject (readonly)

Returns the value of attribute arguments_map.



6
7
8
# File 'lib/db2_query/definitions.rb', line 6

def arguments_map
  @arguments_map
end

#typesObject

Returns the value of attribute types.



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

def types
  @types
end

#types_mapObject

Returns the value of attribute types_map.



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

def types_map
  @types_map
end

Instance Method Details

#describeObject

Raises:



15
16
17
# File 'lib/db2_query/definitions.rb', line 15

def describe
  raise Db2Query::Error, "Please describe query definitions at #{self.class.name}"
end

#lookup(query_name) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/db2_query/definitions.rb', line 29

def lookup(query_name)
  queries.fetch(query_name)
rescue Exception => e
  if defined?(name)
    raise Db2Query::QueryDefinitionError.new(name, query_name)
  else
    raise Db2Query::Error, e.message
  end
end

#lookup_query(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/db2_query/definitions.rb', line 39

def lookup_query(*args)
  query_name, sql = query_definitions(args)
  lookup(query_name).tap do |query|
    query.define_sql(sql)
    query.argument_keys.each do |key|
      key, key_def = query_arg_key(query, key)
      query.argument_types.store(key, data_type_instance(key_def))
    rescue
      raise Db2Query::QueryArgumentError.new(query_name, key)
    end
  end
end

#queriesObject



19
20
21
# File 'lib/db2_query/definitions.rb', line 19

def queries
  @queries ||= {}
end

#query_definition(query_name) {|definition| ... } ⇒ Object

Yields:

  • (definition)


23
24
25
26
27
# File 'lib/db2_query/definitions.rb', line 23

def query_definition(query_name, &block)
  definition = Query.new(query_name)
  yield definition
  queries[query_name] = definition
end