active_record_simple_execute 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -3
- data/README.md +51 -20
- data/Rakefile +0 -9
- data/lib/active_record_simple_execute/version.rb +1 -3
- data/lib/active_record_simple_execute.rb +11 -22
- metadata +6 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a104f89547cb18fd208b3cd22043bfd185cb81109522fc90f9176ff92d341858
|
4
|
+
data.tar.gz: 6aa1b61f1acbabcbebc3deb7190b4fd24a1755774a9c005cc84645511b069e9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da04345171c9cd13bee15de0f766ce34ffc8c8e356949a7bd8ae35a027feae5019c09b857ea441cc8a88fff1a66fe92b4c779b4c7a8a922316eea385afda2e69
|
7
|
+
data.tar.gz: 5f5e6f73d5e51b377e04e5a62bb06173004f33d1a4c3fac0c88f0296b0bb25ed8a23f2b5ecaa1b05df4589b86ce9562911e6935c66fe8b9d15fb21d55d5ee4a5
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
CHANGELOG
|
2
2
|
---------
|
3
3
|
|
4
|
-
- Unreleased - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/
|
4
|
+
- Unreleased - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v1.0.0...master)
|
5
5
|
* Nothing yet
|
6
6
|
|
7
|
-
-
|
8
|
-
* [#
|
7
|
+
- v1.0.0 - Oct 17, 2024 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v0.9.1...v1.0.0)
|
8
|
+
* [#8](https://github.com/westonganger/active_record_simple_execute/pull/8) - Allow usage with different Active Record database connections
|
9
|
+
* [#7](https://github.com/westonganger/active_record_simple_execute/pull/7) - Drop support for Rails 5.1 and below
|
10
|
+
* [#5](https://github.com/westonganger/active_record_simple_execute/pull/5) - Switch from using Active Record `execute` method to `exec_query` method for better memory management
|
11
|
+
|
12
|
+
- v0.9.1 - Feb 13, 2023 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v0.9.0...v0.9.1)
|
13
|
+
* [#1](https://github.com/westonganger/active_record_simple_execute/pull/1) - Utilize active_record lazy loading
|
9
14
|
|
10
15
|
- v0.9.0 - May 20, 2021 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/1546ce4...v0.9.0)
|
11
16
|
* Initial Release
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# ActiveRecord Simple Execute
|
2
2
|
|
3
3
|
<a href="https://badge.fury.io/rb/active_record_simple_execute" target="_blank"><img height="21" style='border:0px;height:21px;' border='0' src="https://badge.fury.io/rb/active_record_simple_execute.svg" alt="Gem Version"></a>
|
4
|
-
<a href='https://github.com/westonganger/active_record_simple_execute/actions' target='_blank'><img src="https://github.com/westonganger/active_record_simple_execute/workflows/
|
4
|
+
<a href='https://github.com/westonganger/active_record_simple_execute/actions' target='_blank'><img src="https://github.com/westonganger/active_record_simple_execute/actions/workflows/test.yml/badge.svg?branch=master" style="max-width:100%;" height='21' style='border:0px;height:21px;' border='0' alt="CI Status"></a>
|
5
5
|
<a href='https://rubygems.org/gems/active_record_simple_execute' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://img.shields.io/gem/dt/active_record_simple_execute?color=brightgreen&label=Rubygems%20Downloads' border='0' alt='RubyGems Downloads' /></a>
|
6
6
|
|
7
7
|
Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax.
|
@@ -9,64 +9,95 @@ Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much
|
|
9
9
|
# Installation
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem
|
12
|
+
gem "active_record_simple_execute"
|
13
13
|
```
|
14
14
|
|
15
15
|
## Comparison with Plain ActiveRecord
|
16
16
|
|
17
17
|
As seen here using `simple_execute` is much easier to remember than all the hoops plain ActiveRecord makes you jump through.
|
18
18
|
|
19
|
-
### Using
|
19
|
+
### Using `simple_execute`
|
20
20
|
```ruby
|
21
21
|
sql_str = <<~SQL.squish
|
22
22
|
SELECT * FROM orders
|
23
|
-
FROM orders
|
23
|
+
FROM orders
|
24
24
|
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
25
25
|
SQL
|
26
26
|
|
27
|
-
records = ActiveRecord::Base.simple_execute(sql_str, company_id: @company.id, user_id: @user.id)
|
27
|
+
records = ActiveRecord::Base.connection.simple_execute(sql_str, company_id: @company.id, user_id: @user.id)
|
28
|
+
# OR use the convenience method excluding the connection portion
|
29
|
+
# ActiveRecord::Base.simple_execute(...)
|
28
30
|
```
|
29
31
|
|
30
|
-
### Using
|
32
|
+
### Using original ActiveRecord `exec_query` method
|
31
33
|
```ruby
|
32
34
|
sql_str = <<~SQL.squish
|
33
|
-
SELECT *
|
34
|
-
FROM orders
|
35
|
+
SELECT *
|
36
|
+
FROM orders
|
35
37
|
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
36
38
|
SQL
|
37
39
|
|
38
|
-
### must use send because this method is private is Rails 5.1 only, Public in 5.0 and 5.2
|
39
40
|
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, company_id: @company.id, user_id: @user.id])
|
40
41
|
|
41
|
-
|
42
|
+
result = ActiveRecord::Base.connection.exec_query(sanitized_sql)
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
records = result.to_a
|
45
|
+
|
46
|
+
return records
|
47
|
+
```
|
48
|
+
|
49
|
+
### Using original ActiveRecord `execute` method
|
50
|
+
```ruby
|
51
|
+
sql_str = <<~SQL.squish
|
52
|
+
SELECT *
|
53
|
+
FROM orders
|
54
|
+
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
55
|
+
SQL
|
56
|
+
|
57
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, company_id: @company.id, user_id: @user.id])
|
58
|
+
|
59
|
+
result = ActiveRecord::Base.connection.execute(sanitized_sql)
|
60
|
+
# OR
|
61
|
+
result = ActiveRecord::Base.connection.exec_query(sanitized_sql)
|
62
|
+
|
63
|
+
if defined?(PG::Result) && result.is_a?(PG::Result)
|
64
|
+
records = result.to_a
|
65
|
+
|
66
|
+
result.clear # to prevent memory leak
|
67
|
+
|
68
|
+
elsif defined?(Mysql2::Result) && result.is_a?(Mysql2::Result)
|
46
69
|
records = []
|
47
70
|
|
48
|
-
|
71
|
+
result.each do |row|
|
49
72
|
h = {}
|
50
73
|
|
51
|
-
|
74
|
+
result.fields.each_with_index do |field,i|
|
52
75
|
h[field] = row[i]
|
53
76
|
end
|
54
77
|
|
55
78
|
records << h
|
56
79
|
end
|
80
|
+
|
57
81
|
else
|
58
|
-
records =
|
82
|
+
records = result
|
59
83
|
end
|
60
84
|
|
61
85
|
return records
|
62
86
|
```
|
63
87
|
|
64
|
-
#
|
88
|
+
# Testing
|
65
89
|
|
66
|
-
|
90
|
+
```
|
91
|
+
bundle exec rake test
|
92
|
+
```
|
93
|
+
|
94
|
+
We can locally test different versions of Rails using `ENV['RAILS_VERSION']`
|
67
95
|
|
68
|
-
|
69
|
-
|
96
|
+
```
|
97
|
+
export RAILS_VERSION=7.0
|
98
|
+
bundle install
|
99
|
+
bundle exec rake test
|
100
|
+
```
|
70
101
|
|
71
102
|
For quicker feedback during gem development or debugging feel free to use the provided `rake console` task. It is defined within the [`Rakefile`](./Rakefile).
|
72
103
|
|
data/Rakefile
CHANGED
@@ -4,33 +4,22 @@ require "active_support/lazy_load_hooks"
|
|
4
4
|
|
5
5
|
ActiveSupport.on_load(:active_record) do
|
6
6
|
|
7
|
-
ActiveRecord::
|
8
|
-
def
|
9
|
-
|
10
|
-
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql_str, **sql_vars])
|
11
|
-
|
12
|
-
results = ActiveRecord::Base.connection.execute(sanitized_sql)
|
13
|
-
|
14
|
-
if defined?(PG::Result) && results.is_a?(PG::Result)
|
15
|
-
records = results.to_a
|
16
|
-
elsif defined?(Mysql2::Result) && results.is_a?(Mysql2::Result)
|
17
|
-
records = []
|
18
|
-
|
19
|
-
results.each do |row|
|
20
|
-
h = {}
|
7
|
+
ActiveRecord::ConnectionAdapters::DatabaseStatements.module_eval do
|
8
|
+
def simple_execute(sql_str, **sql_vars)
|
9
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
|
21
10
|
|
22
|
-
|
23
|
-
h[field] = row[i]
|
24
|
-
end
|
11
|
+
query_result = exec_query(sanitized_sql)
|
25
12
|
|
26
|
-
|
27
|
-
end
|
28
|
-
else
|
29
|
-
records = results
|
30
|
-
end
|
13
|
+
records = query_result.to_a
|
31
14
|
|
32
15
|
return records
|
33
16
|
end
|
34
17
|
end
|
35
18
|
|
19
|
+
ActiveRecord::Base.class_eval do
|
20
|
+
def self.simple_execute(sql_str, **sql_vars)
|
21
|
+
self.connection.simple_execute(sql_str, **sql_vars)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
36
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_simple_execute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,48 +66,6 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: sqlite3
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rails
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: appraisal
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
69
|
- !ruby/object:Gem::Dependency
|
112
70
|
name: warning
|
113
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -150,14 +108,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
150
108
|
requirements:
|
151
109
|
- - ">="
|
152
110
|
- !ruby/object:Gem::Version
|
153
|
-
version:
|
111
|
+
version: '0'
|
154
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
113
|
requirements:
|
156
114
|
- - ">="
|
157
115
|
- !ruby/object:Gem::Version
|
158
116
|
version: '0'
|
159
117
|
requirements: []
|
160
|
-
rubygems_version: 3.
|
118
|
+
rubygems_version: 3.4.22
|
161
119
|
signing_key:
|
162
120
|
specification_version: 4
|
163
121
|
summary: Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with
|