ddl_parser 0.0.6 → 0.0.7

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: 26f392271d3cbfa84aea5054dbb9443153254c72
4
- data.tar.gz: 51fb85b30b4ddbefd547122f39b21d5a5827d1c6
3
+ metadata.gz: ef82b48ea80076815061bcbf9796dfc705378cb8
4
+ data.tar.gz: cc04e393d7673c5941164bf953b2a7e3256d536a
5
5
  SHA512:
6
- metadata.gz: d625d2c3a66f87b9a02c09fa5fa8193c48ea071814d97c971689a9ab5d40ac2900fdc2aea80e10fc89b0c0a6543424f2a30a9a578bdf19527d8023ae592a63f4
7
- data.tar.gz: f3380a9731100e2214efb130c368e0a23fc76d640ec455d79d7afcbecf86998fb92b8bdaa0d31013a76180be9dd39085dd8ad407cb5252c32272c5a9f6bb0da1
6
+ metadata.gz: 8ba708dd93de83da7b45c1a207ded32c4640b6bb2da148feb9402aa0798a540aeda239591c65ff1559e71d4df502f0ac5a252e816751d5aed9a5290450d42883
7
+ data.tar.gz: ac3de3daa335758e0f153d03f7f6bcc8943d755b0a0b18bf187cc4c7bb70d87f9d2c5eed0c5a492c6bb5fc017df754f637445cf57cdb16b8ca50b53091d53c64
@@ -14,7 +14,7 @@ class DDLParser::Translator::AlterTable
14
14
  end
15
15
 
16
16
  def add_columns
17
- elements.map{|e|e[:add]}.compact.map{|e|e[:column]}.compact
17
+ elements.map{|e|e[:add]}.compact.map{|e|e[:column]}.compact.map{|e|DDLParser::Translator::Column.new(e)}
18
18
  end
19
19
 
20
20
  end
@@ -0,0 +1,80 @@
1
+ class DDLParser::Translator::Column
2
+
3
+ def initialize(column_hash)
4
+ @column_hash = column_hash.is_a?(Hash) ? column_hash : {}
5
+ end
6
+
7
+ def to_hash
8
+ @column_hash
9
+ end
10
+
11
+ def name
12
+ @column_hash[:field]
13
+ end
14
+
15
+ def data_type
16
+ return if @column_hash.nil?
17
+ if @column_hash[:data_type].is_a?(Hash)
18
+ @column_hash[:data_type].keys.first
19
+ else
20
+ @column_hash[:data_type].to_sym
21
+ end
22
+
23
+ end
24
+
25
+ def length
26
+ case data_type
27
+ when :decimal
28
+ "#{precision}.#{scale}".to_f
29
+ when :char
30
+ @column_hash[:data_type][:char][:length][:integer].to_i
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def precision
37
+ case data_type
38
+ when :decimal
39
+ @column_hash[:data_type][:decimal][:precision][:total][:integer].to_i
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
45
+ def scale
46
+ case data_type
47
+ when :decimal
48
+ @column_hash[:data_type][:decimal][:precision][:scale][:integer].to_i
49
+ else
50
+ nil
51
+ end
52
+ end
53
+
54
+ def options
55
+ @column_hash[:options] if @column_hash[:options].length > 0
56
+ end
57
+
58
+ def not_null
59
+ if options.nil?
60
+ false
61
+ else
62
+ options.select{|h|h.has_key?(:column_option) && h.has_value?('not null')}.length > 0
63
+ end
64
+ end
65
+
66
+ def default_clause
67
+ unless options.nil?
68
+ default_value = options.select{|h|h.has_key?(:default_clause)}.first
69
+ if default_value
70
+ default_value[:default_clause].each do |c|
71
+ # TODO must handle other then integer
72
+ if c.has_key?(:integer)
73
+ return c[:integer].to_i
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ end
@@ -13,7 +13,7 @@ class DDLParser::Translator::CreateTable
13
13
  end
14
14
 
15
15
  def columns
16
- elements.map{|e|e[:column]}.compact
16
+ elements.map{|e|e[:column]}.compact.map{|e|DDLParser::Translator::Column.new(e)}
17
17
  end
18
18
 
19
19
  def primary_key
@@ -1,6 +1,7 @@
1
1
  module DDLParser::Translator
2
2
  end
3
3
 
4
+ require 'ddl_parser/translator/column'
4
5
  require 'ddl_parser/translator/create_table'
5
6
  require 'ddl_parser/translator/alter_table'
6
7
  require 'ddl_parser/translator/create_index'
@@ -1,3 +1,3 @@
1
1
  module DDLParser
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -45,11 +45,11 @@ describe 'DDLParser::Parser' do
45
45
  it 'translate columns' do
46
46
  sql = 'CREATE TABLE TEST (ID INT, FOO INT)'
47
47
  parser = DDLParser::Parser.new(sql)
48
- parser.translate.columns.should == [{:field=> 'id', :data_type=> 'int', :options=> ''}, {:field=> 'foo', :data_type=> 'int', :options=> ''}]
48
+ parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}, {:field=> 'foo', :data_type=> 'int', :options=> ''}]
49
49
 
50
50
  sql = 'CREATE TABLE TEST (ID INT)'
51
51
  parser = DDLParser::Parser.new(sql)
