Class: DBD4::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/dbd4/dbd4_model_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Table

Returns a new instance of Table.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dbd4/dbd4_model_file.rb', line 90

def initialize(table)
  @id = table.attributes['ID']
  @comments = table.attributes['Comments']
  @name = table.attributes['Tablename']
  @modelname = Inflector.singularize(@name)
  @nm_table = table.attributes['nmTable'];
  @primary_key = nil;
  
  @relation_starts = Array.new
  @relation_ends = Array.new
  @columns = Columns.new(self)
  @non_standard_name = false
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def columns
  @columns
end

#commentsObject (readonly)

Returns the value of attribute comments.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def comments
  @comments
end

#idObject (readonly)

Returns the value of attribute id.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def id
  @id
end

#modelfile=(value) ⇒ Object (writeonly)

Sets the attribute modelfile

Parameters:

  • value

    the value to set the attribute modelfile to.



88
89
90
# File 'lib/dbd4/dbd4_model_file.rb', line 88

def modelfile=(value)
  @modelfile = value
end

#modelnameObject (readonly)

Returns the value of attribute modelname.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def modelname
  @modelname
end

#nameObject

Returns the value of attribute name.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def name
  @name
end

#nm_tableObject

Returns the value of attribute nm_table.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def nm_table
  @nm_table
end

#non_standard_nameObject (readonly)

Returns the value of attribute non_standard_name.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def non_standard_name
  @non_standard_name
end

#relation_endsObject (readonly)

Returns the value of attribute relation_ends.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def relation_ends
  @relation_ends
end

#relation_startsObject (readonly)

Returns the value of attribute relation_starts.



87
88
89
# File 'lib/dbd4/dbd4_model_file.rb', line 87

def relation_starts
  @relation_starts
end

Class Method Details

.createJoinTableName(t1, t2) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/dbd4/dbd4_model_file.rb', line 104

def self.createJoinTableName(t1, t2)
  if t1.name < t2.name
    "#{t1.name}_#{t2.name}"
  else
    "#{t2.name}_#{t1.name}"
  end
end

Instance Method Details

#addColumnFromXml(column_xml) ⇒ Object



112
113
114
115
116
# File 'lib/dbd4/dbd4_model_file.rb', line 112

def addColumnFromXml(column_xml)
  column = Column.new(column_xml, self)
  @primary_key = column if column.primary_key? and ! @primary_key
  columns << column
end

#addEndRelation(e) ⇒ Object



118
119
120
# File 'lib/dbd4/dbd4_model_file.rb', line 118

def addEndRelation(e)
  @relation_ends << e
end

#addStartRelation(e) ⇒ Object



122
123
124
# File 'lib/dbd4/dbd4_model_file.rb', line 122

def addStartRelation(e)
  @relation_starts << e
end

#generateModelFileObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dbd4/dbd4_model_file.rb', line 178

def generateModelFile 
  if @nm_table == '0'
    m = RailsModelFile.new({:modelname => @modelname})
    m.update(self)
  end
  m = RailsMigrationFile.new({:tablename => @name})
  m.update(self)
  
  @relation_starts.each_value do |rs|
    if rs.type == :many2many and rs.join_table == nil
      m = RailsMigrationFile.new({:virtual => true, :tablename => Table.createJoinTableName(self, rs.destination_table)})
      m.update_implicit_join_table(rs)
    elsif rs.type == :acts_as_graph
      m = RailsMigrationFile.new({:virtual => true, :tablename => "#{name}_edges"})
      m.update_acts_as_graph_table(rs)
    end
  end
end

#resolve(allObjects) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dbd4/dbd4_model_file.rb', line 160

def resolve(allObjects)
  @columns.resolve(allObjects)
  tmp = Relations.new
  @relation_starts.each { |r| tmp<<allObjects[:relations][r.id] }
  @relation_starts = tmp
  tmp = Relations.new
  @relation_ends.each { |r| tmp<<allObjects[:relations][r.id] }
  @relation_ends = tmp
  if @relation_ends.values.size == 2 and @nm_table == '1' then
    @relation_ends.values[0].destination_table = @relation_ends.values[1].source_table
    @relation_ends.values[0].join_table = self
    @relation_ends.values[1].destination_table = @relation_ends.values[0].source_table
    @relation_ends.values[1].join_table = self
    @relation_ends.values[0].type = :many2many
    @relation_ends.values[1].type = :many2many
  end
end

#to_strObject



197
198
199
200
201
202
203
204
205
# File 'lib/dbd4/dbd4_model_file.rb', line 197

def to_str
  "Table(id=#{id}, name=#{name}, non_standard_name=#{non_standard_name}, nm_table=#{nm_table})\n" +
  "Start relations\n" +
  @relation_starts.to_str + "\n" +
  "End relations" + "\n" +
  @relation_ends.to_str + "\n" +
  "Columns" + "\n" +
  @columns.to_str + "\n-----------------------\n"
end

#validate(messages) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dbd4/dbd4_model_file.rb', line 126

def validate(messages)
  if @nm_table == '1'
    if @relation_ends.size != 2
      messages[:warnings] << "Warning : join table #{name} must have only 2 foreign keys with one2many types."
    elsif @relation_ends.values[0].type != :many2many or relation_ends.values[1].type != :many2many
      messages[:warnings] << "Warning : join table #{name} has a relation that is not many2many"
    else
      t1 = @relation_ends.values[0].source_table
      t2 = @relation_ends.values[1].source_table
      expected_name = Table.createJoinTableName(t1, t2)
      if @name != expected_name then
        messages[:warnings] << "Warning : join table #{name} differs from expected #{expected_name}"
        @non_standard_name = true
      end
    end
  else
    if @columns.size == 2 and @columns.foreign_keys.size == 2 and @relation_ends.values.size == 2 and @relation_ends.values[0].type == :one2many and @relation_ends.values[1].type == :one2many
      messages[:warnings] << "Warning : table name #{name} seems to be a join table (many2many) please specify 'is nm_table' in DBDesigner"
    end
    
    if @modelname == @name
      messages[:warnings] << "Warning : table name #{name} is not in a valid plural form, should be #{Inflector.pluralize(name)}"
      @non_standard_name = true
    elsif Inflector.pluralize(@modelname) != @name
      messages[:warnings] << "Warning : table name #{name} is not in a valid plural formm should be #{Inflector.pluralize(@modelname)}"
      @non_standard_name = true
    end
  end
  
  columns.validate(messages)
  relation_starts.validate(messages)
  # no need to validate relation_ends, since we would do the job twice... doh
end