auto_sql_formatter 0.1.0.dev → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +3 -1
- data/example/example.rb +7 -7
- data/example/example2.rb +93 -0
- data/example/example3.rb +42 -0
- data/example/example4.rb +0 -0
- data/example/example5.rb +0 -0
- data/lib/.DS_Store +0 -0
- data/lib/auto_sql_formatter/core.rb +13 -13
- data/lib/auto_sql_formatter/formatter.rb +3 -2
- data/lib/auto_sql_formatter/runner.rb +15 -16
- data/lib/auto_sql_formatter/searcher.rb +3 -7
- data/lib/auto_sql_formatter/version.rb +1 -1
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b400f6cb9005cd9caf412e2b9a7109d6e85fd28805efbb97f4613ca59c97c40f
|
4
|
+
data.tar.gz: d4f74bf8a1e545911229967226b1f3ad601dd73d27b7ccb514f7453dd53178af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3aaff527171abb4c149a4b5987f4f02d62f738d37c362af377c95308cbfffcfae7ade069194411d167fad95b55b55cdd3d96db29190c064ed83202f43a10f7d5
|
7
|
+
data.tar.gz: 7092111e81614f6a4c5a9f3ac33830537f2e1ba3b9f985d9a0439e21e9ea68f41176be50a0b83f2592eb7a547b1aa74f29bce646040aacc2f060091a2be80c21
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
auto_sql_formatter (0.1.
|
4
|
+
auto_sql_formatter (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
diff-lcs (1.3)
|
10
|
+
export_strings (0.2.0)
|
10
11
|
rake (10.5.0)
|
11
12
|
rspec (3.8.0)
|
12
13
|
rspec-core (~> 3.8.0)
|
@@ -28,6 +29,7 @@ PLATFORMS
|
|
28
29
|
DEPENDENCIES
|
29
30
|
auto_sql_formatter!
|
30
31
|
bundler (~> 1.17)
|
32
|
+
export_strings
|
31
33
|
rake (~> 10.0)
|
32
34
|
rspec (~> 3.0)
|
33
35
|
|
data/example/example.rb
CHANGED
@@ -4,8 +4,8 @@ class Example
|
|
4
4
|
<<-SQL
|
5
5
|
sasdfasdf
|
6
6
|
adsafsdf
|
7
|
-
|
8
|
-
|
7
|
+
ssdfghs
|
8
|
+
dfasdfa
|
9
9
|
dfasdf
|
10
10
|
a asdfasdfasdfasdfsh
|
11
11
|
SQL
|
@@ -20,18 +20,18 @@ a asdfasdfasdfasdfsh
|
|
20
20
|
def sql2
|
21
21
|
"
|
22
22
|
SELECT *
|
23
|
-
|
23
|
+
from users
|
24
24
|
where user.id > 100
|
25
|
-
|
25
|
+
JOIN profiles ON profiles.user_id =users.id
|
26
26
|
"
|
27
27
|
end
|
28
28
|
|
29
29
|
def sql3
|
30
30
|
"
|
31
|
-
|
32
|
-
|
31
|
+
SELECT *
|
32
|
+
from users
|
33
33
|
where user.id > 100
|
34
|
-
|
34
|
+
JOIN profiles ON profiles.user_id =users.id
|
35
35
|
GROUP BY gender
|
36
36
|
HAVING 100 > Max(#{hogeo})
|
37
37
|
"
|
data/example/example2.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
class Example
|
2
|
+
@pokemon = 'pokemon'
|
3
|
+
|
4
|
+
def str
|
5
|
+
<<-SQL
|
6
|
+
sasdfasdf
|
7
|
+
adsafsdf
|
8
|
+
ssdfghs
|
9
|
+
dfasdfa
|
10
|
+
dfasdf
|
11
|
+
a asdfasdfasdfasdfsh
|
12
|
+
SQL
|
13
|
+
end
|
14
|
+
|
15
|
+
def sql1
|
16
|
+
<<-SQL
|
17
|
+
select * from users where user.id > 100
|
18
|
+
SQL
|
19
|
+
end
|
20
|
+
|
21
|
+
def sql2
|
22
|
+
"
|
23
|
+
SELECT *
|
24
|
+
from users
|
25
|
+
where user.id > 100 JOIN profiles ON profiles.user_id =users.id
|
26
|
+
"
|
27
|
+
end
|
28
|
+
|
29
|
+
def sql3
|
30
|
+
"
|
31
|
+
SELECT * from users
|
32
|
+
where user.id > 100
|
33
|
+
JOIN profiles ON profiles.user_id =users.id
|
34
|
+
GROUP BY gender
|
35
|
+
HAVING 100 > Max(#{hogeo})
|
36
|
+
AND HAVING 1000 < Max(#{test})
|
37
|
+
AND HAVING 1000 < Max(#{String.new})
|
38
|
+
AND HAVING 1000 < Max(#{test; test})
|
39
|
+
AND HAVING 1000 < Max(#{@pokemon})
|
40
|
+
#{!!test ? 'hogeo' : 'hoge'}
|
41
|
+
"
|
42
|
+
end
|
43
|
+
|
44
|
+
def hogeo
|
45
|
+
'test'
|
46
|
+
"test"
|
47
|
+
'test' + "test"
|
48
|
+
'test' << 'test'
|
49
|
+
end
|
50
|
+
|
51
|
+
def test
|
52
|
+
'hi'
|
53
|
+
end
|
54
|
+
|
55
|
+
def single_auot
|
56
|
+
'single_quot_str'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Example2
|
61
|
+
def hogeo2
|
62
|
+
'test'
|
63
|
+
"test"
|
64
|
+
'test' + "test"
|
65
|
+
'test' << 'test'
|
66
|
+
end
|
67
|
+
|
68
|
+
def test2
|
69
|
+
'hi'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Example
|
74
|
+
def str1
|
75
|
+
<<-SQL
|
76
|
+
SELECT *
|
77
|
+
FROM users
|
78
|
+
WHERE id > 100
|
79
|
+
SQL
|
80
|
+
end
|
81
|
+
|
82
|
+
def str2
|
83
|
+
"I like gohan. Do you like gohan.Oh Yeah."
|
84
|
+
end
|
85
|
+
|
86
|
+
def str3
|
87
|
+
"#{hello} Mikel"
|
88
|
+
end
|
89
|
+
|
90
|
+
def hello
|
91
|
+
'hello world'
|
92
|
+
end
|
93
|
+
end
|
data/example/example3.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Example3
|
2
|
+
def str
|
3
|
+
<<-SQL
|
4
|
+
sasdfasdf
|
5
|
+
adsafsdf
|
6
|
+
ssdfghs
|
7
|
+
dfasdfa
|
8
|
+
dfasdf
|
9
|
+
a asdfasdfasdfasdfsh
|
10
|
+
SQL
|
11
|
+
end
|
12
|
+
|
13
|
+
def sql1
|
14
|
+
<<-SQL
|
15
|
+
select * from users where user.id > 100
|
16
|
+
SQL
|
17
|
+
end
|
18
|
+
|
19
|
+
def sql2
|
20
|
+
"
|
21
|
+
SELECT *
|
22
|
+
from users
|
23
|
+
where user.id > 100
|
24
|
+
JOIN profiles ON profiles.user_id =users.id
|
25
|
+
"
|
26
|
+
end
|
27
|
+
|
28
|
+
def sql3
|
29
|
+
"
|
30
|
+
SELECT *
|
31
|
+
from users
|
32
|
+
where user.id > 100
|
33
|
+
JOIN profiles ON profiles.user_id =users.id
|
34
|
+
GROUP BY gender
|
35
|
+
HAVING 100 > Max(#{hogeo})
|
36
|
+
"
|
37
|
+
end
|
38
|
+
|
39
|
+
def hogeo
|
40
|
+
'test'
|
41
|
+
end
|
42
|
+
end
|
data/example/example4.rb
ADDED
File without changes
|
data/example/example5.rb
ADDED
File without changes
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -6,23 +6,23 @@ module AutoSqlFormatter
|
|
6
6
|
attr_reader :workdir
|
7
7
|
|
8
8
|
def initialize(dir = DEFAULT_DIR)
|
9
|
-
@workdir = dir
|
9
|
+
@workdir = dir
|
10
10
|
end
|
11
11
|
|
12
12
|
def execute
|
13
13
|
puts "========Start to format sql========="
|
14
|
-
|
14
|
+
puts
|
15
15
|
files.each do |file|
|
16
16
|
result = Runner.do file[:path]
|
17
|
-
display(file[:
|
17
|
+
display(file[:status] = result)
|
18
18
|
end
|
19
|
-
|
20
|
-
puts
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
puts
|
20
|
+
puts
|
21
|
+
files.each do |file|
|
22
|
+
puts "#{file[:path]} #{file[:status]}\n"
|
23
|
+
end
|
24
|
+
puts
|
25
|
+
puts '===========Completed!!!============'
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -34,12 +34,12 @@ module AutoSqlFormatter
|
|
34
34
|
# { 'path' => 'spec/sql_formatter./sql_formatter._spec.rb', 'state' => 'success' }
|
35
35
|
# ]
|
36
36
|
def files
|
37
|
-
@files ||= Dir.glob("#{workdir}/**/*.rb").map { |file| { path: file,
|
37
|
+
@files ||= Dir.glob("#{workdir}/**/*.rb").map { |file| { path: file, status: nil } }
|
38
38
|
end
|
39
39
|
|
40
40
|
# 実行中に SSSSS・・・E・SSSSみたいに表示される
|
41
|
-
def display(
|
42
|
-
str = case
|
41
|
+
def display(status)
|
42
|
+
str = case status
|
43
43
|
when 'success'
|
44
44
|
'S' #success
|
45
45
|
when 'error'
|
@@ -5,7 +5,7 @@ require 'json'
|
|
5
5
|
module AutoSqlFormatter
|
6
6
|
class Formatter
|
7
7
|
DEFAULT_INDENT = 2
|
8
|
-
DEFAULT_CASE = 'upper'
|
8
|
+
DEFAULT_CASE = 'upper'.freeze
|
9
9
|
API_URI = 'https://sqlformat.org/api/v1/format'.freeze
|
10
10
|
HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded' }.freeze
|
11
11
|
|
@@ -19,8 +19,9 @@ module AutoSqlFormatter
|
|
19
19
|
https = Net::HTTP.new(uri.host, uri.port)
|
20
20
|
https.use_ssl = true
|
21
21
|
|
22
|
-
body = URI.encode_www_form(sql: sql)
|
22
|
+
body = URI.encode_www_form(sql: sql, keyword_case: DEFAULT_CASE)
|
23
23
|
|
24
|
+
'===Net::HTTP==='
|
24
25
|
response = https.post(uri.path, body, HEADERS)
|
25
26
|
JSON.parse(response.body)['result']
|
26
27
|
end
|
@@ -1,24 +1,23 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'auto_sql_formatter/formatter'
|
2
|
+
require 'auto_sql_formatter/searcher'
|
3
3
|
|
4
4
|
module AutoSqlFormatter
|
5
5
|
class Runner
|
6
6
|
def self.do(path)
|
7
7
|
file_text = File.read(path)
|
8
|
-
sqls = Searcher.do
|
9
|
-
if sqls
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
'none'
|
8
|
+
sqls = Searcher.do(file_text)
|
9
|
+
return 'none' if sqls.empty?
|
10
|
+
|
11
|
+
# results
|
12
|
+
# [
|
13
|
+
# {
|
14
|
+
# from: old sql,
|
15
|
+
# to: new sql,
|
16
|
+
# }
|
17
|
+
# ]
|
18
|
+
results = Formatter.do sqls
|
19
|
+
results.each { |result| file_text.gsub! /#{result[:from]}/m, result[:to] }
|
20
|
+
return 'success' if File.open(path, 'w') { |f| f.write file_text }
|
22
21
|
end
|
23
22
|
class Error < StandardError; end
|
24
23
|
end
|
@@ -1,14 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'export_strings'
|
2
|
+
|
2
3
|
module AutoSqlFormatter
|
3
4
|
class Searcher
|
4
5
|
class << self
|
5
6
|
def do(file_text)
|
6
|
-
|
7
|
-
pp r = Ripper.lex(file_text)
|
8
|
-
r = Ripper.lex(file_text).reduce([]) do |acc, element|
|
9
|
-
element[1] == :on_tstring_content && sql?(element[2]) ? acc.push(element[2]) : acc
|
10
|
-
end
|
11
|
-
pp r
|
7
|
+
ExportStrings::Core.execute(file_text).each_with_object([]) { |str, memo| memo << str if sql?(str) }
|
12
8
|
end
|
13
9
|
|
14
10
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_sql_formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tashiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,11 @@ files:
|
|
73
73
|
- bin/console
|
74
74
|
- bin/setup
|
75
75
|
- example/example.rb
|
76
|
+
- example/example2.rb
|
77
|
+
- example/example3.rb
|
78
|
+
- example/example4.rb
|
79
|
+
- example/example5.rb
|
80
|
+
- lib/.DS_Store
|
76
81
|
- lib/auto_sql_formatter.rb
|
77
82
|
- lib/auto_sql_formatter/core.rb
|
78
83
|
- lib/auto_sql_formatter/formatter.rb
|
@@ -97,9 +102,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
102
|
version: '0'
|
98
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
104
|
requirements:
|
100
|
-
- - "
|
105
|
+
- - ">="
|
101
106
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
107
|
+
version: '0'
|
103
108
|
requirements: []
|
104
109
|
rubygems_version: 3.0.3
|
105
110
|
signing_key:
|