auto_sql_formatter 0.1.0.dev → 0.1.1

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
  SHA256:
3
- metadata.gz: c17489584eef662868c6ebb8121706be9d7284b43dc30a778c0fbf02b61b068b
4
- data.tar.gz: 977aa3d4d992399a4e1a9d83db5ac9d7f18226327bb8376d10470c8a9f3aff8f
3
+ metadata.gz: b400f6cb9005cd9caf412e2b9a7109d6e85fd28805efbb97f4613ca59c97c40f
4
+ data.tar.gz: d4f74bf8a1e545911229967226b1f3ad601dd73d27b7ccb514f7453dd53178af
5
5
  SHA512:
6
- metadata.gz: 87c9e3f39d9c9a4de832f6e9691d3d64a738f5f62e618d1d3c537667bbb2837dc6913694efb63fec946ec6a306e02ff6bbc6f99e7f6a6cd4306a4b6f882ab04a
7
- data.tar.gz: 024ca5e191480229b795c079fea1e8639ba5eeb3431dac75f356478c1dbe64a2a53d92597021a8d74b11dfc2749387821ed62a58fe4edd1aaf89463458a4d964
6
+ metadata.gz: 3aaff527171abb4c149a4b5987f4f02d62f738d37c362af377c95308cbfffcfae7ade069194411d167fad95b55b55cdd3d96db29190c064ed83202f43a10f7d5
7
+ data.tar.gz: 7092111e81614f6a4c5a9f3ac33830537f2e1ba3b9f985d9a0439e21e9ea68f41176be50a0b83f2592eb7a547b1aa74f29bce646040aacc2f060091a2be80c21
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in auto_sql_formatter.gemspec
6
6
  gemspec
7
+
8
+ gem 'export_strings'
data/Gemfile.lock CHANGED
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- auto_sql_formatter (0.1.0.dev)
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
- ssdfghs
8
- dfasdfa
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
- from users
23
+ from users
24
24
  where user.id > 100
25
- JOIN profiles ON profiles.user_id =users.id
25
+ JOIN profiles ON profiles.user_id =users.id
26
26
  "
27
27
  end
28
28
 
29
29
  def sql3
30
30
  "
31
- SELECT *
32
- from users
31
+ SELECT *
32
+ from users
33
33
  where user.id > 100
34
- JOIN profiles ON profiles.user_id =users.id
34
+ JOIN profiles ON profiles.user_id =users.id
35
35
  GROUP BY gender
36
36
  HAVING 100 > Max(#{hogeo})
37
37
  "
@@ -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
@@ -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
File without changes
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 || DEFAULT_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[:state] = result)
17
+ display(file[:status] = result)
18
18
  end
19
-
20
- puts "===========Completed!!!============"
21
-
22
- puts <<-STR
23
- Targets and statuses are
24
- #{files.each { |file| "・#{file[:path]} #{file[:status]}/n"}}
25
- STR
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, state: nil } }
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(state)
42
- str = case state
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 "auto_sql_formatter/formatter"
2
- require "auto_sql_formatter/searcher"
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 file_text
9
- if sqls
10
- # results
11
- # [
12
- # {
13
- # from: old sql,
14
- # to: new sql,
15
- # }
16
- # ]
17
- results = Formatter.do sqls
18
- results.each { |result| file_text.gsub! Regexp.new(result[:from]), result[:to] }
19
- return 'success' if File.open(path, 'w') { |f| f.write file_text }
20
- end
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 'ripper'
1
+ require 'export_strings'
2
+
2
3
  module AutoSqlFormatter
3
4
  class Searcher
4
5
  class << self
5
6
  def do(file_text)
6
- pp r = Ripper.sexp(file_text)
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
@@ -1,3 +1,3 @@
1
1
  module AutoSqlFormatter
2
- VERSION = "0.1.0.dev"
2
+ VERSION = "0.1.1"
3
3
  end
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.0.dev
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-05-27 00:00:00.000000000 Z
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: 1.3.1
107
+ version: '0'
103
108
  requirements: []
104
109
  rubygems_version: 3.0.3
105
110
  signing_key: