Module: Card::Query::CardQuery::Run

Included in:
Card::Query::CardQuery
Defined in:
lib/card/query/card_query/run.rb

Overview

run CQL queries

Instance Method Summary collapse

Instance Method Details

#alter_result(name) ⇒ Object



97
98
99
100
# File 'lib/card/query/card_query/run.rb', line 97

def alter_result name
  name_parts = [statement[:prepend], name, statement[:append]].compact
  Card::Name[name_parts]
end

#alter_results?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/card/query/card_query/run.rb', line 106

def alter_results?
  statement[:prepend] || statement[:append]
end

#card_result(record, _field) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/card/query/card_query/run.rb', line 67

def card_result record, _field
  if alter_results?
    Card.fetch name_result(record), new: {}
  else
    fetch_or_instantiate record
  end
end

#count_result(results, _field) ⇒ Object



41
42
43
# File 'lib/card/query/card_query/run.rb', line 41

def count_result results, _field
  results.first["count"].to_i
end

#default_result(record, field) ⇒ Object



45
46
47
# File 'lib/card/query/card_query/run.rb', line 45

def default_result record, field
  record[field]
end

#fetch_or_instantiate(record) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/card/query/card_query/run.rb', line 75

def fetch_or_instantiate record
  card = Card.retrieve_from_cache_by_id record["id"]
  unless card
    card = Card.instantiate record
    Card.write_to_cache card
  end
  card.include_set_modules
  card
end

#id_result(record, field) ⇒ Object



49
50
51
# File 'lib/card/query/card_query/run.rb', line 49

def id_result record, field
  record[field].to_i
end

#key_result(record, pattern = "") ⇒ Object



57
58
59
# File 'lib/card/query/card_query/run.rb', line 57

def key_result record, pattern=""
  name_result(record, pattern).to_name.key
end

#large_list(length) ⇒ Object



27
28
29
# File 'lib/card/query/card_query/run.rb', line 27

def large_list length
  Rails.logger.info "#{length} records returned by #{@statement}"
end

#name_result(record, pattern = "") ⇒ Object



61
62
63
64
65
# File 'lib/card/query/card_query/run.rb', line 61

def name_result record, pattern=""
  name = record["name"]&.to_name
  name ||= Card::Lexicon.lex_to_name [record["left_id"], record["right_id"]]
  process_name name, pattern
end

#process_name(name, pattern = "") ⇒ Object



90
91
92
93
94
95
# File 'lib/card/query/card_query/run.rb', line 90

def process_name name, pattern=""
  name = pattern.to_name.absolute(name) if pattern =~ /_\w+/
  return name unless alter_results?

  alter_result name
end

#raw_result(record, _field) ⇒ Object



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

def raw_result record, _field
  record
end

#result_method(retrn) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/card/query/card_query/run.rb', line 31

def result_method retrn
  case
  when respond_to?(:"#{retrn}_result") then :"#{retrn}_result"
  when (retrn =~ /id$/)                then :id_result
  when (retrn =~ /_\w+/)               then :name_result
  when (retrn == "key")                then :key_result
  else                                      :default_result
  end
end

#return_list(sql_results, retrn) ⇒ Array

Returns:

  • (Array)


20
21
22
23
24
25
# File 'lib/card/query/card_query/run.rb', line 20

def return_list sql_results, retrn
  large_list sql_results.length if sql_results.length > 1000
  sql_results.map do |record|
    return_simple record, retrn
  end
end

#return_simple(sql_result, retrn) ⇒ (not an Array)

Returns:

  • ((not an Array))


15
16
17
# File 'lib/card/query/card_query/run.rb', line 15

def return_simple sql_result, retrn
  send result_method(retrn), sql_result, retrn
end

#runArray

run the current query

Returns:

  • (Array)

    of card objects by default



8
9
10
11
12
# File 'lib/card/query/card_query/run.rb', line 8

def run
  retrn = statement[:return].present? ? statement[:return].to_s : "card"
  return_method = :"return_#{simple_result?(retrn) ? :simple : :list}"
  send return_method, run_sql, retrn
end

#run_sqlObject



85
86
87
88
# File 'lib/card/query/card_query/run.rb', line 85

def run_sql
  # puts "\nSQL = #{sql}"
  ActiveRecord::Base.connection.select_all sql
end

#simple_result?(retrn) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/card/query/card_query/run.rb', line 102

def simple_result? retrn
  retrn == "count"
end