sql_query 0.2.1 → 0.7.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 +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +12 -8
- data/Appraisals +14 -8
- data/CHANGELOG.md +24 -1
- data/Gemfile +2 -0
- data/README.md +47 -4
- data/Rakefile +4 -2
- data/examples/README.md +10 -0
- data/examples/distinct_values.sql.erb +14 -0
- data/gemfiles/3.2.gemfile +5 -3
- data/gemfiles/4.0.gemfile +5 -3
- data/gemfiles/4.1.gemfile +5 -3
- data/gemfiles/4.2.gemfile +5 -3
- data/gemfiles/5.0.gemfile +9 -0
- data/lib/sql_query.rb +51 -19
- data/spec/config_spec.rb +15 -0
- data/spec/spec_helper.rb +9 -12
- data/spec/sql_queries/_email_partial.sql.erb +1 -0
- data/spec/sql_queries/get_player_by_email_with_template.sql.erb +3 -0
- data/spec/sql_query_spec.rb +118 -11
- data/sql_query.gemspec +23 -22
- metadata +55 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f152c55f4f62dabfbd3e85d01341cdb6191f6c7
|
4
|
+
data.tar.gz: 5dfa356ad15704877a746d26943580c04abd92c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fac1c583e039c30dc2c2b7a72fdd5014dc4744185cbbc73bb8868d368b2c09606a7c6913db392799c65a435566c10c94f137b2b38e4a8f83caedc56a94a7601
|
7
|
+
data.tar.gz: 2d8488e10a49b8ee0b71325d6dea182e31eca25b058e26916cd53a6bc868f122e9e874a3510c2cc6cbe5fd854960e220ba23a27da82fe34406e5c40b66757772
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
sql_query
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.3
|
data/.travis.yml
CHANGED
@@ -1,24 +1,28 @@
|
|
1
1
|
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
before_install:
|
4
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
5
|
+
- gem install bundler -v '< 2'
|
6
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
7
|
+
- chmod +x ./cc-test-reporter
|
2
8
|
|
3
9
|
gemfile:
|
4
10
|
- gemfiles/3.2.gemfile
|
5
11
|
- gemfiles/4.0.gemfile
|
6
12
|
- gemfiles/4.1.gemfile
|
7
13
|
- gemfiles/4.2.gemfile
|
14
|
+
- gemfiles/5.0.gemfile
|
8
15
|
|
9
16
|
rvm:
|
10
|
-
- '
|
11
|
-
- '2.0.0'
|
12
|
-
- '2.1.5'
|
13
|
-
- '2.2.1'
|
17
|
+
- '2.3.3'
|
14
18
|
|
15
19
|
addons:
|
16
|
-
postgresql: '9.
|
17
|
-
code_climate:
|
18
|
-
repo_token: 743bf63d7a422064551b35931628682c6cad81a30743edee7ff9ea8af061397d
|
20
|
+
postgresql: '9.4'
|
19
21
|
|
20
22
|
env:
|
21
23
|
- BUILDER=travis
|
22
|
-
|
23
24
|
before_script:
|
24
25
|
- psql -c 'create database travis_ci_test;' -U postgres
|
26
|
+
- ./cc-test-reporter before-build
|
27
|
+
after_script:
|
28
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT -r 743bf63d7a422064551b35931628682c6cad81a30743edee7ff9ea8af061397d
|
data/Appraisals
CHANGED
@@ -1,15 +1,21 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
appraise '3.2' do
|
4
|
+
gem 'activerecord', '~> 3.2.21'
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise '4.0' do
|
8
|
+
gem 'activerecord', '~> 4.0.0'
|
3
9
|
end
|
4
10
|
|
5
|
-
appraise
|
6
|
-
gem
|
11
|
+
appraise '4.1' do
|
12
|
+
gem 'activerecord', '~> 4.1.0'
|
7
13
|
end
|
8
14
|
|
9
|
-
appraise
|
10
|
-
gem
|
15
|
+
appraise '4.2' do
|
16
|
+
gem 'activerecord', '~> 4.2.0'
|
11
17
|
end
|
12
18
|
|
13
|
-
appraise
|
14
|
-
gem
|
19
|
+
appraise '5.0' do
|
20
|
+
gem 'activerecord', '~> 5.0.0'
|
15
21
|
end
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SqlQuery change log
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.7.1 / Unreleased
|
4
4
|
|
5
5
|
* [Added]
|
6
6
|
|
@@ -10,6 +10,29 @@
|
|
10
10
|
|
11
11
|
* [Fixed]
|
12
12
|
|
13
|
+
## 0.7.0 / 2020-08-04
|
14
|
+
|
15
|
+
* [Added] support for exec_query from ActiveRecord https://github.com/sufleR/sql_query/pull/7
|
16
|
+
|
17
|
+
|
18
|
+
## 0.6.0 / 2017-03-30
|
19
|
+
|
20
|
+
* [Added] possibility to override path where gem will look for sql file.
|
21
|
+
|
22
|
+
## 0.5.0 / 2016-04-26
|
23
|
+
|
24
|
+
* [Added] possibility to overwrite default connection class and connection directly using db_connection option.
|
25
|
+
|
26
|
+
## 0.4.0 / 2016-01-20
|
27
|
+
|
28
|
+
* [Added] execute will accept boolean attribute.
|
29
|
+
When set to false it will use raw query instead of prepared for logs.
|
30
|
+
By default it will be set to true.
|
31
|
+
|
32
|
+
## 0.3.0 / 2015-12-10
|
33
|
+
|
34
|
+
* [Added] support for partials
|
35
|
+
|
13
36
|
## 0.2.1 / 2015-05-01
|
14
37
|
|
15
38
|
* [Added] support for activerecord >= 3.2
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
[](http://badge.fury.io/rb/sql_query)
|
2
|
-
[](https://gemnasium.com/sufleR/sql_query)
|
3
2
|
[](https://codeclimate.com/github/sufleR/sql_query)
|
4
3
|
[](https://codeclimate.com/github/sufleR/sql_query)
|
5
4
|
[](https://travis-ci.org/sufleR/sql_query)
|
@@ -79,9 +78,15 @@ Example:
|
|
79
78
|
SqlQuery.new('player/get_by_email', email: 'e@mail.dev')
|
80
79
|
```
|
81
80
|
|
81
|
+
#### Special options
|
82
|
+
|
83
|
+
* db_connection - If you want to change default connection to database you may do it for every query execution using this option.
|
84
|
+
* sql_file_path - it will override default path where gem will look for sql file.
|
85
|
+
|
82
86
|
### Methods
|
83
87
|
|
84
|
-
- **execute** - executes query and returns result data.
|
88
|
+
- **execute** - executes query and returns result data. It accepts boolean argument. When argument is false it will run raw sql query instead of prepared_for_logs.
|
89
|
+
- **exec_query** - similar to `#execute` but with data returned via `ActiveRecord::Result`.
|
85
90
|
- **explain** - runs explain for SQL from template
|
86
91
|
- **sql** - returns SQL string
|
87
92
|
- **pretty_sql** - returns SQL string prettier to read in console
|
@@ -89,15 +94,46 @@ SqlQuery.new('player/get_by_email', email: 'e@mail.dev')
|
|
89
94
|
|
90
95
|
### Configuration
|
91
96
|
|
92
|
-
If you don't like default path to your queries you can configure it in initializer.
|
93
|
-
|
94
97
|
```ruby
|
95
98
|
# config/initializers/sql_query.rb
|
96
99
|
SqlQuery.configure do |config|
|
97
100
|
config.path = '/app/sql_templates'
|
101
|
+
config.adapter = ActiveRecord::Base
|
98
102
|
end
|
99
103
|
```
|
100
104
|
|
105
|
+
#### Configuration options
|
106
|
+
* path - If you don't like default path to your queries you can change it here.
|
107
|
+
|
108
|
+
* adapter - class which implements connection method.
|
109
|
+
|
110
|
+
### Partials
|
111
|
+
|
112
|
+
You can prepare part of sql query in partial file and reuse it in multiple queries.
|
113
|
+
|
114
|
+
Partial file should start with '_'.
|
115
|
+
|
116
|
+
Example:
|
117
|
+
|
118
|
+
```sql
|
119
|
+
# app/sql_queries/_email_partial.sql.erb
|
120
|
+
players.email = <%= quote @email %>
|
121
|
+
```
|
122
|
+
|
123
|
+
and use this partial like this:
|
124
|
+
|
125
|
+
```sql
|
126
|
+
SELECT *
|
127
|
+
FROM players
|
128
|
+
WHERE <%= partial :email_partial %>
|
129
|
+
```
|
130
|
+
|
131
|
+
## Examples
|
132
|
+
|
133
|
+
Check examples folder for some usefull queries.
|
134
|
+
|
135
|
+
If you have some examples to share please make pull request.
|
136
|
+
|
101
137
|
## Contributing
|
102
138
|
|
103
139
|
1. Fork it ( https://github.com/[my-github-username]/sql_query/fork )
|
@@ -105,3 +141,10 @@ end
|
|
105
141
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
106
142
|
4. Push to the branch (`git push origin my-new-feature`)
|
107
143
|
5. Create a new Pull Request
|
144
|
+
|
145
|
+
To run specs, setup Postgres with the following:
|
146
|
+
|
147
|
+
```sql
|
148
|
+
CREATE DATABASE sqlquery;
|
149
|
+
CREATE ROLE sqlquery WITH LOGIN;
|
150
|
+
```
|
data/Rakefile
CHANGED
data/examples/README.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
### File list:
|
2
|
+
|
3
|
+
* __distinct_values.sql.erb__ - implements "loose indexscan" in postgresql. More info [https://wiki.postgresql.org/wiki/Loose_indexscan](https://wiki.postgresql.org/wiki/Loose_indexscan)
|
4
|
+
|
5
|
+
```
|
6
|
+
SqlQuery.new(:distinct_values, table_name: :players, column_name: :player_type).execute
|
7
|
+
|
8
|
+
```
|
9
|
+
###
|
10
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
WITH RECURSIVE t AS (
|
2
|
+
SELECT MIN(<%= @column_name %>) AS <%= @column_name %>
|
3
|
+
FROM <%= @table_name %>
|
4
|
+
UNION ALL
|
5
|
+
SELECT (
|
6
|
+
SELECT MIN(<%= @column_name %>) FROM <%= @table_name %>
|
7
|
+
WHERE <%= @column_name %> > t.<%= @column_name %>)
|
8
|
+
FROM t WHERE t.<%= @column_name %> IS NOT NULL
|
9
|
+
)
|
10
|
+
SELECT <%= @column_name %> FROM t WHERE <%= @column_name %> IS NOT NULL
|
11
|
+
<% if @with_nulls %>
|
12
|
+
UNION ALL
|
13
|
+
SELECT NULL WHERE EXISTS(SELECT 1 FROM <%= @table_name %> WHERE <%= @column_name %> IS NULL)
|
14
|
+
<% end %>
|
data/gemfiles/3.2.gemfile
CHANGED
data/gemfiles/4.0.gemfile
CHANGED
data/gemfiles/4.1.gemfile
CHANGED
data/gemfiles/4.2.gemfile
CHANGED
data/lib/sql_query.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'erb'
|
2
4
|
|
3
5
|
class SqlQuery
|
@@ -5,21 +7,30 @@ class SqlQuery
|
|
5
7
|
|
6
8
|
def initialize(file_name, options = {})
|
7
9
|
unless file_name.is_a?(String) || file_name.is_a?(Symbol)
|
8
|
-
|
10
|
+
raise ArgumentError, 'SQL file name should be String or Symbol'
|
9
11
|
end
|
12
|
+
|
10
13
|
@sql_filename = file_name
|
11
|
-
|
12
|
-
@connection =
|
14
|
+
@options = options
|
15
|
+
@connection = options.try(:delete, :db_connection) ||
|
16
|
+
self.class.config.adapter.connection
|
17
|
+
prepare_variables
|
13
18
|
end
|
14
19
|
|
15
20
|
def explain
|
16
|
-
msg = "EXPLAIN for: \n#{
|
21
|
+
msg = "EXPLAIN for: \n#{sql}\n"
|
17
22
|
msg += connection.explain(sql)
|
18
23
|
pretty(msg)
|
19
24
|
end
|
20
25
|
|
21
|
-
def execute
|
22
|
-
|
26
|
+
def execute(prepare = true)
|
27
|
+
to_execute = prepare ? prepared_for_logs : sql
|
28
|
+
connection.execute(to_execute).entries
|
29
|
+
end
|
30
|
+
|
31
|
+
def exec_query(prepare = true)
|
32
|
+
to_execute = prepare ? prepared_for_logs : sql
|
33
|
+
connection.exec_query(to_execute).to_a
|
23
34
|
end
|
24
35
|
|
25
36
|
def sql
|
@@ -35,13 +46,16 @@ class SqlQuery
|
|
35
46
|
end
|
36
47
|
|
37
48
|
def prepared_for_logs
|
38
|
-
sql.gsub(/(\n|\s)+/,' ')
|
49
|
+
sql.gsub(/(\n|\s)+/, ' ')
|
39
50
|
end
|
40
51
|
|
41
|
-
def
|
42
|
-
|
52
|
+
def partial(partial_name, partial_options = {})
|
53
|
+
path, file_name = split_to_path_and_name(partial_name)
|
54
|
+
self.class.new("#{path}/_#{file_name}",
|
55
|
+
@options.merge(partial_options)).sql
|
43
56
|
end
|
44
57
|
|
58
|
+
attr_writer :config
|
45
59
|
def self.config
|
46
60
|
@config ||= Config.new
|
47
61
|
end
|
@@ -51,44 +65,62 @@ class SqlQuery
|
|
51
65
|
end
|
52
66
|
|
53
67
|
class Config
|
54
|
-
attr_accessor :path
|
68
|
+
attr_accessor :path, :adapter
|
55
69
|
|
56
70
|
def initialize
|
57
71
|
@path = '/app/sql_queries'
|
72
|
+
@adapter = ActiveRecord::Base
|
58
73
|
end
|
59
74
|
end
|
60
75
|
|
61
76
|
private
|
62
77
|
|
78
|
+
def split_to_path_and_name(file)
|
79
|
+
if file.is_a?(Symbol)
|
80
|
+
['', file.to_s]
|
81
|
+
else
|
82
|
+
parts = file.rpartition('/')
|
83
|
+
[parts.first, parts.last]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
63
87
|
def pretty(value)
|
64
88
|
# override inspect to be more human readable from console
|
65
89
|
# code copy from ActiveRecord
|
66
90
|
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/explain.rb#L30
|
67
|
-
def value.inspect
|
91
|
+
def value.inspect
|
92
|
+
self
|
93
|
+
end
|
68
94
|
value
|
69
95
|
end
|
70
96
|
|
71
|
-
def prepare_variables
|
72
|
-
options.
|
97
|
+
def prepare_variables
|
98
|
+
return if @options.blank?
|
99
|
+
|
100
|
+
@options.each do |k, v|
|
73
101
|
instance_variable_set("@#{k}", v)
|
74
102
|
end
|
75
103
|
end
|
76
104
|
|
77
105
|
def file_path
|
78
106
|
files = Dir.glob(path)
|
79
|
-
if files.
|
80
|
-
raise "File not found: #{
|
107
|
+
if files.empty?
|
108
|
+
raise "File not found: #{@sql_filename}"
|
81
109
|
elsif files.size > 1
|
82
|
-
raise "More than one file found: #{
|
110
|
+
raise "More than one file found: #{files.join(', ')}"
|
83
111
|
else
|
84
112
|
files.first
|
85
113
|
end
|
86
114
|
end
|
87
115
|
|
88
116
|
def path
|
89
|
-
|
90
|
-
|
91
|
-
|
117
|
+
if @sql_file_path.present?
|
118
|
+
tmp_path = @sql_file_path
|
119
|
+
else
|
120
|
+
root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
|
121
|
+
tmp_path = "#{root}#{self.class.config.path}"
|
122
|
+
end
|
123
|
+
tmp_path += "/#{@sql_filename}.{sql.erb,erb.sql}"
|
92
124
|
tmp_path
|
93
125
|
end
|
94
126
|
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe SqlQuery::Config do
|
6
|
+
describe 'initialize' do
|
7
|
+
it 'sets path to default' do
|
8
|
+
expect(described_class.new.path).to eq('/app/sql_queries')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'sets adapter to ActiveRecord::Base' do
|
12
|
+
expect(described_class.new.adapter).to eq(ActiveRecord::Base)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
SimpleCov.start do
|
7
|
-
add_filter '/spec/'
|
8
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter '/spec/'
|
9
6
|
end
|
10
7
|
|
11
8
|
require 'active_record'
|
12
9
|
require 'sql_query'
|
13
|
-
require 'pry'
|
10
|
+
#require 'pry'
|
14
11
|
|
15
12
|
SqlQuery.configure do |config|
|
16
13
|
config.path = '/spec/sql_queries'
|
@@ -26,15 +23,15 @@ RSpec.configure do |config|
|
|
26
23
|
end
|
27
24
|
|
28
25
|
connection = if ENV['BUILDER'] == 'travis'
|
29
|
-
|
26
|
+
'postgres://postgres@localhost/travis_ci_test'
|
30
27
|
else
|
31
|
-
|
28
|
+
'postgres://sqlquery:sqlquery@localhost/sqlquery'
|
32
29
|
end
|
33
30
|
|
34
31
|
ActiveRecord::Base.establish_connection(connection)
|
35
32
|
|
36
33
|
ActiveRecord::Base.connection.execute(
|
37
|
-
|
34
|
+
'CREATE TABLE IF NOT EXISTS players (email text);'
|
38
35
|
)
|
39
36
|
|
40
37
|
config.order = :random
|
@@ -0,0 +1 @@
|
|
1
|
+
players.email = <%= quote @email %>
|
data/spec/sql_query_spec.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe SqlQuery do
|
4
|
-
|
5
6
|
let(:options) { { email: 'e@mail.dev' } }
|
6
7
|
let(:file_name) { :get_player_by_email }
|
7
8
|
let(:query) { described_class.new(file_name, options) }
|
8
9
|
|
9
|
-
|
10
|
+
class Model < ActiveRecord::Base
|
11
|
+
self.abstract_class = true
|
12
|
+
end
|
10
13
|
|
14
|
+
describe '#initialize' do
|
11
15
|
it 'sets instance variables for all options' do
|
12
16
|
expect(query.instance_variable_get(:@email)).to eq 'e@mail.dev'
|
13
17
|
end
|
@@ -20,11 +24,21 @@ describe SqlQuery do
|
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
23
|
-
context 'file_name argument is not Symbol or String' do
|
27
|
+
context 'when file_name argument is not Symbol or String' do
|
24
28
|
let(:file_name) { { do: 'something' } }
|
25
29
|
|
26
30
|
it 'raises ArgumentError' do
|
27
|
-
expect{ query }
|
31
|
+
expect { query }
|
32
|
+
.to raise_error(ArgumentError,
|
33
|
+
'SQL file name should be String or Symbol')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with db_connection option' do
|
38
|
+
let(:options) { { db_connection: Model.connection } }
|
39
|
+
|
40
|
+
it 'sets connection to requested' do
|
41
|
+
expect(query.connection).to eq(Model.connection)
|
28
42
|
end
|
29
43
|
end
|
30
44
|
end
|
@@ -39,7 +53,8 @@ describe SqlQuery do
|
|
39
53
|
let(:file_name) { :not_exists }
|
40
54
|
|
41
55
|
it 'raises error' do
|
42
|
-
expect{ query.send(:file_path) }
|
56
|
+
expect { query.send(:file_path) }
|
57
|
+
.to raise_error('File not found: not_exists')
|
43
58
|
end
|
44
59
|
end
|
45
60
|
|
@@ -47,14 +62,27 @@ describe SqlQuery do
|
|
47
62
|
let(:file_name) { :duplicated }
|
48
63
|
|
49
64
|
it 'raises error' do
|
50
|
-
expect{ query.send(:file_path) }
|
65
|
+
expect { query.send(:file_path) }
|
66
|
+
.to raise_error.with_message(/More than one file found:/)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#path' do
|
72
|
+
context 'when there is sql_file_path option' do
|
73
|
+
let(:options) { { sql_file_path: '/path_to_file' } }
|
74
|
+
let(:file_name) { 'fname' }
|
75
|
+
|
76
|
+
it 'sets it as dir for file' do
|
77
|
+
expect(query.send(:path)).to eq('/path_to_file/fname.{sql.erb,erb.sql}')
|
51
78
|
end
|
52
79
|
end
|
53
80
|
end
|
54
81
|
|
55
82
|
describe '#sql' do
|
56
83
|
it 'returns query string' do
|
57
|
-
expect(query.sql)
|
84
|
+
expect(query.sql)
|
85
|
+
.to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n")
|
58
86
|
end
|
59
87
|
|
60
88
|
context 'when file is .erb.sql' do
|
@@ -69,7 +97,8 @@ describe SqlQuery do
|
|
69
97
|
|
70
98
|
describe '#pretty_sql' do
|
71
99
|
it 'returns query string' do
|
72
|
-
expect(query.pretty_sql)
|
100
|
+
expect(query.pretty_sql)
|
101
|
+
.to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n")
|
73
102
|
end
|
74
103
|
end
|
75
104
|
|
@@ -77,7 +106,7 @@ describe SqlQuery do
|
|
77
106
|
let(:explain) { query.explain }
|
78
107
|
it 'returns explain string' do
|
79
108
|
expect(explain).to include 'EXPLAIN for:'
|
80
|
-
expect(explain).to include
|
109
|
+
expect(explain).to include 'FROM players'
|
81
110
|
expect(explain).to include "WHERE email = 'e@mail.dev'"
|
82
111
|
expect(explain).to include 'QUERY PLAN'
|
83
112
|
expect(explain).to include 'Seq Scan on players'
|
@@ -93,12 +122,90 @@ describe SqlQuery do
|
|
93
122
|
|
94
123
|
after do
|
95
124
|
ActiveRecord::Base.connection.execute(
|
96
|
-
|
125
|
+
'DELETE FROM players'
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'returns data from database' do
|
130
|
+
expect(query.execute).to eq [{ 'email' => 'e@mail.dev' }]
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when prepare argument is true' do
|
134
|
+
it 'executes prepared query for logs' do
|
135
|
+
expect(query).to receive(:prepared_for_logs) { '' }
|
136
|
+
query.execute
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when prepare argument is false' do
|
141
|
+
it 'executes unchanged sql query' do
|
142
|
+
expect(query).not_to receive(:prepared_for_logs)
|
143
|
+
query.execute(false)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '#exec_query' do
|
149
|
+
before do
|
150
|
+
ActiveRecord::Base.connection.execute(
|
151
|
+
"INSERT INTO players (email) VALUES ('e@mail.dev')"
|
152
|
+
)
|
153
|
+
end
|
154
|
+
|
155
|
+
after do
|
156
|
+
ActiveRecord::Base.connection.execute(
|
157
|
+
'DELETE FROM players'
|
97
158
|
)
|
98
159
|
end
|
99
160
|
|
100
161
|
it 'returns data from database' do
|
101
|
-
expect(query.
|
162
|
+
expect(query.exec_query).to eq [{ 'email' => 'e@mail.dev' }]
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'when prepare argument is true' do
|
166
|
+
it 'executes prepared query for logs' do
|
167
|
+
expect(query).to receive(:prepared_for_logs) { '' }
|
168
|
+
query.exec_query
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'when prepare argument is false' do
|
173
|
+
it 'executes unchanged sql query' do
|
174
|
+
expect(query).not_to receive(:prepared_for_logs)
|
175
|
+
query.exec_query(false)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe '#partial' do
|
181
|
+
let(:file_name) { :get_player_by_email_with_template }
|
182
|
+
it 'resolves partials as parts of sql queries' do
|
183
|
+
expect(query.sql)
|
184
|
+
.to eq("SELECT *\nFROM players\nWHERE players.email = 'e@mail.dev'\n\n")
|
185
|
+
end
|
186
|
+
|
187
|
+
context 'when partial name is string with file path' do
|
188
|
+
let(:file_name) { :get_player_by_email }
|
189
|
+
|
190
|
+
it 'should find file by whole path and _name' do
|
191
|
+
query
|
192
|
+
expect(described_class)
|
193
|
+
.to receive(:new).with('some/path/to/_file.sql', options) { query }
|
194
|
+
query.partial('some/path/to/file.sql', {})
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe '#prepared_for_logs' do
|
200
|
+
it 'returns string without new lines' do
|
201
|
+
expect(query.prepared_for_logs)
|
202
|
+
.to eq("SELECT * FROM players WHERE email = 'e@mail.dev' ")
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '.config' do
|
207
|
+
it 'returns configuration instance' do
|
208
|
+
expect(described_class.config).to be_kind_of(SqlQuery::Config)
|
102
209
|
end
|
103
210
|
end
|
104
211
|
end
|
data/sql_query.gemspec
CHANGED
@@ -1,34 +1,35 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
|
5
6
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
7
|
-
spec.version =
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
-
spec.summary =
|
11
|
-
spec.description =
|
7
|
+
spec.name = 'sql_query'
|
8
|
+
spec.version = '0.7.0'
|
9
|
+
spec.authors = ['sufleR']
|
10
|
+
spec.email = ['szymon.fracczak@gmail.com']
|
11
|
+
spec.summary = 'Ruby gem to load and execute SQL queries from `.sql.erb` templates'
|
12
|
+
spec.description = "
|
12
13
|
It makes working with pure SQL easier with syntax highlighting.
|
13
14
|
Let's you clean your Ruby code from SQL strings.
|
14
|
-
|
15
|
-
spec.homepage =
|
16
|
-
spec.license =
|
15
|
+
"
|
16
|
+
spec.homepage = 'https://github.com/sufleR/sql_query'
|
17
|
+
spec.license = 'MIT'
|
17
18
|
|
18
19
|
spec.files = `git ls-files -z`.split("\x0")
|
19
20
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
21
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
-
spec.require_paths = [
|
22
|
+
spec.require_paths = ['lib']
|
22
23
|
|
23
|
-
spec.required_ruby_version =
|
24
|
+
spec.required_ruby_version = '>= 1.9.3'
|
24
25
|
|
25
|
-
spec.add_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
26
|
+
spec.add_dependency 'activerecord', '>= 3.2', '< 6.0'
|
27
|
+
spec.add_development_dependency 'appraisal'
|
28
|
+
spec.add_development_dependency 'bundler'
|
29
|
+
spec.add_development_dependency 'pg', '~> 0.18'
|
30
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
32
|
+
spec.add_development_dependency 'rubocop', '<= 0.81'
|
33
|
+
spec.add_development_dependency 'simplecov'
|
34
|
+
spec.add_development_dependency 'with_model', '~> 1.2'
|
34
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sufleR
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -17,6 +17,9 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -24,127 +27,132 @@ dependencies:
|
|
24
27
|
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '3.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
34
|
+
name: appraisal
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
|
-
- - "
|
37
|
+
- - ">="
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
39
|
+
version: '0'
|
34
40
|
type: :development
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
|
-
- - "
|
44
|
+
- - ">="
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
46
|
+
version: '0'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
48
|
+
name: bundler
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
|
-
- - "
|
51
|
+
- - ">="
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
53
|
+
version: '0'
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
51
57
|
requirements:
|
52
|
-
- - "
|
58
|
+
- - ">="
|
53
59
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
60
|
+
version: '0'
|
55
61
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
62
|
+
name: pg
|
57
63
|
requirement: !ruby/object:Gem::Requirement
|
58
64
|
requirements:
|
59
65
|
- - "~>"
|
60
66
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
67
|
+
version: '0.18'
|
62
68
|
type: :development
|
63
69
|
prerelease: false
|
64
70
|
version_requirements: !ruby/object:Gem::Requirement
|
65
71
|
requirements:
|
66
72
|
- - "~>"
|
67
73
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
74
|
+
version: '0.18'
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
76
|
+
name: rake
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
72
78
|
requirements:
|
73
79
|
- - "~>"
|
74
80
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
81
|
+
version: '13.0'
|
76
82
|
type: :development
|
77
83
|
prerelease: false
|
78
84
|
version_requirements: !ruby/object:Gem::Requirement
|
79
85
|
requirements:
|
80
86
|
- - "~>"
|
81
87
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
88
|
+
version: '13.0'
|
83
89
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
90
|
+
name: rspec
|
85
91
|
requirement: !ruby/object:Gem::Requirement
|
86
92
|
requirements:
|
87
93
|
- - "~>"
|
88
94
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
95
|
+
version: '3.4'
|
90
96
|
type: :development
|
91
97
|
prerelease: false
|
92
98
|
version_requirements: !ruby/object:Gem::Requirement
|
93
99
|
requirements:
|
94
100
|
- - "~>"
|
95
101
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
102
|
+
version: '3.4'
|
97
103
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
104
|
+
name: rubocop
|
99
105
|
requirement: !ruby/object:Gem::Requirement
|
100
106
|
requirements:
|
101
|
-
- - "
|
107
|
+
- - "<="
|
102
108
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
109
|
+
version: '0.81'
|
104
110
|
type: :development
|
105
111
|
prerelease: false
|
106
112
|
version_requirements: !ruby/object:Gem::Requirement
|
107
113
|
requirements:
|
108
|
-
- - "
|
114
|
+
- - "<="
|
109
115
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
116
|
+
version: '0.81'
|
111
117
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
118
|
+
name: simplecov
|
113
119
|
requirement: !ruby/object:Gem::Requirement
|
114
120
|
requirements:
|
115
|
-
- - "
|
121
|
+
- - ">="
|
116
122
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0
|
123
|
+
version: '0'
|
118
124
|
type: :development
|
119
125
|
prerelease: false
|
120
126
|
version_requirements: !ruby/object:Gem::Requirement
|
121
127
|
requirements:
|
122
|
-
- - "
|
128
|
+
- - ">="
|
123
129
|
- !ruby/object:Gem::Version
|
124
|
-
version: '0
|
130
|
+
version: '0'
|
125
131
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
132
|
+
name: with_model
|
127
133
|
requirement: !ruby/object:Gem::Requirement
|
128
134
|
requirements:
|
129
|
-
- - "
|
135
|
+
- - "~>"
|
130
136
|
- !ruby/object:Gem::Version
|
131
|
-
version: '
|
137
|
+
version: '1.2'
|
132
138
|
type: :development
|
133
139
|
prerelease: false
|
134
140
|
version_requirements: !ruby/object:Gem::Requirement
|
135
141
|
requirements:
|
136
|
-
- - "
|
142
|
+
- - "~>"
|
137
143
|
- !ruby/object:Gem::Version
|
138
|
-
version: '
|
144
|
+
version: '1.2'
|
139
145
|
description: "\n It makes working with pure SQL easier with syntax highlighting.\n
|
140
146
|
\ Let's you clean your Ruby code from SQL strings.\n "
|
141
147
|
email:
|
142
|
-
- szymon.fracczak@
|
148
|
+
- szymon.fracczak@gmail.com
|
143
149
|
executables: []
|
144
150
|
extensions: []
|
145
151
|
extra_rdoc_files: []
|
146
152
|
files:
|
147
153
|
- ".gitignore"
|
154
|
+
- ".ruby-gemset"
|
155
|
+
- ".ruby-version"
|
148
156
|
- ".travis.yml"
|
149
157
|
- Appraisals
|
150
158
|
- CHANGELOG.md
|
@@ -152,16 +160,22 @@ files:
|
|
152
160
|
- LICENSE.txt
|
153
161
|
- README.md
|
154
162
|
- Rakefile
|
163
|
+
- examples/README.md
|
164
|
+
- examples/distinct_values.sql.erb
|
155
165
|
- gemfiles/3.2.gemfile
|
156
166
|
- gemfiles/4.0.gemfile
|
157
167
|
- gemfiles/4.1.gemfile
|
158
168
|
- gemfiles/4.2.gemfile
|
169
|
+
- gemfiles/5.0.gemfile
|
159
170
|
- lib/sql_query.rb
|
171
|
+
- spec/config_spec.rb
|
160
172
|
- spec/spec_helper.rb
|
173
|
+
- spec/sql_queries/_email_partial.sql.erb
|
161
174
|
- spec/sql_queries/duplicated.erb.sql
|
162
175
|
- spec/sql_queries/duplicated.sql.erb
|
163
176
|
- spec/sql_queries/erb_sql.erb.sql
|
164
177
|
- spec/sql_queries/get_player_by_email.sql.erb
|
178
|
+
- spec/sql_queries/get_player_by_email_with_template.sql.erb
|
165
179
|
- spec/sql_query_spec.rb
|
166
180
|
- sql_query.gemspec
|
167
181
|
homepage: https://github.com/sufleR/sql_query
|
@@ -184,14 +198,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
198
|
version: '0'
|
185
199
|
requirements: []
|
186
200
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.
|
201
|
+
rubygems_version: 2.5.2
|
188
202
|
signing_key:
|
189
203
|
specification_version: 4
|
190
204
|
summary: Ruby gem to load and execute SQL queries from `.sql.erb` templates
|
191
205
|
test_files:
|
206
|
+
- spec/config_spec.rb
|
192
207
|
- spec/spec_helper.rb
|
208
|
+
- spec/sql_queries/_email_partial.sql.erb
|
193
209
|
- spec/sql_queries/duplicated.erb.sql
|
194
210
|
- spec/sql_queries/duplicated.sql.erb
|
195
211
|
- spec/sql_queries/erb_sql.erb.sql
|
196
212
|
- spec/sql_queries/get_player_by_email.sql.erb
|
213
|
+
- spec/sql_queries/get_player_by_email_with_template.sql.erb
|
197
214
|
- spec/sql_query_spec.rb
|