sql_query 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8996df6ea13c7ce6e1b4ffb04d2137d537852326
4
- data.tar.gz: 776b12b91ab006e9ec2170268cfe2cb258f56603
3
+ metadata.gz: 578c4baee7c9f46b29390e5673a3964ea2355df5
4
+ data.tar.gz: 9a5ebd7357ab88afbe51e722450ce7be3cb50358
5
5
  SHA512:
6
- metadata.gz: 0d535b70950bfd98f169873ff9471453296e7eb4fb045dc0cf525d9563153022fa8e743aed56a00533ae67d62fc4ceeda4089d4483638b988a7922077c9dfd29
7
- data.tar.gz: 8615b61a80e9d78e6492404437bf0c6195ab241ec44be1e50f4544b2994c224c9c16982860886157e7659d3e25142a27b15bb3e9a361ea198f51f7b2200d22a3
6
+ metadata.gz: 7dc270655c8c0e38679b97cf60dac8923ca4f91e183d61320978594af24f252cb5c62e2daf533b85b5024c9e74a832d28b8c777106e0b65b8f241d47e381c78c
7
+ data.tar.gz: b97a98c442b2b80fbfec76193690924cefa157ef18e5144c69e19097931163b150351454648d8042402a5e4d467de024fd805a66b391416942b054b403bd3fe2
data/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SqlQuery change log
2
2
 
3
- ## 0.4.1 / Unreleased
3
+ ## 0.5.1 / Unreleased
4
4
 
5
5
  * [Added]
6
6
 
@@ -10,6 +10,10 @@
10
10
 
11
11
  * [Fixed]
12
12
 
13
+ ## 0.5.0 / 2016-04-26
14
+
15
+ * [Added] possibility to overwrite default connection class and connection directly using db_connection option.
16
+
13
17
  ## 0.4.0 / 2016-01-20
14
18
 
15
19
  * [Added] execute will accept boolean attribute.
data/README.md CHANGED
@@ -79,9 +79,13 @@ Example:
79
79
  SqlQuery.new('player/get_by_email', email: 'e@mail.dev')
80
80
  ```
81
81
 
82
+ #### Special options
83
+
84
+ * db_connection - If you want to change default connection to database you may do it for every query execution using this option.
85
+
82
86
  ### Methods
83
87
 
84
- - **execute** - executes query and returns result data. It accepts boolean argument. When argument is false it will raw sql query instead of prepared_for_logs.
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.
85
89
  - **explain** - runs explain for SQL from template
86
90
  - **sql** - returns SQL string
87
91
  - **pretty_sql** - returns SQL string prettier to read in console
@@ -89,14 +93,19 @@ SqlQuery.new('player/get_by_email', email: 'e@mail.dev')
89
93
 
90
94
  ### Configuration
91
95
 
92
- If you don't like default path to your queries you can configure it in initializer.
93
-
94
96
  ```ruby
95
97
  # config/initializers/sql_query.rb
96
98
  SqlQuery.configure do |config|
97
99
  config.path = '/app/sql_templates'
100
+ config.adapter = ActiveRecord::Base
98
101
  end
