ddl_parser 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: 3ad6d742be71dfa2eeaf49b8ccf6bd97fae6d72a
4
- data.tar.gz: ba19049f73204cabe3e133825554dad7d283a765
3
+ metadata.gz: 63d4dbb66ada3b69d10294482aed60ca5b909c2f
4
+ data.tar.gz: 09795b90626029ddb528d381f663d28e9a49d3a5
5
5
  SHA512:
6
- metadata.gz: a615faff1b2ce6b70b50c641ff256101022adb97c3684a2f971f73dbc9ddc72ea0d7e54fcefe752352d32b8db5bdd00890c9dd18b13b34ee30d00b74f31b55d9
7
- data.tar.gz: 759ecd5b9dd0c77998918ba25bb015e148e108ecf9dd192cf67bd2d457b0f94b0a4f458ee7c0a24019c7a8e803e795369271b3836dc17c1038ce2729459b296a
6
+ metadata.gz: bd534f8677dcf48f106e1c453a881e17a06f42a98dfc6e09523882484cc90b56f8d1454a8c4771383d19f81c2fd6b19216895b5fd83785be4a4c4d54e9933f1b
7
+ data.tar.gz: 5a76d6eef1c476c9fe8b633ab8e085a67304261290c3a91682569d6657ee9ef9f023973a5270f15d47ef1a838b65741e4985eef02700c78f1b90dbb72625899c
@@ -1,9 +1,9 @@
1
1
  class DDLParser::Parser
2
2
 
3
- attr_accessor :statement_type, :parse_tree
3
+ attr_accessor :statement_type, :parse_tree, :parse_error
4
4
 
5
- def initialize(statement)
6
- @statement = statement.split.join(' ').downcase
5
+ def initialize(statement, do_downcase=true)
6
+ @statement = do_downcase ? statement.downcase : statement
7
7
  @parse_tree = nil
8
8
  @parse_error = nil
9
9
  begin
@@ -30,7 +30,11 @@ class DDLParser::Parser
30
30
  end
31
31
 
32
32
  def errors
33
- @parse_error.to_s if @parse_error
33
+ @parse_error.cause.ascii_tree if @parse_error
34
+ end
35
+
36
+ def most_likely_error
37
+ @parse_error.cause.ascii_tree.split("\n").last.gsub(' `- ','') if @parse_error
34
38
  end
35
39
 
36
40
  def translate
@@ -9,7 +9,7 @@ module DDLParser
9
9
  rule(:type_integer) {(str('integer')| str('int'))}
10
10
  rule(:type_bigint) {str('bigint').as(:bigint)}
11
11
  rule(:type_decimal) {(((str('decimal') | str('dec') | str('numeric') | str('num')) >>
12
- (lparen >> integer.as(:total) >> comma >> integer.as(:scale) >> rparen).as(:precision))).as(:decimal)}
12
+ (lparen >> integer.as(:total) >> (comma >> integer.as(:scale)).maybe >> rparen).as(:precision))).as(:decimal)}
13
13
  rule(:type_float) {(str('float') >> (lparen >> integer >> rparen).as(:precision)).as(:float)}
14
14
  rule(:type_real) {str('real')}
15
15
  rule(:type_double) {str('double')}
@@ -1,3 +1,3 @@
1
1
  module DDLParser
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -420,7 +420,37 @@ EOF
420
420
  result[:operation].should == 'create table'
421
421
  result[:table_name].should == 'user'
422
422
  end
423
+ it 'parses Example 17' do
423
424
 
425
+ sql = <<EOF
426
+ CREATE TABLE COST_INVOICE
427
+ (DOC_NO CHARACTER(8) NOT NULL,
428
+ STATUS CHARACTER(12) NOT NULL,
429
+ DISPUTE CHARACTER(8),
430
+ SAP_STATUS CHARACTER(1),
431
+ DEPT CHARACTER(5) NOT NULL,
432
+ FROM_DEPT CHARACTER(5) NOT NULL,
433
+ CUSTOMER_NO DECIMAL(12) NOT NULL,
434
+ SENDER_DOC_NO CHARACTER(8) NOT NULL,
435
+ INVOICE_DATE DATE NOT NULL,
436
+ DUE_DATE DATE NOT NULL,
437
+ LASTUSER CHARACTER(4) NOT NULL,
438
+ CREATE_TS TIMESTAMP NOT NULL,
439
+ UPDATE_TS TIMESTAMP NOT NULL,
440
+ UPDATE_TS_REAL_TIME TIMESTAMP NOT NULL,
441
+ UNIQUE_NO INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1 CACHE 20))
442
+ IN CPH_TS001
443
+ EOF
444
+
445
+ begin
446
+ result = parser.parse(sql.downcase)
447
+ rescue Parslet::ParseFailed => error
448
+ puts error.cause.ascii_tree
449
+ end
450
+
451
+ result[:operation].should == 'create table'
452
+ result[:table_name].should == 'cost_invoice'
453
+ end
424
454
 
425
455
  end
426
456
  end
@@ -93,4 +93,37 @@ EOF
93
93
  end
94
94
  end
95
95
 
96
+
97
+ context 'report errors' do
98
+ it 'misses right parant' do
99
+ sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
100
+ parser = DDLParser::Parser.new(sql)
101
+ parser.valid?.should == false
102
+ parser.errors.should include("Premature end of input")
103
+ end
104
+
105
+ it 'misses element delimiter' do
106
+ sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
107
+ parser = DDLParser::Parser.new(sql)
108
+ parser.valid?.should == false
109
+ parser.errors.should include('Expected ")", but got "b"')
110
+ end
111
+
112
+ it 'must likely error 1' do
113
+ sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
114
+ parser = DDLParser::Parser.new(sql)
115
+ parser.valid?.should == false
116
+ parser.most_likely_error.should == 'Premature end of input at line 1 char 62.'
117
+ end
118
+
119
+ it 'must likely error 2' do
120
+ sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
121
+ parser = DDLParser::Parser.new(sql)
122
+ parser.valid?.should == false
123
+ parser.most_likely_error.should == 'Expected ")", but got "b" at line 1 char 41.'
124
+ end
125
+
126
+ end
127
+
128
+
96
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddl_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rasmus Bergholdt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-14 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  requirements: []
128
128
  rubyforge_project:
129
- rubygems_version: 2.0.14
129
+ rubygems_version: 2.3.0
130
130
  signing_key:
131
131
  specification_version: 4
132
132
  summary: will parse statements and make it possible to extract content