sqlpp 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24d6f928aab73c2d65cf5eefc714dfac7f441641
4
- data.tar.gz: df2f51ed9d6aef74270f7dfb64322154d94dc0b3
3
+ metadata.gz: 6d62b8130206d11428dd96ac48cf586e41cfe9e1
4
+ data.tar.gz: 7b6e86ff2aed7c3245191b01a8054b8f4999634d
5
5
  SHA512:
6
- metadata.gz: dda5183a4e28c854494b164f6a725a2e35f788c1def25f256693f557cb9cdf2a95f49915d8ceb2d6c4a3491b81634f1253c30989cd913047b90597f42e39366a
7
- data.tar.gz: 980e4630ec6de08556260f31ac361366b14a250f3ee8af89d77cbbc96c734e2a4d3426d3e0e283feb9f1e3f690d2d2e71898e6fb1e17545c142c45efd24cb6c1
6
+ metadata.gz: 5e0116eb2ab2a16df63333e01959c71016d2f7ef93811ff3cbfe34f41058fbe96e2d196ff9b8765fc905cae61aa48902df4416ee7c1c6373fa901b83bd26b5d1
7
+ data.tar.gz: 73fb8adc6a50638cac01e08d1419bebe200cc7f85e16cddd77e1abf4d931c0e92d3dd9e012da8e56d96415ba62c2367053e1d4ec1e5168b52036bfe724250d43
data/README.md CHANGED
@@ -11,12 +11,24 @@ sql = "..."
11
11
  ast = SQLPP::Parser.parse(sql)
12
12
 
13
13
  puts SQLPP::Formatter.new.format(ast)
14
+
15
+ # or, to wrap projection lists...
16
+ puts SQLPP::Formatter.new(projections: :wrap).format(ast)
14
17
  ```
15
18
 
16
19
  Or, you can use the included `bin/sqlpp` script to format SQL via STDIN:
17
20
 
18
21
  ```sh
