sql_query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c09cd1cecd3969c4c2319bf9f08263dd5cf315c0
4
+ data.tar.gz: c98ae1b84606bbd440c111d5427798742e305218
5
+ SHA512:
6
+ metadata.gz: e1e7bdeadd82d7e7acf349c45b4963efc66394e188ebc97ec6d997073a50556d373517154ba7154657c6f8b677e4ccf1ceb0051a986d8802fe87e5fbab1f9924
7
+ data.tar.gz: 3027e40b223589bb290eee8e71e7cc04f36512a9ef414ccc3383d0ea3ba02faef01a85dfb969eec854a38c91ba1cefd72a40c00212eae42707a700c10a5c993c
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ *.swp
23
+ *.swo
24
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - '1.9.3'
4
+ - '2.0.0'
5
+ - '2.1.5'
6
+ - '2.2.1'
7
+
8
+ addons:
9
+ postgresql: '9.3'
10
+
11
+ env:
12
+ - BUILDER=travis
13
+ before_script:
14
+ - psql -c 'create database travis_ci_test;' -U postgres
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # SqlQuery change log
2
+
3
+ ## 0.0.1 / Unreleased
4
+
5
+ * [Added]
6
+
7
+ * [Deprecated]
8
+
9
+ * [Removed]
10
+
11
+ * [Fixed]
12
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sql_query.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Szymon Frącczak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ [![Gem Version](https://badge.fury.io/rb/sql_query.svg)](http://badge.fury.io/rb/sql_query)
2
+ [![Dependency Status](https://gemnasium.com/sufleR/sql_query.svg)](https://gemnasium.com/sufleR/sql_query)
3
+ [![Code Climate](https://codeclimate.com/github/sufleR/sql_query/badges/gpa.svg)](https://codeclimate.com/github/sufleR/sql_query)
4
+ [![Test Coverage](https://codeclimate.com/github/sufleR/sql_query/badges/coverage.svg)](https://codeclimate.com/github/sufleR/sql_query)
5
+ [![Build Status](https://travis-ci.org/sufleR/sql_query.svg?branch=master)](https://travis-ci.org/sufleR/sql_query)
6
+
7
+ # SqlQuery
8
+
9
+ Ruby gem to load SQL queries from `.sql.erb` templates using ERB.
10
+
11
+ It makes working with pure SQL easier with syntax highlighting.
12
+ Let's you clean your Ruby code from SQL strings.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'sql_query'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install sql_query
27
+
28
+ ## Usage
29
+
30
+ Create SQL query in file in `app/sql_queries` directory
31
+
32
+ ```sql
33
+ # app/sql_queries/get_player_by_email.sql.erb
34
+ SELECT *
35
+ FROM players
36
+ WHERE email = <%= quote @email %>
37
+ ```
38
+
39
+ You can use SQL like this:
40
+
41
+ ```shell
42
+ > query = SqlQuery.new(sql_name: :get_player_by_email, email: 'e@mail.dev')
43
+
44
+ > query.execute
45
+ (0.6ms) SELECT * FROM players WHERE email = 'e@mail.dev'
46
+ => []
47
+
48
+ > query.explain
49
+ => EXPLAIN for:
50
+ SELECT *
51
+ FROM players
52
+ WHERE email = 'e@mail.dev'
53
+
54
+ QUERY PLAN
55
+ ----------------------------------------------------------
56
+ Seq Scan on players (cost=0.00..2.14 rows=1 width=5061)
57
+ Filter: ((email)::text = 'e@mail.dev'::text)
58
+ (2 rows)
59
+
60
+ > query.sql
61
+ => "SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n"
62
+
63
+ > query.pretty_sql
64
+ => SELECT *
65
+ FROM players
66
+ WHERE email = 'e@mail.dev'
67
+ ```
68
+
69
+ ### Methods
70
+
71
+ - **execute** - executes query and returns result data.
72
+
73
+ - **explain** - runs explain for SQL from template
74
+ - **sql** - returns SQL string
75
+ - **pretty_sql** - returns SQL string prettier to read in console
76
+ - prepared_for_logs - returns sql string without new lines and multiple whitespaces.
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/[my-github-username]/sql_query/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/sql_query.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'erb'
2
+
3
+ class SqlQuery
4
+ attr_reader :connection
5
+
6
+ def initialize(options = {})
7
+ prepare_variables(options)
8
+ @connection = ActiveRecord::Base.connection
9
+ end
10
+
11
+ def explain
12
+ msg = "EXPLAIN for: \n#{ sql }\n"
13
+ msg += connection.explain(sql)
14
+ pretty(msg)
15
+ end
16
+
17
+ def execute
18
+ connection.execute(prepared_for_logs).entries
19
+ end
20
+
21
+ def sql
22
+ @sql ||= ERB.new(File.read(path)).result(binding)
23
+ end
24
+
25
+ def pretty_sql
26
+ pretty(sql.dup)
27
+ end
28
+
29
+ def quote(value)
30
+ connection.quote(value)
31
+ end
32
+
33
+ def prepared_for_logs
34
+ sql.gsub(/(\n|\s)+/,' ')
35
+ end
36
+
37
+ def self.config=(value)
38
+ @config = value
39
+ end
40
+
41
+ def self.config
42
+ @config ||= Config.new
43
+ end
44
+
45
+ def self.configure
46
+ yield(config)
47
+ end
48
+
49
+ class Config
50
+ attr_accessor :path
51
+
52
+ def initialize
53
+ @path = '/app/sql_queries'
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def pretty(value)
60
+ # override inspect to be more human readable from console
61
+ # code copy from ActiveRecord
62
+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/explain.rb#L30
63
+ def value.inspect; self; end
64
+ value
65
+ end
66
+
67
+ def prepare_variables(options)
68
+ options.each do |k, v|
69
+ instance_variable_set("@#{k}", v)
70
+ end
71
+ end
72
+
73
+ def path
74
+ root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
75
+ tmp_path = "#{ root }#{self.class.config.path}"
76
+ tmp_path += "#{ @sql_path }/#{ @sql_name }.sql.erb"
77
+ tmp_path
78
+ end
79
+ end
@@ -0,0 +1,42 @@
1
+ if ENV['BUILDER'] == 'travis'
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ else
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ add_filter '/spec/'
8
+ end
9
+ end
10
+
11
+ require 'active_record'
12
+ require 'sql_query'
13
+ require 'pry'
14
+
15
+ SqlQuery.configure do |config|
16
+ config.path = '/spec/sql_queries'
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ config.expect_with :rspec do |expectations|
21
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
22
+ end
23
+
24
+ config.mock_with :rspec do |mocks|
25
+ mocks.verify_partial_doubles = true
26
+ end
27
+
28
+ connection = if ENV['BUILDER'] == 'travis'
29
+ "postgres://postgres@localhost/travis_ci_test"
30
+ else
31
+ "postgres://sqlquery:sqlquery@localhost/sqlquery"
32
+ end
33
+
34
+ ActiveRecord::Base.establish_connection(connection)
35
+
36
+ ActiveRecord::Base.connection.execute(
37
+ "CREATE TABLE IF NOT EXISTS players (email text);"
38
+ )
39
+
40
+ config.order = :random
41
+ Kernel.srand config.seed
42
+ end
@@ -0,0 +1,3 @@
1
+ SELECT *
2
+ FROM players
3
+ WHERE email = <%= quote @email %>
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe SqlQuery do
4
+
5
+ let(:base_options) { { sql_name: :get_player_by_email, email: 'e@mail.dev' } }
6
+ let(:options) { base_options }
7
+ let(:query) { described_class.new(options) }
8
+
9
+ describe '#sql' do
10
+ it 'returns query string' do
11
+ expect(query.sql).to eq "SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n"
12
+ end
13
+ end
14
+
15
+ describe '#pretty_sql' do
16
+ it 'returns query string' do
17
+ expect(query.pretty_sql).to eq "SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n"
18
+ end
19
+ end
20
+
21
+ describe '#explain' do
22
+ let(:explain) { query.explain }
23
+ it 'returns explain string' do
24
+ expect(explain).to include 'EXPLAIN for:'
25
+ expect(explain).to include "FROM players"
26
+ expect(explain).to include "WHERE email = 'e@mail.dev'"
27
+ expect(explain).to include 'QUERY PLAN'
28
+ expect(explain).to include 'Seq Scan on players'
29
+ end
30
+ end
31
+
32
+ describe '#execute' do
33
+ before do
34
+ ActiveRecord::Base.connection.execute(
35
+ "INSERT INTO players (email) VALUES ('e@mail.dev')"
36
+ )
37
+ end
38
+ after do
39
+ ActiveRecord::Base.connection.execute(
40
+ "DELETE FROM players"
41
+ )
42
+ end
43
+
44
+ it 'returns data from database' do
45
+ expect(query.execute).to eq [{ 'email' => 'e@mail.dev'}]
46
+ end
47
+ end
48
+ end
data/sql_query.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sql_query"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["sufleR"]
9
+ spec.email = ["szymon.fracczak@netguru.co"]
10
+ spec.summary = %q{Ruby gem to load and execute SQL queries from `.sql.erb` templates}
11
+ spec.description = %q{
12
+ It makes working with pure SQL easier with syntax highlighting.
13
+ Let's you clean your Ruby code from SQL strings.
14
+ }
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = ">= 1.9.3"
24
+
25
+ spec.add_dependency "activerecord", ">= 4"
26
+ spec.add_development_dependency "bundler", "~> 1.7"
27
+ spec.add_development_dependency "rake", "~> 10"
28
+ spec.add_development_dependency "rspec", "~> 3.2"
29
+ spec.add_development_dependency "pg", "~> 0.18"
30
+ spec.add_development_dependency "pry", "~> 0.10"
31
+ spec.add_development_dependency "with_model", "~> 1.2"
32
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
33
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sufleR
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.18'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: with_model
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: codeclimate-test-reporter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.4'
125
+ description: "\n It makes working with pure SQL easier with syntax highlighting.\n
126
+ \ Let's you clean your Ruby code from SQL strings.\n "
127
+ email:
128
+ - szymon.fracczak@netguru.co
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
135
+ - CHANGELOG.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/sql_query.rb
141
+ - spec/spec_helper.rb
142
+ - spec/sql_queries/get_player_by_email.sql.erb
143
+ - spec/sql_query_spec.rb
144
+ - sql_query.gemspec
145
+ homepage: ''
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 1.9.3
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.4.3
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Ruby gem to load and execute SQL queries from `.sql.erb` templates
169
+ test_files:
170
+ - spec/spec_helper.rb
171
+ - spec/sql_queries/get_player_by_email.sql.erb
172
+ - spec/sql_query_spec.rb