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)


98
99
100
# File 'lib/queries/query.rb', line 98

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

#attribute_exact_facet(attribute = nil) ⇒ Object

params attribute [Symbol]

a facet for use when params include, `author`, and `exact_author` combinations
See queries/source/filter.rb for example use


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

def attribute_exact_facet(attribute = nil)
  a = attribute.to_sym
  return nil if send(a).blank?
  if send("exact_#{a}".to_sym)
    table[a].eq(send(a).strip)
  else
    table[a].matches('%' + send(a).strip.gsub(/\s+/, '%') + '%')
  end
end

#autocompleteArray

Returns default the autocomplete result to all TODO: eliminate

Returns:

  • (Array)

    default the autocomplete result to all TODO: eliminate



300
301
302
# File 'lib/queries/query.rb', line 300

def autocomplete
  all.to_a
end

#autocomplete_cachedActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


325
326
327
328
329
330
331
# File 'lib/queries/query.rb', line 325

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!



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

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_common_name_exactObject



357
358
359
360
# File 'lib/queries/query.rb', line 357

def autocomplete_common_name_exact
  return nil if no_terms?
  query_base.joins(:common_names).where(common_name_name.to_sql).limit(1)
end

#autocomplete_common_name_likeObject



362
363
364
365
# File 'lib/queries/query.rb', line 362

def autocomplete_common_name_like
  return nil if no_terms?
  query_base.joins(:common_names).where(common_name_wild_pieces.to_sql).limit(5)
end

#autocomplete_exact_idActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


305
306
307
308
# File 'lib/queries/query.rb', line 305

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)


334
335
336
337
# File 'lib/queries/query.rb', line 334

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)


340
341
342
343
# File 'lib/queries/query.rb', line 340

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)


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

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



78
79
80
# File 'lib/queries/query.rb', line 78

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



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

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)


282
283
284
285
286
287
288
289
290
# File 'lib/queries/query.rb', line 282

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

#common_name_nameObject



349
350
351
# File 'lib/queries/query.rb', line 349

def common_name_name
  common_name_table[:name].eq(query_string)
end

#common_name_tableObject



345
346
347
# File 'lib/queries/query.rb', line 345

def common_name_table
  ::CommonName.arel_table
end

#common_name_wild_piecesObject



353
354
355
# File 'lib/queries/query.rb', line 353

def common_name_wild_pieces
  common_name_table[:name].matches(wildcard_pieces)
end

#end_wildcardString

Returns:

  • (String)


88
89
90
# File 'lib/queries/query.rb', line 88

def end_wildcard
  query_string + '%'
end

#exactly_namedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


225
226
227
# File 'lib/queries/query.rb', line 225

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.



126
127
128
129
130
131
132
133
# File 'lib/queries/query.rb', line 126

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



114
115
116
# File 'lib/queries/query.rb', line 114

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

#match_ordered_wildcard_pieces_in_cachedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


263
264
265
# File 'lib/queries/query.rb', line 263

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)


269
270
271
# File 'lib/queries/query.rb', line 269

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)


275
276
277
278
279
# File 'lib/queries/query.rb', line 275

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)


220
221
222
# File 'lib/queries/query.rb', line 220

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

#no_terms?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/queries/query.rb', line 71

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.



211
212
213
214
215
216
217
# File 'lib/queries/query.rb', line 211

def only_ids
  if only_integers?
    with_id
  else
    nil
  end
end

#only_integers?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/queries/query.rb', line 119

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

#parentArel::Nodes::TableAlias

Returns:

  • (Arel::Nodes::TableAlias)


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

def parent
  table.alias
end

#parent_child_joinScope

Returns:

  • (Scope)


187
188
189
# File 'lib/queries/query.rb', line 187

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)


193
194
195
196
197
# File 'lib/queries/query.rb', line 193

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



138
139
140
# File 'lib/queries/query.rb', line 138

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)


93
94
95
# File 'lib/queries/query.rb', line 93

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

#start_wildcardString

Returns:

  • (String)


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

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%



154
155
156
# File 'lib/queries/query.rb', line 154

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

#wildcard_wrapped_integersArray

Returns:

  • (Array)


143
144
145
# File 'lib/queries/query.rb', line 143

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

#wildcard_wrapped_yearsArray

Returns:

  • (Array)


148
149
150
# File 'lib/queries/query.rb', line 148

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

#with_cachedArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


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

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

#with_cached_likeArel::Nodes::Matches

Returns:

  • (Arel::Nodes::Matches)


258
259
260
# File 'lib/queries/query.rb', line 258

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



201
202
203
204
205
206
207
# File 'lib/queries/query.rb', line 201

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)


236
237
238
239
240
241
242
# File 'lib/queries/query.rb', line 236

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

#year_letterString?

Returns:

  • (String, nil)


108
109
110
# File 'lib/queries/query.rb', line 108

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

#yearsArray

Returns:

  • (Array)


103
104
105
# File 'lib/queries/query.rb', line 103

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