99
102
  ```
103
+
104
+ #### Configuration options
105
+ * path - If you don't like default path to your queries you can change it here.
106
+
107
+ * adapter - class which implements connection method.
108
+
100
109
  ### Partials
101
110
 
102
111
  You can prepare part of sql query in partial file and reuse it in multiple queries.
data/lib/sql_query.rb CHANGED
@@ -9,8 +9,8 @@ class SqlQuery
9
9
  end
10
10
  @sql_filename = file_name
11
11
  @options = options
12
+ @connection = options.try(:delete, :db_connection) || self.class.config.adapter.connection
12
13
  prepare_variables
13
- @connection = ActiveRecord::Base.connection
14
14
  end
15
15
 
16
16
  def explain
@@ -47,10 +47,8 @@ class SqlQuery
47
47
  ).sql
48
48
  end
49
49
 
50
- def self.config=(value)
51
- @config = value
52
- end
53
50
 
51
+ attr_writer :config
54
52
  def self.config
55
53
  @config ||= Config.new
56
54
  end
@@ -60,10 +58,11 @@ class SqlQuery
60
58
  end
61
59
 
62
60
  class Config
63
- attr_accessor :path
61
+ attr_accessor :path, :adapter
64
62
 
65
63
  def initialize
66
64
  @path = '/app/sql_queries'
65
+ @adapter = ActiveRecord::Base
67
66
  end
68
67
  end
69
68
 
@@ -87,6 +86,7 @@ class SqlQuery
87
86
  end
88
87
 
89
88
  def prepare_variables
89
+ return unless @options.present?
90
90
  @options.each do |k, v|
91
91
  instance_variable_set("@#{k}", v)
92
92
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe SqlQuery::Config do
4
+ describe 'initialize' do
5
+ it 'sets path to default' do
6
+ expect(described_class.new.path).to eq('/app/sql_queries')
7
+ end
8
+
9
+ it 'sets adapter to ActiveRecord::Base' do
10
+ expect(described_class.new.adapter).to eq(ActiveRecord::Base)
11
+ end
12
+ end
13
+ end
@@ -6,6 +6,10 @@ describe SqlQuery do
6
6
  let(:file_name) { :get_player_by_email }
7
7
  let(:query) { described_class.new(file_name, options) }
8
8
 
9
+ class Model < ActiveRecord::Base
10
+ self.abstract_class = true
11
+ end
12
+
9
13
  describe '#initialize' do
10
14
 
11
15
  it 'sets instance variables for all options' do
@@ -20,13 +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
31
  expect{ query }.to raise_error(ArgumentError, 'SQL file name should be String or Symbol')
28
32
  end
29
33
  end
34
+
35
+ context 'with db_connection option' do
36
+ let(:options) { { db_connection: Model.connection } }
37
+
38
+ it 'sets connection to requested' do
39
+ expect(query.connection).to eq(Model.connection)
40
+ end
41
+ end
30
42
  end
31
43
 
32
44
  describe '#file_path' do
@@ -121,6 +133,17 @@ describe SqlQuery do
121
133
  it 'resolves partials as parts of sql queries' do
122
134
  expect(query.sql).to eq("SELECT *\nFROM players\nWHERE players.email = 'e@mail.dev'\n\n")
123
135
  end
136
+
137
+ context 'when partial name is string with file path' do
138
+ let(:file_name) { :get_player_by_email }
139
+
140
+ it 'should find file by whole path and _name' do
141
+ query
142
+ expect(described_class)
143
+ .to receive(:new).with('some/path/to/_file.sql', options) { query }
144
+ query.partial('some/path/to/file.sql', {})
145
+ end
146
+ end
124
147
  end
125
148
 
126
149
  describe '#prepared_for_logs' do
@@ -129,4 +152,10 @@ describe SqlQuery do
129
152
  .to eq("SELECT * FROM players WHERE email = 'e@mail.dev' ")
130
153
  end
131
154
  end
155
+
156
+ describe '.config' do
157
+ it 'returns configuration instance' do
158
+ expect(described_class.config).to be_kind_of(SqlQuery::Config)
159
+ end
160
+ end
132
161
  end
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.4.0"
7
+ spec.version = "0.5.0"
8
8
  spec.authors = ["sufleR"]
9
9
  spec.email = ["szymon.fracczak@gmail.com"]
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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sufleR
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-20 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -159,6 +159,7 @@ files:
159
159
  - gemfiles/4.1.gemfile
160
160
  - gemfiles/4.2.gemfile
161
161
  - lib/sql_query.rb
162
+ - spec/config_spec.rb
162
163
  - spec/spec_helper.rb
163
164
  - spec/sql_queries/_email_partial.sql.erb
164
165
  - spec/sql_queries/duplicated.erb.sql
@@ -193,6 +194,7 @@ signing_key:
193
194
  specification_version: 4
194
195
  summary: Ruby gem to load and execute SQL queries from `.sql.erb` templates
195
196
  test_files:
197
+ - spec/config_spec.rb
196
198
  - spec/spec_helper.rb
197
199
  - spec/sql_queries/_email_partial.sql.erb
198
200
  - spec/sql_queries/duplicated.erb.sql