Class: QueryResult

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

Overview

Author

Eric Crane ([email protected])

Copyright

Copyright © 2022 Eric Crane. All rights reserved.

The result of a SQL database query.

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



21
22
23
24
25
# File 'lib/query_result.rb', line 21

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:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/query_result.rb', line 47

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



64
65
66
# File 'lib/query_result.rb', line 64

def show
  single_row_result? ? show_single_row : show_rows
end

#show_rowsObject

Show multiple rows in a table view.



83
84
85
# File 'lib/query_result.rb', line 83

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

#show_single_rowObject

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



71
72
73
74
75
76
77
78
# File 'lib/query_result.rb', line 71

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:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/query_result.rb', line 36

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.



94
95
96
97
# File 'lib/query_result.rb', line 94

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.



102
103
104
105
# File 'lib/query_result.rb', line 102

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.



123
124
125
126
127
128
129
130
131
# File 'lib/query_result.rb', line 123

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.



136
137
138
139
140
141
142
143
# File 'lib/query_result.rb', line 136

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.



111
112
113
114
115
116
117
118
# File 'lib/query_result.rb', line 111

def update_single_row
  row = @data[0]
  @heads.each_with_index do |h, i|
    child = @result_can.find_child h
    child = Gloo::Objs::Alias.resolve_alias( @engine, child )
    child.set_value row[i] if child
  end
end