sql_formatter 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3407f644b957a15838ea5f8353a471189f1405fe
4
+ data.tar.gz: 3831c4697d02f500573f4c377d7d3e478477d000
5
+ SHA512:
6
+ metadata.gz: 608580c195424f5d16eecf139ccf11b7afa2e3beac6ee5f950adc7a13f204cf9e63262f9a3e3a9fb0e2990222d7b0a30614931dd4b001567943e7b008889daa6
7
+ data.tar.gz: a312a0e5b5c9847ca1fabbd6efed511208d48e7a7a4f3ac6ee28e5edeb840d2d5fbff54c8f0fdfeb1ee7ee5a98fa40737493ac556257ef86d1a81a5300583f84
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sql_formatter.gemspec
4
+ gemspec
5
+ gem "pry"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kotaro Mochizuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # SqlFormatter
2
+
3
+ It is ruby gem to format SQL.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install sql_formatter
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ pry> require 'sql_formatter'
15
+ => true
16
+
17
+ pry> sql_string = "select col1, col2 from table1 where col3 in (select col3 from table2) order by col1;"
18
+ => "select col1, col2 from table1 where col3 in (select col3 from table2) order by col1;"
19
+
20
+ pry> puts SqlFormatter.format(sql_string)
21
+ select
22
+ col1, col2
23
+ from
24
+ table1
25
+ where
26
+ col3 in (
27
+ select col3
28
+ from table2
29
+ )
30
+ order by
31
+ col1
32
+ ;
33
+ => nil
34
+ ```
35
+
36
+
37
+ ## Development
38
+
39
+ ```
40
+ $ git clone git@github.com:mochizukikotaro/sql_formatter.git
41
+ $ cd sql_formatter
42
+ $ bundle exec irb
43
+ irb(main):001:0> require 'sql_formatter'
44
+ => true
45
+ irb(main):002:0> puts SqlFormatter.format('select * from table;')
46
+ select
47
+ *
48
+ from
49
+ table
50
+ ;
51
+ => nil
52
+ ```
53
+
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mochizukikotaro/sql_formatter.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sql_formatter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,162 @@
1
+ module SqlFormatter
2
+ class Formatter
3
+
4
+ NEWLINE = "\n"
5
+ SPACE = " " * 1
6
+ TAB = " " * 2
7
+ COMMA = ","
8
+
9
+ RESERVED_TOPLEVEL = [
10
+ 'select',
11
+ 'from',
12
+ 'where',
13
+ 'group by',
14
+ 'order by'
15
+ ]
16
+ BOUNDARY = %w( , ; : ( ) . = < > + - * / ! ~ % | & # )
17
+
18
+ REGEX_RESERVED_TOPLEVEL = Regexp.new(RESERVED_TOPLEVEL.join('|').gsub(/ /, '\s+'))
19
+ REGEX_BOUNDARY = Regexp.new(BOUNDARY.map{ |m| Regexp.escape m }.join('|'))
20
+
21
+
22
+ def initialize(string)
23
+ @ss = StringScanner.new(string)
24
+ @tokens = []
25
+ @space = true
26
+ @newline = false
27
+ @indent_level = 0
28
+ @inline_parentheses = false
29
+ @inline_parentheses_level = 0
30
+ tokenize
31
+ remove_space
32
+ end
33
+
34
+
35
+ def format
36
+ output = ''
37
+ @tokens.each_with_index do |token, index|
38
+
39
+ case
40
+
41
+ # Reserved toplevel
42
+ when RESERVED_TOPLEVEL.include?(token)
43
+ lower_indent_level
44
+ output += NEWLINE if index > 0
45
+ output += TAB * @indent_level
46
+
47
+ # TODO: Add optional..
48
+ if @indent_level >= 2
49
+ @newline = false
50
+ else
51
+ @newline = true
52
+ end
53
+
54
+ raise_indent_level
55
+
56
+ # End of tokens
57
+ when token == ';' && index == (@tokens.size - 1)
58
+ output += NEWLINE
59
+
60
+ # Comma
61
+ when token == COMMA
62
+ @space = true
63
+
64
+ # Left parenthesis
65
+ when token == '('
66
+ if @newline
67
+ output += NEWLINE + TAB * @indent_level
68
+ end
69
+
70
+ if inline_left_parenthesis?(index)
71
+ @newline = false
72
+ @space = false
73
+ @inline_parentheses = true
74
+ @inline_parentheses_level += 1
75
+ output += SPACE if @tokens[index - 1] == COMMA
76
+ else
77
+ @newline = true
78
+ @indent_level += 2
79
+ output += SPACE
80
+ end
81
+
82
+ # Right parenthesis
83
+ when token == ')'
84
+ if inline_right_parenthesis?
85
+ @inline_parentheses_level = [0, @inline_parentheses_level - 1].max
86
+
87
+ if @inline_parentheses_level == 0
88
+ @inline_parentheses = false
89
+ @space = true
90
+ end
91
+ else
92
+ @indent_level -= 2
93
+ output += NEWLINE + TAB
94
+ end
95
+
96
+ else
97
+ if @inline_parentheses
98
+ # Nothing to do
99
+ elsif @newline
100
+ output += NEWLINE + TAB * @indent_level
101
+ @newline = false
102
+ elsif @space
103
+ output += SPACE
104
+ else
105
+ # Nothing to do
106
+ end
107
+ end
108
+
109
+ output += token
110
+ end
111
+ output
112
+ end
113
+
114
+ private
115
+
116
+ def raise_indent_level
117
+ @indent_level += 1
118
+ end
119
+
120
+ def lower_indent_level
121
+ @indent_level = [0, @indent_level -1].max
122
+ end
123
+
124
+ def inline_left_parenthesis?(index)
125
+ RESERVED_TOPLEVEL.include?(@tokens[index + 1]) ? false : true
126
+ end
127
+
128
+ def inline_right_parenthesis?
129
+ @inline_parentheses ? true : false
130
+ end
131
+
132
+ def tokenize
133
+ until @ss.eos?
134
+ @tokens << next_token
135
+ end
136
+ end
137
+
138
+ def next_token
139
+ patterns = [
140
+ /\s+/,
141
+ REGEX_RESERVED_TOPLEVEL,
142
+ REGEX_BOUNDARY,
143
+ /(^'.*?'|^".*?"|^`.*?`)/,
144
+ /\w+/
145
+ ]
146
+ patterns.each do |pattern|
147
+ return @ss.matched if @ss.scan(pattern)
148
+ end
149
+
150
+ # TODO: Add error handling
151
+ p 'Warning'
152
+ p @ss.peek 20
153
+ @ss.pos += 1
154
+ return nil
155
+ end
156
+
157
+ def remove_space
158
+ @tokens.select! { |token| !token.match(/^\s+$/) }
159
+ end
160
+
161
+ end
162
+ end
@@ -0,0 +1,3 @@
1
+ module SqlFormatter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "sql_formatter/version"
2
+ require "sql_formatter/formatter"
3
+
4
+ module SqlFormatter
5
+ def self.format(string)
6
+ formatter = SqlFormatter::Formatter.new(string)
7
+ formatter.format
8
+ end
9
+ end
data/sample_sql.sql ADDED
@@ -0,0 +1,20 @@
1
+ create table users (
2
+ id serial,
3
+ name varchar(255),
4
+ team integer
5
+ )
6
+ ;
7
+
8
+ insert into users (name, team) values
9
+ ('taro', 1),
10
+ ('yuki', 1),
11
+ ('kent', 1),
12
+ ('hoge', 2),
13
+ ('piyo', 2),
14
+ ('fuga', 2),
15
+ ('aaaa', 3),
16
+ ('bbbb', 3)
17
+ ;
18
+
19
+ -- Sample query
20
+ puts SqlFormatter.format "select (1), ((2)), hoge, piyo, count(*), max(score) from users where name in (select *, aa from hoge) and team = '3' group by team order by id;"
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sql_formatter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sql_formatter"
8
+ spec.version = SqlFormatter::VERSION
9
+ spec.authors = ["Kotaro Mochizuki"]
10
+ spec.email = ["mochizuki@basicinc.jp"]
11
+
12
+ spec.summary = %q{ SQL formatter on Ruby }
13
+ spec.description = %q{ Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/mochizukikotaro"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kotaro Mochizuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: " Write a longer description or delete this line."
42
+ email:
43
+ - mochizuki@basicinc.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/sql_formatter.rb
56
+ - lib/sql_formatter/formatter.rb
57
+ - lib/sql_formatter/version.rb
58
+ - sample_sql.sql
59
+ - sql_formatter.gemspec
60
+ homepage: https://github.com/mochizukikotaro
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: SQL formatter on Ruby
84
+ test_files: []
85
+ has_rdoc: