active_record_simple_execute 1.0.0 → 1.1.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 +4 -1
- data/README.md +16 -7
- data/lib/active_record_simple_execute/version.rb +1 -1
- data/lib/active_record_simple_execute.rb +15 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2bdba6b0063a89fdb67b412435ac5f1216e9d91bae6409dbc6508e4800fc1a4
|
4
|
+
data.tar.gz: bc6be6a5ce0e62d0f16f3e02f8367de49729c79e9c68e7408ea2e384861bfeca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 385b6d4fbe5a37e64d6f701aa91020f94a283e8445dd40726ac9974d7089705e5f1d56ad7c87b871ab6d46ad5d3ea41815e8854223d90d2228ab0033fb96e049
|
7
|
+
data.tar.gz: 8a9967b9f13f356f529a8dd479abe16d06afc02d66c04ae7fec694ed93518fe1e58789504e5acf9685201742a3138cd7cdfeba34ffb21fadf5b53b81f3199e1c
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
CHANGELOG
|
2
2
|
---------
|
3
3
|
|
4
|
-
- Unreleased - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v1.
|
4
|
+
- Unreleased - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v1.1.0...master)
|
5
5
|
* Nothing yet
|
6
6
|
|
7
|
+
- v1.1.0 - Nov 27, 2024 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v1.0.0...v1.1.0)
|
8
|
+
* [#9](https://github.com/westonganger/active_record_simple_execute/pull/9) - Automatically switch between `select_all` and `exec_query` based on if query is read or write, update readme examples with new knowledge around `Arel.sql` vs `sanitize_sql_array`
|
9
|
+
|
7
10
|
- v1.0.0 - Oct 17, 2024 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v0.9.1...v1.0.0)
|
8
11
|
* [#8](https://github.com/westonganger/active_record_simple_execute/pull/8) - Allow usage with different Active Record database connections
|
9
12
|
* [#7](https://github.com/westonganger/active_record_simple_execute/pull/7) - Drop support for Rails 5.1 and below
|
data/README.md
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax.
|
8
8
|
|
9
|
+
This gem is in response to a lack of proper documentation of best practices within Rails - I've [created a documentation PR](https://github.com/rails/rails/pull/53719) to resolve this but until this this is merged this gem seems necessary for the eco-system.
|
10
|
+
|
9
11
|
# Installation
|
10
12
|
|
11
13
|
```ruby
|
@@ -29,7 +31,7 @@ records = ActiveRecord::Base.connection.simple_execute(sql_str, company_id: @com
|
|
29
31
|
# ActiveRecord::Base.simple_execute(...)
|
30
32
|
```
|
31
33
|
|
32
|
-
### Using original ActiveRecord `exec_query` method
|
34
|
+
### Using original ActiveRecord `select_all` or `exec_query` method
|
33
35
|
```ruby
|
34
36
|
sql_str = <<~SQL.squish
|
35
37
|
SELECT *
|
@@ -37,16 +39,24 @@ sql_str = <<~SQL.squish
|
|
37
39
|
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
38
40
|
SQL
|
39
41
|
|
40
|
-
|
42
|
+
### FOR READ OPERATIONS
|
43
|
+
sanitized_sql = Arel.sql(sql_str, company_id: @company.id, user_id: @user.id)
|
44
|
+
result = ActiveRecord::Base.connection.select_all(sanitized_sql)
|
41
45
|
|
42
|
-
|
46
|
+
### OR FOR WRITE OPERATIONS (you probably shouldnt be doing this anyways)
|
47
|
+
### (while exec_query is capable of read & write operations, recommended only for write operations as it affects the query cache)
|
48
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, {company_id: @company.id, user_id: @user.id}]) # Must use sanitize_sql_array, since Arel.sql is not yet compatible with `exec_query`
|
49
|
+
result = ActiveRecord::Base.connection.exec_query(sanitized_sql) # recommended only for write operations as it affects the query cache
|
43
50
|
|
44
|
-
records = result.to_a
|
51
|
+
records = result.to_a # convert the ActiveRecord::Result object into an array of hashes
|
45
52
|
|
46
53
|
return records
|
47
54
|
```
|
48
55
|
|
49
56
|
### Using original ActiveRecord `execute` method
|
57
|
+
|
58
|
+
It should be noted that it is recommended to avoid all usage of `execute` and to instead use `select_all` or `exec_query` which returns generic ActiveRecord::Result objects
|
59
|
+
|
50
60
|
```ruby
|
51
61
|
sql_str = <<~SQL.squish
|
52
62
|
SELECT *
|
@@ -54,11 +64,10 @@ sql_str = <<~SQL.squish
|
|
54
64
|
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
55
65
|
SQL
|
56
66
|
|
57
|
-
|
67
|
+
# Must use sanitize_sql_array, since Arel.sql is not yet compatible with `execute`
|
68
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, {company_id: @company.id, user_id: @user.id}])
|
58
69
|
|
59
70
|
result = ActiveRecord::Base.connection.execute(sanitized_sql)
|
60
|
-
# OR
|
61
|
-
result = ActiveRecord::Base.connection.exec_query(sanitized_sql)
|
62
71
|
|
63
72
|
if defined?(PG::Result) && result.is_a?(PG::Result)
|
64
73
|
records = result.to_a
|
@@ -6,9 +6,21 @@ ActiveSupport.on_load(:active_record) do
|
|
6
6
|
|
7
7
|
ActiveRecord::ConnectionAdapters::DatabaseStatements.module_eval do
|
8
8
|
def simple_execute(sql_str, **sql_vars)
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
readonly = sql_str.strip.downcase.start_with?("select ")
|
10
|
+
|
11
|
+
if readonly
|
12
|
+
if Rails::VERSION::STRING.to_f >= 7.1
|
13
|
+
sanitized_sql = Arel.sql(sql_str, **sql_vars)
|
14
|
+
else
|
15
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
|
16
|
+
end
|
17
|
+
query_result = select_all(sanitized_sql)
|
18
|
+
else
|
19
|
+
# Must use sanitize_sql_array, since Arel.sql is not yet compatible with `exec_query` or `execute`, https://github.com/rails/rails/pull/53740
|
20
|
+
|
21
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
|
22
|
+
query_result = exec_query(sanitized_sql)
|
23
|
+
end
|
12
24
|
|
13
25
|
records = query_result.to_a
|
14
26
|
|
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: 1.
|
4
|
+
version: 1.1.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: 2024-
|
11
|
+
date: 2024-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|