class Oracle::Model::Generator
Constants
- VERSION
The version of the oracle-model-generator library
Attributes
An array of parent tables that the table has a foreign key relationship with.
An array of raw OCI::Metadata::Column objects.
The raw OCI8 connection.
An array of hashes that contain per-column constraint information.
A list of dependencies for the table.
An array of foreign key names.
The name of the active record model to be generated.
An array of primary keys for the column. May contain one or more values.
The table name associated with the generator.
Boolean indicating whether the generator is for a regular table or a view.
Public Class Methods
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
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 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
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
# 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 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
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
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
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