Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
771e793
rewrite wip start
eamigo13 Jun 14, 2019
636d96d
Added query string class with ability to modify
eamigo13 Jun 17, 2019
b2a62e9
removed byebug in test
eamigo13 Jun 17, 2019
48ce33e
got query working
eamigo13 Jun 18, 2019
f6a5322
Add pagination
eamigo13 Jun 18, 2019
efc0d96
updated tests
eamigo13 Jun 18, 2019
5735d54
Add sorting
eamigo13 Jun 18, 2019
6353ab7
load associations
eamigo13 Jun 18, 2019
8770702
Removed old files, fixed some tests
eamigo13 Jun 18, 2019
2bf458f
url params helper
eamigo13 Jun 18, 2019
24c4411
query helper concern
eamigo13 Jun 18, 2019
3d614b7
query helper concern
eamigo13 Jun 18, 2019
ee68edc
change to query helper concern
eamigo13 Jun 18, 2019
89f2269
don't lowercase "true" and "false"
eamigo13 Jun 19, 2019
1fcd6fc
account for subqueries when determining insert indexes
eamigo13 Jun 19, 2019
a4bcd7c
add ActiveRecordQuery class
eamigo13 Jun 21, 2019
d67b18f
rename classes
eamigo13 Jun 21, 2019
99ff561
Updates
eamigo13 Jun 21, 2019
a777276
updates to remove comments, improve regex to find keywords, autofind …
eamigo13 Jun 26, 2019
437a723
fixed some tests and fixed some bugs
eamigo13 Jun 27, 2019
ebbcd76
Moved the logic to find default aliases to the column mappings class
eamigo13 Jun 27, 2019
d317207
Readme update
eamigo13 Jun 27, 2019
402af5c
readme update
eamigo13 Jun 27, 2019
fe62a07
readme update
eamigo13 Jun 27, 2019
0c02159
readme update
eamigo13 Jun 27, 2019
ab53fe6
readme update
eamigo13 Jun 27, 2019
f640393
readme update
eamigo13 Jun 27, 2019
5c74b96
readme update
eamigo13 Jun 27, 2019
69354df
readme update
eamigo13 Jun 27, 2019
3f16bf6
readme update
eamigo13 Jun 27, 2019
592d080
readme update
eamigo13 Jun 27, 2019
6a9be71
readme update
eamigo13 Jun 27, 2019
fe03ff3
renamed to QueryHelper
eamigo13 Jun 28, 2019
c01e7a1
SqlManipulator and SqlParser
eamigo13 Jun 29, 2019
6db1e8c
WIP
eamigo13 Jun 30, 2019
a2c5dcd
Use custom errors
eamigo13 Jul 12, 2019
61d76a1
WIP
eamigo13 Jul 15, 2019
18f5c7a
got a fake rails env running in rspec
eamigo13 Jul 15, 2019
e90d90a
WIP
eamigo13 Jul 15, 2019
f5d47e9
wip
eamigo13 Jul 15, 2019
3ee8926
updates
eamigo13 Jul 16, 2019
32f91cb
uncomment some tests
eamigo13 Jul 16, 2019
82caf32
wip
eamigo13 Jul 16, 2019
579efbf
Fix filtering
eamigo13 Jul 19, 2019
c86d306
updates
eamigo13 Jul 19, 2019
ea78a77
add filter method
eamigo13 Jul 23, 2019
d568831
version update
eamigo13 Jul 23, 2019
d7c4d93
remove old specs
eamigo13 Jul 23, 2019
7dbff89
Merge pull request #8 from eamigo13/rename
eamigo13 Jul 23, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in pattern_query_helper.gemspec
# Specify your gem's dependencies in query_helper.gemspec
gemspec
47 changes: 32 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
PATH
remote: .
specs:
pattern_query_helper (0.2.9)
query_helper (0.0.0)
activerecord (~> 5.0)
kaminari (~> 1.1.1)
activesupport (~> 5.0)

