kwatable 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/ChangeLog.txt +28 -2
  2. data/README.txt +1 -1
  3. data/bin/kwatable +2 -2
  4. data/examples/ex1/Makefile +1 -1
  5. data/examples/ex1/example1.yaml +18 -7
  6. data/examples/ex2/Makefile +1 -1
  7. data/kwatable.gemspec +2 -2
  8. data/lib/kwatable/error-msg.rb +15 -14
  9. data/lib/kwatable/kwatable.schema.yaml +34 -81
  10. data/lib/kwatable/main-program.rb +49 -41
  11. data/lib/kwatable/manipulator.rb +222 -0
  12. data/lib/kwatable/templates/ddl-mysql.eruby +15 -12
  13. data/lib/kwatable/templates/ddl-postgresql.eruby +14 -10
  14. data/lib/kwatable/templates/defaults.yaml +4 -4
  15. data/lib/kwatable/templates/dto-java.eruby +49 -59
  16. data/lib/kwatable/templates/dto-ruby.eruby +47 -54
  17. data/lib/kwatable.rb +3 -4
  18. data/test/assert-diff.rb +1 -1
  19. data/test/test.rb +24 -17
  20. data/test/test1/test1.ddl-mysql.expected +3 -2
  21. data/test/test1/test1.ddl-postgresql.expected +2 -1
  22. data/test/test1/test1.dto-java.Group.expected +12 -9
  23. data/test/test1/test1.dto-java.User.expected +12 -14
  24. data/test/test1/test1.dto-ruby.Group.expected +9 -10
  25. data/test/test1/test1.dto-ruby.User.expected +12 -15
  26. data/test/test1/test1.yaml +18 -7
  27. data/test/test2/test2.ddl-mysql.expected +4 -4
  28. data/test/test2/test2.dto-java.Address.expected +6 -8
  29. data/test/test2/test2.dto-java.Customer.expected +6 -8
  30. data/test/test2/test2.dto-java.Item.expected +5 -7
  31. data/test/test2/test2.dto-java.SalesOrder.expected +6 -8
  32. data/test/test2/test2.dto-java.SalesOrderLine.expected +6 -8
  33. data/test/test2/test2.dto-ruby.Address.expected +6 -9
  34. data/test/test2/test2.dto-ruby.Customer.expected +6 -9
  35. data/test/test2/test2.dto-ruby.Item.expected +5 -8
  36. data/test/test2/test2.dto-ruby.SalesOrder.expected +6 -9
  37. data/test/test2/test2.dto-ruby.SalesOrderLine.expected +6 -9
  38. data/test/test2/test2.yaml +3 -3
  39. metadata +65 -64
  40. data/lib/kwatable/manufactory.rb +0 -213
@@ -5,13 +5,12 @@
5
5
 
6
6
  ## Address master table
7
7
  class Address
8
-
9
- def initialize(id, zipcode, addr1, addr2, addr3)
10
- @id = id
11
- @zipcode = zipcode
12
- @addr1 = addr1
13
- @addr2 = addr2
14
- @addr3 = addr3
8
+ def populate(values={})
9
+ self.id = values['id']
10
+ self.zipcode = values['zipcode']
11
+ self.addr1 = values['addr1']
12
+ self.addr2 = values['addr2']
13
+ self.addr3 = values['addr3']
15
14
  end
16
15
 
17
16
  attr_accessor :id #
@@ -20,6 +19,4 @@ class Address
20
19
  attr_accessor :addr2 #
21
20
  attr_accessor :addr3 #
22
21
 
23
- // -----------
24
-
25
22
  end
@@ -6,13 +6,12 @@ require 'Address'
6
6
 
7
7
  ## Customer master table
8
8
  class Customer
9
-
10
- def initialize(id, name, tel, email, address_id)
11
- @id = id
12
- @name = name
13
- @tel = tel
14
- @email = email
15
- @address_id = address_id
9
+ def populate(values={})
10
+ self.id = values['id']
11
+ self.name = values['name']
12
+ self.tel = values['tel']
13
+ self.email = values['email']
14
+ self.address_id = values['address_id']
16
15
  end
17
16
 
18
17
  attr_accessor :id #
@@ -21,8 +20,6 @@ class Customer
21
20
  attr_accessor :email #
22
21
  attr_accessor :address_id #
23
22
 
24
- // -----------
25
-
26
23
  attr_reader :address
27
24
  def address=(address)
28
25
  @address = address
@@ -5,12 +5,11 @@
5
5
 
6
6
  ## Item master table
7
7
  class Item
