Class: IBRuby::InterBaseTable

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

Constant Summary collapse

SQL_ALL =
:SQL_ALL
SQL_TABLE =
:SQL_TABLE
SQL_INDICES =
:SQL_INDICES
SQL_PRIMARY_KEYS =
:SQL_PRIMARY_KEYS
SQL_FOREIGN_KEYS =
:SQL_FOREIGN_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, columns = [], indices = [], constraints = []) ⇒ InterBaseTable

Returns a new instance of InterBaseTable.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ibmeta.rb', line 165

def initialize(name, columns=[], indices=[], constraints=[] )
  #puts "table name new table: #{name}"
  @name = name.to_s.upcase
  @columns = columns
  @indices = indices
  @constraints = constraints
  
  if @constraints
    @constraints.each() {|c| c.table = self }
  end
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



157
158
159
# File 'lib/ibmeta.rb', line 157

def columns
  @columns
end

#constraintsObject

Returns the value of attribute constraints.



157
158
159
# File 'lib/ibmeta.rb', line 157

def constraints
  @constraints
end

#indicesObject

Returns the value of attribute indices.



157
158
159
# File 'lib/ibmeta.rb', line 157

def indices
  @indices
end

#nameObject

Returns the value of attribute name.



157
158
159
# File 'lib/ibmeta.rb', line 157

def name
  @name
end

Instance Method Details

#create_table(conn) ⇒ Object



194
195
196
197
198
199
# File 'lib/ibmeta.rb', line 194

def create_table(conn)
  to_sql.each() do |sql|
    #puts "executing: #{sql}"
    conn.execute_immediate( sql )
  end
end

#drop_table(conn) ⇒ Object



189
190
191
192
# File 'lib/ibmeta.rb', line 189

def drop_table(conn)
  #puts "DROP TABLE #{name}"
  conn.execute_immediate( "DROP TABLE #{name}" )
end

#load(conn) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ibmeta.rb', line 177

def load(conn)
  @columns = InterBaseMetaFunctions.table_fields(conn,@name,true)
  @indices = InterBaseMetaFunctions.indices(conn,@name)
  @constraints = InterBaseMetaFunctions.table_constraints(conn,@name)
  
  #puts "#{@constraints.size} constraints found"
  
  @constraints.each() {|c| c.table = self }
  
  @loaded = true
end

#rename_table(conn, ntable_name) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ibmeta.rb', line 234

def rename_table(conn, ntable_name)
  new_table_name = ntable_name.to_s.upcase
  
  if @loaded.nil? or !@loaded
    load(conn)
  end
  
  old_table_name = @name
  load(conn)  # load the definition
  @name = new_table_name
  to_sql(:SQL_TABLE).each() {|sql| conn.execute_immediate(sql) }
  # copy all the data across
  conn.execute_immediate( "insert into #{new_table_name} select * from #{old_table_name}")
  to_sql(:SQL_PRIMARY_KEYS).each() {|sql| conn.execute_immediate(sql) }
  to_sql(:SQL_FOREIGN_KEYS).each() {|sql| conn.execute_immediate(sql) }
  @indices.each() do |index| 
    index.remove_index(conn)
    index.table = new_table_name
    index.create_index(conn)
  end
  @name = old_table_name
  drop_table(conn)
end

#to_sql(sql_restriction = :SQL_ALL) ⇒ Object

returns an array of sql required to create the table and all dependents when reconstructing the database, create all the tables, then create the primary keys and then create the foreign keys and then the indices



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ibmeta.rb', line 204

def to_sql(sql_restriction=:SQL_ALL)
  sql = []
  
  if ( [:SQL_ALL, :SQL_TABLE].include?(sql_restriction) )
    sql << to_sql_create_table
  end
  
  if ( [:SQL_ALL, :SQL_INDICES].include?(sql_restriction) && @indices )
    @indices.each() {|index| sql << index.to_sql }
  end
  
  if ( [:SQL_ALL, :SQL_PRIMARY_KEYS].include?(sql_restriction) && @constraints )
    @constraints.each() do |c| 
      if (c.type == InterBaseConstraint::PRIMARY_KEY) 
        sql << c.to_sql
      end 
    end
  end
  
  if ( [:SQL_ALL, :SQL_FOREIGN_KEYS].include?(sql_restriction) && @constraints )
    @constraints.each() do |c| 
      if (c.type == InterBaseConstraint::FOREIGN_KEY) 
        sql << c.to_sql
      end 
    end
  end
  
  sql
end