Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
query_helper (0.2.10)
query_helper (0.0.0)
activerecord (~> 5.0)
activesupport (~> 5.0)

Expand Down
33 changes: 23 additions & 10 deletions lib/query_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class QueryHelper

attr_accessor :model, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query
attr_accessor :model, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload

def initialize(
model: nil, # the model to run the query against
Expand All @@ -26,7 +26,8 @@ def initialize(
single_record: false, # whether or not you expect the record to return a single result, if toggled, only the first result will be returned
associations: nil, # a list of activerecord associations you'd like included in the payload
as_json_options: nil, # a list of as_json options you'd like run before returning the payload
custom_mappings: {} # custom keyword => sql_expression mappings
custom_mappings: {}, # custom keyword => sql_expression mappings
api_payload: false # Return the paginated payload or simply return the result array
)
@model = model
@query = query
Expand All @@ -39,6 +40,7 @@ def initialize(
@associations = associations
@as_json_options = as_json_options
@custom_mappings = custom_mappings
@api_payload = api_payload

if @page && @per_page
# Determine limit and offset
Expand All @@ -50,6 +52,16 @@ def initialize(
end
end

def update_query(query: nil, model:nil, bind_variables: {})
@model = model if model
@query = query if query
@bind_variables.merge!(bind_variables)
end

def add_filter(operator_code:, criterion:, comparate:)
@sql_filter.filter_values["comparate"] = { operator_code => criterion }
end

def execute_query
# Correctly set the query and model based on query type
determine_query_type()
Expand All @@ -60,7 +72,7 @@ def execute_query
@sql_sort.column_maps = column_maps

# create the filters from the column maps
@sql_filter.create_filters
@sql_filter.create_filters()

# merge the filter bind variables into the query bind variables
@bind_variables.merge!(@sql_filter.bind_variables)
Expand All @@ -82,20 +94,21 @@ def execute_query
clean_results()
end

def results
def results()
execute_query()
@results
return paginated_results() if @api_payload
return @results
end

def paginated_results
execute_query()
{ pagination: pagination_results(),
data: @results }
end


private

def paginated_results
{ pagination: pagination_results(),
data: @results }
end

def determine_query_type
# If a custom sql string is passed in, make sure a valid model is passed in as well
if @query.class == String
Expand Down
3 changes: 2 additions & 1 deletion lib/query_helper/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(
@operator_code = operator_code
@criterion = criterion # Converts to a string to be inserted into sql.
@comparate = comparate
@aggregate = aggregate
@bind_variable = ('a'..'z').to_a.shuffle[0,20].join.to_sym

translate_operator_code()
Expand Down Expand Up @@ -105,7 +106,7 @@ def validate_criterion
end

def invalid_criterion_error
raise InvalidQueryError.new("'#{criterion}' is not a valid criterion for the '#{operator}' operator")
raise InvalidQueryError.new("'#{criterion}' is not a valid criterion for the '#{@operator}' operator")
end
end
end
5 changes: 3 additions & 2 deletions lib/query_helper/query_helper_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ def query_helper
end

def create_query_helper
@query_helper = QueryHelper.new(**query_helper_params)
@query_helper = QueryHelper.new(**query_helper_params, api_payload: true)
end

def create_query_helper_filter
QueryHelper::SqlFilter.new(filter_values: params[:filter])
filter_values = params[:filter].permit!.to_h
QueryHelper::SqlFilter.new(filter_values: filter_values)
end

def create_query_helper_sort
Expand Down
18 changes: 6 additions & 12 deletions lib/query_helper/sql_manipulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ def initialize(
include_limit_clause: false
)
@parser = SqlParser.new(sql)
@sql = @parser.sql
@sql = @parser.sql.dup
@where_clauses = where_clauses
@having_clauses = having_clauses
@order_by_clauses = order_by_clauses
@include_limit_clause = include_limit_clause
end

def build
insert_order_by_clause()
insert_limit_clause()
insert_having_clauses()
insert_where_clauses()
insert_total_count_select_clause()
@sql
insert_order_by_and_limit_clause()
@sql.squish
end

private
Expand All @@ -53,16 +52,11 @@ def insert_having_clauses
@sql.insert(@parser.insert_having_index, " #{begin_string} #{filter_string} ")
end

def insert_order_by_clause
return unless @order_by_clauses.length > 0
def insert_order_by_and_limit_clause
@sql.slice!(@parser.limit_clause) if @parser.limit_included? # remove existing limit clause
@sql.slice!(@parser.order_by_clause) if @parser.order_by_included? # remove existing order by clause
@sql.insert(@parser.insert_having_index, " order by #{@order_by_clauses.join(", ")} ")
end

def insert_limit_clause
return unless @include_limit_clause
@sql.insert(@parser.insert_limit_index, " limit :limit offset :offset ")
@sql += " order by #{@order_by_clauses.join(", ")} " if @order_by_clauses.length > 0
@sql += " limit :limit offset :offset " if @include_limit_clause
end
end
end
12 changes: 6 additions & 6 deletions lib/query_helper/sql_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,31 @@ def insert_limit_index
end

def select_clause
@sql[select_index()..insert_select_index()] if select_included?
@sql[select_index()..insert_select_index()].strip if select_included?
end

def from_clause
@sql[from_index()..insert_join_index()] if from_included?
@sql[from_index()..insert_join_index()].strip if from_included?
end

def where_clause
@sql[where_index()..insert_where_index()] if where_included?
@sql[where_index()..insert_where_index()].strip if where_included?
end

# def group_by_clause
# @sql[group_by_index()..insert_group_by_index()] if group_by_included?
# end

def having_clause
@sql[having_index()..insert_having_index()] if having_included?
@sql[having_index()..insert_having_index()].strip if having_included?
end

def order_by_clause
@sql[order_by_index()..insert_order_by_index()] if order_by_included?
@sql[order_by_index()..insert_order_by_index()].strip if order_by_included?
end

def limit_clause
@sql[limit_index()..insert_limit_index()] if limit_included?
@sql[limit_index()..insert_limit_index()].strip if limit_included?
end

def find_aliases
Expand Down
2 changes: 1 addition & 1 deletion lib/query_helper/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class QueryHelper
VERSION = "0.2.10"
VERSION = "0.0.0"
end
6 changes: 3 additions & 3 deletions spec/fixtures/controllers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class ApplicationController < ActionController::API
class ParentsController < ApplicationController
def index
@query_helper.query = Parent.all
render json: @query_helper.paginated_results()
render json: @query_helper.results()
end

def test
test_query = ExampleQueries::SQL_QUERIES[params[:test_number].to_i]
@query_helper.query = test_query[:query]
@query_helper.model = test_query[:model]
results = @query_helper.paginated_results()
results = @query_helper.results()
puts "EXECUTED QUERY: #{@query_helper.executed_query()}"
render json: @query_helper.paginated_results()
render json: @query_helper.results()
end
end
17 changes: 6 additions & 11 deletions spec/fixtures/example_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module ExampleQueries
},
{
alias: "name",
operator_codes: ["in, not_in"],
class: Array
operator_codes: ["in", "notin"],
class: String
},
{
alias: "age",
Expand All @@ -39,20 +39,15 @@ module ExampleQueries
query: %(
select p.name, p.age, count(c.id) as children_count
from parents p join children c on c.parent_id = p.id
where p.age > 35
where p.age > 30
group by p.name
having count(c.id) > 3
order by p.name
limit 1
),
model: Child,
model: Parent,
expected_sorts: ["name", "age", "children_count"],
expected_filters: [
{
alias: "id",
operator_codes: ["gte", "gt", "lte", "lt", "eql", "noteql"],
class: Integer
},
{
alias: "age",
operator_codes: ["gte", "gt", "lte", "lt", "eql", "noteql"],
Expand All @@ -65,8 +60,8 @@ module ExampleQueries
},
{
alias: "name",
operator_codes: ["in, not_in"],
class: Array
operator_codes: ["in", "notin"],
class: String
},
{
alias: "age",
Expand Down
41 changes: 0 additions & 41 deletions spec/query_helper/associations_spec.rb

This file was deleted.

26 changes: 0 additions & 26 deletions spec/query_helper/column_map_spec.rb

This file was deleted.

Loading