8
-
9
- def initialize(id, name, desc, price)
10
- @id = id
11
- @name = name
12
- @desc = desc
13
- @price = price
8
+ def populate(values={})
9
+ self.id = values['id']
10
+ self.name = values['name']
11
+ self.desc = values['desc']
12
+ self.price = values['price']
14
13
  end
15
14
 
16
15
  attr_accessor :id #
@@ -18,6 +17,4 @@ class Item
18
17
  attr_accessor :desc #
19
18
  attr_accessor :price #
20
19
 
21
- // -----------
22
-
23
20
  end
@@ -6,13 +6,12 @@ require 'Customer'
6
6
 
7
7
  ## Sales order master table
8
8
  class SalesOrder
9
-
10
- def initialize(id, customer_id, ordered_date, delivery_date, amount)
11
- @id = id
12
- @customer_id = customer_id
13
- @ordered_date = ordered_date
14
- @delivery_date = delivery_date
15
- @amount = amount
9
+ def populate(values={})
10
+ self.id = values['id']
11
+ self.customer_id = values['customer_id']
12
+ self.ordered_date = values['ordered_date']
13
+ self.delivery_date = values['delivery_date']
14
+ self.amount = values['amount']
16
15
  end
17
16
 
18
17
  attr_accessor :id #
@@ -21,8 +20,6 @@ class SalesOrder
21
20
  attr_accessor :delivery_date #
22
21
  attr_accessor :amount #
23
22
 
24
- // -----------
25
-
26
23
  attr_reader :customer
27
24
  def customer=(customer)
28
25
  @customer = customer
@@ -7,13 +7,12 @@ require 'Item'
7
7
 
8
8
  ## Sales Order detail lines
9
9
  class SalesOrderLine
10
-
11
- def initialize(num, order_id, item_id, price, quantity)
12
- @num = num
13
- @order_id = order_id
14
- @item_id = item_id
15
- @price = price
16
- @quantity = quantity
10
+ def populate(values={})
11
+ self.num = values['num']
12
+ self.order_id = values['order_id']
13
+ self.item_id = values['item_id']
14
+ self.price = values['price']
15
+ self.quantity = values['quantity']
17
16
  end
18
17
 
19
18
  attr_accessor :num #
@@ -22,8 +21,6 @@ class SalesOrderLine
22
21
  attr_accessor :price #
23
22
  attr_accessor :quantity #
24
23
 
25
- // -----------
26
-
27
24
  attr_reader :order
28
25
  def order=(order)
29
26
  @order = order
@@ -53,7 +53,7 @@ tables:
53
53
  - name: desc
54
54
  - name: price
55
55
  type: money
56
- not-null: yes
56
+ not-null: yes
57
57
 
58
58
  - name: sales_orders
59
59
  class: SalesOrder
@@ -62,7 +62,7 @@ tables:
62
62
  - name: id
63
63
  - name: customer_id
64
64
  ref: customers.id
65
- not-null: yes
65
+ not-null: yes
66
66
  - name: ordered_date
67
67
  type: date
68
68
  not-null: yes
@@ -80,7 +80,7 @@ tables:
80
80
  not-null: yes
81
81
  - name: order_id
82
82
  ref: sales_orders.id
83
- not-null: yes
83
+ not-null: yes
84
84
  - name: item_id
85
85
  ref: items.id
86
86
  not-null: yes
metadata CHANGED
@@ -1,91 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ !ruby/object:Gem::Specification
2
2
  rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: kwatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.1
7
- date: 2005-09-12 00:00:00 +09:00
6
+ version: 0.1.0
7
+ date: 2005-10-14 00:00:00 +09:00
8
8
  summary: SQL and DTO generator from table definition
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email:
12
12
  homepage: http://www.kuwata-lab.com/kwatable
13
13
  rubyforge_project:
14
- description: "Kwatable is a pretty tool to generate SQL and DTO from database table
15
- definition. Kwatable reads table definition written in YAML, and generates
16
- 'create table' SQL script for MySQL and PostgreSQL, or generates DTO class file
17
- for Java and Ruby. You can add your own eRuby template."
14
+ description: Kwatable is a pretty tool to generate SQL and DTO from database table definition. Kwatable reads table definition written in YAML, and generates 'create table' SQL script for MySQL and PostgreSQL, or generates DTO class file for Java and Ruby. You can add your own eRuby template.
18
15
  autorequire:
19
16
  default_executable:
20
17
  bindir: bin
21
18
  has_rdoc: false
22
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
23
20
  requirements:
24
- -
25
- - ">"
26
- - !ruby/object:Gem::Version
27
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
28
24
  version:
