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
data/ChangeLog.txt CHANGED
@@ -1,7 +1,33 @@
1
1
  .=title: ChangeLog
2
2
  .?copyright: copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- .?release: $Release: 0.0.1 $
4
- .?version: $Rev: 12 $
3
+ .?release: $Release: 0.1.0 $
4
+ .?version: $Rev: 15 $
5
+
6
+ .: 2005-10-13 rev.16
7
+ .- [release] 0.1.0
8
+
9
+ .: 2005-10-13 rev.15
10
+ .- [change] rename class 'Manufactory' to 'Manipulator'
11
+ .- [change] delete column['ref-table'] and column['ref-column']
12
+ .- [change] add column['table']
13
+ .- [change] change column['ref'] to refer a column
14
+ .- [change] delete Manipulator#parse()
15
+ .- [change] delete constructor from dto-{java,ruby}.eruby
16
+ .- [enhance] add populator to dto-{java,ruby}.eruby
17
+ .- [refactor] use singleton method and instance variabe in template files
18
+
19
+ .: 2005-09-17 rev.14
20
+ .- add table name and column name on error message
21
+ .- add '_' prefix on private method
22
+ .- new method Manufacture#_error() added
23
+ .- new method MainProgram#_option_error() added
24
+
25
+ .: 2005-09-15 rev.13
26
+ .- change 'values:' to 'enum:' to adopt same keyword of kwalify
27
+ .- change to use 'str', 'int', and 'bool' rather than 'string', 'integer', and 'boolean'
28
+ .- change to use 'enum' instead of 'values'
29
+ .- modiy kwatable.schema.yaml
30
+ .- add java keyword 'assert' and 'enum' to dto-java.eruby
5
31
 
6
32
  .: 2005-09-12 rev.12 (release 0.0.1)
7
33
  .- change ERB trim_mode from '%' to '>'
data/README.txt CHANGED
@@ -1,5 +1,5 @@
1
1
  .=title: README
2
- .?release: $Release: 0.0.1 $
2
+ .?release: $Release: 0.1.0 $
3
3
 
4
4
 
5
5
  .$ About
data/bin/kwatable CHANGED
@@ -4,8 +4,8 @@
4
4
  ### kwatable - create table, dto class, or other from table definition
5
5
  ###
6
6
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
7
- ### $Release: 0.0.1 $
8
- ### $Rev$
7
+ ### $Release: 0.1.0 $
8
+ ### $Rev: 13 $
9
9
  ###
10
10
 
11
11
  require 'kwatable'
@@ -1,6 +1,6 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.0.1 $
3
+ ### $Release: 0.1.0 $
4
4
  ### $Rev: 11 $
5
5
  ###
6
6
 
@@ -2,19 +2,19 @@
2
2
  ## example data file for kwatable
3
3
  ##
4
4
  ## copyright(c) 2005 kuwata-lab.com all rights reserved.
5
- ## $Release: 0.0.1 $
6
- ## $Rev: 10 $
5
+ ## $Release: 0.1.0 $
6
+ ## $Rev: 15 $
7
7
  ##
8
8
 
9
9
  columns:
10
10
  - name: id
11
11
  type: integer
12
- primary-key: yes
12
+ ident: yes
13
13
  serial: yes
14
14
 
15
15
  - name: name
16
16
  type: string
17
- not-null: yes
17
+ required: yes
18
18
  width: 63
19
19
 
20
20
  - name: desc
@@ -53,7 +53,7 @@ columns:
53
53
 
54
54
  - name: gender
55
55
  type: string
56
- values:
56
+ enum:
57
57
  - M
58
58
  - F
59
59
 
@@ -63,23 +63,34 @@ tables:
63
63
  desc: Group master table
64
64
  columns:
65
65
  - name: id
66
+ desc: group id
66
67
  - name: name
68
+ desc: group name
67
69
  - name: desc
70
+ desc: group description
71
+ - name: class
72
+ desc: group class
73
+ type: string
74
+ enum: [ ADMIN, SYSTEM, USER, UNUSED ]
68
75
 
69
76
  - name: users
70
77
  class: User
71
78
  desc: User master table
72
79
  columns:
73
80
  - name: id
