oracle-model-generator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/README +2 -1
- data/Rakefile +3 -5
- data/bin/omg +81 -31
- data/lib/oracle/model/generator.rb +17 -3
- data/oracle-model-generator.gemspec +1 -1
- data/test/test_oracle_model_generator.rb +1 -1
- metadata +6 -6
data/CHANGES
CHANGED
@@ -1,2 +1,10 @@
|
|
1
|
+
= 0.2.0 - 8-Mar-2011
|
2
|
+
* Added the -r/--rails command line option. By default the omg binary will
|
3
|
+
now generate Rails 3 style validations. If you want Rails 2 validations,
|
4
|
+
specify "-r 2" on the command line.
|
5
|
+
* The numericality validations are now more strict, and will match the
|
6
|
+
scale and precision exactly as defined instead of rounding to the nearest
|
7
|
+
whole number, for Rails 3 anyway.
|
8
|
+
|
1
9
|
= 0.1.0 - 6-Oct-2010
|
2
10
|
* Initial release.
|
data/README
CHANGED
@@ -47,6 +47,7 @@ primary keys.
|
|
47
47
|
|
48
48
|
= Future Plans
|
49
49
|
Add support for views.
|
50
|
+
Add automatic test suite generation.
|
50
51
|
|
51
52
|
= Known Issues
|
52
53
|
None known. If you find any issues, please report them on the github project
|
@@ -58,7 +59,7 @@ implied warranties, including, without limitation, the implied
|
|
58
59
|
warranties of merchantability and fitness for a particular purpose.
|
59
60
|
|
60
61
|
= Copyright
|
61
|
-
(C) 2010, Daniel J. Berger
|
62
|
+
(C) 2010, 2011 Daniel J. Berger
|
62
63
|
All Rights Reserved
|
63
64
|
|
64
65
|
= License
|
data/Rakefile
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
|
+
require 'rake/clean'
|
3
4
|
require 'rbconfig'
|
4
5
|
include Config
|
5
6
|
|
6
|
-
|
7
|
-
desc 'Remove any old gem files'
|
8
|
-
task :clean do
|
9
|
-
Dir['*.gem'].each{ |f| File.delete(f) }
|
10
|
-
end
|
7
|
+
CLEAN.include("**/*.gem", "**/*.rbc", "**/*.log")
|
11
8
|
|
9
|
+
namespace 'gem' do
|
12
10
|
desc 'Create the oracle-model-generator gem'
|
13
11
|
task :create => :clean do
|
14
12
|
spec = eval(IO.read('oracle-model-generator.gemspec'))
|
data/bin/omg
CHANGED
@@ -11,12 +11,14 @@ opts = Getopt::Long.getopts(
|
|
11
11
|
['--user', '-u', Getopt::REQUIRED],
|
12
12
|
['--password', '-p', Getopt::REQUIRED],
|
13
13
|
['--database', '-d', Getopt::REQUIRED],
|
14
|
-
['--output', '-o', Getopt::REQUIRED]
|
14
|
+
['--output', '-o', Getopt::REQUIRED],
|
15
|
+
['--rails', '-r', Getopt::REQUIRED]
|
15
16
|
)
|
16
17
|
|
17
18
|
def help
|
19
|
+
version = Oracle::Model::Generator::VERSION
|
18
20
|
%Q{
|
19
|
-
Available options for the Oracle Model Generator are:
|
21
|
+
Available options for the Oracle Model Generator (version #{version}) are:
|
20
22
|
|
21
23
|
-h, --help => Display the help text you're looking at now.
|
22
24
|
-t, --table => The name of the table you wish to model.
|
@@ -24,6 +26,7 @@ def help
|
|
24
26
|
-o, --output => The name of the file to create.
|
25
27
|
-u, --user => The user used to establish a connection to the database.
|
26
28
|
-p, --password => The password used to establish a connection to the database.
|
29
|
+
-r, --rails => The version of rails you're using (2 or 3).
|
27
30
|
|
28
31
|
If no user or password are supplied, then OMG will attempt to glean that
|
29
32
|
information using a combination of the database name and your .dbrc file.
|
@@ -37,9 +40,12 @@ def help
|
|
37
40
|
|
38
41
|
Examples:
|
39
42
|
|
40
|
-
# Create a User model for the users table
|
43
|
+
# Create a User model for the users table (rails 3)
|
41
44
|
omg -d some_database -u scott -p tiger -t users
|
42
45
|
|
46
|
+
# Create a User model for the users table (rails 2)
|
47
|
+
omg -d some_database -r 2 -u scott -p tiger -t users
|
48
|
+
|
43
49
|
# Same thing, using dbi-dbrc behind the scenes
|
44
50
|
omg -d some_database -t users
|
45
51
|
|
@@ -93,6 +99,14 @@ omg.generate(table || view, view)
|
|
93
99
|
|
94
100
|
ofile = opts['o'] || table + '.rb'
|
95
101
|
|
102
|
+
# Default to Rails 3
|
103
|
+
rails = (opts['rails'] && opts['rails'].to_i) || 3
|
104
|
+
|
105
|
+
if rails < 2 || rails > 3
|
106
|
+
puts "Invalid version of Rails. Specify either 2 or 3."
|
107
|
+
exit!
|
108
|
+
end
|
109
|
+
|
96
110
|
File.open(ofile, 'w') do |fh|
|
97
111
|
fh.puts "class #{omg.table} < ActiveRecord::Base"
|
98
112
|
fh.puts " set_table_name :#{table}"
|
@@ -111,43 +125,79 @@ File.open(ofile, 'w') do |fh|
|
|
111
125
|
|
112
126
|
fh.puts "\n # Validations\n\n"
|
113
127
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
128
|
+
if rails == 2
|
129
|
+
# Character fields, size
|
130
|
+
omg.column_info.each{ |col|
|
131
|
+
data_type = col.data_type.to_s
|
132
|
+
if ['char', 'varchar', 'varchar2'].include?(data_type)
|
133
|
+
validation = "validates_size_of :#{col.name.downcase}, :maximum => #{col.data_size}"
|
134
|
+
validation << ", :allow_blank => #{col.nullable?}" if col.nullable?
|
135
|
+
fh.puts " #{validation}"
|
136
|
+
end
|
137
|
+
}
|
123
138
|
|
124
|
-
|
139
|
+
fh.puts # Line break
|
125
140
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
141
|
+
# Fields that must be present
|
142
|
+
omg.column_info.each{ |col|
|
143
|
+
unless col.nullable?
|
144
|
+
validation = "validates_presence_of :#{col.name.downcase}"
|
145
|
+
fh.puts " #{validation}"
|
146
|
+
end
|
147
|
+
}
|
133
148
|
|
134
|
-
|
149
|
+
fh.puts # Line break
|
135
150
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
151
|
+
# Numeric fields
|
152
|
+
omg.column_info.each{ |col|
|
153
|
+
if col.data_type.to_s == 'number'
|
154
|
+
max = ("9" * (col.precision - col.scale)).to_i
|
155
|
+
|
156
|
+
validation = "validates_numericality_of :#{col.name.downcase}"
|
157
|
+
validation << ", :less_than => #{max + 1}, :greater_than => -#{max + 1}"
|
158
|
+
|
159
|
+
if col.scale == 0
|
160
|
+
validation << ", :only_integer => true"
|
161
|
+
end
|
162
|
+
|
163
|
+
fh.puts " #{validation}"
|
164
|
+
end
|
165
|
+
}
|
166
|
+
else
|
167
|
+
# Character fields, size
|
168
|
+
omg.column_info.each{ |col|
|
140
169
|
|
141
|
-
|
142
|
-
validation << ", :less_than => #{max + 1}, :greater_than => -#{max + 1}"
|
170
|
+
data_type = col.data_type.to_s
|
143
171
|
|
144
|
-
|
145
|
-
|
172
|
+
case data_type
|
173
|
+
when 'char', 'varchar', 'varchar2'
|
174
|
+
validation = "validates :#{col.name.downcase}, "
|
175
|
+
validation << ":length => {:maximum => #{col.data_size}}"
|
176
|
+
validation << ", :presence => #{!col.nullable?}" unless col.nullable?
|
177
|
+
when 'number'
|
178
|
+
max = "9" * col.precision
|
179
|
+
max.insert(col.precision - col.scale, ".") if col.scale > 0
|
180
|
+
|
181
|
+
validation = "\n validates :#{col.name.downcase}, "
|
182
|
+
|
183
|
+
unless col.nullable?
|
184
|
+
validation << ":presence => #{!col.nullable?}, "
|
185
|
+
end
|
186
|
+
|
187
|
+
validation << ":numericality => {"
|
188
|
+
validation << "\n :less_than_or_equal_to => #{max}, "
|
189
|
+
validation << "\n :greater_than_or_equal_to => -#{max}"
|
190
|
+
|
191
|
+
if col.scale == 0
|
192
|
+
validation << ",\n :only_integer => true"
|
193
|
+
end
|
194
|
+
|
195
|
+
validation << "\n }"
|
146
196
|
end
|
147
197
|
|
148
198
|
fh.puts " #{validation}"
|
149
|
-
|
150
|
-
|
199
|
+
}
|
200
|
+
end
|
151
201
|
|
152
202
|
fh.puts # Line break
|
153
203
|
header_printed = false
|
@@ -3,7 +3,7 @@ require 'oci8'
|
|
3
3
|
module Oracle
|
4
4
|
module Model
|
5
5
|
class Generator
|
6
|
-
VERSION = '0.
|
6
|
+
VERSION = '0.2.0'
|
7
7
|
|
8
8
|
attr_reader :connection
|
9
9
|
attr_reader :constraints
|
@@ -32,6 +32,13 @@ module Oracle
|
|
32
32
|
@table = nil
|
33
33
|
end
|
34
34
|
|
35
|
+
# Generates an Oracle::Model::Generator object for +table+. If this is
|
36
|
+
# a view (materialized or otherwise), set the +view+ argument to true.
|
37
|
+
#
|
38
|
+
# This method does not actually generate a file of any sort. It merely
|
39
|
+
# sets instance variables which you can then use in your own class/file
|
40
|
+
# generation programs.
|
41
|
+
#
|
35
42
|
def generate(table, view = false)
|
36
43
|
@table = table.split('_').map{ |e| e.downcase.capitalize }.join
|
37
44
|
@view = view
|
@@ -58,6 +65,8 @@ module Oracle
|
|
58
65
|
}
|
59
66
|
end
|
60
67
|
|
68
|
+
# Returns an array of foreign keys.
|
69
|
+
#
|
61
70
|
def get_foreign_keys
|
62
71
|
@constraints.each{ |hash|
|
63
72
|
if hash['CONSTRAINT_TYPE'] == 'R'
|
@@ -68,12 +77,17 @@ module Oracle
|
|
68
77
|
get_belongs_to()
|
69
78
|
end
|
70
79
|
|
80
|
+
# Returns an array of tables that the current table has foreign key
|
81
|
+
# ties to.
|
82
|
+
#
|
71
83
|
def get_belongs_to
|
72
84
|
@foreign_keys.each{ |fk|
|
73
85
|
@belongs_to << find_fk_table(fk)
|
74
86
|
}
|
75
87
|
end
|
76
88
|
|
89
|
+
# Find table name based on a foreign key name.
|
90
|
+
#
|
77
91
|
def find_fk_table(fk)
|
78
92
|
sql = %Q{
|
79
93
|
select table_name
|
@@ -83,12 +97,12 @@ module Oracle
|
|
83
97
|
|
84
98
|
begin
|
85
99
|
cursor = @connection.exec(sql)
|
86
|
-
|
100
|
+
table = cursor.fetch.first
|
87
101
|
ensure
|
88
102
|
cursor.close if cursor
|
89
103
|
end
|
90
104
|
|
91
|
-
|
105
|
+
table
|
92
106
|
end
|
93
107
|
|
94
108
|
# Get a list of constraints for a given table.
|
@@ -13,7 +13,7 @@ class TC_Oracle_Model_Generator < Test::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
|
15
15
|
test "version number is correct" do
|
16
|
-
assert_equal('0.
|
16
|
+
assert_equal('0.2.0', Oracle::Model::Generator::VERSION)
|
17
17
|
end
|
18
18
|
|
19
19
|
test "constructor accepts an oci8 connection object" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oracle-model-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel J. Berger
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-03-08 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
requirements: []
|
115
115
|
|
116
116
|
rubyforge_project: N/A
|
117
|
-
rubygems_version: 1.
|
117
|
+
rubygems_version: 1.6.0
|
118
118
|
signing_key:
|
119
119
|
specification_version: 3
|
120
120
|
summary: A Ruby interface for determining protocol information
|