active_record_simple_execute 0.9.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/CHANGELOG.md +8 -0
- data/LICENSE +21 -0
- data/README.md +86 -0
- data/Rakefile +21 -0
- data/lib/active_record_simple_execute.rb +34 -0
- data/lib/active_record_simple_execute/version.rb +5 -0
- data/test/dummy_app/Rakefile +7 -0
- data/test/dummy_app/app/assets/config/manifest.js +3 -0
- data/test/dummy_app/app/assets/javascripts/application.js +0 -0
- data/test/dummy_app/app/assets/stylesheets/application.css +3 -0
- data/test/dummy_app/app/controllers/application_controller.rb +3 -0
- data/test/dummy_app/app/models/application_record.rb +3 -0
- data/test/dummy_app/app/models/post.rb +3 -0
- data/test/dummy_app/app/views/layouts/application.html.erb +14 -0
- data/test/dummy_app/config.ru +4 -0
- data/test/dummy_app/config/application.rb +70 -0
- data/test/dummy_app/config/boot.rb +10 -0
- data/test/dummy_app/config/database.yml +20 -0
- data/test/dummy_app/config/environment.rb +5 -0
- data/test/dummy_app/config/environments/development.rb +30 -0
- data/test/dummy_app/config/environments/production.rb +60 -0
- data/test/dummy_app/config/environments/test.rb +41 -0
- data/test/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy_app/config/initializers/inflections.rb +10 -0
- data/test/dummy_app/config/initializers/mime_types.rb +5 -0
- data/test/dummy_app/config/initializers/secret_token.rb +11 -0
- data/test/dummy_app/config/initializers/session_store.rb +8 -0
- data/test/dummy_app/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy_app/config/locales/en.yml +5 -0
- data/test/dummy_app/config/routes.rb +6 -0
- data/test/dummy_app/config/secrets.yml +22 -0
- data/test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb +28 -0
- data/test/dummy_app/db/test +0 -0
- data/test/dummy_app/db/test.sqlite3 +0 -0
- data/test/dummy_app/log/test.log +612 -0
- data/test/test_helper.rb +43 -0
- data/test/unit/active_record_simple_execute_test.rb +63 -0
- metadata +227 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 16dfd3763ff0e3d1608b6d8273f2383095e46eefb1e1b24f53a71045a0774a14
|
4
|
+
data.tar.gz: 4a794790debf2e983e0b3eceebb672d4425afe73eff8087bab7434ea71ed925e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 337341b7d8e9b7cd2e2dd1dc37f36ddf90eaf07032dee701eb3bbd458b9c136d597986bcb05a9906d7fdda62531704f1922be4f30f66b88bee09cc1bd50c771d
|
7
|
+
data.tar.gz: 85f87510db2c678cfd15536eced074212f82d2f0a592905aebb4b9aa02d5dada717a9d6cc0e9208cc76b8565987ea51bf6acf0fc6acd0f86617f47aa3a0ee3e4
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
---------
|
3
|
+
|
4
|
+
- Unreleased - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/v0.9.0...master)
|
5
|
+
* Nothing yet
|
6
|
+
|
7
|
+
- v0.9.0 - May 20, 2021 - [View Diff](https://github.com/westonganger/active_record_simple_execute/compare/1546ce4...v0.9.0)
|
8
|
+
* Initial Release
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Weston Ganger
|
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,86 @@
|
|
1
|
+
# ActiveRecord Simple Execute
|
2
|
+
|
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/Tests/badge.svg" style="max-width:100%;" height='21' style='border:0px;height:21px;' border='0' alt="CI Status"></a>
|
5
|
+
<a href='https://rubygems.org/gems/active_record_simple_execute' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://ruby-gem-downloads-badge.herokuapp.com/active_record_simple_execute?label=rubygems&type=total&total_label=downloads&color=brightgreen' border='0' alt='RubyGems Downloads' /></a>
|
6
|
+
|
7
|
+
Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax.
|
8
|
+
|
9
|
+
## Comparison with Plain ActiveRecord
|
10
|
+
|
11
|
+
As seen here using `simple_execute` is much easier to remember than all the hoops plain ActiveRecord makes you jump through.
|
12
|
+
|
13
|
+
**Using Simple Execute**
|
14
|
+
```ruby
|
15
|
+
records = ActiveRecord::Base.simple_execute(sql_str, company_id: @company.id, @user.id)
|
16
|
+
```
|
17
|
+
|
18
|
+
**Using Plain ActiveRecord Syntax**
|
19
|
+
```ruby
|
20
|
+
### must use send because this method is private is Rails 5.1 only, Public in 5.0 and 5.2
|
21
|
+
sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
|
22
|
+
|
23
|
+
results = ActiveRecord::Base.connection.execute(sanitized_sql)
|
24
|
+
|
25
|
+
if defined?(PG::Result) && results.is_a?(PG::Result)
|
26
|
+
records = results.to_a
|
27
|
+
elsif defined?(Mysql2::Result) && results.is_a?(Mysql2::Result)
|
28
|
+
records = []
|
29
|
+
|
30
|
+
results.each do |row|
|
31
|
+
h = {}
|
32
|
+
|
33
|
+
results.fields.each_with_index do |field,i|
|
34
|
+
h[field] = row[i]
|
35
|
+
end
|
36
|
+
|
37
|
+
records << h
|
38
|
+
end
|
39
|
+
else
|
40
|
+
records = results
|
41
|
+
end
|
42
|
+
|
43
|
+
return records
|
44
|
+
```
|
45
|
+
|
46
|
+
# Installation
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
gem 'active_record_simple_execute'
|
50
|
+
```
|
51
|
+
|
52
|
+
# Usage
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
### Example with plain SQL String
|
56
|
+
sql = <<~SQL.squish
|
57
|
+
SELECT *
|
58
|
+
FROM orders
|
59
|
+
WHERE orders.foo = 'bar'
|
60
|
+
SQL
|
61
|
+
|
62
|
+
results = ActiveRecord::Base.simple_execute(sql_str)
|
63
|
+
|
64
|
+
### Example with ActiveRecord SQL Variables
|
65
|
+
|
66
|
+
sql = <<~SQL.squish
|
67
|
+
SELECT *
|
68
|
+
FROM orders
|
69
|
+
WHERE orders.company_id = :company_id AND orders.updated_by_user_id = :user_id
|
70
|
+
SQL
|
71
|
+
|
72
|
+
results = ActiveRecord::Base.simple_execute(sql_str, company_id: @company.id, @user.id)
|
73
|
+
```
|
74
|
+
|
75
|
+
# Contributing
|
76
|
+
|
77
|
+
We test multiple versions of `Rails` using the `appraisal` gem. Please use the following steps to test using `appraisal`.
|
78
|
+
|
79
|
+
1. `bundle exec appraisal install`
|
80
|
+
2. `bundle exec appraisal rake test`
|
81
|
+
|
82
|
+
For quicker feedback during gem development or debugging feel free to use the provided `rake console` task. It is defined within the [`Rakefile`](./Rakefile).
|
83
|
+
|
84
|
+
# Credits
|
85
|
+
|
86
|
+
Created & Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "lib/active_record_simple_execute/version"
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
task default: [:test]
|
13
|
+
|
14
|
+
task :console do
|
15
|
+
require 'active_record_simple_execute'
|
16
|
+
|
17
|
+
require 'test/dummy_app/app/models/post'
|
18
|
+
|
19
|
+
require 'irb'
|
20
|
+
binding.irb
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "active_record_simple_execute/version"
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
ActiveRecord::Base.class_eval do
|
6
|
+
|
7
|
+
def self.simple_execute(sql_str, **sql_vars)
|
8
|
+
### must use send because this method is private is Rails 5.1 only, Public in 5.0 and 5.2
|
9
|
+
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql_str, **sql_vars])
|
10
|
+
|
11
|
+
results = ActiveRecord::Base.connection.execute(sanitized_sql)
|
12
|
+
|
13
|
+
if defined?(PG::Result) && results.is_a?(PG::Result)
|
14
|
+
records = results.to_a
|
15
|
+
elsif defined?(Mysql2::Result) && results.is_a?(Mysql2::Result)
|
16
|
+
records = []
|
17
|
+
|
18
|
+
results.each do |row|
|
19
|
+
h = {}
|
20
|
+
|
21
|
+
results.fields.each_with_index do |field,i|
|
22
|
+
h[field] = row[i]
|
23
|
+
end
|
24
|
+
|
25
|
+
records << h
|
26
|
+
end
|
27
|
+
else
|
28
|
+
records = results
|
29
|
+
end
|
30
|
+
|
31
|
+
return records
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
File without changes
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'warning'
|
5
|
+
Warning.ignore(
|
6
|
+
%r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings
|
7
|
+
)
|
8
|
+
rescue LoadError
|
9
|
+
# Do nothing
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rails/all'
|
13
|
+
|
14
|
+
Bundler.require
|
15
|
+
|
16
|
+
module Dummy
|
17
|
+
class Application < Rails::Application
|
18
|
+
# Settings in config/environments/* take precedence over those specified here.
|
19
|
+
# Application configuration should go into files in config/initializers
|
20
|
+
# -- all .rb files in that directory are automatically loaded.
|
21
|
+
|
22
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
23
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
24
|
+
|
25
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
26
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
27
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
28
|
+
|
29
|
+
# Activate observers that should always be running.
|
30
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
31
|
+
|
32
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
33
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
34
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
35
|
+
|
36
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
37
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
38
|
+
# config.i18n.default_locale = :de
|
39
|
+
|
40
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
41
|
+
config.encoding = "utf-8"
|
42
|
+
|
43
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
44
|
+
config.filter_parameters += [:password]
|
45
|
+
|
46
|
+
# Enable the asset pipeline
|
47
|
+
config.assets.enabled = true
|
48
|
+
|
49
|
+
config.assets.quiet = true
|
50
|
+
|
51
|
+
# Version of your assets, change this if you want to expire all your assets
|
52
|
+
config.assets.version = '1.0'
|
53
|
+
|
54
|
+
config.generators.test_framework = false
|
55
|
+
config.generators.helper = false
|
56
|
+
config.generators.stylesheets = false
|
57
|
+
config.generators.javascripts = false
|
58
|
+
|
59
|
+
config.after_initialize do
|
60
|
+
ActiveRecord::Migration.migrate(Rails.root.join("db/migrate/*").to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
if ActiveRecord.respond_to?(:gem_version)
|
64
|
+
gem_version = ActiveRecord.gem_version
|
65
|
+
if gem_version.to_s.start_with?("5.2.")
|
66
|
+
config.active_record.sqlite3.represent_boolean_as_integer = true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
default: &default
|
2
|
+
<% if defined?(SQLite3) %>
|
3
|
+
adapter: sqlite3
|
4
|
+
database: db/test.sqlite3
|
5
|
+
|
6
|
+
<% elsif defined?(Mysql2) %>
|
7
|
+
adapter: mysql2
|
8
|
+
database: active_record_simple_execute_test
|
9
|
+
|
10
|
+
<% elsif defined?(PG) %>
|
11
|
+
adapter: postgresql
|
12
|
+
database: active_record_simple_execute_test
|
13
|
+
|
14
|
+
<% end %>
|
15
|
+
|
16
|
+
development:
|
17
|
+
<<: *default
|
18
|
+
|
19
|
+
test:
|
20
|
+
<<: *default
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the web server when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
|
16
|
+
# Don't care if the mailer can't send
|
17
|
+
config.action_mailer.raise_delivery_errors = false
|
18
|
+
|
19
|
+
# Print deprecation notices to the Rails logger
|
20
|
+
config.active_support.deprecation = :log
|
21
|
+
|
22
|
+
# Only use best-standards-support built into browsers
|
23
|
+
config.action_dispatch.best_standards_support = :builtin
|
24
|
+
|
25
|
+
# Do not compress assets
|
26
|
+
config.assets.compress = false
|
27
|
+
|
28
|
+
# Expands the lines which load the assets
|
29
|
+
config.assets.debug = true
|
30
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# Code is not reloaded between requests
|
5
|
+
config.cache_classes = true
|
6
|
+
|
7
|
+
# Full error reports are disabled and caching is turned on
|
8
|
+
config.consider_all_requests_local = false
|
9
|
+
config.action_controller.perform_caching = true
|
10
|
+
|
11
|
+
# Disable Rails's static asset server (Apache or nginx will already do this)
|
12
|
+
config.serve_static_files = false
|
13
|
+
|
14
|
+
# Compress JavaScripts and CSS
|
15
|
+
config.assets.compress = true
|
16
|
+
|
17
|
+
# Don't fallback to assets pipeline if a precompiled asset is missed
|
18
|
+
config.assets.compile = false
|
19
|
+
|
20
|
+
# Generate digests for assets URLs
|
21
|
+
config.assets.digest = true
|
22
|
+
|
23
|
+
# Defaults to Rails.root.join("public/assets")
|
24
|
+
# config.assets.manifest = YOUR_PATH
|
25
|
+
|
26
|
+
# Specifies the header that your server uses for sending files
|
27
|
+
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
|
28
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
|
29
|
+
|
30
|
+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
31
|
+
# config.force_ssl = true
|
32
|
+
|
33
|
+
# See everything in the log (default is :info)
|
34
|
+
# config.log_level = :debug
|
35
|
+
|
36
|
+
# Use a different logger for distributed setups
|
37
|
+
# config.logger = SyslogLogger.new
|
38
|
+
|
39
|
+
# Use a different cache store in production
|
40
|
+
# config.cache_store = :mem_cache_store
|
41
|
+
|
42
|
+
# Enable serving of images, stylesheets, and JavaScripts from an asset server
|
43
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
44
|
+
|
45
|
+
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
|
46
|
+
# config.assets.precompile += %w( search.js )
|
47
|
+
|
48
|
+
# Disable delivery errors, bad email addresses will be ignored
|
49
|
+
# config.action_mailer.raise_delivery_errors = false
|
50
|
+
|
51
|
+
# Enable threaded mode
|
52
|
+
# config.threadsafe!
|
53
|
+
|
54
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
55
|
+
# the I18n.default_locale when a translation can not be found)
|
56
|
+
config.i18n.fallbacks = true
|
57
|
+
|
58
|
+
# Send deprecation notices to registered listeners
|
59
|
+
config.active_support.deprecation = :notify
|
60
|
+
end
|