81
+ desc: user id
74
82
  - name: name
83
+ desc: user name
75
84
  - name: desc
85
+ desc: description about user
76
86
  - name: email
77
87
  - name: group_id
78
88
  ref: groups.id
89
+ desc: group id to which user belongs
79
90
  - name: account
80
91
  type: string
81
92
  width: 31
82
- not-null: yes
93
+ required: yes
83
94
  - name: password
84
- not-null: yes
95
+ required: yes
85
96
 
@@ -1,6 +1,6 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.0.1 $
3
+ ### $Release: 0.1.0 $
4
4
  ### $Rev: 11 $
5
5
  ###
6
6
 
data/kwatable.gemspec CHANGED
@@ -4,7 +4,7 @@
4
4
  ### RubyGems Specification file for kwatable
5
5
  ###
6
6
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
7
- ### $Release: 0.0.1 $
7
+ ### $Release: 0.1.0 $
8
8
  ### $Rev: 10 $
9
9
  ###
10
10
 
@@ -14,7 +14,7 @@ spec = Gem::Specification.new do |s|
14
14
  ## package information
15
15
  s.name = 'kwatable'
16
16
  s.author = 'makoto kuwata'
17
- s.version = ("$Release: 0.0.1 $" =~ /Release: ([\.\d]+)/) && $1
17
+ s.version = ("$Release: 0.1.0 $" =~ /Release: ([\.\d]+)/) && $1
18
18
  s.platform = Gem::Platform::RUBY
19
19
  s.homepage = 'http://www.kuwata-lab.com/kwatable'
20
20
  s.summary = "SQL and DTO generator from table definition"
@@ -1,7 +1,7 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.0.1 $
4
- ### $Rev: 12 $
3
+ ### $Release: 0.1.0 $
4
+ ### $Rev: 15 $
5
5
  ###
6
6
 
7
7
  module Kwatable
@@ -13,6 +13,8 @@ module Kwatable
13
13
  end
14
14
 
15
15
  #----- begin auto generate
16
+ @@messages[:tabledef_empty] = "table definition file is empty."
17
+ @@messages[:tabledef_notmap] = "table definition is not a mapping."
16
18
  @@messages[:template_required] = "template is not specified."
17
19
  @@messages[:template_notfound] = "`%s': template file not found."
18
20
  @@messages[:file_generated] = "generated: %s"
@@ -20,18 +22,17 @@ module Kwatable
20
22
  @@messages[:outdir_required] = "-%s: output directory required."
21
23
  @@messages[:directory_required] = "-%s: directory required."
22
24
  @@messages[:option_invalid] = "-%s: invalid option."
23
- @@messages[:colname_required] = "column definition doesn't have a name."
24
- @@messages[:regexp_invalid] = "column %s: %s"
25
- @@messages[:coldef_duplicated] = "column definition `%s' is duplicated."
26
- @@messages[:colname_required] = "column definition doesn't have a name nor namepattern."
27
- @@messages[:coldef_duplicated] = "column definition `%s' is duplicated."
28
- @@messages[:tablename_required] = "table definition doesn't have a name."
29
- @@messages[:tabledef_duplicated] = "table definition `%s' is duplicated."
30
- @@messages[:tablecolumn_required] = "table '%s': column name requried."
31
- @@messages[:tablecolumn_duplicated] = "table '%s': column '%s' is duplicated."
32
- @@messages[:tabletype_required] = "table `%s': type of column `%s' is not determined."
33
- @@messages[:reftable_notfound] = "`ref: %s': table not found."
34
- @@messages[:refcolumn_notfound] = "`ref: %s': column not found in the table."
25
+ @@messages[:colname_required] = "table '%s': column name required (index=%d)."
26
+ @@messages[:regexp_invalid] = "table '%s': column '%s': pattern %s: %s"
27
+ @@messages[:coldef_duplicated] = "table '%s': column '%s': column name is duplicated."
28
+ @@messages[:tablename_required] = "table definition doesn't have a name (index=%d)."
29
+ @@messages[:tabledef_duplicated] = "table '%s': table name is duplicated."
30
+ @@messages[:tablecolumn_required] = "table '%s': column name requried (index=%d)."
31
+ @@messages[:tablecolumn_duplicated] = "table '%s': column '%s': column name is duplicated."
32
+ @@messages[:tabletype_required] = "table `%s': column `%s': type is not determined."
33
+ @@messages[:alias_conflict] = "table '%s', column '%s': alias key '%1:' and '%2:' are not allowed to use in the same time."
34
+ @@messages[:reftable_notfound] = "`table '%s': column '%s': 'ref: %s': reference table not found."
35
+ @@messages[:refcolumn_notfound] = "`table '%s': column '%s': ref: %s': reference column not found in the table."
35
36
  #----- end auto generate
36
37
 
37
38
  end
@@ -2,8 +2,8 @@
2
2
  ### schema definition of kwalify for kwatable
3
3
  ###
4
4
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
5
- ### $Release: 0.0.1 $
6
- ### $Rev$
5
+ ### $Release: 0.1.0 $
6
+ ### $Rev: 15 $
7
7
  ###
8
8
 
9
9
 
@@ -13,121 +13,74 @@ mapping:
13
13
  type: seq
14
14
  sequence:
15
15
  - type: map
16
- mapping:
16
+ mapping: &columrule
17
17
  "name":
18
- type: string
19
- "namepattern":
20
- type: string
21
- pattern: /^\/.*\/$/
18
+ type: str
22
19
  "desc":
23
20
  type: text
24
21
  "type":
25
- type: string
26
- values:
22
+ type: str
23
+ enum:
27
24
  - char
28
25
  - short
29
- - integer
30
26
  - int
31
- - string
27
+ - integer
32
28
  - str
29
+ - string
33
30
  - text
34
31
  - float
35
32
  - double
36
- - boolean
37
33
  - bool
34
+ - boolean
38
35
  - date
36
+ - time
39
37
  - timestamp
40
38
  - money
41
39
  "width":
42
- type: integer
40
+ type: int
41
+ #"identifier":
42
+ "ident":
43
+ type: bool
43
44
  "primary-key":
44
- type: boolean
45
- "identifier":
46
- type: boolean
47
- "not-null":
48
- type: boolean
45
+ type: bool
46
+ desc: alias of 'ident'
49
47
  "required":
50
- type: boolean
48
+ type: bool
49
+ "not-null":
50
+ type: bool
51
+ desc: alias of 'required'
51
52
  "unique":
52
- type: boolean
53
- "sequence":
54
- type: boolean
53
+ type: bool
55
54
  "serial":
56
- type: boolean
57
- "values":
55
+ type: bool
56
+ "enum":
58
57
  type: seq
59
58
  sequence:
60
- - type: string
59
+ - type: text
61
60
  "ref":
62
- type: string
63
- pattern: /^\w+\.\w+$/
61
+ type: str
62
+ pattern: /^\w+(\.\w+|\(\w+\))$/
64
63
  "ref-name":
65
- type: string
64
+ type: str
66
65
  "pattern":
67
- type: string
68
- pattern: /^\/.*\/$/
66
+ type: str
67
+ pattern: /^\/.*\/$/
68
+
69
69
 
70
70
  "tables":
71
- type: seq
71
+ type: seq
72
72
  sequence:
73
73
  - type: map
74
74
  mapping:
75
75
  "name":
76
- type: string
76
+ type: str
77
77
  required: yes
78
78
  "desc":
79
79
  type: text
80
80
  "class":
81
- type: string
81
+ type: str
82
82
  "columns":
83
83
  type: seq
84
84
  sequence:
85
85
  - type: map
86
- mapping:
87
- "name":
88
- type: string
89
- "desc":
90
- type: text
91
- "type":
92
- type: string
93
- values:
94
- - char
95
- - short
96
- - integer
97
- - int
98
- - string
99
- - str
100
- - text
101
- - float
102
- - double
103
- - boolean
104
- - bool
105
- - money
106
- "width":
107
- type: integer
108
- "primary-key":
109
- type: boolean
110
- "identifier":
111
- type: boolean
112
- "not-null":
113
- type: boolean
114
- "required":
115
- type: boolean
116
- "unique":
117
- type: boolean
118
- "sequence":
119
- type: boolean
120
- "serial":
121
- type: boolean
122
- "values":
123
- type: seq
124
- sequence:
125
- - type: string
126
- "ref":
127
- type: string
128
- pattern: /^\w+\.\w+$/
129
- "ref-name":
130
- type: string
131
- "pattern":
132
- type: string
133
- pattern: /^\/.*\/$/
86
+ mapping: *columrule
@@ -1,9 +1,10 @@
1
1
  ###
2
2
  ### copyright(c) 2005 kuwata-lab.com all rights reserved.
3
- ### $Release: 0.0.1 $
4
- ### $Rev: 12 $
3
+ ### $Release: 0.1.0 $
4
+ ### $Rev: 15 $
5
5
  ###
6
6
 
7
+ require 'yaml'
7
8
  require 'erb'
8
9
 
9
10
  module Kwatable
@@ -17,12 +18,12 @@ module Kwatable
17
18
  end
18
19
 
19
20
  def execute()
20
- options, properties = parse_options(@argv)
21
+ options, properties = _parse_options(@argv)
21
22
 
22
23
  ## help or version
23
24
  if options[?h] || options[?v]
24
- puts version() if options[?v]
25
- puts usage() if options[?h]
25
+ puts _version() if options[?v]
26
+ puts _usage() if options[?h]
26
27
  return
27
28
  end
28
29
 
@@ -36,44 +37,51 @@ module Kwatable
36
37
  end
37
38
  end
38
39
  end
39
- yaml = YAML.load(s)
40
+ tabledef = YAML.load(s)
41
+ unless tabledef
42
+ #* key=:tabledef_empty msg="table definition file is empty."
43
+ raise _option_error(:tabledef_empty)
44
+ end
45
+ unless tabledef.is_a?(Hash)
46
+ #* key=:tabledef_notmap msg="table definition is not a mapping."
47
+ raise _option_error(:tabledef_notmap)
48
+ end
40
49
 
41
- ## manufacture
42
- manufactory = Manufactory.new()
43
- manufactory.manufacture(yaml)
44
- $stderr.print yaml.to_yaml if options[?D]
50
+ ## manipulation
51
+ manipulator = Manipulator.new()
52
+ manipulator.manipulate(tabledef)
53
+ $stderr.print tabledef.to_yaml if options[?D]
45
54
 
46
55
  ## template filename
47
56
  template = options[?f]
48
57
  unless template
49
58
  return nil if options[?D]
50
59
  #* key=:template_required msg="template is not specified."
51
- raise CommandOptionError.new(Kwatable.msg(:template_required))
60
+ raise _option_error(:template_required)
52
61
  end
53
62
 
54
63
  ## template filepath
55
64
  template_filepath = nil
56
65
  if test(?f, template)
57
66
  template_filepath = template
58
- elsif options[?I] && t = find_template(template, options[?I])
67
+ elsif options[?I] && t = _find_template(template, options[?I])
59
68
  template_filepath = t
60
69
  else
61
- template_filepath = find_template(template, Kwatable.template_path)
70
+ template_filepath = _find_template(template, Kwatable.template_path)
62
71
  end
63
72
  unless template_filepath
64
73
  #* key=:template_notfound msg="`%s': template file not found."
65
- raise CommandOptionError.new(Kwatable.msg(:template_notfound) % template)
74
+ raise _option_error(:template_notfound, template)
66
75
  end
67
76
 
68
77
  ## apply template
69
78
  if !options[?m]
70
- context = { 'tables' => yaml['tables'], 'properties' => properties, }
71
- output = apply_template(template_filepath, context)
72
- return output
79
+ context = { 'tables' => tabledef['tables'], 'properties' => properties, }
80
+ output = _apply_template(template_filepath, context)
73
81
  else
74
- yaml['tables'].each do |table|
82
+ tabledef['tables'].each do |table|
75
83
  context = { 'table' => table, 'properties' => properties, }
76
- output = apply_template(template_filepath, context)
84
+ output = _apply_template(template_filepath, context)
77
85
  output_filename = context[:output_filename]
78
86
  output_filename = "#{options[?d]}/#{output_filename}" if options[?d]
79
87
  File.open(output_filename, 'w') { |f| f.write(output) }
