Class: Sequel::Schema::DbSchemaParser
- Inherits:
-
Object
- Object
- Sequel::Schema::DbSchemaParser
- Defined in:
- lib/sequel/schema/db_schema_parser.rb
Overview
Builds an abstract representation of a database schema.
Sample usage:
parser = DbSchemaParser.for_db( sequel_db_connection )
parser.parse_db_schema
# => Returns an array of table definitions
Class Method Summary collapse
-
.for_db(db) ⇒ Object
Returns an appropriate schema parser for the database connection.
Instance Method Summary collapse
-
#parse_db_schema ⇒ Object
Parses the schema from a Sequel Database connection.
-
#parse_table_schema(db_schema) ⇒ Object
Extracts an array of hashes representing the columns in the table, given an Array of Arrays returned by DB.schema(:table).
Class Method Details
.for_db(db) ⇒ Object
Returns an appropriate schema parser for the database connection.
15 16 17 |
# File 'lib/sequel/schema/db_schema_parser.rb', line 15 def self.for_db(db) self.new(db) end |
Instance Method Details
#parse_db_schema ⇒ Object
Parses the schema from a Sequel Database connection.
Returns a hash of table representations.
Example:
builder.parse_db_schema(db)
# => {:table1 => { :columns => [ DbColumns ... ] }
:table2 => { ... } }
29 30 31 32 33 34 35 36 37 |
# File 'lib/sequel/schema/db_schema_parser.rb', line 29 def parse_db_schema @db.tables.inject({}) do |result, table_name| result[table_name] = { :indexes => @db.indexes(table_name, :partial => true), :columns => parse_table_schema(@db.schema(table_name)) } result end end |
#parse_table_schema(db_schema) ⇒ Object
Extracts an array of hashes representing the columns in the table, given an Array of Arrays returned by DB.schema(:table).
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sequel/schema/db_schema_parser.rb', line 42 def parse_table_schema(db_schema) db_schema.map do |column| type = parse_type(column.last[:db_type]) DbColumn.build_from_hash(:name => column.first, :default => extract_default(column), :null => column.last[:allow_null], :column_type => type, :unsigned => extract_unsigned(column.last[:db_type], type), :size => extract_size(column.last[:db_type], type), :elements => extract_enum_elements(column.last[:db_type], type)) end end |