kaigyo 0.1.0

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
+ SHA256:
3
+ metadata.gz: 46a5d35f7d9e0b80bc03dd9b3fa3b74bb6ec1b88ff27eea1671ac7d556690387
4
+ data.tar.gz: 82258983eaef98abfa7816f7fcc154fc9e112763ceb2091ab2250896c6b3cf90
5
+ SHA512:
6
+ metadata.gz: 77235c1177c84f4b45040e75c6e8406408dad4caf0086cdd87ec5401a0c9523ab01fd09e89b42aabc559bdf9522a3d58475f0571a7b5ac594cac31a5504926bd
7
+ data.tar.gz: 9a8d8ffff31bedeff101ea4bd2f1868b0419a639a0cd2144ca1b614dfe9f54bccca0483386a096e03d44cd55749969d923091333023db1e413a2fca2622b56db
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ vendor/bundle
10
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.10
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at satoakick@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in kaigyo.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Kaigyo
2
+
3
+ A formatter for SQL
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kaigyo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kaigyo
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ irb(main):001:0> puts "select a,b from foo inner join bar on foo.id = bar.id where a = 1 group by c order by d".kaigyo
25
+ select a,b
26
+ from foo
27
+ inner join bar on foo.id = bar.id
28
+ where a = 1
29
+ group by c
30
+ order by d
31
+ => nil
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/satoakick/kaigyo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the Kaigyo project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/satoakick/kaigyo/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kaigyo"
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
data/kaigyo.gemspec ADDED
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "kaigyo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kaigyo"
8
+ spec.version = Kaigyo::VERSION
9
+ spec.authors = ["satoakick"]
10
+ spec.email = ["satoakick@gmail.com"]
11
+
12
+ spec.summary = 'A formatter for sql'
13
+ spec.description = 'kaigyo is a sql formatter'
14
+ spec.homepage = "https://github.com/satoakick/kaigyo"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/satoakick/kaigyo"
22
+ spec.metadata["changelog_uri"] = "https://github.com/satoakick/kaigyo/blob/main/CHANGELOG.md"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "minitest", "~> 5.0"
40
+ end
@@ -0,0 +1,222 @@
1
+ module Kaigyo
2
+ class Tokenizer
3
+ attr_reader :source
4
+ attr_accessor :current
5
+
6
+ WITH = 'with'
7
+ SELECT = 'select'
8
+ FROM = 'from'
9
+ WHERE = 'where'
10
+ GROUP = 'group'
11
+ HAVING = 'having'
12
+ WINDOW = 'window'
13
+ UNION = 'union'
14
+ INTERSECT = 'intersect'
15
+ EXCEPT = 'except'
16
+ ORDER = 'order'
17
+ LIMIT = 'limit'
18
+ OFFSET = 'offset'
19
+ FETCH = 'fetch'
20
+ FOR = 'for'
21
+ BY = 'by'
22
+ LEFT_PAREN = 'left_paren'
23
+ RIGHT_PAREN = 'right_paren'
24
+ INNER = 'inner'
25
+ OUTER = 'outer'
26
+ LEFT = 'left'
27
+ RIGHT = 'right'
28
+ FULL = 'full'
29
+ CROSS = 'cross'
30
+ JOIN = 'join'
31
+ PANCUTUATION = 'punctuation'
32
+ EQUAL = 'eaual'
33
+ LESS_THAN_EQUAL = 'less_than_equal'
34
+ LESS_THAN = 'less_than'
35
+ GREATER_THAN_EQUAL = 'greater_than_equal'
36
+ GREATER_THAN = 'greater_than'
37
+ BETWEEN = 'between'
38
+ AND = 'and'
39
+ OR = 'or'
40
+ ON = 'on'
41
+ DESC = 'desc'
42
+ ASC = 'asc'
43
+ AS = 'as'
44
+ SUM = 'sum'
45
+ AVG = 'avg'
46
+ IN = 'in'
47
+ LIKE = 'like'
48
+
49
+
50
+ def initialize(source)
51
+ @source = source
52
+ @current = 0
53
+ @in_single_quote = false
54
+ end
55
+
56
+ def next
57
+ return nil if current >= source.length
58
+
59
+ token = []
60
+
61
+ target = source[current..-1]
62
+ target.chars.each_with_index do |c, i|
63
+ #puts "c: #{c} i: #{i} current: #{current}"
64
+
65
+ @current += 1
66
+
67
+ # begin single quote
68
+ if c == "'" && !@in_single_quote
69
+ @in_single_quote = true
70
+ token << c
71
+ #puts "begin single quote #{token}"
72
+ next
73
+ end
74
+
75
+ # in single quote
76
+ if c != "'" && @in_single_quote
77
+ token << c
78
+ #puts "in single quote #{token}"
79
+ next
80
+ end
81
+
82
+ # end single quote
83
+ if c == "'" && @in_single_quote
84
+ @in_single_quote = false
85
+ token << c
86
+ #puts "end single quote #{token}"
87
+ break
88
+ end
89
+
90
+ case c
91
+ when '(', ')', ','
92
+ token << c
93
+
94
+ break
95
+ when ' '
96
+ next if token.size.zero?
97
+
98
+ break
99
+ when '<', '>'
100
+ token << c
101
+ break if target[i+1] != '='
102
+ when '='
103
+ token << c
104
+ break
105
+ else
106
+ token << c
107
+
108
+ case target[i+1]
109
+ when ',', ' ', '(', ')', '=', '<', '>'
110
+ break
111
+ end
112
+ end
113
+ end
114
+ token.join
115
+ end
116
+
117
+ def token_analysis
118
+ tokens = []
119
+ while token = self.next do
120
+ tokens << token
121
+ end
122
+
123
+ result = []
124
+ idx = tokens.size-1
125
+
126
+ prev_prev_token = nil
127
+ prev_token = nil
128
+
129
+ tokens.each_with_index do |token, i|
130
+ token_name = token.downcase
131
+ node = make_node(token_name, prev_prev_token, prev_token, token)
132
+ result << node if node
133
+
134
+ prev_prev_token = prev_token
135
+ prev_token = token
136
+ end
137
+ result
138
+ end
139
+
140
+ def make_node(token_name, prev_prev_token, prev_token, token)
141
+ # puts "token_name: #{token_name} prev_prev_token: #{prev_prev_token} prev_token: #{prev_token} token: #{token}"
142
+
143
+ case token_name
144
+ when SELECT, FROM, WHERE, HAVING, LIMIT, OFFSET, WINDOW, UNION, INTERSECT, EXCEPT
145
+ [:clause, token]
146
+ when AND
147
+ if prev_prev_token.downcase == BETWEEN
148
+ [:between_and, token]
149
+ else
150
+ [:and, token]
151
+ end
152
+ when ON
153
+ [:on, token]
154
+ when OR
155
+ [:or, token]
156
+ when BETWEEN
157
+ [:between, token]
158
+ when LIKE
159
+ [:like, token]
160
+ when AS
161
+ [:as, token]
162
+ when IN
163
+ [:in, token]
164
+ when ASC, DESC
165
+ [:order_option, token]
166
+ when JOIN
167
+ if prev_token.downcase == INNER
168
+ [:clause, "#{prev_token} #{token}"]
169
+ elsif prev_prev_token.downcase == LEFT && prev_token.downcase == OUTER
170
+ [:clause, "#{prev_prev_token} #{prev_token} #{token}"]
171
+ elsif prev_token.downcase == LEFT
172
+ [:clause, "#{prev_token} #{token}"]
173
+ elsif prev_prev_token.downcase == RIGHT && prev_token.downcase == OUTER
174
+ [:clause, "#{prev_prev_token} #{prev_token} #{token}"]
175
+ elsif prev_token.downcase == RIGHT
176
+ [:clause, "#{prev_token} #{token}"]
177
+ elsif prev_prev_token.downcase == FULL && prev_token.downcase == OUTER
178
+ [:clause, "#{prev_prev_token} #{prev_token} #{token}"]
179
+ elsif prev_token.downcase == FULL
180
+ [:clause, "#{prev_token} #{token}"]
181
+ elsif prev_token.downcase == CROSS
182
+ [:clause, "#{prev_token} #{token}"]
183
+ else
184
+ [:clause, token]
185
+ end
186
+ when BY
187
+ if prev_token.downcase == GROUP || prev_token.downcase == ORDER
188
+ [:clause, "#{prev_token} #{token}"]
189
+ end
190
+ when INNER, OUTER, LEFT, RIGHT, FULL, CROSS, GROUP, ORDER
191
+ # suppress for duplicate node.
192
+ # cf. GROUP BY, ORDER BY, INNER JOIN
193
+ nil
194
+ when ','
195
+ [:pancutuation, token]
196
+ when ')'
197
+ [:right_paren, token]
198
+ when '('
199
+ [:left_paren, token]
200
+ when '<'
201
+ [:less_than, token]
202
+ when '>'
203
+ [:greater_than, token]
204
+ when '<='
205
+ [:less_than_equal, token]
206
+ when '>='
207
+ [:greater_than_equal, token]
208
+ when '='
209
+ [:equal, token]
210
+ when /\'.+\'/
211
+ [:literal, token]
212
+ when AVG, SUM
213
+ [:function, token]
214
+ when /\d+/
215
+ [:number, token]
216
+ else
217
+ [:identifier, token]
218
+ end
219
+ end
220
+ end
221
+ end
222
+
@@ -0,0 +1,3 @@
1
+ module Kaigyo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/kaigyo.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "kaigyo/version"
2
+ require "kaigyo/tokenizer"
3
+
4
+ module Kaigyo
5
+ class Error < StandardError; end
6
+
7
+ def kaigyo
8
+ return self unless self.downcase.include?("select")
9
+
10
+ tokenizer = Tokenizer.new(self)
11
+ tokens = tokenizer.token_analysis
12
+ result = []
13
+ prev_token = nil
14
+ indent_size = 0
15
+
16
+ tokens.each do |token|
17
+ if token.first == :left_paren
18
+ indent_size += 1
19
+ end
20
+ if token.first == :right_paren
21
+ indent_size -= 1
22
+ end
23
+
24
+ if token.first == :clause
25
+ result << [indent(indent_size) + token[1]]
26
+ elsif token.first == :and || token.first == :or
27
+ result << [indent(indent_size) + ' ' + token[1]]
28
+ else
29
+ result.last << token[1]
30
+ end
31
+
32
+ prev_token = token
33
+ end
34
+
35
+ result.map do |row|
36
+ row.each.with_index.inject("") do |str, (c, idx)|
37
+ if idx == 0
38
+ str += c
39
+ else
40
+ str += c == ',' ? c : " #{c}"
41
+ end
42
+ end
43
+ end.join("\n")
44
+ end
45
+
46
+ def indent(size)
47
+ ' ' * size
48
+ end
49
+ end
50
+
51
+ class String
52
+ include Kaigyo
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaigyo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - satoakick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-19 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: kaigyo is a sql formatter
56
+ email:
57
+ - satoakick@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - kaigyo.gemspec
71
+ - lib/kaigyo.rb
72
+ - lib/kaigyo/tokenizer.rb
73
+ - lib/kaigyo/version.rb
74
+ homepage: https://github.com/satoakick/kaigyo
75
+ licenses: []
76
+ metadata:
77
+ homepage_uri: https://github.com/satoakick/kaigyo
78
+ source_code_uri: https://github.com/satoakick/kaigyo
79
+ changelog_uri: https://github.com/satoakick/kaigyo/blob/main/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.0.3.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A formatter for sql
99
+ test_files: []