@@ -82,13 +90,19 @@ module Kwatable
82
90
  $stderr.puts(Kwatable.msg(:file_generated) % output_filename)
83
91
  end
84
92
  end
85
- return nil
93
+ output = nil
86
94
  end
95
+ return output
87
96
  end
88
97
 
89
98
  private
99
+
100
+ def _option_error(message_key, *args)
101
+ msg = Kwatable.msg(message_key) % args
102
+ return CommandOptionError.new(msg)
103
+ end
90
104
 
91
- def parse_options(argv)
105
+ def _parse_options(argv)
92
106
  options = {}
93
107
  properties = {}
94
108
  while argv[0] && argv[0][0] == ?-
@@ -122,31 +136,25 @@ module Kwatable
122
136
  when ?f, ?t
123
137
  arg = optstr.empty? ? argv.shift : optstr
124
138
  optstr = nil
125
- unless arg
126
- #* key=:template_required msg="-%s: template filename required."
127
- raise CommandOptionError.new(Kwatable.msg(:template_required) % optchar.chr)
128
- end
139
+ #* key=:template_required msg="-%s: template filename required."
140
+ raise _option_error(:template_required, optchar.chr) unless arg
129
141
  options[?f] = arg
130
142
  #options[optchar] = arg
131
143
  when ?d
132
144
  arg = optstr.empty? ? argv.shift : optstr
133
145
  optstr = nil
134
- unless arg
135
- #* key=:outdir_required msg="-%s: output directory required."
136
- raise CommandOptionError.new(Kwatable.msg(:outdir_required) % optchar.chr)
137
- end
146
+ #* key=:outdir_required msg="-%s: output directory required."
147
+ raise _option_error(:outdir_required, optchar.chr) unless arg
138
148
  options[optchar] = arg
139
149
  when ?I
140
150
  arg = optstr.empty? ? argv.shift : optstr
141
151
  optstr = nil
142
- unless arg
143
- #* key=:directory_required msg="-%s: directory required."
144
- raise CommandOptionError.new(Kwatable.msg(:directory_required) % optchar.chr)
145
- end
152
+ #* key=:directory_required msg="-%s: directory required."
153
+ raise _option_error(:directory_required, optchar.chr) unless arg
146
154
  (options[optchar] ||= []).concat(arg.split(/,/))
147
155
  else
148
156
  #* key=:option_invalid msg="-%s: invalid option."
149
- raise CommandOptionError.new(Kwatable.msg(:option_invalid) % optchar.chr)
157
+ raise _option_error(:option_invalid, optchar.chr)
150
158
  end
151
159
  end # end while
152
160
  end # end if
@@ -154,7 +162,7 @@ module Kwatable
154
162
  return options, properties
155
163
  end
156
164
 
157
- def find_template(template, path_list)
165
+ def _find_template(template, path_list)
158
166
  path_list.each do |path|
159
167
  t = "#{path}/#{template}"
160
168
  return t if test(?f, t)
@@ -162,19 +170,19 @@ module Kwatable
162
170
  return nil
163
171
  end
164
172
 
165
- def apply_template(filename, context)
173
+ def _apply_template(filename, context)
166
174
  str = File.open(filename) { |f| f.read() }
167
175
  trim_mode = '>' # or '%'
168
176
  erb = ERB.new(str, $SAFE, trim_mode)
169
- result = eval_erb(erb, context)
177
+ result = _eval_erb(erb, context)
170
178
  return result
171
179
  end
172
180
 
173
- def eval_erb(__erb, context)
181
+ def _eval_erb(__erb, context)
174
182
  return __erb.result(binding())
175
183
  end
176
184
 
177
- def usage()
185
+ def _usage()
178
186
  command = File::basename($0)
179
187
  s = ""
180
188
  s << "Usage: #{command} [-hvm] [-I path] [-d dir] -f template datafile [datafile2 ...]\n"
@@ -188,8 +196,8 @@ module Kwatable
188
196
  return s
189
197
  end
190
198
 
191
- def version()
192
- return ("$Release: 0.0.1 $" =~ /[\.\d]+/ && $&)
199
+ def _version()
200
+ return ("$Release: 0.1.0 $" =~ /[\.\d]+/ && $&)
193
201
  end
194
202
 
195
203
  end