Class: Queries::Query

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

Direct Known Subclasses

BiologicalRelationship::Autocomplete, CommonName::Filter, Container::Autocomplete, ControlledVocabularyTerm::Autocomplete, Descriptor::Autocomplete, Document::Autocomplete, Georeference::Filter, Namespace::Autocomplete, ObservationMatrixRow::Autocomplete, Otu::Autocomplete, Otu::Filter, AssertedDistribution::Autocomplete, CollectingEvent::Autocomplete, CollectionObject::Autocomplete, DataAttribute::Autocomplete, DataAttribute::ValueAutocomplete, GeographicArea::Autocomplete, Identifier::Autocomplete, Image::Autocomplete, Loan::Autocomplete, Note::Autocomplete, Organization::Autocomplete, Person::Autocomplete, Serial::Autocomplete, Source::Autocomplete, Source::Filter, TaxonDetermination::Filter, TaxonName::Autocomplete, TaxonNameRelationshipsFilterQuery, TypeMaterialAutocompleteQuery, Repository::Autocomplete, User::Autocomplete

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Query) initialize(string, project_id: nil, **keyword_args)

Returns a new instance of Query

Parameters:

  • args (Hash)


33
34
35
36
37
38
# File 'lib/queries/query.rb', line 33

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

Instance Attribute Details

- (Integer) dynamic_limit

Returns:

  • (Integer)


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

def dynamic_limit
  @dynamic_limit
end

- (Object) options

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



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

def options
  @options
end

- (Object) project_id

Returns the value of attribute project_id



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

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



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

def query_string
  @query_string
end

- (Array) terms

Returns:

  • (Array)


60
61
62
# File 'lib/queries/query.rb', line 60

def terms
  @terms
end

Instance Method Details

- (Array) alphabetic_strings

Returns:

  • (Array)


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

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

- (Array) autocomplete

Returns default the autocomplete result to all

Returns:

  • (Array)

    default the autocomplete result to all



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

def autocomplete
  all.to_a
end

- (ActiveRecord::Relation) autocomplete_cached

Returns:

  • (ActiveRecord::Relation)


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

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

- (ActiveRecord::Relation) autocomplete_cached_wildcard_anywhere

Returns removes years/integers!

Returns:

  • (ActiveRecord::Relation)

    removes years/integers!



328
329
330
331
332
# File 'lib/queries/query.rb', line 328

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

- (ActiveRecord::Relation) autocomplete_exact_id

Returns:

  • (ActiveRecord::Relation)


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

def autocomplete_exact_id
  base_query.where(id: query_string).limit(1)
end

- (ActiveRecord::Relation) autocomplete_ordered_wildcard_pieces_in_cached

Returns:

  • (ActiveRecord::Relation)


322
323
324
# File 'lib/queries/query.rb', line 322

def autocomplete_ordered_wildcard_pieces_in_cached
  base_query.where(match_ordered_wildcard_pieces_in_cached.to_sql).limit(5)
end

- (Object) autocomplete_start_date



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

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

- (Array) build_terms

Ultimately we should replace this concept with full text indexing.

Returns:

  • (Array)

    a reasonable (starting) interpretation of any query string



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

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

- (ActiveRecord::Relation?) cached

TODO: rename :cached_matches or similar

Returns:

  • (ActiveRecord::Relation, nil)

    cached matches full query string wildcarded



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

def cached
  if !terms.empty?
    table[:cached].matches_any(terms)
  else
    nil
  end
end

- (Arel::Nodes::Grouping) combine_or_clauses(clauses)

Returns:

  • (Arel::Nodes::Grouping)

Raises:

  • (TaxonWorks::Error)


296
297
298
299
300
301
302
303
304
# File 'lib/queries/query.rb', line 296

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

- (String) end_wildcard

Returns:

  • (String)


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

def end_wildcard
  query_string + '%'
end

- (Arel::Nodes::Matches) exactly_named

Returns:

  • (Arel::Nodes::Matches)


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

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

- (Array) fragments

Used in unordered AND searches

Returns:

  • (Array)

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



120
121
122
123
124
125
126
127
# File 'lib/queries/query.rb', line 120

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

- (Array) integers

Returns of strings representing integers

Returns:

  • (Array)

    of strings representing integers



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

