drysql 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/associations.rb +19 -11
- data/lib/base.rb +0 -8
- data/lib/connection_adapters/oracle_adapter.rb +8 -5
- metadata +8 -8
data/lib/associations.rb
CHANGED
@@ -21,9 +21,7 @@ module ActiveRecord
|
|
21
21
|
logger.info("DRYSQL >> Cleared cached schema for: #{self}")
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
!(reflections.detect {|key, reflection| key.to_s.singularize == class_name_symbol.to_s.singularize}).nil?
|
26
|
-
end
|
24
|
+
|
27
25
|
|
28
26
|
private
|
29
27
|
|
@@ -31,7 +29,7 @@ module ActiveRecord
|
|
31
29
|
def generate_belongs_to_associations
|
32
30
|
foreign_keys = table_constraints.select {|constraint| constraint.foreign_key?}
|
33
31
|
foreign_keys.each do |foreign_key|
|
34
|
-
belongs_to_class_name = (class_name(foreign_key.referenced_table_name))
|
32
|
+
belongs_to_class_name = association_class_name_from_table_name(class_name(foreign_key.referenced_table_name))
|
35
33
|
self.send(:belongs_to, :"#{belongs_to_class_name}", :foreign_key=>foreign_key.column_name.to_a[0])
|
36
34
|
logger.info("DRYSQL >> GENERATED ASSOCIATION: #{self.name} belongs_to :#{belongs_to_class_name}, :foreign_key=>#{foreign_key.column_name.to_a[0]}")
|
37
35
|
end
|
@@ -42,11 +40,11 @@ module ActiveRecord
|
|
42
40
|
def generate_has_many_associations
|
43
41
|
foreign_constraints.each do |foreign_constraint|
|
44
42
|
if foreign_constraint_column_is_unique?(foreign_constraint)
|
45
|
-
has_one_class_name = (class_name(foreign_constraint.table_name))
|
43
|
+
has_one_class_name = association_class_name_from_table_name(class_name(foreign_constraint.table_name))
|
46
44
|
self.send(:has_one, :"#{has_one_class_name}", :foreign_key=>foreign_constraint.column_name.to_a[0])
|
47
45
|
logger.info("DRYSQL >> GENERATED ASSOCIATION: #{self.name} has_one :#{has_one_class_name}, :foreign_key=>#{foreign_constraint.column_name.to_a[0]}")
|
48
46
|
else
|
49
|
-
has_many_class_name = (class_name(foreign_constraint.table_name)).
|
47
|
+
has_many_class_name = association_class_name_from_table_name(class_name(foreign_constraint.table_name)).pluralize
|
50
48
|
self.send(:has_many, :"#{has_many_class_name}", :foreign_key=>foreign_constraint.column_name.to_a[0])
|
51
49
|
logger.info("DRYSQL >> GENERATED ASSOCIATION: #{self.name} has_many :#{has_many_class_name}, :foreign_key=>#{foreign_constraint.column_name.to_a[0]}")
|
52
50
|
end
|
@@ -80,15 +78,25 @@ module ActiveRecord
|
|
80
78
|
has_many_associations.each do |association|
|
81
79
|
# FIXME the singularization is a hack. We should be able to figure the exact class name without relying
|
82
80
|
# on naming conventions
|
83
|
-
foreign_class = instance_eval(association[0].to_s.camelize.singularize)
|
81
|
+
foreign_class = instance_eval(association[0].to_s.camelize.singularize)
|
84
82
|
eligible_foreign_keys = foreign_class.table_constraints.select {|c| c.foreign_key? && c.referenced_table_name.upcase != table_name.upcase}
|
85
|
-
eligible_foreign_keys.select {|key| !
|
86
|
-
has_many_class_name =
|
87
|
-
self.send(:has_many, has_many_class_name, :through=>association[0])
|
88
|
-
logger.info("DRYSQL >> GENERATED ASSOCIATION: #{self.name} has_many
|
83
|
+
eligible_foreign_keys.select {|key| !is_associated_with(association_class_name_from_table_name(key.referenced_table_name))}.each do |fk|
|
84
|
+
has_many_class_name = association_class_name_from_table_name(class_name(fk.referenced_table_name)).pluralize
|
85
|
+
self.send(:has_many, :"#{has_many_class_name}", :through=>association[0])
|
86
|
+
logger.info("DRYSQL >> GENERATED ASSOCIATION: #{self.name} has_many :#{has_many_class_name}, :through=>:#{association[0]}")
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
90
|
+
|
91
|
+
|
92
|
+
def is_associated_with(class_name_symbol)
|
93
|
+
!(reflections.detect {|key, reflection| key.to_s.singularize == class_name_symbol.to_s.singularize}).nil?
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def association_class_name_from_table_name(table_name)
|
98
|
+
Inflector.underscore(Inflector.demodulize(class_name(table_name)))
|
99
|
+
end
|
92
100
|
|
93
101
|
end # end of class methods
|
94
102
|
|
data/lib/base.rb
CHANGED
@@ -156,14 +156,6 @@ module ActiveRecord
|
|
156
156
|
reflections.select {|key, reflection| reflection.macro == :has_many}
|
157
157
|
end
|
158
158
|
|
159
|
-
def symbolized_class_name_from_table_name(table_name)
|
160
|
-
(class_name(table_name)).downcaseFirstLetter.to_sym
|
161
|
-
end
|
162
|
-
|
163
|
-
def pluralized_symbolized_class_name_from_table_name(table_name)
|
164
|
-
(class_name(table_name)).downcaseFirstLetter.pluralize.to_sym
|
165
|
-
end
|
166
|
-
|
167
159
|
def construct_table_name_from_class_name(class_name)
|
168
160
|
name = "#{table_name_prefix}#{undecorated_table_name(class_name)}#{table_name_suffix}"
|
169
161
|
end
|
@@ -93,25 +93,28 @@ module ActiveRecord
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def constraints(table_name, name = nil)#:nodoc:
|
96
|
-
constraints = []
|
96
|
+
constraints = []
|
97
|
+
upcase_table_name = table_name.upcase
|
97
98
|
sql = "select UC.constraint_name, UC.constraint_type, UC.table_name, COL.column_name, \
|
98
99
|
REF.r_constraint_name as referenced_constraint_name, REF.constraint_name as foreign_constraint_name, \
|
99
100
|
REF.delete_rule as foreign_delete_rule, COLREF.table_name as foreign_table_name, COLREF.column_name as foreign_column_name from \
|
100
101
|
(select owner, constraint_name, constraint_type, table_name, r_owner, r_constraint_name \
|
101
|
-
from all_constraints where table_name='#{
|
102
|
-
(select constraint_name, table_name, column_name from all_cons_columns where table_name='#{
|
102
|
+
from all_constraints where table_name='#{upcase_table_name}') UC inner join \
|
103
|
+
(select constraint_name, table_name, column_name from all_cons_columns where table_name='#{upcase_table_name}') COL on \
|
103
104
|
(COL.constraint_name = UC.constraint_name) left join all_constraints REF on \
|
104
105
|
(UC.constraint_name=REF.constraint_name OR UC.constraint_name=REF.r_constraint_name) left join all_cons_columns COLREF on \
|
105
|
-
(not COLREF.table_name='#{
|
106
|
+
(not COLREF.table_name='#{upcase_table_name}' AND (REF.constraint_name=COLREF.constraint_name OR REF.r_constraint_name=COLREF.constraint_name))"
|
106
107
|
|
107
108
|
results = select_all(sql, name)
|
108
109
|
constraint_name_hash = {}
|
109
110
|
results.each do |row|
|
110
111
|
# The author(s) of the Oracle adapter chose to selectively downcase column names for
|
111
|
-
# "cleanliness" within our Rails code. I had to follow suit with constraint column names
|
112
|
+
# "cleanliness" within our Rails code. I had to follow suit with constraint column & table names
|
112
113
|
# so that model objects could look up their data values using the generated column accessor methods
|
113
114
|
column_name = oracle_downcase(row['column_name'])
|
114
115
|
unless row['foreign_column_name'].nil? then row['foreign_column_name'] = oracle_downcase(row['foreign_column_name']) end
|
116
|
+
unless row['table_name'].nil? then row['table_name'] = oracle_downcase(row['table_name']) end
|
117
|
+
unless row['foreign_table_name'].nil? then row['foreign_table_name'] = oracle_downcase(row['foreign_table_name']) end
|
115
118
|
constraint_name = row['constraint_name']
|
116
119
|
foreign_constraint_name = row['foreign_constraint_name']
|
117
120
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: drysql
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.2.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.2.1
|
7
|
+
date: 2007-02-08 00:00:00 -05:00
|
8
8
|
summary: Dynamic, Reflective, Invisible Object-Relational Mapping for Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -28,18 +28,18 @@ cert_chain:
|
|
28
28
|
authors:
|
29
29
|
- Bryan Evans
|
30
30
|
files:
|
31
|
-
- lib/validations.rb
|
32
|
-
- lib/dependencies.rb
|
33
31
|
- lib/associations.rb
|
34
|
-
- lib/drysql.rb
|
35
32
|
- lib/base.rb
|
33
|
+
- lib/dependencies.rb
|
34
|
+
- lib/drysql.rb
|
35
|
+
- lib/validations.rb
|
36
36
|
- lib/helpers/string.rb
|
37
|
-
- lib/connection_adapters/
|
37
|
+
- lib/connection_adapters/abstract_adapter.rb
|
38
|
+
- lib/connection_adapters/ibm_db2_adapter.rb
|
38
39
|
- lib/connection_adapters/mysql_adapter.rb
|
39
40
|
- lib/connection_adapters/oracle_adapter.rb
|
41
|
+
- lib/connection_adapters/postgresql_adapter.rb
|
40
42
|
- lib/connection_adapters/sqlserver_adapter.rb
|
41
|
-
- lib/connection_adapters/abstract_adapter.rb
|
42
|
-
- lib/connection_adapters/ibm_db2_adapter.rb
|
43
43
|
- lib/connection_adapters/abstract/schema_definitions.rb
|
44
44
|
- README
|
45
45
|
test_files: []
|