ddl_parser 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ddl_parser/ddl/db2/parser.rb +2 -12
- data/lib/ddl_parser/parser.rb +1 -1
- data/lib/ddl_parser/translator/column.rb +0 -1
- data/lib/ddl_parser/translator/create_index_column.rb +26 -0
- data/lib/ddl_parser/translator/create_index_header.rb +45 -0
- data/lib/ddl_parser/translator.rb +7 -7
- data/lib/ddl_parser/version.rb +1 -1
- data/spec/ddl_parser/ddl/db2/{create_index_spec.rb → create_index_header_spec.rb} +55 -22
- data/spec/ddl_parser/parser_spec.rb +145 -145
- data/spec/ddl_parser/translator/index_column_spec.rb +33 -0
- data/spec/ddl_parser/translator/index_header_spec.rb +40 -0
- metadata +10 -5
- data/lib/ddl_parser/translator/create_index.rb +0 -29
@@ -94,16 +94,7 @@ module DDLParser
|
|
94
94
|
str('alter table').as(:operation) >> space? >> (identifier).as(:table_name)
|
95
95
|
}
|
96
96
|
rule(:create_index_statement) {
|
97
|
-
str('create').as(:operation) >> (space? >> str('unique').as(:object_property)).maybe >> space? >> str('index').as(:object_type) >> space? >> (identifier).as(:index_name)
|
98
|
-
}
|
99
|
-
rule(:create_index_statement_1) {
|
100
|
-
str('create').as(:operation) >> (space? >> str('unique').as(:object_property)).maybe
|
101
|
-
}
|
102
|
-
rule(:create_index_statement_1_a) {
|
103
|
-
str('create').as(:operation)
|
104
|
-
}
|
105
|
-
rule(:create_index_statement_2) {
|
106
|
-
str('index').as(:object_type) >> space? >> (identifier).as(:index_name)
|
97
|
+
str('create').as(:operation) >> (space? >> str('unique').as(:object_property)).maybe >> space? >> str('index').as(:object_type) >> space? >> (identifier).as(:index_name) >> space?
|
107
98
|
}
|
108
99
|
rule(:index_table) {
|
109
100
|
space? >> str('on') >> space? >> (identifier).as(:table_name) >> space?
|
@@ -115,9 +106,8 @@ module DDLParser
|
|
115
106
|
spaces.maybe >> alter_table_statement >> spaces >> (alter_table_element.repeat).as(:elements)
|
116
107
|
}
|
117
108
|
rule(:create_index) {
|
118
|
-
spaces.maybe >> create_index_statement >> spaces >> index_table >> spaces >> lparen >> element_list_index >> spaces.maybe >> rparen >> (space? >> index_option).maybe
|
109
|
+
spaces.maybe >> create_index_statement >> spaces >> index_table >> spaces >> lparen >> element_list_index >> spaces.maybe >> rparen >> (space? >> index_option).maybe >> space?
|
119
110
|
}
|
120
|
-
|
121
111
|
rule(:expression) { create_table | alter_table | create_index}
|
122
112
|
root :expression
|
123
113
|
end
|
data/lib/ddl_parser/parser.rb
CHANGED
@@ -44,7 +44,7 @@ class DDLParser::Parser
|
|
44
44
|
when :alter_table
|
45
45
|
DDLParser::Translator::AlterTable.new(parse_tree)
|
46
46
|
when :create_index
|
47
|
-
DDLParser::Translator::
|
47
|
+
DDLParser::Translator::CreateIndexHeader.new(parse_tree)
|
48
48
|
else
|
49
49
|
raise "Unknown statement_type #{statement_type}"
|
50
50
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class DDLParser::Translator::CreateIndexColumn
|
2
|
+
|
3
|
+
attr_accessor :parse_tree
|
4
|
+
|
5
|
+
|
6
|
+
def initialize(index_column_hash)
|
7
|
+
@index_column_hash = index_column_hash.is_a?(Hash) ? index_column_hash : {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_hash
|
11
|
+
@index_column_hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def index_column
|
15
|
+
@index_column_hash[:index_column]
|
16
|
+
end
|
17
|
+
|
18
|
+
def sequence_no
|
19
|
+
@index_column_hash[:sequence_no]
|
20
|
+
end
|
21
|
+
|
22
|
+
def sort_type
|
23
|
+
@index_column_hash[:sort_type]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class DDLParser::Translator::CreateIndexHeader
|
2
|
+
|
3
|
+
attr_accessor :index_head, :index_columns
|
4
|
+
|
5
|
+
|
6
|
+
def initialize(index_hash)
|
7
|
+
|
8
|
+
if index_hash.is_a?(Array) and index_hash.count > 1
|
9
|
+
@index_head = index_hash.first.is_a?(Hash) ? index_hash.first : {}
|
10
|
+
@index_columns = index_hash[1..-1]
|
11
|
+
else
|
12
|
+
@index_head = {}
|
13
|
+
@index_columns = []
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_hash
|
18
|
+
{:head => @index_head, :columns => @index_columns}
|
19
|
+
end
|
20
|
+
|
21
|
+
def index_name
|
22
|
+
@index_head[:index_name]
|
23
|
+
end
|
24
|
+
|
25
|
+
def index_table
|
26
|
+
@index_head[:index_table]
|
27
|
+
end
|
28
|
+
|
29
|
+
def table_name
|
30
|
+
@index_head[:table_name]
|
31
|
+
end
|
32
|
+
|
33
|
+
def object_property
|
34
|
+
@index_head[:object_property]
|
35
|
+
end
|
36
|
+
|
37
|
+
def option_reverse_scans
|
38
|
+
@index_head[:option_reverse_scans]
|
39
|
+
end
|
40
|
+
|
41
|
+
def columns
|
42
|
+
index_columns.map{|e|DDLParser::Translator::Column.new(e)}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
module DDLParser::Translator
|
2
|
-
end
|
3
|
-
|
4
|
-
require 'ddl_parser/translator/column'
|
5
|
-
require 'ddl_parser/translator/create_table'
|
6
|
-
require 'ddl_parser/translator/alter_table'
|
7
|
-
require 'ddl_parser/translator/
|
1
|
+
module DDLParser::Translator
|
2
|
+
end
|
3
|
+
|
4
|
+
require 'ddl_parser/translator/column'
|
5
|
+
require 'ddl_parser/translator/create_table'
|
6
|
+
require 'ddl_parser/translator/alter_table'
|
7
|
+
require 'ddl_parser/translator/create_index_header'
|
data/lib/ddl_parser/version.rb
CHANGED
@@ -42,11 +42,9 @@ EOF
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
context 'element list index' do
|
45
|
-
it 'parses one element'
|
45
|
+
it 'parses one element' do
|
46
46
|
sql = 'deptno asc'
|
47
|
-
parser.element_list_index.parse(sql).should == {:
|
48
|
-
:data_type=>{:char=>{:length=>{:integer=> '3'}}},
|
49
|
-
:options=>[{:column_option=> 'not null'}]}}}
|
47
|
+
parser.element_list_index.parse(sql).should == {:field=> 'deptno',:sort_id=> 'asc'}
|
50
48
|
end
|
51
49
|
it 'parses two element' do
|
52
50
|
sql = 'field1, field2'
|
@@ -86,32 +84,27 @@ EOF
|
|
86
84
|
end
|
87
85
|
it 'parses index_option with reverse scans' do
|
88
86
|
sql = 'allow reverse scans'
|
89
|
-
parser.index_option.parse(sql).should
|
87
|
+
parser.index_option.parse(sql).should include(:index_option => 'allow reverse scans')
|
90
88
|
end
|
91
89
|
end
|
92
90
|
context 'create_index_statement' do
|
93
|
-
it '
|
91
|
+
it 'create_index_statement_1_unique_1' do
|
94
92
|
sql = <<EOF
|
95
|
-
CREATE
|
93
|
+
CREATE UNIQUE INDEX TEST_INDEX
|
96
94
|
EOF
|
97
|
-
parser.
|
95
|
+
parser.create_index_statement.parse(sql.downcase.strip).should include(:operation=>'create')
|
98
96
|
|
99
97
|
end
|
100
|
-
it '
|
101
|
-
|
102
|
-
CREATE
|
103
|
-
EOF
|
104
|
-
parser.create_index_statement_1_a.parse(sql.downcase).should == {:operation=>'create',:object_property=>'unique'}
|
105
|
-
|
98
|
+
it 'create_index_test_statement_unique_1_a' do
|
99
|
+
parser.create_index_statement.parse('create unique index test_index').should == {:operation=>'create',:object_property=>'unique',:object_type=>'index',:index_name=>'test_index'}
|
106
100
|
end
|
107
|
-
it '
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
parser.
|
112
|
-
|
101
|
+
it 'create_index_test_statement_unique_1_b' do
|
102
|
+
parser.create_index_statement.parse('create unique index test_index').should include(:operation=>'create')
|
103
|
+
end
|
104
|
+
it 'create_index_statement_unique_3' do
|
105
|
+
parser.create_index_statement.parse('create index cost_invoice_1').should == {:operation=>'create',:object_type=>'index',:index_name=>'cost_invoice_1'}
|
113
106
|
end
|
114
|
-
it '
|
107
|
+
it 'create_index_statement_unique_4' do
|
115
108
|
sql = <<EOF
|
116
109
|
CREATE UNIQUE INDEX COST_INVOICE_1
|
117
110
|
EOF
|
@@ -129,12 +122,33 @@ EOF
|
|
129
122
|
end
|
130
123
|
end
|
131
124
|
context 'full index example with reverse scan option' do
|
132
|
-
it 'with reverse scan'
|
125
|
+
it 'with reverse scan' do
|
133
126
|
sql = <<EOF
|
134
127
|
CREATE UNIQUE INDEX COST_INVOICE_1
|
135
128
|
ON COST_INVOICE
|
136
129
|
(DOC_NO)
|
137
130
|
ALLOW REVERSE SCANS
|
131
|
+
EOF
|
132
|
+
begin
|
133
|
+
result = parser.parse(sql.downcase)
|
134
|
+
rescue Parslet::ParseFailed => error
|
135
|
+
puts error.cause.ascii_tree
|
136
|
+
end
|
137
|
+
result.count.should == 7
|
138
|
+
result.should include(:operation => 'create')
|
139
|
+
result.should include(:object_property => 'unique')
|
140
|
+
result.should include(:object_type => 'index')
|
141
|
+
result.should include(:index_name => 'cost_invoice_1')
|
142
|
+
result.should include(:table_name => 'cost_invoice')
|
143
|
+
result.should include(:field => 'doc_no')
|
144
|
+
result.should include(:index_option => 'allow reverse scans')
|
145
|
+
|
146
|
+
end
|
147
|
+
it 'without reverse scan' do
|
148
|
+
sql = <<EOF
|
149
|
+
CREATE UNIQUE INDEX COST_INVOICE_1
|
150
|
+
ON COST_INVOICE
|
151
|
+
(DOC_NO)
|
138
152
|
EOF
|
139
153
|
begin
|
140
154
|
result = parser.parse(sql.downcase)
|
@@ -150,5 +164,24 @@ EOF
|
|
150
164
|
result.should include(:field => 'doc_no')
|
151
165
|
|
152
166
|
end
|
167
|
+
it 'live example' do
|
168
|
+
sql = <<EOF
|
169
|
+
CREATE INDEX COST_JOB_AGR_1
|
170
|
+
ON COST_JOB_AGR
|
171
|
+
(JOB_ID)
|
172
|
+
EOF
|
173
|
+
begin
|
174
|
+
result = parser.parse(sql.downcase)
|
175
|
+
rescue Parslet::ParseFailed => error
|
176
|
+
puts error.cause.ascii_tree
|
177
|
+
end
|
178
|
+
result.count.should == 5
|
179
|
+
result.should include(:operation => 'create')
|
180
|
+
result.should include(:object_type => 'index')
|
181
|
+
result.should include(:index_name => 'cost_job_agr_1')
|
182
|
+
result.should include(:table_name => 'cost_job_agr')
|
183
|
+
result.should include(:field => 'job_id')
|
184
|
+
|
185
|
+
end
|
153
186
|
end
|
154
187
|
end
|
@@ -1,146 +1,146 @@
|
|
1
|
-
require_relative '../../spec/spec_helper'
|
2
|
-
|
3
|
-
describe 'DDLParser::Parser' do
|
4
|
-
|
5
|
-
it "knows if it's valid" do
|
6
|
-
sql = 'CREATE TABLE TEST (ID INT)'
|
7
|
-
parser = DDLParser::Parser.new(sql)
|
8
|
-
parser.valid?.should == true
|
9
|
-
end
|
10
|
-
|
11
|
-
it "knows if it's invalid" do
|
12
|
-
sql = 'CREATE TABLE TEST ()'
|
13
|
-
parser = DDLParser::Parser.new(sql)
|
14
|
-
parser.valid?.should == false
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'can report errors' do
|
18
|
-
sql = 'CREATE TABLE TEST ()'
|
19
|
-
parser = DDLParser::Parser.new(sql)
|
20
|
-
parser.errors.should include('CREATE_TABLE_STATEMENT')
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'statement_type' do
|
24
|
-
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
25
|
-
parser = DDLParser::Parser.new(sql)
|
26
|
-
parser.statement_type.should == :create_table
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'returns the parse tree' do
|
30
|
-
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
31
|
-
parser = DDLParser::Parser.new(sql)
|
32
|
-
parser.parse_tree.should == {:operation=> 'create table', :table_name=> 'test', :elements=>[
|
33
|
-
{:column=>{:field=> 'id', :data_type=> 'int', :options=> ''}},
|
34
|
-
{:column=>{:field=> 'foo', :data_type=> 'int', :options=> ''}}]}
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'create table translate' do
|
38
|
-
it 'translate table_name' do
|
39
|
-
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
40
|
-
parser = DDLParser::Parser.new(sql)
|
41
|
-
parser.translate.table_name.should == 'test'
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
it 'translate columns' do
|
46
|
-
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
47
|
-
parser = DDLParser::Parser.new(sql)
|
48
|
-
parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}, {:field=> 'foo', :data_type=> 'int', :options=> ''}]
|
49
|
-
|
50
|
-
sql = 'CREATE TABLE TEST (ID INT)'
|
51
|
-
parser = DDLParser::Parser.new(sql)
|
52
|
-
parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}]
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'translate primary key' do
|
56
|
-
sql = 'CREATE TABLE TEST (ID INT, PRIMARY KEY (ID))'
|
57
|
-
parser = DDLParser::Parser.new(sql)
|
58
|
-
parser.translate.primary_key.should == 'id'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'alter table translate' do
|
63
|
-
let(:sql) {
|
64
|
-
sql = <<EOF
|
65
|
-
ALTER TABLE BUDGET
|
66
|
-
ADD COLUMN BUDGET_AMOUNT_IN_CO DECIMAL(15,2) NOT NULL DEFAULT 0
|
67
|
-
ADD COLUMN BUDGET_AMOUNT_CURR CHARACTER(4)
|
68
|
-
EOF
|
69
|
-
sql
|
70
|
-
}
|
71
|
-
|
72
|
-
it 'translate table_name' do
|
73
|
-
parser = DDLParser::Parser.new(sql)
|
74
|
-
parser.translate.table_name.should == 'budget'
|
75
|
-
end
|
76
|
-
|
77
|
-
|
78
|
-
it 'translate columns' do
|
79
|
-
parser = DDLParser::Parser.new(sql)
|
80
|
-
parser.translate.add_columns.map(&:to_hash).should == [{:field=> 'budget_amount_in_co',
|
81
|
-
:data_type=>{:decimal=>{:precision=>{:total=>{:integer=> '15'}, :scale=>{:integer=> '2'}}}},
|
82
|
-
:options=>[{:column_option=> 'not null'},
|
83
|
-
{:default_clause=>[{:integer=> '0'}]}]},
|
84
|
-
{:field=> 'budget_amount_curr',
|
85
|
-
:data_type=>{:char=>{:length=>{:integer=> '4'}}},
|
86
|
-
:options=> ''}]
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'translate primary key' do
|
90
|
-
sql = 'CREATE TABLE TEST (ID INT, PRIMARY KEY (ID))'
|
91
|
-
parser = DDLParser::Parser.new(sql)
|
92
|
-
parser.translate.primary_key.should == 'id'
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context 'create index translate' do
|
97
|
-
before :each do
|
98
|
-
sql = 'CREATE UNIQUE INDEX REPLAN_ADDR_1 ON REPLAN_ADDR (CONSIGNMENT ASCENDING, COLLI DESCENDING)'
|
99
|
-
@parser = DDLParser::Parser.new(sql)
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'table name' do
|
103
|
-
@parser.translate.table_name.should == 'replan_addr'
|
104
|
-
end
|
105
|
-
it 'index name' do
|
106
|
-
@parser.translate.index_name.should == 'replan_addr_1'
|
107
|
-
end
|
108
|
-
it 'columns' do
|
109
|
-
@parser.translate.
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
context 'report errors' do
|
115
|
-
it 'misses right parant' do
|
116
|
-
sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
|
117
|
-
parser = DDLParser::Parser.new(sql)
|
118
|
-
parser.valid?.should == false
|
119
|
-
parser.errors.should include("Premature end of input")
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'misses element delimiter' do
|
123
|
-
sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
|
124
|
-
parser = DDLParser::Parser.new(sql)
|
125
|
-
parser.valid?.should == false
|
126
|
-
parser.errors.should include('Expected ")", but got "b"')
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'must likely error 1' do
|
130
|
-
sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
|
131
|
-
parser = DDLParser::Parser.new(sql)
|
132
|
-
parser.valid?.should == false
|
133
|
-
parser.most_likely_error.should == 'Premature end of input at line 1 char 62.'
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'must likely error 2' do
|
137
|
-
sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
|
138
|
-
parser = DDLParser::Parser.new(sql)
|
139
|
-
parser.valid?.should == false
|
140
|
-
parser.most_likely_error.should == 'Expected ")", but got "b" at line 1 char 41.'
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
|
1
|
+
require_relative '../../spec/spec_helper'
|
2
|
+
|
3
|
+
describe 'DDLParser::Parser' do
|
4
|
+
|
5
|
+
it "knows if it's valid" do
|
6
|
+
sql = 'CREATE TABLE TEST (ID INT)'
|
7
|
+
parser = DDLParser::Parser.new(sql)
|
8
|
+
parser.valid?.should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "knows if it's invalid" do
|
12
|
+
sql = 'CREATE TABLE TEST ()'
|
13
|
+
parser = DDLParser::Parser.new(sql)
|
14
|
+
parser.valid?.should == false
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'can report errors' do
|
18
|
+
sql = 'CREATE TABLE TEST ()'
|
19
|
+
parser = DDLParser::Parser.new(sql)
|
20
|
+
parser.errors.should include('CREATE_TABLE_STATEMENT')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'statement_type' do
|
24
|
+
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
25
|
+
parser = DDLParser::Parser.new(sql)
|
26
|
+
parser.statement_type.should == :create_table
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns the parse tree' do
|
30
|
+
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
31
|
+
parser = DDLParser::Parser.new(sql)
|
32
|
+
parser.parse_tree.should == {:operation=> 'create table', :table_name=> 'test', :elements=>[
|
33
|
+
{:column=>{:field=> 'id', :data_type=> 'int', :options=> ''}},
|
34
|
+
{:column=>{:field=> 'foo', :data_type=> 'int', :options=> ''}}]}
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'create table translate' do
|
38
|
+
it 'translate table_name' do
|
39
|
+
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
40
|
+
parser = DDLParser::Parser.new(sql)
|
41
|
+
parser.translate.table_name.should == 'test'
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
it 'translate columns' do
|
46
|
+
sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
|
47
|
+
parser = DDLParser::Parser.new(sql)
|
48
|
+
parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}, {:field=> 'foo', :data_type=> 'int', :options=> ''}]
|
49
|
+
|
50
|
+
sql = 'CREATE TABLE TEST (ID INT)'
|
51
|
+
parser = DDLParser::Parser.new(sql)
|
52
|
+
parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}]
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'translate primary key' do
|
56
|
+
sql = 'CREATE TABLE TEST (ID INT, PRIMARY KEY (ID))'
|
57
|
+
parser = DDLParser::Parser.new(sql)
|
58
|
+
parser.translate.primary_key.should == 'id'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'alter table translate' do
|
63
|
+
let(:sql) {
|
64
|
+
sql = <<EOF
|
65
|
+
ALTER TABLE BUDGET
|
66
|
+
ADD COLUMN BUDGET_AMOUNT_IN_CO DECIMAL(15,2) NOT NULL DEFAULT 0
|
67
|
+
ADD COLUMN BUDGET_AMOUNT_CURR CHARACTER(4)
|
68
|
+
EOF
|
69
|
+
sql
|
70
|
+
}
|
71
|
+
|
72
|
+
it 'translate table_name' do
|
73
|
+
parser = DDLParser::Parser.new(sql)
|
74
|
+
parser.translate.table_name.should == 'budget'
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
it 'translate columns' do
|
79
|
+
parser = DDLParser::Parser.new(sql)
|
80
|
+
parser.translate.add_columns.map(&:to_hash).should == [{:field=> 'budget_amount_in_co',
|
81
|
+
:data_type=>{:decimal=>{:precision=>{:total=>{:integer=> '15'}, :scale=>{:integer=> '2'}}}},
|
82
|
+
:options=>[{:column_option=> 'not null'},
|
83
|
+
{:default_clause=>[{:integer=> '0'}]}]},
|
84
|
+
{:field=> 'budget_amount_curr',
|
85
|
+
:data_type=>{:char=>{:length=>{:integer=> '4'}}},
|
86
|
+
:options=> ''}]
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'translate primary key' do
|
90
|
+
sql = 'CREATE TABLE TEST (ID INT, PRIMARY KEY (ID))'
|
91
|
+
parser = DDLParser::Parser.new(sql)
|
92
|
+
parser.translate.primary_key.should == 'id'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'create index translate' do
|
97
|
+
before :each do
|
98
|
+
sql = 'CREATE UNIQUE INDEX REPLAN_ADDR_1 ON REPLAN_ADDR (CONSIGNMENT ASCENDING, COLLI DESCENDING)'
|
99
|
+
@parser = DDLParser::Parser.new(sql)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'table name' do
|
103
|
+
@parser.translate.table_name.should == 'replan_addr'
|
104
|
+
end
|
105
|
+
it 'index name' do
|
106
|
+
@parser.translate.index_name.should == 'replan_addr_1'
|
107
|
+
end
|
108
|
+
it 'columns' do
|
109
|
+
@parser.translate.columns.map(&:to_hash).should == [{:field=>"consignment", :sort_id=>"ascending"}, {:field=>"colli", :sort_id=>"descending"}]
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'report errors' do
|
115
|
+
it 'misses right parant' do
|
116
|
+
sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
|
117
|
+
parser = DDLParser::Parser.new(sql)
|
118
|
+
parser.valid?.should == false
|
119
|
+
parser.errors.should include("Premature end of input")
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'misses element delimiter' do
|
123
|
+
sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
|
124
|
+
parser = DDLParser::Parser.new(sql)
|
125
|
+
parser.valid?.should == false
|
126
|
+
parser.errors.should include('Expected ")", but got "b"')
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'must likely error 1' do
|
130
|
+
sql = "Create table USER (FOO CHAR(4) NOT NULL, BAR CHAR(4) NOT NULL"
|
131
|
+
parser = DDLParser::Parser.new(sql)
|
132
|
+
parser.valid?.should == false
|
133
|
+
parser.most_likely_error.should == 'Premature end of input at line 1 char 62.'
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'must likely error 2' do
|
137
|
+
sql = "Create table USER (FOO CHAR(4) NOT NULL BAR CHAR(4) NOT NULL)"
|
138
|
+
parser = DDLParser::Parser.new(sql)
|
139
|
+
parser.valid?.should == false
|
140
|
+
parser.most_likely_error.should == 'Expected ")", but got "b" at line 1 char 41.'
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
146
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative '../../../spec/spec_helper'
|
2
|
+
require_relative '../../../lib/ddl_parser/translator/create_index_column'
|
3
|
+
|
4
|
+
describe 'DDLParser::Translator::CreateIndexColumn' do
|
5
|
+
|
6
|
+
let(:unique_no_column) {
|
7
|
+
DDLParser::Translator::CreateIndexColumn.new(
|
8
|
+
{:index_column=> 'test_index_column',
|
9
|
+
:sequence_no=> 5,
|
10
|
+
:sort_type=> 'asc'
|
11
|
+
})
|
12
|
+
}
|
13
|
+
|
14
|
+
let(:index_name_test) {
|
15
|
+
DDLParser::Translator::CreateIndexColumn.new({:field=> 'test_index'})
|
16
|
+
}
|
17
|
+
|
18
|
+
let(:table_name) {
|
19
|
+
DDLParser::Translator::CreateIndexColumn.new({:field=> 'table_name'})
|
20
|
+
}
|
21
|
+
|
22
|
+
it 'must receive hash' do
|
23
|
+
DDLParser::Translator::CreateIndexColumn.new('').to_hash.should == {}
|
24
|
+
DDLParser::Translator::CreateIndexColumn.new({:foo => :bar}).to_hash.should == {:foo => :bar}
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should return name' do
|
28
|
+
unique_no_column.index_column.should == 'test_index_column'
|
29
|
+
unique_no_column.sequence_no.should == 5
|
30
|
+
unique_no_column.sort_type.should == 'asc'
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative '../../../spec/spec_helper'
|
2
|
+
require_relative '../../../lib/ddl_parser/translator/create_index_header'
|
3
|
+
|
4
|
+
describe 'DDLParser::Translator::CreateIndexHeader' do
|
5
|
+
|
6
|
+
let(:unique_no_column) {
|
7
|
+
DDLParser::Translator::CreateIndexHeader.new(
|
8
|
+
[{:index_name=> 'test_index',
|
9
|
+
:index_table=> 'on test_table',
|
10
|
+
:table_name=> 'test_table',
|
11
|
+
:object_property=> 'unique',
|
12
|
+
:option_reverse_scans=>'allow reverse scans'
|
13
|
+
}, {}])
|
14
|
+
}
|
15
|
+
|
16
|
+
let(:index_name_test) {
|
17
|
+
DDLParser::Translator::CreateIndexHeader.new({:field=> 'test_index'})
|
18
|
+
|
19
|
+
# DDLParser::Translator::Column.new({:field=> 'int_field', :data_type=> 'int', :options=> ''})
|
20
|
+
}
|
21
|
+
|
22
|
+
let(:table_name) {
|
23
|
+
DDLParser::Translator::CreateIndexHeader.new({:field=> 'table_name'})
|
24
|
+
}
|
25
|
+
|
26
|
+
it 'must receive Array of hashes' do
|
27
|
+
DDLParser::Translator::CreateIndexHeader.new('').to_hash.should == {:head => {}, :columns => []}
|
28
|
+
DDLParser::Translator::CreateIndexHeader.new([{:foo => :bar}, {:bar => :foo}]).to_hash.
|
29
|
+
should == {:head => {:foo => :bar}, :columns => [{:bar => :foo}]}
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return name' do
|
33
|
+
unique_no_column.index_name.should == 'test_index'
|
34
|
+
unique_no_column.index_table.should == 'on test_table'
|
35
|
+
unique_no_column.table_name.should == 'test_table'
|
36
|
+
unique_no_column.object_property.should == 'unique'
|
37
|
+
unique_no_column.option_reverse_scans.should == 'allow reverse scans'
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ddl_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-09-
|
12
|
+
date: 2014-09-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|
@@ -105,18 +105,21 @@ files:
|
|
105
105
|
- lib/ddl_parser/translator.rb
|
106
106
|
- lib/ddl_parser/translator/alter_table.rb
|
107
107
|
- lib/ddl_parser/translator/column.rb
|
108
|
-
- lib/ddl_parser/translator/
|
108
|
+
- lib/ddl_parser/translator/create_index_column.rb
|
109
|
+
- lib/ddl_parser/translator/create_index_header.rb
|
109
110
|
- lib/ddl_parser/translator/create_table.rb
|
110
111
|
- lib/ddl_parser/version.rb
|
111
112
|
- lib/parslet_extentions.rb
|
112
113
|
- spec/ddl_parser/ddl/db2/alter_table_spec.rb
|
113
|
-
- spec/ddl_parser/ddl/db2/
|
114
|
+
- spec/ddl_parser/ddl/db2/create_index_header_spec.rb
|
114
115
|
- spec/ddl_parser/ddl/db2/create_table_spec.rb
|
115
116
|
- spec/ddl_parser/parser_spec.rb
|
116
117
|
- spec/ddl_parser/shared_rules/constants_spec.rb
|
117
118
|
- spec/ddl_parser/shared_rules/data_types_spec.rb
|
118
119
|
- spec/ddl_parser/sql/db2/select_parser_spec.rb
|
119
120
|
- spec/ddl_parser/translator/column_spec.rb
|
121
|
+
- spec/ddl_parser/translator/index_column_spec.rb
|
122
|
+
- spec/ddl_parser/translator/index_header_spec.rb
|
120
123
|
- spec/spec_helper.rb
|
121
124
|
homepage: ''
|
122
125
|
licenses:
|
@@ -145,11 +148,13 @@ specification_version: 3
|
|
145
148
|
summary: will parse statements and make it possible to extract content
|
146
149
|
test_files:
|
147
150
|
- spec/ddl_parser/ddl/db2/alter_table_spec.rb
|
148
|
-
- spec/ddl_parser/ddl/db2/
|
151
|
+
- spec/ddl_parser/ddl/db2/create_index_header_spec.rb
|
149
152
|
- spec/ddl_parser/ddl/db2/create_table_spec.rb
|
150
153
|
- spec/ddl_parser/parser_spec.rb
|
151
154
|
- spec/ddl_parser/shared_rules/constants_spec.rb
|
152
155
|
- spec/ddl_parser/shared_rules/data_types_spec.rb
|
153
156
|
- spec/ddl_parser/sql/db2/select_parser_spec.rb
|
154
157
|
- spec/ddl_parser/translator/column_spec.rb
|
158
|
+
- spec/ddl_parser/translator/index_column_spec.rb
|
159
|
+
- spec/ddl_parser/translator/index_header_spec.rb
|
155
160
|
- spec/spec_helper.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
class DDLParser::Translator::CreateIndex
|
2
|
-
|
3
|
-
attr_accessor :parse_tree
|
4
|
-
|
5
|
-
def initialize(parse_tree)
|
6
|
-
@parse_tree=parse_tree
|
7
|
-
end
|
8
|
-
|
9
|
-
def elements
|
10
|
-
[@parse_tree[:elements]].flatten
|
11
|
-
end
|
12
|
-
|
13
|
-
def table_name
|
14
|
-
@parse_tree.first[:table_name].to_s
|
15
|
-
end
|
16
|
-
|
17
|
-
def index_name
|
18
|
-
@parse_tree.first[:index_name].to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
def fields
|
22
|
-
@parse_tree.select{|e|e.has_key?(:field)}.compact
|
23
|
-
end
|
24
|
-
|
25
|
-
def primary_key
|
26
|
-
elements.map{|e| e[:primary_key]}.compact.first[:item]
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|