sql_query 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CHANGELOG.md +3 -0
- data/README.md +15 -3
- data/Rakefile +4 -0
- data/lib/sql_query.rb +13 -2
- data/spec/sql_queries/duplicated.erb.sql +1 -0
- data/spec/sql_queries/duplicated.sql.erb +1 -0
- data/spec/sql_queries/erb_sql.erb.sql +1 -0
- data/spec/sql_query_spec.rb +32 -2
- data/sql_query.gemspec +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 009df4df82b72078bd0ac55ecc561782d3e807cd
|
4
|
+
data.tar.gz: 40b8ecd5c19a9a926dc28c97aad2ed46e6c488ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a89bc2585da42c4aa61604186efdbefbff685ce035b9349e87363b338e33b53b15b0d282a230c4512e6344c3662932594a6d95c7a027e318fbc8b8f087d2ee35
|
7
|
+
data.tar.gz: 4f560eeb5d953480a2a6493cba61e1b912dcde3640af6a0c0ce1a477b6905df3588c0f8f7d2fe98b62a837a02ad17a8fd06b98ce014bee8536385e2e9cabf915
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -9,6 +9,7 @@
|
|
9
9
|
Ruby gem to load SQL queries from `.sql.erb` templates using ERB.
|
10
10
|
|
11
11
|
It makes working with pure SQL easier with syntax highlighting.
|
12
|
+
|
12
13
|
Let's you clean your Ruby code from SQL strings.
|
13
14
|
|
14
15
|
## Installation
|
@@ -35,10 +36,11 @@ SELECT *
|
|
35
36
|
FROM players
|
36
37
|
WHERE email = <%= quote @email %>
|
37
38
|
```
|
39
|
+
`quote` method is an alias to `ActiveRecord.connection.quote` method. You can use it to sanitize your variables for SQL.
|
38
40
|
|
39
41
|
You can use SQL like this:
|
40
42
|
|
41
|
-
```
|
43
|
+
```ruby
|
42
44
|
> query = SqlQuery.new(sql_name: :get_player_by_email, email: 'e@mail.dev')
|
43
45
|
|
44
46
|
> query.execute
|
@@ -69,11 +71,21 @@ WHERE email = 'e@mail.dev'
|
|
69
71
|
### Methods
|
70
72
|
|
71
73
|
- **execute** - executes query and returns result data.
|
72
|
-
|
73
74
|
- **explain** - runs explain for SQL from template
|
74
75
|
- **sql** - returns SQL string
|
75
76
|
- **pretty_sql** - returns SQL string prettier to read in console
|
76
|
-
- prepared_for_logs - returns sql string without new lines and multiple whitespaces.
|
77
|
+
- **prepared_for_logs** - returns sql string without new lines and multiple whitespaces.
|
78
|
+
|
79
|
+
### Configuration
|
80
|
+
|
81
|
+
If you don't like default path to your queries you can configure it in initializer.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# config/initializers/sql_query.rb
|
85
|
+
SqlQuery.configure do |config|
|
86
|
+
config.path = '/app/sql_templates'
|
87
|
+
end
|
88
|
+
```
|
77
89
|
|
78
90
|
## Contributing
|
79
91
|
|
data/Rakefile
CHANGED
data/lib/sql_query.rb
CHANGED
@@ -19,7 +19,7 @@ class SqlQuery
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def sql
|
22
|
-
@sql ||= ERB.new(File.read(
|
22
|
+
@sql ||= ERB.new(File.read(file_path)).result(binding)
|
23
23
|
end
|
24
24
|
|
25
25
|
def pretty_sql
|
@@ -70,10 +70,21 @@ class SqlQuery
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
def file_path
|
74
|
+
files = Dir.glob(path)
|
75
|
+
if files.size == 0
|
76
|
+
raise "File not found with name: #{ @sql_name } in #{self.class.config.path}#{ @file_path }"
|
77
|
+
elsif files.size > 1
|
78
|
+
raise "More than one file found: #{ files.join(', ')}"
|
79
|
+
else
|
80
|
+
files.first
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
73
84
|
def path
|
74
85
|
root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
|
75
86
|
tmp_path = "#{ root }#{self.class.config.path}"
|
76
|
-
tmp_path += "#{ @sql_path }/#{ @sql_name }.sql.erb"
|
87
|
+
tmp_path += "#{ @sql_path }/#{ @sql_name }.{sql.erb,erb.sql}"
|
77
88
|
tmp_path
|
78
89
|
end
|
79
90
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
SELECT 1
|
@@ -0,0 +1 @@
|
|
1
|
+
SELECT 2
|
@@ -0,0 +1 @@
|
|
1
|
+
SELECT <%= @fake %>
|
data/spec/sql_query_spec.rb
CHANGED
@@ -2,14 +2,44 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe SqlQuery do
|
4
4
|
|
5
|
-
let(:
|
6
|
-
let(:options) { base_options }
|
5
|
+
let(:options) { { sql_name: :get_player_by_email, email: 'e@mail.dev' } }
|
7
6
|
let(:query) { described_class.new(options) }
|
8
7
|
|
8
|
+
describe '#file_path' do
|
9
|
+
context 'when there is only one file with name' do
|
10
|
+
it 'returns path to it' do
|
11
|
+
expect(query.send(:file_path)).to include('get_player_by_email.sql.erb')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
context 'when there are no files' do
|
15
|
+
let(:options) { { sql_name: :not_exists } }
|
16
|
+
|
17
|
+
it 'raises error' do
|
18
|
+
expect{ query.send(:file_path) }.to raise_error('File not found with name: not_exists in /spec/sql_queries')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when there are more than one matching files' do
|
23
|
+
let(:options) { { sql_name: :duplicated } }
|
24
|
+
|
25
|
+
it 'raises error' do
|
26
|
+
expect{ query.send(:file_path) }.to raise_error.with_message(/More than one file found:/)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
9
31
|
describe '#sql' do
|
10
32
|
it 'returns query string' do
|
11
33
|
expect(query.sql).to eq "SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n"
|
12
34
|
end
|
35
|
+
|
36
|
+
context 'when file is .erb.sql' do
|
37
|
+
let(:options) { { sql_name: :erb_sql, fake: 12 } }
|
38
|
+
|
39
|
+
it 'returns query string' do
|
40
|
+
expect(query.sql).to eq "SELECT 12\n"
|
41
|
+
end
|
42
|
+
end
|
13
43
|
end
|
14
44
|
|
15
45
|
describe '#pretty_sql' do
|
data/sql_query.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "sql_query"
|
7
|
-
spec.version = "0.0
|
7
|
+
spec.version = "0.1.0"
|
8
8
|
spec.authors = ["sufleR"]
|
9
9
|
spec.email = ["szymon.fracczak@netguru.co"]
|
10
10
|
spec.summary = %q{Ruby gem to load and execute SQL queries from `.sql.erb` templates}
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sufleR
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -139,6 +139,9 @@ files:
|
|
139
139
|
- Rakefile
|
140
140
|
- lib/sql_query.rb
|
141
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/sql_queries/duplicated.erb.sql
|
143
|
+
- spec/sql_queries/duplicated.sql.erb
|
144
|
+
- spec/sql_queries/erb_sql.erb.sql
|
142
145
|
- spec/sql_queries/get_player_by_email.sql.erb
|
143
146
|
- spec/sql_query_spec.rb
|
144
147
|
- sql_query.gemspec
|
@@ -168,5 +171,8 @@ specification_version: 4
|
|
168
171
|
summary: Ruby gem to load and execute SQL queries from `.sql.erb` templates
|
169
172
|
test_files:
|
170
173
|
- spec/spec_helper.rb
|
174
|
+
- spec/sql_queries/duplicated.erb.sql
|
175
|
+
- spec/sql_queries/duplicated.sql.erb
|
176
|
+
- spec/sql_queries/erb_sql.erb.sql
|
171
177
|
- spec/sql_queries/get_player_by_email.sql.erb
|
172
178
|
- spec/sql_query_spec.rb
|