Class: Queries::Query

Inherits:
Object
  • Object
show all
Includes:
Arel::Nodes, Concerns::Identifiers
Defined in:
lib/queries/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, project_id: nil, **keyword_args) ⇒ Query

Returns a new instance of Query

Parameters:

  • args (Hash)


35
36
37
38
39
40
# File 'lib/queries/query.rb', line 35

def initialize(string, project_id: nil, **keyword_args)
  @query_string = string
  @options = keyword_args
  @project_id = project_id
  build_terms
end

Instance Attribute Details

#dynamic_limitInteger

Returns:

  • (Integer)


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

def dynamic_limit
  @dynamic_limit
end

#optionsObject

parameters from keyword_args, used to group and pass along things like annotator params



29
30
31
# File 'lib/queries/query.rb', line 29

def options
  @options
end

#project_idObject

Returns the value of attribute project_id



26
27
28
# File 'lib/queries/query.rb', line 26

def project_id
  @project_id
end

#query_stringString?

Returns the initial, unparsed value

Returns:

  • (String, nil)

    the initial, unparsed value



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

def query_string
  @query_string
end

#termsArray

Returns:

  • (Array)


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

def terms
  @terms
end

Instance Method Details

#alphabetic_stringsArray

Returns:

  • (Array)


95
96
97
# File 'lib/queries/query.rb', line 95

def alphabetic_strings
  Utilities::Strings.alphabetic_strings(query_string)
end

#autocompleteArray

Returns default the autocomplete result to all TODO: eliminate

Returns:

  • (Array)

    default the autocomplete result to all TODO: eliminate



284
285
286
# File 'lib/queries/query.rb', line 284

def autocomplete
  all.to_a
end

#autocomplete_cachedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


309
310
311
312
313
314
315
# File 'lib/queries/query.rb', line 309

def autocomplete_cached
  if a = cached
    base_query.where(a.to_sql).limit(20)
  else
    nil
  end
end

#autocomplete_cached_wildcard_anywhereActiveRecord::Relation

Returns removes years/integers!

Returns:

  • (ActiveRecord::Relation)

    removes years/integers!



302
303
304
305
306
# File 'lib/queries/query.rb', line 302

def autocomplete_cached_wildcard_anywhere
  a = match_wildcard_in_cached
  return nil if a.nil?
  base_query.where(a.to_sql).limit(20)
end

#autocomplete_exact_idActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


289
290
291
292
# File 'lib/queries/query.rb', line 289

def autocomplete_exact_id
  return nil if no_terms?
  base_query.where(id: query_string).limit(1)
end

#autocomplete_exactly_namedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


318
319
320
321
# File 'lib/queries/query.rb', line 318

def autocomplete_exactly_named
  return nil if no_terms?
  base_query.where(exactly_named.to_sql).limit(20)
end

#autocomplete_namedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


324
325
326
327
# File 'lib/queries/query.rb', line 324

def autocomplete_named
  return nil if no_terms?
  base_query.where(named.to_sql).limit(5)
end

#autocomplete_ordered_wildcard_pieces_in_cachedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


295
296
297
298
# File 'lib/queries/query.rb', line 295

def autocomplete_ordered_wildcard_pieces_in_cached
  return nil if no_terms?
  base_query.where(match_ordered_wildcard_pieces_in_cached.to_sql).limit(5)
end

#build_termsArray

Ultimately we should replace this concept with full text indexing.

Returns:

  • (Array)

    a reasonable (starting) interpretation of any query string



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

def build_terms
  @terms = @query_string.blank? ? [] : [end_wildcard, start_and_end_wildcard]
end

#cachedActiveRecord::Relation?

TODO: rename :cached_matches or similar

Returns:

  • (ActiveRecord::Relation, nil)

    cached matches full query string wildcarded



231
232
233
234
# File 'lib/queries/query.rb', line 231

def cached
  return nil if no_terms?
  table[:cached].matches_any(terms)
end

#combine_or_clauses(clauses) ⇒ Arel::Nodes::Grouping

Returns:

  • (Arel::Nodes::Grouping)

Raises:

  • (TaxonWorks::Error)


266
267
268
269
270
271
272
273
274
# File 'lib/queries/query.rb', line 266

def combine_or_clauses(clauses)
  clauses.compact!
  raise TaxonWorks::Error, 'combine_or_clauses called without a clause, ensure at least one exists' unless !clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

#end_wildcardString

Returns:

  • (String)


85
86
87
# File 'lib/queries/query.rb', line 85

def end_wildcard
  query_string + '%'
end

#exactly_namedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


209
210
211
# File 'lib/queries/query.rb', line 209

def exactly_named
  table[:name].eq(query_string) if !query_string.blank?
end

#fragmentsArray

Used in unordered AND searches

Returns:

  • (Array)

    if 1-5 alphabetic_strings, those alphabetic_strings wrapped in wildcards, else none.



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

def fragments
  a = alphabetic_strings
  if a.size > 0 && a.size < 6
    a.collect{|a| "%#{a}%"}
  else
    []
  end
end

#integersArray

Returns of strings representing integers

Returns:

  • (Array)

    of strings representing integers



111
112
113
# File 'lib/queries/query.rb', line 111

def integers
  Utilities::Strings.integers(query_string)
end

#match_ordered_wildcard_pieces_in_cachedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


247
248
249
# File 'lib/queries/query.rb', line 247