29
25
  platform: ruby
30
26
  signing_key:
31
27
  cert_chain:
32
28
  authors:
33
- - makoto kuwata
29
+ - makoto kuwata
34
30
  files:
35
- - lib/kwatable
36
- - lib/kwatable.rb
37
- - lib/kwatable/error-msg.rb
38
- - lib/kwatable/kwatable.schema.yaml
39
- - lib/kwatable/main-program.rb
40
- - lib/kwatable/manufactory.rb
41
- - lib/kwatable/templates
42
- - lib/kwatable/templates/ddl-mysql.eruby
43
- - lib/kwatable/templates/ddl-postgresql.eruby
44
- - lib/kwatable/templates/defaults.yaml
45
- - lib/kwatable/templates/dto-java.eruby
46
- - lib/kwatable/templates/dto-ruby.eruby
47
- - bin/kwatable
48
- - examples/ex1
49
- - examples/ex2
50
- - examples/ex1/example1.yaml
51
- - examples/ex1/Makefile
52
- - examples/ex2/example2.yaml
53
- - examples/ex2/Makefile
54
- - test/assert-diff.rb
55
- - test/test.rb
56
- - test/test1
57
- - test/test2
58
- - test/test1/test1.ddl-mysql.expected
59
- - test/test1/test1.ddl-postgresql.expected
60
- - test/test1/test1.dto-java.Group.expected
61
- - test/test1/test1.dto-java.User.expected
62
- - test/test1/test1.dto-ruby.Group.expected
63
- - test/test1/test1.dto-ruby.User.expected
64
- - test/test1/test1.yaml
65
- - test/test2/test2.ddl-mysql.expected
66
- - test/test2/test2.ddl-postgresql.expected
67
- - test/test2/test2.dto-java.Address.expected
68
- - test/test2/test2.dto-java.Customer.expected
69
- - test/test2/test2.dto-java.Item.expected
70
- - test/test2/test2.dto-java.SalesOrder.expected
71
- - test/test2/test2.dto-java.SalesOrderLine.expected
72
- - test/test2/test2.dto-ruby.Address.expected
73
- - test/test2/test2.dto-ruby.Customer.expected
74
- - test/test2/test2.dto-ruby.Item.expected
75
- - test/test2/test2.dto-ruby.SalesOrder.expected
76
- - test/test2/test2.dto-ruby.SalesOrderLine.expected
77
- - test/test2/test2.yaml
78
- - README.txt
79
- - ChangeLog.txt
80
- - COPYING
81
- - setup.rb
82
- - kwatable.gemspec
31
+ - lib/kwatable
32
+ - lib/kwatable.rb
33
+ - lib/kwatable/error-msg.rb
34
+ - lib/kwatable/kwatable.schema.yaml
35
+ - lib/kwatable/main-program.rb
36
+ - lib/kwatable/manipulator.rb
37
+ - lib/kwatable/templates
38
+ - lib/kwatable/templates/ddl-mysql.eruby
39
+ - lib/kwatable/templates/ddl-postgresql.eruby
40
+ - lib/kwatable/templates/defaults.yaml
41
+ - lib/kwatable/templates/dto-java.eruby
42
+ - lib/kwatable/templates/dto-ruby.eruby
43
+ - bin/kwatable
44
+ - examples/ex1
45
+ - examples/ex2
46
+ - examples/ex1/example1.yaml
47
+ - examples/ex1/Makefile
48
+ - examples/ex2/example2.yaml
49
+ - examples/ex2/Makefile
50
+ - test/assert-diff.rb
51
+ - test/test.rb
52
+ - test/test1
53
+ - test/test2
54
+ - test/test1/test1.ddl-mysql.expected
55
+ - test/test1/test1.ddl-postgresql.expected
56
+ - test/test1/test1.dto-java.Group.expected
57
+ - test/test1/test1.dto-java.User.expected
58
+ - test/test1/test1.dto-ruby.Group.expected
59
+ - test/test1/test1.dto-ruby.User.expected
60
+ - test/test1/test1.yaml
61
+ - test/test2/test2.ddl-mysql.expected
62
+ - test/test2/test2.ddl-postgresql.expected
63
+ - test/test2/test2.dto-java.Address.expected
64
+ - test/test2/test2.dto-java.Customer.expected
65
+ - test/test2/test2.dto-java.Item.expected
66
+ - test/test2/test2.dto-java.SalesOrder.expected
67
+ - test/test2/test2.dto-java.SalesOrderLine.expected
68
+ - test/test2/test2.dto-ruby.Address.expected
69
+ - test/test2/test2.dto-ruby.Customer.expected
70
+ - test/test2/test2.dto-ruby.Item.expected
71
+ - test/test2/test2.dto-ruby.SalesOrder.expected
72
+ - test/test2/test2.dto-ruby.SalesOrderLine.expected
73
+ - test/test2/test2.yaml
74
+ - README.txt
75
+ - ChangeLog.txt
76
+ - COPYING
77
+ - setup.rb
78
+ - kwatable.gemspec
83
79
  test_files:
