sql_query 0.1.0 → 0.2.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 +11 -1
- data/README.md +13 -2
- data/lib/sql_query.rb +7 -3
- data/spec/sql_query_spec.rb +32 -6
- data/sql_query.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd5a1fd8db226ffab78b0ed0cfd6c52bdcb0be54
|
4
|
+
data.tar.gz: 6b7e65e29f6d9feb80b1d8f0969e429c1be2687b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9c888ada8499947bec7a29225b837a4d834522eec0ebc34804d61570cb946147a287a634be18965294a81e54341797a1cc7b052e3a24a58259d23b3c4cc315d
|
7
|
+
data.tar.gz: 9d748e7bb3b793e4b0f717568c42872d040227f1ca2b83ccd5cc2e2f1bcfae11d7555bdb205b56ded42116a24d5351ff8159419bcee4f720eff7a3c04d318d08
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SqlQuery change log
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.2.1 / Unreleased
|
4
4
|
|
5
5
|
* [Added]
|
6
6
|
|
@@ -10,6 +10,16 @@
|
|
10
10
|
|
11
11
|
* [Fixed]
|
12
12
|
|
13
|
+
## 0.2.0 / 2015-05-01
|
14
|
+
|
15
|
+
* [Added] First argument in initialize as sql file name with path
|
16
|
+
|
17
|
+
* [Deprecated]
|
18
|
+
|
19
|
+
* [Removed] sql_name and sql_path in options hash
|
20
|
+
|
21
|
+
* [Fixed]
|
22
|
+
|
13
23
|
## 0.1.0 / 2015-04-27
|
14
24
|
|
15
25
|
* [Added] support for .erb.sql extension
|
data/README.md
CHANGED
@@ -6,12 +6,14 @@
|
|
6
6
|
|
7
7
|
# SqlQuery
|
8
8
|
|
9
|
-
Ruby gem to load SQL queries from
|
9
|
+
Ruby gem to load SQL queries from templates using ERB.
|
10
10
|
|
11
11
|
It makes working with pure SQL easier with syntax highlighting.
|
12
12
|
|
13
13
|
Let's you clean your Ruby code from SQL strings.
|
14
14
|
|
15
|
+
Supported extensions: `.sql.erb` or `.erb.sql`
|
16
|
+
|
15
17
|
## Installation
|
16
18
|
|
17
19
|
Add this line to your application's Gemfile:
|
@@ -41,7 +43,7 @@ WHERE email = <%= quote @email %>
|
|
41
43
|
You can use SQL like this:
|
42
44
|
|
43
45
|
```ruby
|
44
|
-
> query = SqlQuery.new(
|
46
|
+
> query = SqlQuery.new(:get_player_by_email, email: 'e@mail.dev')
|
45
47
|
|
46
48
|
> query.execute
|
47
49
|
(0.6ms) SELECT * FROM players WHERE email = 'e@mail.dev'
|
@@ -68,6 +70,15 @@ FROM players
|
|
68
70
|
WHERE email = 'e@mail.dev'
|
69
71
|
```
|
70
72
|
|
73
|
+
### initialization
|
74
|
+
|
75
|
+
If you need to have nested paths to your queries like ```player/get_by_email``` just use string instead of symbol as file name.
|
76
|
+
|
77
|
+
Example:
|
78
|
+
```ruby
|
79
|
+
SqlQuery.new('player/get_by_email', email: 'e@mail.dev')
|
80
|
+
```
|
81
|
+
|
71
82
|
### Methods
|
72
83
|
|
73
84
|
- **execute** - executes query and returns result data.
|
data/lib/sql_query.rb
CHANGED
@@ -3,7 +3,11 @@ require 'erb'
|
|
3
3
|
class SqlQuery
|
4
4
|
attr_reader :connection
|
5
5
|
|
6
|
-
def initialize(options = {})
|
6
|
+
def initialize(file_name, options = {})
|
7
|
+
unless file_name.is_a?(String) || file_name.is_a?(Symbol)
|
8
|
+
fail ArgumentError, 'SQL file name should be String or Symbol'
|
9
|
+
end
|
10
|
+
@sql_filename = file_name
|
7
11
|
prepare_variables(options)
|
8
12
|
@connection = ActiveRecord::Base.connection
|
9
13
|
end
|
@@ -73,7 +77,7 @@ class SqlQuery
|
|
73
77
|
def file_path
|
74
78
|
files = Dir.glob(path)
|
75
79
|
if files.size == 0
|
76
|
-
raise "File not found
|
80
|
+
raise "File not found: #{ @sql_filename }"
|
77
81
|
elsif files.size > 1
|
78
82
|
raise "More than one file found: #{ files.join(', ')}"
|
79
83
|
else
|
@@ -84,7 +88,7 @@ class SqlQuery
|
|
84
88
|
def path
|
85
89
|
root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
|
86
90
|
tmp_path = "#{ root }#{self.class.config.path}"
|
87
|
-
tmp_path += "
|
91
|
+
tmp_path += "/#{ @sql_filename }.{sql.erb,erb.sql}"
|
88
92
|
tmp_path
|
89
93
|
end
|
90
94
|
end
|
data/spec/sql_query_spec.rb
CHANGED
@@ -2,8 +2,32 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe SqlQuery do
|
4
4
|
|
5
|
-
let(:options) { {
|
6
|
-
let(:
|
5
|
+
let(:options) { { email: 'e@mail.dev' } }
|
6
|
+
let(:file_name) { :get_player_by_email }
|
7
|
+
let(:query) { described_class.new(file_name, options) }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
|
11
|
+
it 'sets instance variables for all options' do
|
12
|
+
expect(query.instance_variable_get(:@email)).to eq 'e@mail.dev'
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when options are set not in parentheses' do
|
16
|
+
let(:query) { described_class.new(file_name, email: 'e@mail.dev') }
|
17
|
+
|
18
|
+
it 'sets instance variables for all options' do
|
19
|
+
expect(query.instance_variable_get(:@email)).to eq 'e@mail.dev'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'file_name argument is not Symbol or String' do
|
24
|
+
let(:file_name) { { do: 'something' } }
|
25
|
+
|
26
|
+
it 'raises ArgumentError' do
|
27
|
+
expect{ query }.to raise_error(ArgumentError, 'SQL file name should be String or Symbol')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
7
31
|
|
8
32
|
describe '#file_path' do
|
9
33
|
context 'when there is only one file with name' do
|
@@ -12,15 +36,15 @@ describe SqlQuery do
|
|
12
36
|
end
|
13
37
|
end
|
14
38
|
context 'when there are no files' do
|
15
|
-
let(:
|
39
|
+
let(:file_name) { :not_exists }
|
16
40
|
|
17
41
|
it 'raises error' do
|
18
|
-
expect{ query.send(:file_path) }.to raise_error('File not found
|
42
|
+
expect{ query.send(:file_path) }.to raise_error('File not found: not_exists')
|
19
43
|
end
|
20
44
|
end
|
21
45
|
|
22
46
|
context 'when there are more than one matching files' do
|
23
|
-
let(:
|
47
|
+
let(:file_name) { :duplicated }
|
24
48
|
|
25
49
|
it 'raises error' do
|
26
50
|
expect{ query.send(:file_path) }.to raise_error.with_message(/More than one file found:/)
|
@@ -34,7 +58,8 @@ describe SqlQuery do
|
|
34
58
|
end
|
35
59
|
|
36
60
|
context 'when file is .erb.sql' do
|
37
|
-
let(:options) { {
|
61
|
+
let(:options) { { fake: 12 } }
|
62
|
+
let(:file_name) { :erb_sql }
|
38
63
|
|
39
64
|
it 'returns query string' do
|
40
65
|
expect(query.sql).to eq "SELECT 12\n"
|
@@ -65,6 +90,7 @@ describe SqlQuery do
|
|
65
90
|
"INSERT INTO players (email) VALUES ('e@mail.dev')"
|
66
91
|
)
|
67
92
|
end
|
93
|
+
|
68
94
|
after do
|
69
95
|
ActiveRecord::Base.connection.execute(
|
70
96
|
"DELETE FROM players"
|
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.
|
7
|
+
spec.version = "0.2.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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|