Class: Gloo::Objs::QueryResult

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/objs/data/query_result.rb

Constant Summary collapse

DB =
'database'.freeze
SQL =
'sql'.freeze
RESULT =
'result'.freeze
PARAMS =
'params'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(heads, data, engine = nil) ⇒ QueryResult

Create the Result object



23
24
25
26
27
# File 'lib/gloo/objs/data/query_result.rb', line 23

def initialize( heads, data, engine=nil )
  @heads = heads
  @data = data
  @engine = engine
end

Instance Method Details

#has_data_to_show?Boolean

Does this query result have data to show?

Returns:



49
50
51
52
53
54
55
56
# File 'lib/gloo/objs/data/query_result.rb', line 49

def has_data_to_show?
  return false unless @heads
  return false unless @data
  return false if @heads.count == 0
  return false if @data.count == 0

  return true
end

#showObject

Show the result of the query



66
67
68
# File 'lib/gloo/objs/data/query_result.rb', line 66

def show
  single_row_result? ? show_single_row : show_rows
end

#show_rowsObject

Show multiple rows in a table view.



85
86
87
# File 'lib/gloo/objs/data/query_result.rb', line 85

def show_rows
  @engine.platform.table.show @heads, @data
end

#show_single_rowObject

Show a single row in a vertical, form style view.



73
74
75
76
77
78
79
80
# File 'lib/gloo/objs/data/query_result.rb', line 73

def show_single_row
  arr = []
  row = @data[0]
  @heads.each_with_index do |h, i|
    arr << [ h, row[i] ]
  end
  @engine.platform.table.show [ 'Field', 'Value' ], arr
end

#single_row_result?Boolean

Does the data contain a single row? OR, if the result is empty, return false.

Returns:



38
39
40
41
42
43
44
# File 'lib/gloo/objs/data/query_result.rb', line 38

def single_row_result?
  if @result_can && ( @result_can.child_count == 0 )
    return false
  end

  return @data.count == 1
end

#update_result_container(in_can) ⇒ Object

Update the result container with the data from the query.



96
97
98
99
# File 'lib/gloo/objs/data/query_result.rb', line 96

def update_result_container( in_can )
  @result_can = in_can
  single_row_result? ? update_single_row : update_rows
end

#update_result_container_simple(in_can) ⇒ Object

Update the result container with the data from the query.



104
105
106
107
# File 'lib/gloo/objs/data/query_result.rb', line 104

def update_result_container_simple( in_can )
  @result_can = in_can
  single_row_result? ? update_single_row : update_rows_simple
end

#update_rowsObject

Put all rows in the result object.



124
125
126
127
128
129
130
131
132
# File 'lib/gloo/objs/data/query_result.rb', line 124

def update_rows
  @data.each_with_index do |row, i|
    can = @result_can.find_add_child( i.to_s, 'can' )
    row.each_with_index do |v, i|
      o = can.find_add_child( @heads[i], 'untyped' )
      o.set_value v
    end
  end
end

#update_rows_simpleObject

Put all rows in the result object.



137
138
139
140
141
142
143
144
# File 'lib/gloo/objs/data/query_result.rb', line 137

def update_rows_simple
  @data.each do |row|
    row.each do |val|
      o = @result_can.find_add_child( val, 'untyped' )
      o.set_value val
    end
  end
end

#update_single_rowObject

The result has a single row. Map values from the result set to objects that are present.



113
114
115
116
117
118
119
# File 'lib/gloo/objs/data/query_result.rb', line 113

def update_single_row
  row = @data[0]
  @heads.each_with_index do |h, i|
    child = @result_can.find_child h
    child.set_value row[i] if child
  end
end