Class: IBRuby::InterBaseConstraint

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

Constant Summary collapse

PRIMARY_KEY =
:PRIMARY_KEY
FOREIGN_KEY =
:FOREIGN_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, columns, foreign_cols = nil, foreign_table_name = nil, name = nil) ⇒ InterBaseConstraint

columns are strings only, not complete columns. if they are complete columns then we just get their name



106
107
108
109
110
111
112
113
114
# File 'lib/ibmeta.rb', line 106

def initialize(type, columns, foreign_cols=nil, foreign_table_name=nil, name=nil)
  @name = name
  @type = type
  @columns = columns
  @foreign_cols=foreign_cols
  @foreign_table_name=foreign_table_name
  
  #puts "constraint created #{to_sql}"
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



99
100
101
# File 'lib/ibmeta.rb', line 99

def columns
  @columns
end

#foreign_colsObject

Returns the value of attribute foreign_cols.



99
100
101
# File 'lib/ibmeta.rb', line 99

def foreign_cols
  @foreign_cols
end

#foreign_table_nameObject

Returns the value of attribute foreign_table_name.



99
100
101
# File 'lib/ibmeta.rb', line 99

def foreign_table_name
  @foreign_table_name
end

#nameObject

Returns the value of attribute name.



99
100
101
# File 'lib/ibmeta.rb', line 99

def name
  @name
end

#tableObject

Returns the value of attribute table.



99
100
101
# File 'lib/ibmeta.rb', line 99

def table
  @table
end

#typeObject

Returns the value of attribute type.



99
100
101
# File 'lib/ibmeta.rb', line 99

def type
  @type
end

Instance Method Details

#to_sqlObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ibmeta.rb', line 116

def to_sql
  tab_name = table.nil? ? "NULL" : table.name
  sql = "alter table #{tab_name} add "
  
  if @type == :PRIMARY_KEY
    sql << "primary key ("
    sql << cycle_cols
    sql << ")"
  elsif @type == :FOREIGN_KEY
    sql << "foreign key ("
    sql << cycle_cols
    sql << ") references #{foreign_table_name} ("
    sql << cycle_cols(foreign_cols)
    sql << ")"
  else
    raise NotImplementedError, "Other constraints #{@type.to_s} not implemented yet"
  end
end