Class: DBD4::Relation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Relation

Returns a new instance of Relation.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/dbd4/dbd4_model_file.rb', line 283

def initialize(relation)
  @join_table = nil
  @fk_fields = ForeignKeyFields.new(relation.attributes['FKFields'])
  @id = relation.attributes['ID']
  @name = relation.attributes['RelationName']
  @kind = relation.attributes['Kind']
  @type = nil
  case @kind
    when '5'
      @type = :one2one
    when '2'
      @type = :one2many  
    when '3'
      @type = :many2many 
  end
  @source_table = relation.attributes['SrcTable']
  @destination_table = relation.attributes['DestTable']
end

Instance Attribute Details

#destination_columnObject (readonly)

Returns the value of attribute destination_column.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def destination_column
  @destination_column
end

#destination_tableObject

Returns the value of attribute destination_table.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def destination_table
  @destination_table
end

#fk_fieldsObject (readonly)

Returns the value of attribute fk_fields.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def fk_fields
  @fk_fields
end

#idObject (readonly)

Returns the value of attribute id.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def id
  @id
end

#join_tableObject

Returns the value of attribute join_table.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def join_table
  @join_table
end

#nameObject (readonly)

Returns the value of attribute name.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def name
  @name
end

#source_columnObject (readonly)

Returns the value of attribute source_column.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def source_column
  @source_column
end

#source_tableObject

Returns the value of attribute source_table.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def source_table
  @source_table
end

#typeObject

Returns the value of attribute type.



280
281
282
# File 'lib/dbd4/dbd4_model_file.rb', line 280

def type
  @type
end

Instance Method Details

#resolve(allObjects) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/dbd4/dbd4_model_file.rb', line 302

def resolve(allObjects)
  @source_table = allObjects[:tables][@source_table] 
  @destination_table = allObjects[:tables][@destination_table]
  if @type != :many2many
    @source_column = @source_table.columns.primary_keys[@fk_fields[0].source_field]
    @destination_column = @destination_table.columns.foreign_keys[@fk_fields[0].destination_field]      
  end
  
  if @type == :many2many and @name =~ /acts_as_graph/
    @type = :acts_as_graph
  end
end

#to_strObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/dbd4/dbd4_model_file.rb', line 337

def to_str
  result = "Relation(id=#{id}, name=#{name}, type=#{type},"
  if source_column != nil
    result += " src_col=#{source_column.name},"
  else
    result += " src_col=nil,"
  end
  if destination_column != nil
    result += " dst_col=#{destination_column.name},"
  else
    result += " dst_col=nil,"
  end
  result += " src=#{source_table.name}, dst=#{destination_table.name}, fk=#{fk_fields.to_str},"
  if join_table != nil
    result += " join_table=#{join_table.name}"
  else
    result += " join_table=nil"
  end
  result += ")"
  result
end

#validate(messages) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dbd4/dbd4_model_file.rb', line 315

def validate(messages)
  if @type == nil
    messages[:warnings] << "Warning : between table #{@destination_table.name} and table #{@source_table.name}, type #{@kind} is invalid"
  end
  
  if @fk_fields.size > 1
    messages[:warnings] << "Warning : between table #{@destination_table.name} and table #{@source_table.name}, relation #{name} targets more than 1 foreign_key" 
  end
  
  if !( @type != nil and (@type == :many2many or @type == :acts_as_graph) and @join_table == nil )
    if (@source_table.name == @destination_table.name) 
      messages[:warnings] << "Warning : relation #{name} has same source and target table name : \"#{@source_table.name}\", dbd4 does not support this yet"
    else
      expected_fkey_name = Inflector.singularize(@source_table.name) + "_id"
      if @destination_column.name != expected_fkey_name and @destination_column.polymorphic == nil then
        messages[:warnings] << "Warning : table #{@destination_column.table.name}, foreign key #{@destination_column.name} does not match expected name #{expected_fkey_name}"
        @destination_column.non_standard_name = true
      end
    end
  end
end