Class: Ramen::Metadata::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/ramen/metadata/database.rb

Overview

Database is the top of a tree of collections and classes that contain the reflected information about the schema.

Database is synonymous with Catalog on some database engines.

Database contains many Schema objects, which are accessable through the schema attribute. Schema contains many Table objects, which are accessable through Schema#table. Table contains many Column, ForeignKey, Index and KeyConstraint objects. ForeignKey contains ForeignKeyColumn objects; which reference the appropriate Column. Index contains IndexColumn objects; which reference the appropriate Column. KeyConstraint objects refernece their Index.

For more details see: ramen.rubyforge.org or readme.txt

usage:

db = Ramen.create( configuration ) # a short-cut for Database.new
schema = db.schema['HumanResources']

Links: readme.txt; source

Instance Method Summary collapse

Constructor Details

#initialize(connection, home) ⇒ Database

Database.new( connection, home ) #=> instance of Database

db = Database.new( DBI.connect(...), Home.new( :sql => Sql2005.new ) )

Creates a database and loads all the objects (Schema, Table, Column, etc). To get the best performance for typical usage, Database.new will load all objects. Loading all objects during Database.new makes the fewest queries to the database server.

Ramen does not hold on to the connection. After this method returns, it is safe to close the connection or use it for other queries.

see Home.new for configuration options.

Ramen.create in an alternative method to calling Database.new



49
50
51
52
# File 'lib/ramen/metadata/database.rb', line 49

def initialize( connection, home )
  @home = home
  load_all_objects( connection )
end

Instance Method Details

#add_schema(obj) ⇒ Object

add_schema( obj ) #=> obj

Adds the given schema to this database. Throws a RamenError if the obj is not a Schema.



97
98
99
# File 'lib/ramen/metadata/database.rb', line 97

def add_schema( obj )
  @schemas.add( obj )
end

#inspectObject

inspect() #=> string

Returns a nice and brief output of the database and it’s schema. This was redefined since the default inspect would typically output many pages of details.

Each schema is listed by id:name.

usage:

db = Ramen.create()
db.inspect  #=> "#<Ramen::Database:0x2da0668 schema=[1:dbo, 2:guest,..."


81
82
83
84
85
86
87
# File 'lib/ramen/metadata/database.rb', line 81

def inspect
  children = []
  @schemas.each_by_id do |id,schema|
    children << (id.to_s+':'+schema.schema_name)
  end
  to_s[0..-2]+' schema=['+children.join(', ')+']>'
end

#list_tables(connection) ⇒ Object

list_tables() #=> Array of Array containing table_name, table_id, schema_name, schema_id

Intended for use by testing. This lists all tables in the database using a call which is different than the one used to load_all_objects.



106
107
108
109
110
111
112
# File 'lib/ramen/metadata/database.rb', line 106

def list_tables( connection )
  result = Array.new
  @home.execute( connection, @home.sql::Sql.list_tables ) do |row|
    result << [row['table_schema'], row['table_name']]
  end
  result
end

#schemaObject

schema() #=> RamenHash reference

This attribute provides access to a RamenHash of all Schema in the Database. The hash is indexed by schema_name and schema_id.

usage:

require 'lib/ramen'
db = Ramen.create( configuration )
db.schema['Sales']  #=> #<Schema schema_name='Sales', schema_id='1234567' ...>
db.schema[1234567]  #=> #<Schema schema_name='Sales', schema_id='1234567' ...>


65
66
67
# File 'lib/ramen/metadata/database.rb', line 65

def schema
  @schemas
end