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

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

Overview

run CQL queries

Instance Method Summary collapse

Instance Method Details

#alter_result(name) ⇒ Object



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

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

#alter_results?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'card/lib/card/query/card_query/run.rb', line 112

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

#card_result(record, _field) ⇒ Object



73
74
75
76
77
78
79
# File 'card/lib/card/query/card_query/run.rb', line 73

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



47
48
49
# File 'card/lib/card/query/card_query/run.rb', line 47

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

#default_result(record, field) ⇒ Object



51
52
53
# File 'card/lib/card/query/card_query/run.rb', line 51

def default_result record, field
  record[field]
end

#fetch_or_instantiate(record) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'card/lib/card/query/card_query/run.rb', line 81

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



55
56
57
# File 'card/lib/card/query/card_query/run.rb', line 55

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

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



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

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

#large_list(length) ⇒ Object



33
34
35
# File 'card/lib/card/query/card_query/run.rb', line 33

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

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



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

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



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

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



59
60
61
# File 'card/lib/card/query/card_query/run.rb', line 59

def raw_result record, _field
  record
end

#result_method(retrn) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'card/lib/card/query/card_query/run.rb', line 37

def result_method retrn
  case
  when respond_to?(:"#{retrn}_result") then :"#{retrn}_result"
  when retrn.match?(/id$/)             then :id_result
  when retrn.match?(/_\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
26
# File 'card/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
  seed_cache sql_results, retrn
  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 'card/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 'card/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



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

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

#seed_cache(sql_results, retrn) ⇒ Object



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

def seed_cache sql_results, retrn
  id_field = retrn.match?(/id$/) ? retrn : "id"
  Cache.populate_ids(sql_results.map { |record| record[id_field] })
end

#simple_result?(retrn) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'card/lib/card/query/card_query/run.rb', line 108

def simple_result? retrn
  retrn == "count"
end