class Oracle::Model::Generator

Constants

VERSION

The version of the oracle-model-generator library

Attributes

belongs_to[R]

An array of parent tables that the table has a foreign key relationship with.

column_info[R]

An array of raw OCI::Metadata::Column objects.

connection[R]

The raw OCI8 connection.

constraints[R]

An array of hashes that contain per-column constraint information.

dependencies[R]

A list of dependencies for the table.

foreign_keys[R]

An array of foreign key names.

model[R]

The name of the active record model to be generated.

primary_keys[R]

An array of primary keys for the column. May contain one or more values.

table[R]

The table name associated with the generator.

view[R]

Boolean indicating whether the generator is for a regular table or a view.

Public Class Methods

new(connection) click to toggle source

Creates and returns a new Oracle::Model::Generator object. It accepts an Oracle::Connection object, which is what OCI8.new returns.

Example:

connection = Oracle::Connection.new(user, password, database)
ogenerator = Oracle::Model::Generator.new(connection)
ogenerator.generate('users')
# File lib/oracle/model/generator.rb, line 49
def initialize(connection)
  @connection   = connection
  @constraints  = []
  @primary_keys = []
  @foreign_keys = []
  @dependencies = []
  @belongs_to   = []
  @column_info  = []
  @table        = nil
  @model        = nil
end

Public Instance Methods

generate(table, view = false) click to toggle source

Generates an Oracle::Model::Generator object for table. If this is a view (materialized or otherwise), set the view argument to true.

This method does not actually generate a file of any sort. It merely sets instance variables which you can then use in your own class/file generation programs.

# File lib/oracle/model/generator.rb, line 72
def generate(table, view = false)
  @table = table.upcase
  @model = table.split('_').map{ |e| e.downcase.capitalize }.join
  @view  = view

  # Remove trailing 's'
  @model.chop! if @model[-1].chr.upcase == 'S'

  unless view
    get_constraints
    get_foreign_keys
    get_column_info
  end

  get_primary_keys
  get_dependencies
end

Private Instance Methods

find_fk_table(fk) click to toggle source

Find table name based on a foreign key name.

# File lib/oracle/model/generator.rb, line 130
def find_fk_table(fk)
  sql = %Q{
    select table_name
    from all_constraints
    where constraint_name = '#{fk}'
  }

  begin
    cursor = @connection.exec(sql)
    table = cursor.fetch.first
  ensure
    cursor.close if cursor
  end

  table
end
get_belongs_to() click to toggle source

Returns an array of tables that the current table has foreign key ties to.

# File lib/oracle/model/generator.rb, line 122
def get_belongs_to
  @foreign_keys.each{ |fk|
    @belongs_to << find_fk_table(fk)
  }
end
get_column_info() click to toggle source
# File lib/oracle/model/generator.rb, line 92
def get_column_info
  table = @connection.describe_table(@table)
  table.columns.each{ |col| @column_info << col }
end
get_constraints() click to toggle source

Get a list of constraints for a given table.

# File lib/oracle/model/generator.rb, line 149
def get_constraints
  sql = %Q{
    select *
    from all_cons_columns a, all_constraints b
    where a.owner = b.owner
    and a.constraint_name = b.constraint_name
    and a.table_name = b.table_name
    and b.table_name = '#{@table}'
  }

  begin
    cursor = @connection.exec(sql)
    while rec = cursor.fetch_hash
      @constraints << rec
    end
  ensure
    cursor.close if cursor
  end
end
get_dependencies() click to toggle source

An array of hashes indicating objects that are dependent on the table.

# File lib/oracle/model/generator.rb, line 171
def get_dependencies
  sql = %Q{
    select *
    from all_dependencies dep
    where referenced_name = '#{@table}'
  }

  begin
    cursor = @connection.exec(sql)
    while rec = cursor.fetch_hash
      @dependencies << rec
    end
  ensure
    cursor.close if cursor
  end
end
get_foreign_keys() click to toggle source

Returns an array of foreign keys.

# File lib/oracle/model/generator.rb, line 109
def get_foreign_keys
  @constraints.each{ |hash|
    if hash['CONSTRAINT_TYPE'] == 'R'
      @foreign_keys << hash['R_CONSTRAINT_NAME']
    end
  }

  get_belongs_to()
end
get_primary_keys() click to toggle source

Returns an array of primary keys.

# File lib/oracle/model/generator.rb, line 99
def get_primary_keys
  @constraints.each{ |hash|
    if hash['CONSTRAINT_TYPE'] == 'P'
      @primary_keys << hash['COLUMN_NAME'].downcase
    end
  }
end