GEM
remote: https://rubygems.org/
specs:
actionpack (5.2.3)
actionview (= 5.2.3)
activesupport (= 5.2.3)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.3)
activesupport (= 5.2.3)
builder (~> 3.1)
Expand Down Expand Up @@ -36,30 +43,28 @@ GEM
i18n (>= 0.7)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (0.9.2)
mini_portile2 (2.4.0)
minitest (5.11.3)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
rack (2.0.7)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.3)
actionpack (= 5.2.3)
activesupport (= 5.2.3)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rake (10.5.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
Expand All @@ -73,8 +78,17 @@ GEM
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-rails (3.8.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
sqlite3 (1.3.13)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
Expand All @@ -83,12 +97,15 @@ PLATFORMS
ruby

DEPENDENCIES
actionpack
activesupport
bundler (~> 1.16)
byebug
faker (~> 1.9.3)
pattern_query_helper!
query_helper!
rake (~> 10.0)
rspec (~> 3.0)
rspec-rails
sqlite3 (~> 1.3.6)

BUNDLED WITH
Expand Down
176 changes: 162 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PatternQueryHelper
[![TravisCI](https://travis-ci.org/iserve-products/pattern_query_helper.svg?branch=master)](https://travis-ci.org/iserve-products/pattern_query_helper)
[![Gem Version](https://badge.fury.io/rb/pattern_query_helper.svg)](https://badge.fury.io/rb/pattern_query_helper)
# QueryHelper
[![TravisCI](https://travis-ci.org/iserve-products/query_helper.svg?branch=master)](https://travis-ci.org/iserve-products/query_helper)
[![Gem Version](https://badge.fury.io/rb/query_helper.svg)](https://badge.fury.io/rb/query_helper)

Ruby Gem developed and used at Pattern to paginate, sort, filter, and include associations on sql and active record queries.

Expand All @@ -9,7 +9,7 @@ Ruby Gem developed and used at Pattern to paginate, sort, filter, and include as
Add this line to your application's Gemfile:

```ruby
gem 'pattern_query_helper'
gem 'query_helper'
```

And then execute:
Expand All @@ -18,27 +18,175 @@ And then execute:

Or install it yourself as:

$ gem install pattern_query_helper
$ gem install query_helper

## Use

### SQL Queries

#### Initialize

To create a new sql query object run

```ruby
QueryHelper::Sql.new(
model:, # required
query:, # required
query_params: , # optional
column_mappings: , # optional
filters: , # optional
sorts: , # optional
page: , # optional
per_page: , # optional
single_record: , # optional, default: false
associations: , # optional
as_json_options: , # optional
run: # optional, default: true
)
```

The following arguments are accepted when creating a new objects

<table>
<tr>
<th>Argument</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td>model</td>
<td>the model to run the query against</td>
<td>
<pre lang="ruby">
Parent
</pre>
</td>
</tr>
<tr>
<td>query</td>
<td>the custom sql string to be executed</td>
<td>
<pre lang="ruby">
'select * from parents'
</pre>
</td>
</tr>
<tr>
<td>query_params</td>
<td>a hash of bind variables to be embedded into the sql query</td>
<td>
<pre lang="ruby">
{
age: 20,
name: 'John'
}
</pre>
</td>
</tr>
<tr>
<td>column_mappings</td>
<td>A hash that translates aliases to sql expressions</td>
<td>
<pre lang="ruby">
{
"age" => "parents.age"
"children_count" => {
sql_expression: "count(children.id)",
aggregate: true
}
}
</pre>
</td>
</tr>
<tr>
<td>filters</td>
<td>a list of filters in the form of `{"comparate_alias"=>{"operator_code"=>"value"}}`</td>
<td>
<pre lang="ruby">
{
"age" => { "lt" => 100 },
"children_count" => { "gt" => 0 }
}
</pre>
</td>
</tr>
<tr>
<td>sorts</td>
<td>a comma separated string with a list of sort values</td>
<td>
<pre lang="ruby">
"age:desc,name:asc:lowercase"
</pre>
</td>
</tr>
<tr>
<td>page</td>
<td>the page you want returned</td>
<td>
<pre lang="ruby">
5
</pre>
</td>
</tr>
<tr>
<td>per_page</td>
<td>the number of results per page</td>
<td>
<pre lang="ruby">
20
</pre>
</td>
</tr>
<tr>
<td>single_record</td>
<td>whether or not you expect the record to return a single result, if toggled, only the first result will be returned</td>
<td>
<pre lang="ruby">
false
</pre>
</td>
</tr>
<tr>
<td>associations</td>
<td>a list of activerecord associations you'd like included in the payload </td>
<td>
<pre lang="ruby">

</pre>
</td>
</tr>
<tr>
<td>as_json_options</td>
<td>a list of as_json options you'd like run before returning the payload</td>
<td>
<pre lang="ruby">

</pre>
</td>
</tr>
<tr>
<td>run</td>
<td>whether or not you'd like to run the query on initilization</td>
<td>
<pre lang="ruby">
false
</pre>
</td>
</tr>
</table>

### Active Record Queries

To run an active record query execute
```ruby
PatternQueryHelper.run_active_record_query(active_record_call, query_helpers, valid_columns, single_record)
QueryHelper.run_active_record_query(active_record_call, query_helpers, valid_columns, single_record)
```
active_record_call: Valid active record syntax (i.e. ```Object.where(state: 'Active')```)
query_helpers: See docs below
valid_columns: Default is []. Pass in an array of columns you want to allow sorting and filtering on.
single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects

### Custom SQL Queries

To run a custom sql query execute
```ruby
PatternQueryHelper.run_sql_query(model, query, query_params, query_helpers, valid_columns, single_record)
```
model: A valid ActiveRecord model
query: A string containing your custom SQL query
query_params: a symbolized hash of binds to be included in your SQL query
Expand Down Expand Up @@ -143,7 +291,7 @@ query_helpers = {

## Payload Formats

The PatternQueryHelper gem will return results in one of three formats
The QueryHelper gem will return results in one of three formats

### Paginated List Payload
```json
Expand Down Expand Up @@ -222,12 +370,12 @@ The PatternQueryHelper gem will return results in one of three formats

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pattern_query_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/query_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the PatternQueryHelper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pattern_query_helper/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the QueryHelper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/query_helper/blob/master/CODE_OF_CONDUCT.md).
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "pattern_query_helper"
require "query_helper"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
Loading