Module: Queryfy

Extended by:
Configuration
Defined in:
lib/queryfy.rb,
lib/queryfy/version.rb

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Methods included from Configuration

configuration, define_setting

Class Method Details

.build_query(klass, querystring, limit = 50, offset = 0) ⇒ Object

Actually builds the query



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/queryfy.rb', line 14

def self.build_query(klass, querystring, limit = 50, offset = 0)
  limit = [max_limit, limit.to_i].min
  offset = offset.to_i
  # Handle empty and nil queries
  if (querystring.nil? || querystring == '')
    return {
      data: klass.limit(limit).offset(offset), 
      count: klass.all.count, 
      limit: limit.to_i, offset: offset.to_i
    }
  end

  begin
    tree = FilterLexer::Parser.parse(querystring)
  rescue FilterLexer::ParseException => e
    raise FilterParseError, "Failed to parse querystring, #{ e.message }"
    return
  end

  # Build the query with pagination
  query = klass.arel_table.project(Arel.star).take(limit).skip(offset)

  cleaned_tree = self.clean_tree(tree)
  arel_tree = self.cleaned_to_arel(klass.arel_table, cleaned_tree)
  # If we want to actually query, add the conditions to query
  query = query.where(arel_tree) unless arel_tree.nil?

  total = 0
  if arel_tree.nil?
    total = klass.all.count
  else
    countquery = klass.arel_table.project(klass.arel_table[klass.primary_key.to_sym].count.as('total')).where(arel_tree)
    results = klass.connection.execute(countquery.to_sql)
    if results.count == 0
      raise QueryfyError, 'Failed to select count, this should not happen'
    else
      total = results[0]['total']
    end
  end

  # Return the results
  return {data: klass.find_by_sql(query.to_sql), count: total.to_i, limit: limit.to_i, offset: offset.to_i}
end

.clean_tree(tree, input = []) ⇒ Object

Cleans the filterlexer tree Output is an array with either filterentries and operators, or an array of filterentries and operators The latter represents a group



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/queryfy.rb', line 61

def self.clean_tree(tree, input = [])
  tree.elements.each do |el|
    if el.is_a?(FilterLexer::Expression)
      input += clean_tree(el)
    elsif el.is_a?(FilterLexer::Group)
      input += [clean_tree(el)]
    else
      input.push(el)
    end
  end
  return input
end

.cleaned_to_arel(arel_table, tree, ast = nil) ⇒ Object

Converts a cleaned tree to something arel can understand



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/queryfy.rb', line 75

def self.cleaned_to_arel(arel_table, tree, ast = nil)
  tree.each_with_index do |el, idx|
    next if el.is_a?(FilterLexer::LogicalOperator)
    operator = nil
    operator = tree[idx - 1] if idx > 0
    if el.is_a?(Array)
      ast = join_ast(ast, arel_table.grouping(cleaned_to_arel(arel_table, el)), operator)
    else
      ast = join_ast(ast, el.to_arel(arel_table), operator)
    end
  end

  return ast
end

.from_queryparams(klass, queryparams) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/queryfy.rb', line 103

def self.from_queryparams(klass, queryparams)
  filter = ''
  offset = 0
  limit = Queryfy::default_limit
  if (queryparams.is_a?(Hash))
    filter = queryparams['filter'] unless queryparams['filter'].nil?
    offset = queryparams['offset'] unless queryparams['offset'].nil?
    limit = queryparams['limit'] unless queryparams['limit'].nil?
  elsif(queryparams.is_a?(String))
    filter = queryparams
  end
  return Queryfy.build_query(klass, filter, limit, offset)
end

.join_ast(ast, nodes, operator) ⇒ Object

Merges an existing ast with the passed nodes and uses the operator as a merge operator



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/queryfy.rb', line 91

def self.join_ast(ast, nodes, operator)
  if ast.nil? && !operator.nil?
    raise InvalidFilterFormat, "Cannot join on nil tree with operator near #{operator.text_value}"
  end
  if operator.nil? || ast.nil?
    ast = nodes
  else
    ast = ast.send(operator.to_arel, nodes)
  end
  return ast
end