52
- parser.translate.columns.should == [{:field=> 'id', :data_type=> 'int', :options=> ''}]
52
+ parser.translate.columns.map(&:to_hash).should == [{:field=> 'id', :data_type=> 'int', :options=> ''}]
53
53
  end
54
54
 
55
55
  it 'translate primary key' do
@@ -77,7 +77,7 @@ EOF
77
77
 
78
78
  it 'translate columns' do
79
79
  parser = DDLParser::Parser.new(sql)
80
- parser.translate.add_columns.should == [{:field=> 'budget_amount_in_co',
80
+ parser.translate.add_columns.map(&:to_hash).should == [{:field=> 'budget_amount_in_co',
81
81
  :data_type=>{:decimal=>{:precision=>{:total=>{:integer=> '15'}, :scale=>{:integer=> '2'}}}},
82
82
  :options=>[{:column_option=> 'not null'},
83
83
  {:default_clause=>[{:integer=> '0'}]}]},
@@ -0,0 +1,83 @@
1
+ require_relative '../../../spec/spec_helper'
2
+ require_relative '../../../lib/ddl_parser/translator/column'
3
+
4
+ describe 'DDLParser::Translator::Column' do
5
+
6
+ let(:int_column) {
7
+ DDLParser::Translator::Column.new({:field=> 'int_field', :data_type=> 'int', :options=> ''})
8
+ }
9
+
10
+ let(:char_column) {
11
+ DDLParser::Translator::Column.new(
12
+ {:field=> 'char_field',
13
+ :data_type=>{:char=>{:length=>{:integer=> '4'}}},
14
+ :options=> ''})
15
+ }
16
+
17
+ let(:decimal_column) {
18
+ DDLParser::Translator::Column.new(
19
+ {:field=> 'decimal_field',
20
+ :data_type=>{:decimal=>{:precision=>{:total=>{:integer=> '15'}, :scale=>{:integer=> '2'}}}},
21
+ :options=>[{:column_option=> 'not null'}, {:default_clause=>[{:integer=> '0'}]}]
22
+ })
23
+ }
24
+
25
+ before :each do
26
+
27
+ end
28
+
29
+ it 'must receive hash' do
30
+ DDLParser::Translator::Column.new('').to_hash.should == {}
31
+ DDLParser::Translator::Column.new({:foo => :bar}).to_hash.should == {:foo => :bar}
32
+ end
33
+
34
+ it 'should return name' do
35
+ int_column.name.should == 'int_field'
36
+ char_column.name.should == 'char_field'
37
+ decimal_column.name.should == 'decimal_field'
38
+ end
39
+
40
+ it 'should return data_type' do
41
+ int_column.data_type.should == :int
42
+ char_column.data_type.should == :char
43
+ decimal_column.data_type.should == :decimal
44
+ end
45
+
46
+ it 'should return length' do
47
+ int_column.length.should == nil
48
+ char_column.length.should == 4
49
+ decimal_column.length.should == 15.2
50
+ end
51
+
52
+ it 'should return precision' do
53
+ int_column.precision.should == nil
54
+ char_column.precision.should == nil
55
+ decimal_column.precision.should == 15
56
+ end
57
+
58
+ it 'should return scale' do
59
+ int_column.scale.should == nil
60
+ char_column.scale.should == nil
61
+ decimal_column.scale.should == 2
62
+ end
63
+
64
+ it 'should return options' do
65
+ int_column.options.should == nil
66
+ char_column.options.should == nil
67
+ decimal_column.options.should == [{:column_option=> 'not null'}, {:default_clause=>[{:integer=> '0'}]}]
68
+ end
69
+
70
+ it 'should return not null' do
71
+ int_column.not_null.should == false
72
+ char_column.not_null.should == false
73
+ decimal_column.not_null.should == true
74
+ end
75
+
76
+ it 'should return default clause' do
77
+ int_column.default_clause.should == nil
78
+ char_column.default_clause.should == nil
79
+ decimal_column.default_clause.should == 0
80
+ end
81
+
82
+
83
+ 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.6
4
+ version: 0.0.7
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-07-08 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet
@@ -94,6 +94,7 @@ files:
94
94
  - lib/ddl_parser/sql/db2/select_parser.rb
95
95
  - lib/ddl_parser/translator.rb
96
96
  - lib/ddl_parser/translator/alter_table.rb
97
+ - lib/ddl_parser/translator/column.rb
97
98
  - lib/ddl_parser/translator/create_index.rb
98
99
  - lib/ddl_parser/translator/create_table.rb
99
100
  - lib/ddl_parser/version.rb
@@ -105,6 +106,7 @@ files:
105
106
  - spec/ddl_parser/shared_rules/constants_spec.rb
106
107
  - spec/ddl_parser/shared_rules/data_types_spec.rb
107
108
  - spec/ddl_parser/sql/db2/select_parser_spec.rb
109
+ - spec/ddl_parser/translator/column_spec.rb
108
110
  - spec/spec_helper.rb
109
111
  homepage: ''
110
112
  licenses:
@@ -138,5 +140,6 @@ test_files:
138
140
  - spec/ddl_parser/shared_rules/constants_spec.rb
139
141
  - spec/ddl_parser/shared_rules/data_types_spec.rb
140
142
  - spec/ddl_parser/sql/db2/select_parser_spec.rb
143
+ - spec/ddl_parser/translator/column_spec.rb
141
144
  - spec/spec_helper.rb
142
145
  has_rdoc: