drysql 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/associations.rb CHANGED
@@ -16,6 +16,11 @@ module ActiveRecord
16
16
  end
17
17
  end
18
18
 
19
+ def clear_cached_associations
20
+ @associations = nil
21
+ logger.info("Cleared cached schema for: #{self}")
22
+ end
23
+
19
24
  def is_associated_with(class_name_symbol)
20
25
  !(reflections.detect {|key, reflection| key.to_s.singularize == class_name_symbol.to_s.singularize}).nil?
21
26
  end
data/lib/base.rb CHANGED
@@ -48,8 +48,10 @@ module ActiveRecord
48
48
  # ---------------------------------------------------------------------------------------------------
49
49
  def primary_key
50
50
  primary = table_constraints.detect {|constraint| constraint.primary_key?}
51
+ if primary.nil? then logger.error("No primary key defined for table #{table_name}") end
51
52
  primary_name = primary.column_name
52
53
  set_primary_key(primary_name)
54
+ logger.info("Identified PRIMARY KEY for #{self}: #{primary_name}")
53
55
  primary_name
54
56
  end
55
57
 
@@ -91,12 +93,23 @@ module ActiveRecord
91
93
  end
92
94
 
93
95
  def table_exists?(table_name=nil)
94
- if connection.respond_to?(:tables)
95
- connection.tables.include? construct_table_name_from_class_name(table_name)
96
+ if connection.respond_to?(:tables)
97
+ connection.tables.include? table_name
96
98
  else
97
99
  false
98
100
  end
99
101
  end
102
+
103
+ def regenerate_cached_schema
104
+ clear_cached_associations
105
+ clear_cached_validations
106
+ @constraints = nil
107
+ @columns = nil
108
+ primary = table_constraints.detect {|constraint| constraint.primary_key?}
109
+ primary_name = primary.column_name
110
+ set_primary_key(primary_name)
111
+ logger.info("Reset PRIMARY KEY for #{self}: #{primary_name}")
112
+ end
100
113
 
101
114
 
102
115
  private
@@ -121,10 +134,6 @@ module ActiveRecord
121
134
  def pluralized_symbolized_class_name_from_table_name(table_name)
122
135
  (class_name(table_name)).downcaseFirstLetter.pluralize.to_sym
123
136
  end
124
-
125
- def construct_table_name_from_class_name(class_name)
126
- name = "#{table_name_prefix}#{undecorated_table_name(class_name)}#{table_name_suffix}"
127
- end
128
137
 
129
138
  end #end class << self (Class Methods)
130
139
 
@@ -6,10 +6,10 @@ module ActiveRecord
6
6
  # databases compatible with the ISO SQL:2003 information schema standard
7
7
  class AbstractTableConstraint
8
8
 
9
- @@PRIMARY_KEY_TYPE = "PRIMARY KEY"
10
- @@FOREIGN_KEY_TYPE = "FOREIGN KEY"
11
- @@UNIQUE_KEY_TYPE = "UNIQUE"
12
- @@CHECK_CONSTRAINT_TYPE = "CHECK"
9
+ PRIMARY_KEY_TYPE = "PRIMARY KEY"
10
+ FOREIGN_KEY_TYPE = "FOREIGN KEY"
11
+ UNIQUE_KEY_TYPE = "UNIQUE"
12
+ CHECK_CONSTRAINT_TYPE = "CHECK"
13
13
 
14
14
  attr_reader :constraint_catalog, :constraint_schema, :constraint_name, :constraint_type,
15
15
  :table_catalog, :table_schema, :table_name, :column_name,
@@ -24,15 +24,15 @@ module ActiveRecord
24
24
  alias :initialize :raise_subclass_responsibility_error
25
25
 
26
26
  def primary_key?
27
- constraint_type == @@PRIMARY_KEY_TYPE
27
+ constraint_type == PRIMARY_KEY_TYPE
28
28
  end
29
29
 
30
30
  def foreign_key?
31
- constraint_type == @@FOREIGN_KEY_TYPE
31
+ constraint_type == FOREIGN_KEY_TYPE
32
32
  end
33
33
 
34
34
  def component_of_unique_key?
35
- constraint_type == @@UNIQUE_KEY_TYPE
35
+ constraint_type == UNIQUE_KEY_TYPE
36
36
  end
37
37
 
38
38
  def unique_key?
@@ -0,0 +1,120 @@
1
+ module ActiveRecord
2
+
3
+ module ConnectionAdapters
4
+
5
+ class OracleColumn < Column
6
+
7
+ attr_accessor :primary_key
8
+
9
+ def default_specified?
10
+ !default.nil?
11
+ end
12
+
13
+ # The only means that Oracle currently provides for auto-generating a column
14
+ # is to define a sequence and a trigger on insert that inserts sequence.nextval
15
+ #
16
+ # As a result of this, there is no reliable way to determine whether a particular
17
+ # column is generated by the DB.
18
+ #
19
+ # DrySQL currently makes the assumption that a column is auto-generated
20
+ # if the column is of type integer and is the primary key for the table
21
+ #
22
+ # Ideally a future release of Oracle will support declaring a default value
23
+ # of sequence.nextval for a column, which will eliminate this problem.
24
+ def generated?
25
+ @type == :integer && @primary_key == true
26
+ end
27
+
28
+ end
29
+
30
+ class OracleConstraint < AbstractTableConstraint
31
+
32
+ PRIMARY_KEY_TYPE = "P"
33
+ FOREIGN_KEY_TYPE = "R"
34
+ UNIQUE_KEY_TYPE = "U"
35
+ CHECK_CONSTRAINT_TYPE = "C"
36
+
37
+ def initialize(constraint_catalog, constraint_schema, constraint_name, table_schema, table_name, constraint_type,
38
+ column_name, referenced_table_schema, referenced_table_name, referenced_column_name)
39
+ @constraint_catalog = constraint_catalog
40
+ @constraint_schema = constraint_schema
41
+ @constraint_name = constraint_name
42
+ @table_schema = table_schema
43
+ @table_name = table_name
44
+ @constraint_type = constraint_type
45
+ @column_name = column_name
46
+ @referenced_table_schema = referenced_table_schema
47
+ @referenced_table_name = referenced_table_name
48
+ @referenced_column_name = referenced_column_name
49
+ end
50
+
51
+ def primary_key?
52
+ constraint_type == PRIMARY_KEY_TYPE
53
+ end
54
+
55
+ def foreign_key?
56
+ constraint_type == FOREIGN_KEY_TYPE
57
+ end
58
+
59
+ def component_of_unique_key?
60
+ constraint_type == UNIQUE_KEY_TYPE
61
+ end
62
+
63
+ end
64
+
65
+ class OracleAdapter < AbstractAdapter
66
+
67
+ alias :base_columns :columns
68
+ def columns(table_name, name = nil) #:nodoc:
69
+ columns = base_columns(table_name)
70
+
71
+ # This is a hack. Once Oracle offers a better means of auto-generating IDs,
72
+ # this will be fixed
73
+ # FIXME must be fixed once composite primary keys are supported
74
+ sql = "select t1.column_name from all_cons_columns t1 inner join all_constraints t2 on \
75
+ (t1.owner = t2.owner AND t1.constraint_name = t2.constraint_name) where t1.table_name='#{table_name}' and t2.constraint_type='P'"
76
+ primary_key_column_name = select_one(sql, "Primary Key")['column_name']
77
+
78
+ columns.each do |column|
79
+ column.primary_key=(oracle_downcase(primary_key_column_name) == column.name)
80
+ end
81
+ end
82
+
83
+ def constraints(table_name, name = nil)#:nodoc:
84
+ constraints = []
85
+ sql = "select t1.owner, t1.constraint_name, t1.constraint_type, t1.table_name, t1.r_owner, t1.r_constraint_name, \
86
+ t1.delete_rule, t2.column_name, t3.table_name as r_table_name, t3.column_name as r_column_name from all_constraints t1 \
87
+ inner join all_cons_columns t2 on ( t1.owner = t2.owner AND t1.constraint_name = t2.constraint_name) left outer join \
88
+ all_cons_columns t3 on (t1.r_owner = t3.owner AND t1.r_constraint_name = t3.constraint_name) where \
89
+ t1.table_name='#{table_name}' or t3.table_name='#{table_name}'"
90
+
91
+ results = select_all(sql, name)
92
+ constraint_name_hash = {}
93
+ results.each do |row|
94
+
95
+ # The author(s) of the Oracle adapter chose to selectively downcase column names for
96
+ # "cleanliness" within our Rails code. I had to follow suit with constraint column names
97
+ # so that model objects could look up their data values using the generated column accessor methods
98
+ row['column_name'] = oracle_downcase(row['column_name'])
99
+ unless row['r_column_name'].nil? then row['r_column_name'] = oracle_downcase(row['r_column_name']) end
100
+
101
+ constraints << OracleConstraint.new(nil, row['owner'], row['constraint_name'], row['owner'], row['table_name'],
102
+ row['constraint_type'], row['column_name'], row['r_owner'], row['r_table_name'], row['r_column_name'])
103
+ comparable_constraint_name = row['constraint_name'].upcase
104
+ constraint_name_count = constraint_name_hash[comparable_constraint_name]
105
+ constraint_name_count ?
106
+ constraint_name_hash[comparable_constraint_name] = constraint_name_count + 1 :
107
+ constraint_name_hash[comparable_constraint_name] = 1
108
+ end
109
+
110
+ constraints.each do | constraint|
111
+ constraint.member_of_composite=(constraint_name_hash[constraint.constraint_name.upcase] > 1)
112
+ end
113
+ constraints
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -38,6 +38,9 @@ module ActiveRecord
38
38
 
39
39
  class PostgreSQLAdapter < AbstractAdapter
40
40
 
41
+ # FIXME
42
+ # patch postgresql_adapter so I don't need to re-define columns here in order
43
+ # to get sufficient column metadata
41
44
  def columns(table_name, name = nil) #:nodoc:
42
45
  columns = []
43
46
  column_definitions(table_name).each do |name, type, default, notnull, typmod|
data/lib/drysql.rb CHANGED
@@ -7,6 +7,7 @@ require 'connection_adapters/abstract_adapter'
7
7
  require 'connection_adapters/mysql_adapter'
8
8
  require 'connection_adapters/postgresql_adapter'
9
9
  require 'connection_adapters/sqlserver_adapter'
10
+ require 'connection_adapters/oracle_adapter'
10
11
  require 'connection_adapters/abstract/schema_definitions'
11
12
  require 'associations'
12
13
  require 'validations'
data/lib/validations.rb CHANGED
@@ -99,7 +99,11 @@ module ActiveRecord
99
99
  @validations=true
100
100
  end
101
101
 
102
-
102
+ def clear_cached_validations
103
+ @validations = nil
104
+ logger.info("Cleared cached validations for: #{self}")
105
+ end
106
+
103
107
  end # end of class methods
104
108
 
105
109
  end # end of class Base
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.1.3
7
- date: 2006-11-16 00:00:00 -05:00
6
+ version: 0.1.4
7
+ date: 2006-11-26 00:00:00 -05:00
8
8
  summary: Dynamic, Reflective, Invisible Object-Relational Mapping for Ruby
9
9
  require_paths:
10
10
  - lib
@@ -28,16 +28,17 @@ 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/postgresql_adapter.rb
37
+ - lib/connection_adapters/abstract_adapter.rb
38
38
  - lib/connection_adapters/mysql_adapter.rb
39
+ - lib/connection_adapters/oracle_adapter.rb
40
+ - lib/connection_adapters/postgresql_adapter.rb
39
41
  - lib/connection_adapters/sqlserver_adapter.rb
40
- - lib/connection_adapters/abstract_adapter.rb
41
42
  - lib/connection_adapters/abstract/schema_definitions.rb
42
43
  - README
43
44
  test_files: []