def match_ordered_wildcard_pieces_in_cached
  table[:cached].matches(wildcard_pieces)
end

#match_wildcard_end_in_cachedArel::Nodes::Matches

match ALL wildcards, but unordered, if 2 - 6 pieces provided

Returns:

  • (Arel::Nodes::Matches)


253
254
255
# File 'lib/queries/query.rb', line 253

def match_wildcard_end_in_cached
  table[:cached].matches(end_wildcard)
end

#match_wildcard_in_cachedArel::Nodes::Matches

match ALL wildcards, but unordered, if 2 - 6 pieces provided

Returns:

  • (Arel::Nodes::Matches)


259
260
261
262
263
# File 'lib/queries/query.rb', line 259

def match_wildcard_in_cached
  b = fragments
  return nil if b.empty?
  table[:cached].matches_all(b)
end

#namedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


204
205
206
# File 'lib/queries/query.rb', line 204

def named
  table[:name].matches_any(terms) if terms.any?
end

#no_terms?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/queries/query.rb', line 68

def no_terms?
  @terms.none?
end

#only_idsQuery?

Returns used in or_clauses, match on id only if integers alone provided.

Returns:

  • (Query, nil)

    used in or_clauses, match on id only if integers alone provided.



195
196
197
198
199
200
201
# File 'lib/queries/query.rb', line 195

def only_ids
  if only_integers?
    with_id
  else
    nil
  end
end

#only_integers?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/queries/query.rb', line 116

def only_integers?
  Utilities::Strings.only_integers?(query_string)
end

#parentArel::Nodes::TableAlias

Returns:

  • (Arel::Nodes::TableAlias)


214
215
216
# File 'lib/queries/query.rb', line 214

def parent
  table.alias
end

#parent_child_joinScope

Returns:

  • (Scope)


171
172
173
# File 'lib/queries/query.rb', line 171

def parent_child_join
  table.join(parent).on(table[:parent_id].eq(parent[:id])).join_sources # !! join_sources ftw
end

#parent_child_whereArel::Nodes::Grouping

Match at two levels, for example, 'wa te“ will match ”Washington Co., Texas“

Returns:

  • (Arel::Nodes::Grouping)


177
178
179
180
181
# File 'lib/queries/query.rb', line 177

def parent_child_where
  a,b = query_string.split(/\s+/, 2)
  return table[:id].eq(-1) if a.nil? || b.nil?
  table[:name].matches("#{a}%").and(parent[:name].matches("#{b}%"))
end

#piecesArray

TODO: used?!

Returns:

  • (Array)

    split on whitespace



135
136
137
# File 'lib/queries/query.rb', line 135

def pieces
  query_string.split(/\s+/)
end

#resultArray

TODO: deprecate? probably unused

Returns:

  • (Array)

    the results of the query as ActiveRecord objects



45
46
47
# File 'lib/queries/query.rb', line 45

def result
  []
end

#scopeScope

stub TODO: deprecate? probably unused

Returns:

  • (Scope)


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

def scope
  where('1 = 2')
end

#start_and_end_wildcardString

Returns:

  • (String)


90
91
92
# File 'lib/queries/query.rb', line 90

def start_and_end_wildcard
  '%' + query_string + '%'
end

#start_wildcardString

Returns:

  • (String)


80
81
82
# File 'lib/queries/query.rb', line 80

def start_wildcard
  '%' + query_string
end

#wildcard_piecesString

Returns if `foo, and 123 and stuff` then %foo%and%123%and%stuff%

Returns:

  • (String)

    if `foo, and 123 and stuff` then %foo%and%123%and%stuff%



151
152
153
# File 'lib/queries/query.rb', line 151

def wildcard_pieces
  '%' + query_string.gsub(/[\W]+/, '%') + '%'
end

#wildcard_wrapped_integersArray

Returns:

  • (Array)


140
141
142
# File 'lib/queries/query.rb', line 140

def wildcard_wrapped_integers
  integers.collect{|i| "%#{i}%"}
end

#wildcard_wrapped_yearsArray

Returns:

  • (Array)


145
146
147
# File 'lib/queries/query.rb', line 145

def wildcard_wrapped_years
  years.collect{|i| "%#{i}%"}
end

#with_cachedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


237
238
239
# File 'lib/queries/query.rb', line 237

def with_cached
  table[:cached].eq(query_string)
end

#with_cached_likeArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


242
243
244
# File 'lib/queries/query.rb', line 242

def with_cached_like
  table[:cached].matches(start_and_end_wildcard)
end

#with_idQuery?

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



185
186
187
188
189
190
191
# File 'lib/queries/query.rb', line 185

def with_id
  if integers.any?
    table[:id].eq_any(integers)
  else
    nil
  end
end

#with_project_idArel::Nodes::Equality

TODO: nil/or clause this

Returns:

  • (Arel::Nodes::Equality)


220
221
222
223
224
225
226
# File 'lib/queries/query.rb', line 220

def with_project_id
  if project_id
    table[:project_id].eq(project_id)
  else
    nil
  end
end

#year_letterString?

Returns:

  • (String, nil)


105
106
107
# File 'lib/queries/query.rb', line 105

def year_letter
  Utilities::Strings.year_letter(query_string)
end

#yearsArray

Returns:

  • (Array)


100
101
102
# File 'lib/queries/query.rb', line 100

def years
  Utilities::Strings.years(query_string)
end