Class: QueryHelper
- Inherits:
-
Object
- Object
- QueryHelper
- Defined in:
- lib/query_helper.rb,
lib/query_helper/filter.rb,
lib/query_helper/version.rb,
lib/query_helper/sql_sort.rb,
lib/query_helper/column_map.rb,
lib/query_helper/sql_filter.rb,
lib/query_helper/sql_parser.rb,
lib/query_helper/associations.rb,
lib/query_helper/sql_manipulator.rb,
lib/query_helper/invalid_query_error.rb,
lib/query_helper/query_helper_concern.rb
Defined Under Namespace
Modules: QueryHelperConcern Classes: Associations, ColumnMap, Filter, InvalidQueryError, SqlFilter, SqlManipulator, SqlParser, SqlSort
Constant Summary collapse
- VERSION =
"0.1.3"
Instance Attribute Summary collapse
-
#api_payload ⇒ Object
Returns the value of attribute api_payload.
-
#as_json_options ⇒ Object
Returns the value of attribute as_json_options.
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#bind_variables ⇒ Object
Returns the value of attribute bind_variables.
-
#executed_query ⇒ Object
Returns the value of attribute executed_query.
-
#model ⇒ Object
Returns the value of attribute model.
-
#page ⇒ Object
Returns the value of attribute page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#preload ⇒ Object
Returns the value of attribute preload.
-
#query ⇒ Object
Returns the value of attribute query.
-
#single_record ⇒ Object
Returns the value of attribute single_record.
-
#sql_filter ⇒ Object
Returns the value of attribute sql_filter.
-
#sql_sort ⇒ Object
Returns the value of attribute sql_sort.
Instance Method Summary collapse
- #add_filter(operator_code:, criterion:, comparate:) ⇒ Object
- #execute_query ⇒ Object
-
#initialize(model: nil, query: nil, bind_variables: {}, sql_filter: SqlFilter.new(), sql_sort: SqlSort.new(), page: nil, per_page: nil, single_record: false, associations: [], as_json_options: nil, custom_mappings: {}, api_payload: false, preload: []) ⇒ QueryHelper
constructor
A new instance of QueryHelper.
- #results ⇒ Object
- #update(query: nil, model: nil, bind_variables: {}, filters: [], associations: [], as_json_options: nil, single_record: nil, custom_mappings: nil, preload: []) ⇒ Object
Constructor Details
#initialize(model: nil, query: nil, bind_variables: {}, sql_filter: SqlFilter.new(), sql_sort: SqlSort.new(), page: nil, per_page: nil, single_record: false, associations: [], as_json_options: nil, custom_mappings: {}, api_payload: false, preload: []) ⇒ QueryHelper
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 |
# File 'lib/query_helper.rb', line 18 def initialize( model: nil, # the model to run the query against query: nil, # a sql string or an active record query bind_variables: {}, # a list of bind variables to be embedded into the query sql_filter: SqlFilter.new(), # a SqlFilter object sql_sort: SqlSort.new(), # a SqlSort object page: nil, # define the page you want returned per_page: nil, # define how many results you want per page 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: [], # 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 api_payload: false, # Return the paginated payload or simply return the result array preload: [] # preload activerecord associations - used instead of `associations` when you don't want them included in the payload ) @model = model @query = query @bind_variables = bind_variables @sql_filter = sql_filter @sql_sort = sql_sort @page = page.to_i if page @per_page = per_page.to_i if per_page @single_record = single_record @associations = associations @as_json_options = @custom_mappings = custom_mappings @api_payload = api_payload @preload = preload if @page && @per_page # Determine limit and offset limit = @per_page offset = (@page - 1) * @per_page # Merge limit/offset variables into bind_variables @bind_variables.merge!({limit: limit, offset: offset}) end end |
Instance Attribute Details
#api_payload ⇒ Object
Returns the value of attribute api_payload.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def api_payload @api_payload end |
#as_json_options ⇒ Object
Returns the value of attribute as_json_options.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def @as_json_options end |
#associations ⇒ Object
Returns the value of attribute associations.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def associations @associations end |
#bind_variables ⇒ Object
Returns the value of attribute bind_variables.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def bind_variables @bind_variables end |
#executed_query ⇒ Object
Returns the value of attribute executed_query.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def executed_query @executed_query end |
#model ⇒ Object
Returns the value of attribute model.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def model @model end |
#page ⇒ Object
Returns the value of attribute page.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def page @page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def per_page @per_page end |
#preload ⇒ Object
Returns the value of attribute preload.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def preload @preload end |
#query ⇒ Object
Returns the value of attribute query.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def query @query end |
#single_record ⇒ Object
Returns the value of attribute single_record.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def single_record @single_record end |
#sql_filter ⇒ Object
Returns the value of attribute sql_filter.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def sql_filter @sql_filter end |
#sql_sort ⇒ Object
Returns the value of attribute sql_sort.
16 17 18 |
# File 'lib/query_helper.rb', line 16 def sql_sort @sql_sort end |
Instance Method Details
#add_filter(operator_code:, criterion:, comparate:) ⇒ Object
79 80 81 |
# File 'lib/query_helper.rb', line 79 def add_filter(operator_code:, criterion:, comparate:) @sql_filter.filter_values["comparate"] = { operator_code => criterion } end |
#execute_query ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/query_helper.rb', line 83 def execute_query # Correctly set the query and model based on query type determine_query_type() # Create column maps to be used by the filter and sort objects column_maps = create_column_maps() @sql_filter.column_maps = column_maps @sql_sort.column_maps = column_maps # create the filters from the column maps @sql_filter.create_filters() # merge the filter bind variables into the query bind variables @bind_variables.merge!(@sql_filter.bind_variables) # Execute Sql Query manipulator = SqlManipulator.new( sql: @query, where_clauses: @sql_filter.where_clauses, having_clauses: @sql_filter.having_clauses, order_by_clauses: @sql_sort.parse_sort_string, include_limit_clause: @page && @per_page ? true : false, additional_select_clauses: @sql_sort.select_strings ) @executed_query = manipulator.build() @results = @model.find_by_sql([@executed_query, @bind_variables]) # Execute Sql Query @results = @results.first if @single_record # Return a single result if requested determine_count() preload_associations() load_associations() clean_results() end |
#results ⇒ Object
118 119 120 121 122 |
# File 'lib/query_helper.rb', line 118 def results() execute_query() return paginated_results() if @api_payload return @results end |
#update(query: nil, model: nil, bind_variables: {}, filters: [], associations: [], as_json_options: nil, single_record: nil, custom_mappings: nil, preload: []) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/query_helper.rb', line 57 def update( query: nil, model: nil, bind_variables: {}, filters: [], associations: [], as_json_options: nil, single_record: nil, custom_mappings: nil, preload: [] ) @model = model if model @query = query if query @bind_variables.merge!(bind_variables) filters.each{ |f| add_filter(**f) } @associations = @associations | associations @single_record = single_record if single_record @as_json_options = if @custom_mappings = custom_mappings if custom_mappings @preload = preload if preload end |