19
- $ sqlpp < query.sql
22
+ $ sqlpp -h
23
+ SQLPP (SQL Pretty Printer)
24
+
25
+ Usage: sqlpp -h -? -wp < SQL
26
+
27
+ -h or -?: this list of options
28
+ -wp: wrap the projection lists
29
+
30
+ $ sqlpp -wp < query.sql
31
+ ...
20
32
  ```
21
33
 
22
34
  ## Output
data/bin/sqlpp CHANGED
@@ -4,5 +4,18 @@ require 'sqlpp'
4
4
 
5
5
  sql = STDIN.read
6
6
 
7
+ if ARGV.grep(/^-[h?]$/).any?
8
+ puts "SQLPP (SQL Pretty Printer)"
9
+ puts
10
+ puts "Usage: #{$0} -h -? -wp < SQL"
11
+ puts
12
+ puts " -h or -?: this list of options"
13
+ puts " -wp: wrap the projection lists"
14
+ puts
15
+ exit
16
+ end
17
+
18
+ projections = ARGV.include?("-wp") ? :wrap : nil
19
+
7
20
  ast = SQLPP::Parser.parse(sql)
8
- puts SQLPP::Formatter.new.format(ast)
21
+ puts SQLPP::Formatter.new(projections: projections).format(ast)
@@ -1,8 +1,10 @@
1
1
  module SQLPP
2
2
  class Formatter
3
- def initialize
3
+ def initialize(projections: nil)
4
4
  @indent = nil
5
5
  @state = nil
6
+
7
+ @projections = projections
6
8
  end
7
9
 
8
10
  def format(node)
@@ -20,8 +22,10 @@ module SQLPP
20
22
  output << "\n"
21
23
  end
22
24
 
23
- output << "#{_indent}SELECT "
24
- output << node.projections.map { |c| format(c) }.join(", ")
25
+ output << (select = "#{_indent}SELECT ")
26
+ link = ","
27
+ link << ((@projections == :wrap) ? "\n#{" " * select.length}" : " ")
28
+ output << node.projections.map { |c| format(c) }.join(link)
25
29
  output << "\n"
26
30
 
27
31
  if node.froms
@@ -86,9 +86,9 @@ module SQLPP
86
86
  Token.new(:punct, punct, pos)
87
87
  elsif (punct = @scanner.scan(/[<>=\(\).*,\/+\-]/))
88
88
  Token.new(:punct, punct, pos)
89
- elsif @scanner.scan(/"/)
90
- contents = _scan_to_delim('"', pos)
91
- Token.new(:id, "\"#{contents}\"", pos)
89
+ elsif (delim = @scanner.scan(/["`]/))
90
+ contents = _scan_to_delim(delim, pos)
91
+ Token.new(:id, "#{delim}#{contents}#{delim}", pos)
92
92
  elsif @scanner.scan(/'/)
93
93
  contents = _scan_to_delim("'", pos)
94
94
  Token.new(:lit, "'#{contents}'", pos)
@@ -100,14 +100,17 @@ module SQLPP
100
100
  end
101
101
 
102
102
  def _scan_to_delim(delim, pos)
103
+ escape, if_peek = case delim
104
+ when '"', '`' then ["\\", nil]
105
+ when "'" then ["'", "'"]
106
+ end
107
+
103
108
  string = ""
104
109
  loop do
105
110
  ch = @scanner.getch
106
111
 
107
- if delim == '"' && ch == "\\"
112
+ if ch == escape && (if_peek.nil? || @scanner.peek(1) == if_peek)
108
113
  ch << @scanner.getch
109
- elsif delim == "'" && ch == "'"
110
- ch << @scanner.getch if @scanner.peek(1) == "'"
111
114
  end
112
115
 
113
116
  case ch
data/lib/sqlpp/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module SQLPP
2
2
  module Version
3
3
  MAJOR = 1
4
- MINOR = 0
4
+ MINOR = 1
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join(".")
@@ -31,11 +31,22 @@ ORDER BY z ASC
31
31
  SQL
32
32
  end
33
33
 
34
+ def test_format_wrap_projections
35
+ ast = _parser("select a, b, c, d from foo").parse
36
+ assert_equal <<-SQL, _format(ast, projections: :wrap)
37
+ SELECT a,
38
+ b,
39
+ c,
40
+ d
41
+ FROM foo
42
+ SQL
43
+ end
44
+
34
45
  def _parser(string)
35
46
  SQLPP::Parser.new(string)
36
47
  end
37
48
 
38
- def _format(ast)
39
- SQLPP::Formatter.new.format(ast)
49
+ def _format(ast, options={})
50
+ SQLPP::Formatter.new(options).format(ast)
40
51
  end
41
52
  end
@@ -41,7 +41,7 @@ class TokenizerTest < Minitest::Test
41
41
  end
42
42
 
43
43
  def test_it_should_recognize_identifiers
44
- _setup_tokenizer "word word123 \"quoted word\" \"with \\\"escape\\\" word\""
44
+ _setup_tokenizer "word word123 \"quoted word\" \"with \\\"escape\\\" word\" `mysql word` `mysql \\`escape\\` word`"
45
45
 
46
46
  assert_token _next, type: :id, text: "word"
47
47
  _skip :space
@@ -50,6 +50,10 @@ class TokenizerTest < Minitest::Test
50
50
  assert_token _next, type: :id, text: '"quoted word"'
51
51
  _skip :space
52
52
  assert_token _next, type: :id, text: '"with \"escape\" word"'
53
+ _skip :space
54
+ assert_token _next, type: :id, text: '`mysql word`'
55
+ _skip :space
56
+ assert_token _next, type: :id, text: '`mysql \`escape\` word`'
53
57
  end
54
58
 
55
59
  def test_it_should_recognize_number_literals
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamis Buck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-16 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake