sql_query 0.2.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 748c4c52de904df3008bce29a438a87fb35999a7
4
- data.tar.gz: d6f2f18bd2182837f558290c57119fe33137272d
3
+ metadata.gz: bee55d0da7461b026132a7282340f29ad1a17768
4
+ data.tar.gz: 0460233cafaf0331980a61e538fcf01058d76511
5
5
  SHA512:
6
- metadata.gz: c72a0c1ddbe997fa7508b03abe76724ba9cb2c0beaf193668c2dbbca397c1e925810cc267be4b410610ea64b1d018111fac214499dc17caf869e1070b7dddcf2
7
- data.tar.gz: c25bc31e5266d0ae7ec96b559e42a08ccc604aeb996852fc575d36591fe04ed7d318b12778c66f0f4d2f812020cb60e639086666a7250d0987ac74af6684c910
6
+ metadata.gz: 8bb059c5e3f304165e409f8ec22fbce07f1e9a13cc6793242dd2bb63f0bc3bdf527e339e72fb294468a750abd9838817d86f68e00419b418da50f4abb4536fba
7
+ data.tar.gz: 8509f4a76b9e9772e0eda5e8db5ea58bf5e5d173210609e49c272ab88d965582f3ea90dd3824a85bc9f73e4eab2cffae08e8b4809388bb028b7cad30f47e2b9b
data/.travis.yml CHANGED
@@ -10,7 +10,7 @@ rvm:
10
10
  - '1.9.3'
11
11
  - '2.0.0'
12
12
  - '2.1.5'
13
- - '2.2.1'
13
+ - '2.2.3'
14
14
 
15
15
  addons:
16
16
  postgresql: '9.3'
data/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SqlQuery change log
2
2
 
3
- ## 0.2.2 / Unreleased
3
+ ## 0.3.1 / Unreleased
4
4
 
5
5
  * [Added]
6
6
 
@@ -10,6 +10,10 @@
10
10
 
11
11
  * [Fixed]
12
12
 
13
+ ## 0.3.0 / 2015-12-10
14
+
15
+ * [Added] support for partials
16
+
13
17
  ## 0.2.1 / 2015-05-01
14
18
 
15
19
  * [Added] support for activerecord >= 3.2
data/README.md CHANGED
@@ -97,6 +97,32 @@ SqlQuery.configure do |config|
97
97
  config.path = '/app/sql_templates'
98
98
  end
99
99
  ```
100
+ ### Partials
101
+
102
+ You can prepare part of sql query in partial file and reuse it in multiple queries.
103
+
104
+ Partial file should start with '_'.
105
+
106
+ Example:
107
+
108
+ ```sql
109
+ # app/sql_queries/_email_partial.sql.erb
110
+ players.email = <%= quote @email %>
111
+ ```
112
+
113
+ and use this partial like this:
114
+
115
+ ```sql
116
+ SELECT *
117
+ FROM players
118
+ WHERE <%= partial :email_partial %>
119
+ ```
120
+
121
+ ## Examples
122
+
123
+ Check examples folder for some usefull queries.
124
+
125
+ If you have some examples to share please make pull request.
100
126
 
101
127
  ## Contributing
102
128
 
@@ -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/lib/sql_query.rb CHANGED
@@ -8,7 +8,8 @@ class SqlQuery
8
8
  fail ArgumentError, 'SQL file name should be String or Symbol'
9
9
  end
10
10
  @sql_filename = file_name
11
- prepare_variables(options)
11
+ @options = options
12
+ prepare_variables
12
13
  @connection = ActiveRecord::Base.connection
13
14
  end
14
15
 
@@ -38,6 +39,13 @@ class SqlQuery
38
39
  sql.gsub(/(\n|\s)+/,' ')
39
40
  end
40
41
 
42
+ def partial(partial_name, partial_options = {})
43
+ path, file_name = split_to_path_and_name(partial_name)
44
+ self.class.new("#{path}/_#{file_name}",
45
+ @options.merge(partial_options),
46
+ ).sql
47
+ end
48
+
41
49
  def self.config=(value)
42
50
  @config = value
43
51
  end
@@ -60,6 +68,15 @@ class SqlQuery
60
68
 
61
69
  private
62
70
 
71
+ def split_to_path_and_name(file)
72
+ if file.is_a?(Symbol)
73
+ ['', file.to_s]
74
+ else
75
+ parts = file.rpartition('/')
76
+ [parts.first, parts.last]
77
+ end
78
+ end
79
+
63
80
  def pretty(value)
64
81
  # override inspect to be more human readable from console
65
82
  # code copy from ActiveRecord
@@ -68,8 +85,8 @@ class SqlQuery
68
85
  value
69
86
  end
70
87
 
71
- def prepare_variables(options)
72
- options.each do |k, v|
88
+ def prepare_variables
89
+ @options.each do |k, v|
73
90
  instance_variable_set("@#{k}", v)
74
91
  end
75
92
  end
@@ -77,7 +94,7 @@ class SqlQuery
77
94
  def file_path
78
95
  files = Dir.glob(path)
79
96
  if files.size == 0
80
- raise "File not found: #{ @sql_filename }"
97
+ raise "File not found: #{@sql_filename}"
81
98
  elsif files.size > 1
82
99
  raise "More than one file found: #{ files.join(', ')}"
83
100
  else
@@ -87,8 +104,8 @@ class SqlQuery
87
104
 
88
105
  def path
89
106
  root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
90
- tmp_path = "#{ root }#{self.class.config.path}"
91
- tmp_path += "/#{ @sql_filename }.{sql.erb,erb.sql}"
107
+ tmp_path = "#{root}#{self.class.config.path}"
108
+ tmp_path += "/#{@sql_filename}.{sql.erb,erb.sql}"
92
109
  tmp_path
93
110
  end
94
111
  end
@@ -0,0 +1 @@
1
+ players.email = <%= quote @email %>
@@ -0,0 +1,3 @@
1
+ SELECT *
2
+ FROM players
3
+ WHERE <%= partial :email_partial %>
@@ -101,4 +101,11 @@ describe SqlQuery do
101
101
  expect(query.execute).to eq [{ 'email' => 'e@mail.dev'}]
102
102
  end
103
103
  end
104
+
105
+ describe '#partial' do
106
+ let(:file_name) { :get_player_by_email_with_template }
107
+ it 'resolves partials as parts of sql queries' do
108
+ expect(query.sql).to eq("SELECT *\nFROM players\nWHERE players.email = 'e@mail.dev'\n\n")
109
+ end
110
+ end
104
111
  end
data/sql_query.gemspec CHANGED
@@ -4,9 +4,9 @@ $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.2.1"
7
+ spec.version = "0.3.0"
8
8
  spec.authors = ["sufleR"]
9
- spec.email = ["szymon.fracczak@netguru.co"]
9
+ spec.email = ["szymon.fracczak@gmail.com"]
10
10
  spec.summary = %q{Ruby gem to load and execute SQL queries from `.sql.erb` templates}
11
11
  spec.description = %q{
12
12
  It makes working with pure SQL easier with syntax highlighting.
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.2.1
4
+ version: 0.3.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-05-01 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -139,7 +139,7 @@ dependencies:
139
139
  description: "\n It makes working with pure SQL easier with syntax highlighting.\n
140
140
  \ Let's you clean your Ruby code from SQL strings.\n "
141
141
  email:
142
- - szymon.fracczak@netguru.co
142
+ - szymon.fracczak@gmail.com
143
143
  executables: []
144
144
  extensions: []
145
145
  extra_rdoc_files: []
@@ -152,16 +152,20 @@ files:
152
152
  - LICENSE.txt
153
153
  - README.md
154
154
  - Rakefile
155
+ - examples/README.md
156
+ - examples/distinct_values.sql.erb
155
157
  - gemfiles/3.2.gemfile
156
158
  - gemfiles/4.0.gemfile
157
159
  - gemfiles/4.1.gemfile
158
160
  - gemfiles/4.2.gemfile
159
161
  - lib/sql_query.rb
160
162
  - spec/spec_helper.rb
163
+ - spec/sql_queries/_email_partial.sql.erb
161
164
  - spec/sql_queries/duplicated.erb.sql
162
165
  - spec/sql_queries/duplicated.sql.erb
163
166
  - spec/sql_queries/erb_sql.erb.sql
164
167
  - spec/sql_queries/get_player_by_email.sql.erb
168
+ - spec/sql_queries/get_player_by_email_with_template.sql.erb
165
169
  - spec/sql_query_spec.rb
166
170
  - sql_query.gemspec
167
171
  homepage: https://github.com/sufleR/sql_query
@@ -184,14 +188,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
188
  version: '0'
185
189
  requirements: []
186
190
  rubyforge_project:
187
- rubygems_version: 2.4.3
191
+ rubygems_version: 2.5.0
188
192
  signing_key:
189
193
  specification_version: 4
190
194
  summary: Ruby gem to load and execute SQL queries from `.sql.erb` templates
191
195
  test_files:
192
196
  - spec/spec_helper.rb
197
+ - spec/sql_queries/_email_partial.sql.erb
193
198
  - spec/sql_queries/duplicated.erb.sql
194
199
  - spec/sql_queries/duplicated.sql.erb
195
200
  - spec/sql_queries/erb_sql.erb.sql
196
201
  - spec/sql_queries/get_player_by_email.sql.erb
202
+ - spec/sql_queries/get_player_by_email_with_template.sql.erb
197
203
  - spec/sql_query_spec.rb