Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (76 sloc) 3.56 KB
# DataObjects::Schema::
# Database
# Table
#
# Column::
# String < Column
# Numeric < Column
# Integer < Numeric
# Float < Numeric
# Decimal < Numeric
# ...
#
# Index
# UniqueIndex < Index
# PrimaryKey < UniqueIndex
# ForeignKey
# Reference
#
# OrderedSet
# Columns (has-a OrderedSet)
# Indexes (has-a OrderedSet)
# UniqueIndexes (has-a OrderedSet)
# ForeignKeys (has-a OrderedSet)
uri = Addressable::URI.parse('mysql://localhost/dm_core_test')
database = DataObjects::Schema.load(uri)
database.name # => "dm_core_test"
database.uri # => uri
database.tables # => Tables.new([ Table1, Table2, ... ])
database.tables.each do |table|
# Table API
table # => Table.new('customers', [ Column1, Column2, ... ], :indexes => indexes)
table.name # => "customers"
# Column API
table.columns # => Columns.new([ Column1, Column2, ... ])
table.each do |column|
column # => Column::String.new('name', :length => 0..10)
column.name # => "name"
column.required? # => true
column.default # => "a"
# NOTE: Column subclasses will be able to add extra accessors for type
# specific data. For example Column::String may define #length,
# and Column::Decimal may define #precision and #scale
end
# Primary Key API
table.primary_key # => PrimaryKey.new(name, [ Column1, Column2, ... ])
table.primary_key.columns # => Columns.new([ Column1, Column2, ... ])
table.primary_key.unique? # => true
# Index API
table.indexes # => Indexes.new([ Index1, Index2, ... ])
table.indexes.each do |index|
index # => Index.new('index_name', [ Column1, Column2, ... ])
index.name # => "index_name"
index.unique? # => false
index.columns # => Columns.new([ Column1, Column2, ... ])
end
# Unique Index API
table.unique_indexes # => UniqueIndexes.new([ UniqueIndex1, UniqueIndex1, ... ])
table.unique_indexes.each do |unique_index|
unique_index # => UniqueIndex.new('unique_name', [ Column1, Column2, ... ])
unique_index.name # => "unique_name"
unique_index.unique? # => true
unique_index.columns # => Columns.new([ Column1, Column2, ... ])
end
# Foreign Key API
table.foreign_keys # => ForeignKeys.new([ ForeignKey1, ForeignKey2, ... ])
table.foreign_keys.each do |foreign_key|
foreign_key # => ForeignKey.new('foreign_key_name', [ Column1, Column2, ... ], reference, :on_delete => :restrict, :on_update => :restrict)
foreign_key.name # => "foreign_key_name"
foreign_key.columns # => Columns.new([ Column1, Column2, ... ])
foreign_key.reference # => Reference.new(other_table, other_table.primary_key)
foreign_key.on_delete # => :restrict
foreign_key.on_update # => nil
end
end
# NOTES: Table, Index, UniqueIndex, PrimaryKey, ForeignKey and Reference
# are Enumerable, and delegate #each to #columns