84
- - test/test.rb
80
+ - test/test.rb
85
81
  rdoc_options: []
82
+
86
83
  extra_rdoc_files: []
84
+
87
85
  executables:
88
- - kwatable
86
+ - kwatable
89
87
  extensions: []
88
+
90
89
  requirements: []
91
- dependencies: []
90
+
91
+ dependencies: []
92
+
@@ -1,213 +0,0 @@
1
- ###
2
- ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.0.1 $
4
- ### $Rev: 11 $
5
- ###
6
-
7
- require 'yaml'
8
-
9
- module Kwatable
10
-
11
- class ManufactureError < KwatableError
12
- end
13
-
14
-
15
- ##
16
- ## ex.
17
- ## manufactory = Kwatable::Manufactory.new
18
- ## tabledef = manufactory.parse(input) # input is String or File
19
- ## p tabledef[:columns]
20
- ## p tabledef[:tables]
21
- ##
22
- class Manufactory
23
-
24
- def parse(input)
25
- str = ''
26
- input.each_line do |line|
27
- str << line.gsub(/([^\t]{8})|([^\t]*)\t/n){[$+].pack("A8")} ## expand tab
28
- end
29
- ingredient = YAML.load(str)
30
- manufacture(ingredient)
31
- return ingredient
32
- end
33
-
34
- def manufacture(ingredient)
35
- #assert unless ingredient.is_a?(Hash)
36
-
37
- column_map, patterned_columns = manufacture_columns(ingredient['columns'])
38
- #assert unless column_map.is_a?(Hash)
39
- #assert unless patterned_columns.is_a?(Array)
40
-
41
- table_map = manufacture_tables(ingredient['tables'], column_map, patterned_columns)
42
- #assert unless table_map.is_a?(Hash)
43
-
44
- ingredient['column_map'] = column_map # Hash
45
- ingredient['table_map'] = table_map # Hash
46
- return ingredient
47
- end
48
-
49
- private
50
-
51
- def manufacture_columns(columns)
52
- column_map = {}
53
- patterned_columns = []
54
- columns.each do |column|
55
- name = column['name']
56
- unless name
57
- #* key=:colname_required msg="column definition doesn't have a name."
58
- raise ManufactureError.new(Kwatable.msg(:colname_required))
59
- end
60
- if name =~ /\A\/(.*)\/\z/
61
- pattern = $1
62
- begin
63
- name = Regexp.compile(pattern)
64
- rescue RegexpError => ex
65
- #* key=:regexp_invalid msg="column %s: %s"
66
- raise ManufactureError.new(Kwatable.msg(:regexp_invalid) % [name, ex.message])
67
- end
68
- column['namepattern'] = name
69
- #column.delete('name')
70
- patterned_columns << column
71
- else
72
- if column_map.key?(name)
73
- #* key=:coldef_duplicated msg="column definition `%s' is duplicated."
74
- raise ManufactureError.new(Kwatable.msg(:coldef_duplicated) % name)
75
- end
76
- column_map[name] = column
77
- end
78
- #name = column['name'].strip
79
- #namepattern = column['namepattern']
80
- #unless name || namepattern
81
- # #* key=:colname_required msg="column definition doesn't have a name nor namepattern."
82
- # raise ManufactureError.new(Kwatable.msg(:colname_required))
83
- #end
84
- #if name
85
- # if column_map.key?(name)
86
- # #* key=:coldef_duplicated msg="column definition `%s' is duplicated."
87
- # raise ManufactureError.new(Kwatable.msg(:coldef_duplicated) % [name])
88
- # end
89
- # column_map[name] = column
90
- #end
91
- #if namepattern
92
- # pattern = namepattern.strip
93
- # pattern = $1 if pattern =~ /^\/(.*)\/$/
94
- # column['namepattern'] = Regexp.compile(pattern)
95
- # patterned_columns << column
96
- #end
97
- end if columns
98
- return column_map, patterned_columns
99
- end
100
-
101
- def manufacture_tables(tables, column_map, patterned_columns)
102
- #assert unless tables.is_a?(Array)
103
- #assert unless column_map.is_a?(Hash)
104
- #assert unless patterned_columns.is_a?(Array)
105
-
106
- ## create table_map
107
- table_map = {}
108
- tables.each do |table|
109
- name = table['name']
110
- unless name
111
- #* key=:tablename_required msg="table definition doesn't have a name."
112
- raise ManufactureError.new(Kwatable.msg(:tablename_required))
113
- end
114
- if table_map.key?(name)
115
- #* key=:tabledef_duplicated msg="table definition `%s' is duplicated."
116
- raise ManufactureError.new(Kwatable.msg(:tabledef_duplicated) % [name])
117
- end
118
- table_map[name] = table
119
- end
120
-
121
- ## manufacture table columns
122
- tables.each do |table|
123
- name_map = {}
124
- table['columns'].each do |column|
125
- name = column['name']
126
- unless name
127
- #* key=:tablecolumn_required msg="table '%s': column name requried."
128
- raise ManufactureError.new(Kwatable.msg(:tablecolumn_required % [table['name']]))
129
- end
130
- if name_map[name]
131
- #* key=:tablecolumn_duplicated msg="table '%s': column '%s' is duplicated."
132
- raise ManufactureError.new(Kwatable.msg(:tablecolumn_duplicated % [table['name'], name]))
133
- end
134
- name_map[name] = true
135
- set_defaults(column, column_map, patterned_columns)
136
- #alias_keys(column, "primary-key", "primarykey", "identifier")
137
- #alias_keys(column, "not-null", "notnull", "required")
138
- alias_key(column, "primary-key", "identifier")
139
- alias_key(column, "not-null", "required")
140
- handle_ref(column, table_map) if column['ref']
141
- handle_values(column) if column['values']
142
- unless column['type']
143
- #* key=:tabletype_required msg="table `%s': type of column `%s' is not determined."
144
- raise ManufactureError.new(Kwatable.msg(:tabletype_required) % [table['name'], column['name']])
145
- end
146
- end if table['columns']
147
- end if tables
148
-
149
- return table_map
150
- end
151
-
152
- def set_defaults(column, column_map, patterned_columns)
153
- colname = column['name']
154
- defaults = column_map[colname]
155
- defaults ||= patterned_columns.find { |col| colname =~ col['namepattern'] }
156
- defaults.each do |key, val|
157
- column[key] = val if !column.key?(key) && key != 'namepattern'
158
- end if defaults
159
- end
160
-
161
- def alias_keys(column, key, *old_keys) # not used
162
- old_keys.each do |old_key|
163
- column[key] = column[old_key] if !column.key?(key) && column.key?(old_key)
164
- end if old_keys
165
- end
166
-
167
- def alias_key(column, key1, key2)
168
- if column[key1] && !column.key?(key2)
169
- column[key2] = column[key1]
170
- elsif column[key2] && !column.key?(key1)
171
- column[key1] = column[key2]
172
- end
173
- end
174
-
175
- def handle_values(column)
176
- return unless column['values']
177
- width = 0
178
- column['values'].each do |value|
179
- len = value.to_s.length
180
- width = len if len > width
181
- end
182
- column['type'] ||= 'string'
183
- column['width'] ||= width
184
- end
185
-
186
- def handle_ref(column, table_map)
187
- ref = column['ref']
188
- return unless ref
189
- ref = ref.strip
190
- return unless ref =~ /\A(\w+)\.(\w+)\z/ || ref =~ /\A(\w+)\((\w+)\)\z/
191
- ref_table_name = $1
192
- ref_column_name = $2
193
- ref_table = table_map[ref_table_name]
194
- unless ref_table
195
- #* key=:reftable_notfound msg="`ref: %s': table not found."
196
- raise ManufactureError.new(Kwatable.msg(:reftable_notfound) % column['ref'])
197
- end
198
- cols = ref_table['columns']
199
- ref_column = cols ? cols.find { |col| col['name'] == ref_column_name } : nil
200
- unless ref_column
201
- #* key=:refcolumn_notfound msg="`ref: %s': column not found in the table."
202
- raise ManufactureError.new(Kwatable.msg(:refcolumn_notfound) % column['ref'])
203
- end
204
- column['ref-table'] = ref_table
205
- column['ref-column'] = ref_column
206
- column['ref-name'] ||= column['name'].sub(/_#{ref_column_name}$/, '')
207
- column['type'] = ref_column['type']
208
- column['width'] ||= ref_column['width'] if ref_column.key?('width')
209
- end
210
-
211
- end
212
-
213
- end