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) ⇒ QueryResult

Create the Result object



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

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

Instance Method Details

#has_data_to_show?Boolean

Does this query result have data to show?

Returns:



45
46
47
48
49
50
51
52
# File 'lib/gloo/objs/data/query_result.rb', line 45

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



62
63
64
# File 'lib/gloo/objs/data/query_result.rb', line 62

def show
  single_row_result? ? show_single_row : show_rows
end

#show_rowsObject

Show multiple rows in a table view.



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

def show_rows
  table = TTY::Table.new( @heads, @data )
  renderer = TTY::Table::Renderer::Unicode.new( table, padding: [0,1] )
  puts renderer.render
end

#show_single_rowObject

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/gloo/objs/data/query_result.rb', line 69

def show_single_row
  arr = []
  row = @data[0]
  @heads.each_with_index do |h, i|
    arr << [ h, row[i] ]
  end
  table = TTY::Table.new( [ 'Field', 'Value' ], arr )
  renderer = TTY::Table::Renderer::Unicode.new( table, padding: [0,1] )
  puts renderer.render
end

#single_row_result?Boolean

Does the data contain a single row?

Returns:



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

def single_row_result?
  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_rowsObject

Put all rows in the result object.



116
117
118
119
120
121
122
123
124
# File 'lib/gloo/objs/data/query_result.rb', line 116

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_single_rowObject

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



105
106
107
108
109
110
111
# File 'lib/gloo/objs/data/query_result.rb', line 105

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