simple_query 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +96 -0
- data/lib/simple_query/builder.rb +183 -0
- data/lib/simple_query/version.rb +5 -0
- data/lib/simple_query.rb +16 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6095e96524f3fda796bd8965a24595814091f2e84e74c3ca1789ca644269a22
|
4
|
+
data.tar.gz: b665f9077a68854e225c14188ce83948390d665ef7a6d30c4a04a3f2349d5150
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff9b7c518565311e9a1bbd23b03a5d6ce988ac9ed3bac2ef8f1e16088d846629190818d36163c2a9cfd95b943c734c1b6a3b3ab843413b6acfaf504528388d32
|
7
|
+
data.tar.gz: ce323624e8df7f73cedfea917acedc8cc07c295a10e86b9ecb896c34fa3ca151becfff1357d80d68b388cbe3998522ffe21cb2557d9cdab590d68a8bf435e43d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Alex Kholodniak
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# SimpleQuery
|
2
|
+
|
3
|
+
SimpleQuery is a lightweight and efficient query builder for ActiveRecord, designed to provide a flexible and performant way to construct complex database queries in Ruby on Rails applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_query'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```bash
|
15
|
+
bundle install
|
16
|
+
```
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
```bash
|
20
|
+
gem install simple_query
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
SimpleQuery offers an intuitive interface for building queries with joins, conditions, and aggregations. Here are some examples:
|
26
|
+
|
27
|
+
Basic query
|
28
|
+
```ruby
|
29
|
+
User.simple_query.select(:name, :email).where(active: true).execute
|
30
|
+
```
|
31
|
+
|
32
|
+
Query with join
|
33
|
+
```ruby
|
34
|
+
User.simple_query
|
35
|
+
.select(:name, :email)
|
36
|
+
.join(:users, :companies, foreign_key: :user_id, primary_key: :id)
|
37
|
+
.where(Company.arel_table[:name].eq("TechCorp"))
|
38
|
+
.execute
|
39
|
+
```
|
40
|
+
|
41
|
+
Complex query with multiple joins and conditions
|
42
|
+
```ruby
|
43
|
+
User.simple_query
|
44
|
+
.select(:name)
|
45
|
+
.join(:users, :companies, foreign_key: :user_id, primary_key: :id)
|
46
|
+
.join(:companies, :projects, foreign_key: :company_id, primary_key: :id)
|
47
|
+
.where(Company.arel_table[:industry].eq("Technology"))
|
48
|
+
.where(Project.arel_table[:status].eq("active"))
|
49
|
+
.where(User.arel_table[:admin].eq(true))
|
50
|
+
.execute
|
51
|
+
```
|
52
|
+
|
53
|
+
Lazy execution
|
54
|
+
```ruby
|
55
|
+
User.simple_query
|
56
|
+
.select(:name)
|
57
|
+
.where(active: true)
|
58
|
+
.lazy_execute
|
59
|
+
```
|
60
|
+
|
61
|
+
## Features
|
62
|
+
|
63
|
+
- Efficient query building
|
64
|
+
- Support for complex joins
|
65
|
+
- Lazy execution
|
66
|
+
- DISTINCT queries
|
67
|
+
- Aggregations
|
68
|
+
- LIMIT and OFFSET
|
69
|
+
- ORDER BY clause
|
70
|
+
- Subqueries
|
71
|
+
|
72
|
+
## Performance
|
73
|
+
|
74
|
+
SimpleQuery is designed to potentially outperform standard ActiveRecord queries on large datasets. In our benchmarks with 100,000 records, SimpleQuery showed improved performance compared to equivalent ActiveRecord queries.
|
75
|
+
|
76
|
+
```
|
77
|
+
🚀 Performance Results (100,000 records):
|
78
|
+
ActiveRecord Query: 0.43343 seconds
|
79
|
+
SimpleQuery Execution: 0.06186 seconds
|
80
|
+
```
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kholdrex/simple_query. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kholdrex/simple_query/blob/master/CODE_OF_CONDUCT.md).
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
93
|
+
|
94
|
+
## Code of Conduct
|
95
|
+
|
96
|
+
Everyone interacting in the SimpleQuery project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kholdrex/simple_query/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleQuery
|
4
|
+
class Builder
|
5
|
+
attr_reader :model, :arel_table, :selects, :wheres, :joins, :orders, :limits, :offsets
|
6
|
+
|
7
|
+
def initialize(source)
|
8
|
+
@model = source
|
9
|
+
@arel_table = @model.arel_table
|
10
|
+
@selects = []
|
11
|
+
@wheres = []
|
12
|
+
@joins = []
|
13
|
+
@orders = []
|
14
|
+
@limits = nil
|
15
|
+
@offsets = nil
|
16
|
+
@distinct_flag = false
|
17
|
+
@query_cache = {}
|
18
|
+
@result_struct = nil
|
19
|
+
@query_built = false
|
20
|
+
end
|
21
|
+
|
22
|
+
def select(*fields)
|
23
|
+
@selects.concat(fields.map { |field| parse_select_field(field) })
|
24
|
+
reset_query
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def where(condition)
|
29
|
+
@wheres.concat(parse_where_condition(condition))
|
30
|
+
reset_query
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def join(table1, table2, foreign_key:, primary_key:)
|
35
|
+
@joins << {
|
36
|
+
table1: arel_table(table1),
|
37
|
+
table2: arel_table(table2),
|
38
|
+
foreign_key: foreign_key,
|
39
|
+
primary_key: primary_key
|
40
|
+
}
|
41
|
+
reset_query
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def order(order_conditions)
|
46
|
+
@orders.concat(parse_order_conditions(order_conditions))
|
47
|
+
reset_query
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def limit(number)
|
52
|
+
validate_positive_integer(number, "LIMIT")
|
53
|
+
@limits = number
|
54
|
+
reset_query
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def offset(number)
|
59
|
+
validate_non_negative_integer(number, "OFFSET")
|
60
|
+
@offsets = number
|
61
|
+
reset_query
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def distinct
|
66
|
+
@distinct_flag = true
|
67
|
+
reset_query
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
def execute
|
72
|
+
records = ActiveRecord::Base.connection.select_all(cached_sql)
|
73
|
+
build_result_objects(records)
|
74
|
+
end
|
75
|
+
|
76
|
+
def lazy_execute
|
77
|
+
Enumerator.new do |yielder|
|
78
|
+
records = ActiveRecord::Base.connection.select_all(cached_sql)
|
79
|
+
struct = result_struct(records.columns)
|
80
|
+
records.rows.each { |row| yielder << struct.new(*row) }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def build_query
|
85
|
+
return @query if @query_built
|
86
|
+
|
87
|
+
@query = Arel::SelectManager.new(Arel::Table.engine)
|
88
|
+
@query.from(@arel_table)
|
89
|
+
@query.project(*(@selects.empty? ? [@arel_table[Arel.star]] : @selects))
|
90
|
+
@query.distinct if @distinct_flag
|
91
|
+
|
92
|
+
apply_where_conditions
|
93
|
+
apply_joins
|
94
|
+
apply_order_conditions
|
95
|
+
apply_limit_and_offset
|
96
|
+
|
97
|
+
@query_built = true
|
98
|
+
@query
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def reset_query
|
104
|
+
@query_built = false
|
105
|
+
@query_cache.clear
|
106
|
+
end
|
107
|
+
|
108
|
+
def cached_sql
|
109
|
+
@query_cache[@wheres] ||= build_query.to_sql
|
110
|
+
end
|
111
|
+
|
112
|
+
def result_struct(columns)
|
113
|
+
@result_struct ||= Struct.new(*columns.map(&:to_sym))
|
114
|
+
end
|
115
|
+
|
116
|
+
def parse_select_field(field)
|
117
|
+
case field
|
118
|
+
when Symbol then @arel_table[field]
|
119
|
+
when String then Arel.sql(field)
|
120
|
+
when Arel::Nodes::Node then field
|
121
|
+
else
|
122
|
+
raise ArgumentError, "Unsupported select field type: #{field.class}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def parse_where_condition(condition)
|
127
|
+
case condition
|
128
|
+
when Hash then condition.map { |field, value| @arel_table[field].eq(value) }
|
129
|
+
when Arel::Nodes::Node then [condition]
|
130
|
+
else [Arel.sql(condition.to_s)]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def arel_table(table)
|
135
|
+
table.is_a?(Arel::Table) ? table : Arel::Table.new(table)
|
136
|
+
end
|
137
|
+
|
138
|
+
def parse_order_conditions(order_conditions)
|
139
|
+
order_conditions.map do |field, direction|
|
140
|
+
validate_order_direction(direction)
|
141
|
+
@arel_table[field].send(direction)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def validate_order_direction(direction)
|
146
|
+
return if [:asc, :desc].include?(direction)
|
147
|
+
|
148
|
+
raise ArgumentError, "Invalid order direction: #{direction}. Use :asc or :desc."
|
149
|
+
end
|
150
|
+
|
151
|
+
def validate_positive_integer(number, label)
|
152
|
+
raise ArgumentError, "#{label} must be a positive integer" unless number.is_a?(Integer) && number.positive?
|
153
|
+
end
|
154
|
+
|
155
|
+
def validate_non_negative_integer(number, label)
|
156
|
+
raise ArgumentError, "#{label} must be a non-negative integer" unless number.is_a?(Integer) && number >= 0
|
157
|
+
end
|
158
|
+
|
159
|
+
def build_result_objects(records)
|
160
|
+
struct = result_struct(records.columns)
|
161
|
+
records.rows.map { |row| struct.new(*row) }
|
162
|
+
end
|
163
|
+
|
164
|
+
def apply_where_conditions
|
165
|
+
@query.where(@wheres.inject(&:and)) if @wheres.any?
|
166
|
+
end
|
167
|
+
|
168
|
+
def apply_joins
|
169
|
+
@joins.each do |join|
|
170
|
+
@query.join(join[:table2]).on(join[:table2][join[:foreign_key]].eq(join[:table1][join[:primary_key]]))
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def apply_order_conditions
|
175
|
+
@orders.each { |order| @query.order(order) }
|
176
|
+
end
|
177
|
+
|
178
|
+
def apply_limit_and_offset
|
179
|
+
@query.take(@limits) if @limits
|
180
|
+
@query.skip(@offsets) if @offsets
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
data/lib/simple_query.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require "simple_query/builder"
|
5
|
+
|
6
|
+
module SimpleQuery
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def self.simple_query
|
11
|
+
SimpleQuery::Builder.new(self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.include SimpleQuery
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Kholodniak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-02-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7.1'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '13.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '13.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rubocop
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.21'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.21'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: sqlite3
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 1.5.0
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 1.5.0
|
89
|
+
description: SimpleQuery provides a flexible and performant way to construct complex
|
90
|
+
database queries in Ruby on Rails applications. It offers an intuitive interface
|
91
|
+
for building queries with joins, conditions, and aggregations, while potentially
|
92
|
+
outperforming standard ActiveRecord queries on large datasets.
|
93
|
+
email:
|
94
|
+
- alexandrkholodniak@gmail.com
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files: []
|
98
|
+
files:
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- lib/simple_query.rb
|
102
|
+
- lib/simple_query/builder.rb
|
103
|
+
- lib/simple_query/version.rb
|
104
|
+
homepage: https://oneruby.dev
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
homepage_uri: https://oneruby.dev
|
109
|
+
source_code_uri: https://github.com/kholdrex/simple_query
|
110
|
+
changelog_uri: https://github.com/kholdrex/simple_query/blob/main/CHANGELOG.md
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 2.7.0
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubygems_version: 3.5.9
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: A lightweight and efficient query builder for ActiveRecord.
|
130
|
+
test_files: []
|