Class: Query

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/query.rb

Overview

Author

Eric Crane (mailto:eric.crane@mac.com)

Copyright

Copyright (c) 2020 Eric Crane. All rights reserved.

A SQL database query. Relies on a database connection object.

Constant Summary collapse

KEYWORD =
'query'.freeze
KEYWORD_SHORT =
'sql'.freeze
DB =
'database'.freeze
SQL =
'sql'.freeze
RESULT =
'result'.freeze
PARAMS =
'params'.freeze
SIMPLE_LIST =
'simple_list'.freeze
DB_MISSING_ERR =
'The database connection is missing!'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the object's documentation data.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/query.rb', line 272

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A SQL Query — a SELECT, INSERT, UPDATE or other ' \
      'SQL statement. The query requires a valid database connection.',
    :children => [
      'database (alias) — Reference to the database we\'re going to query.',
      'sql (string) — The SQL query to execute.',
      'result (container) — Optional. The result of the query will be a container for each row, with an object for each column. If not present, results are displayed in the console instead.',
      'params (container) — Optional list of parameters for the query.'
    ],
    :messages => [
      'run — Run the query and get back the data.'
    ],
    :examples => <<~EXAMPLES.strip
      sqlite [can] :
        on_load [script] : run sqlite.sql
        db [sqlite] :
          database : test.db
        sql [query] :
          database [alias] : sqlite.db
          sql : SELECT id, key, value FROM key_values
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



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

def self.messages
  return super + [ 'run' ]
end

.short_typenameObject

The short name of the object type.



31
32
33
# File 'lib/query.rb', line 31

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


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

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



62
63
64
65
66
67
# File 'lib/query.rb', line 62

def add_default_children
  fac = @engine.factory
  fac.create_alias DB, nil, self
  fac.create_string SQL, nil, self
  fac.create_can RESULT, self
end

#get_result_canObject

Get the result container if it exists.



38
39
40
41
42
# File 'lib/query.rb', line 38

def get_result_can
  result_can = find_child RESULT
  result_can = Gloo::Objs::Alias.resolve_alias( @engine, result_can )
  return result_can
end

#log_query(sql, params) ⇒ Object

Write the query to the log.



126
127
128
129
# File 'lib/query.rb', line 126

def log_query sql, params
  @engine.log.info "SQL PARAMS: #{params}" if params
  @engine.log.info "SQL: #{sql}"
end

#msg_runObject

Run the query and process the results.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/query.rb', line 83

def msg_run
  db = db_obj
  return unless db

  begin
    clear_results

    log_query sql_value, param_array
    result = db.query( sql_value, param_array )
    process_result( result, db )
  rescue => e
    @engine.log_exception e
    return
  end
end

#run_queryObject

Run the query and return the results.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/query.rb', line 102

def run_query
  db = db_obj
  return unless db

  begin
    log_query sql_value, param_array

    db_start = ::Time.now
    result = db.query( sql_value, param_array )
    db_done = ::Time.now
    elapsed = ( ( db_done - db_start ) * 1000.0 ).round(2)

    app = @engine.running_app
    app.add_db_time elapsed if app
    return result
  rescue => e
    @engine.log_exception e
    return
  end
end

#simple_list?Boolean

Should the output be put in a simple list?

Returns:

  • (Boolean)


139
140
141
142
143
144
# File 'lib/query.rb', line 139

def simple_list?
  o = find_child SIMPLE_LIST
  return false unless o

  return o.value
end