def integers
  return [] if query_string.blank?
  query_string.split(/\s+/).select{|t| Utilities::Strings.is_i?(t)}
end

- (Arel::Nodes::Matches) match_ordered_wildcard_pieces_in_cached

Returns:

  • (Arel::Nodes::Matches)


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

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

- (Arel::Nodes::Matches) match_wildcard_end_in_cached

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

Returns:

  • (Arel::Nodes::Matches)


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

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

- (Arel::Nodes::Matches) match_wildcard_in_cached

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

Returns:

  • (Arel::Nodes::Matches)


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

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

- (Arel::Nodes::Matches) named

Returns:

  • (Arel::Nodes::Matches)


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

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

- (String) no_digits

Returns:

  • (String)


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

def no_digits
  query_string.gsub(/\d/, '').strip
end

- (Query?) only_ids

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.



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

def only_ids
  if only_integers?
    with_id
  else
    nil
  end
end

- (Boolean) only_integers?

Returns true if the query string only contains integers

Returns:

  • (Boolean)

    true if the query string only contains integers



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

def only_integers?
  !(query_string =~ /[^\d\s]/i) && !integers.empty?
end

- (Arel::Nodes::TableAlias) parent

Returns:

  • (Arel::Nodes::TableAlias)


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

def parent
  table.alias
end

- (Scope) parent_child_join

Returns:

  • (Scope)


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

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

- (Arel::Nodes::Grouping) parent_child_where

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

Returns:

  • (Arel::Nodes::Grouping)


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

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

- (Array) pieces

Returns split on whitespace

Returns:

  • (Array)

    split on whitespace



131
132
133
# File 'lib/queries/query.rb', line 131

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

- (Array) result

Returns the results of the query as ActiveRecord objects

Returns:

  • (Array)

    the results of the query as ActiveRecord objects



42
43
44
# File 'lib/queries/query.rb', line 42

def result
  []
end

- (Scope) scope

stub

Returns:

  • (Scope)


48
49
50
# File 'lib/queries/query.rb', line 48

def scope
  where('1 = 2')
end

- (Date.new?) simple_date

Returns:

  • (Date.new, nil)


205
206
207
208
209
210
211
# File 'lib/queries/query.rb', line 205

def simple_date
  begin
    Date.parse(query_string)
  rescue ArgumentError
    return nil
  end
end

- (String) start_and_end_wildcard

Returns:

  • (String)


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

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

- (String) start_wildcard

Returns:

  • (String)


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

def start_wildcard
  '%' + query_string
end

- (String) wildcard_pieces

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%



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

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

- (Array) wildcard_wrapped_integers

Returns:

  • (Array)


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

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

- (Array) wildcard_wrapped_years

Returns:

  • (Array)


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

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

- (Arel::Nodes::Matches) with_cached

Returns:

  • (Arel::Nodes::Matches)


267
268
269
# File 'lib/queries/query.rb', line 267

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

- (Arel::Nodes::Matches) with_cached_like

Returns:

  • (Arel::Nodes::Matches)


272
273
274
# File 'lib/queries/query.rb', line 272

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

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



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

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

- (Arel::Nodes::Equality) with_project_id

TODO: nil/or clause this

Returns:

  • (Arel::Nodes::Equality)


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

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

- (Object) with_start_date



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/queries/query.rb', line 213

def with_start_date
  if d = simple_date
    r = []
    r.push(table[:start_date_day].eq(d.day)) if d.day
    r.push(table[:start_date_month].eq(d.month)) if d.month
    r.push(table[:start_date_year].eq(d.year)) if d.year

    q = r.pop
    r.each do |z|
      q = q.and(z)
    end
    q
  else
    nil
  end
end

- (String?) year_letter

Returns the first letter recognized as coming directly past the first year

`Smith, 1920a. ... ` returns `a`

Returns:

  • (String, nil)

    the first letter recognized as coming directly past the first year

    `Smith, 1920a. ... ` returns `a`


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

def year_letter
  query_string.match(/\d{4}([a-zAZ]+)/).to_a.last
end

- (Array) years

Returns those strings that represent years

Returns:

  • (Array)

    those strings that represent years



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

def years
  query_string.scan(/\d{4}/).to_a.uniq
end