Path: | doc/reflection.rdoc |
Last Update: | Thu Feb 18 18:19:30 +0000 2010 |
Sequel supports reflection information in multiple ways.
You can get the adapter in use using Database.adapter_scheme. As this is a class method, you generally need to do DB.class.adapter_scheme:
DB.class.adapter_scheme # e.g. :postgres, :jdbc, :odbc
In some cases, the adapter scheme will be the same as the database to which you are connecting. However, many adapters support multiple databases. You can use the Database#database_type method to get the type of database to which you are connecting:
DB.database_type # :postgres, :h2, :mssql
On many database types/adapters, Database#tables exists and gives an array of table name symbols:
DB.tables # [:table1, :table2, :table3, ...]
On a few database types/adapters, Database#indexes takes a table name gives a hash of index information. Keys are index names, values are subhashes with the keys :columns and :unique :
DB.indexes(:table1) # {:index1=>{:columns=>[:column1], :unique=>false}, :index2=>{:columns=>[:column2, :column3], :unique=>true}}
Index information generally does not include partial indexes, functional indexes, or indexes on the primary key of the table.
Database#schema takes a table symbol and returns column information in an array with each element being an array with two elements. The first elements of the subarray is a column symbol, and the second element is a hash of information about that column. The hash should include the following keys:
Example:
DB.schema(:table) # [[:column1, {:allow_null=>true, :db_type=>'varchar(255)', :default=>'blah', :primary_key=>false, :type=>:string}], ...]
Model#db_schema returns pretty much the same information, except it returns it as a hash with column keys instead of an array of two element arrays.
Model.db_schema # {:column1=>{:allow_null=>true, :db_type=>'varchar(255)', :default=>'blah', :primary_key=>false, :type=>:string}, ...}
Dataset#columns returns the columns of the current dataset as an array of symbols:
DB[:table].columns # [:column1, :column2, :column3, ...]
Dataset#columns! does the same thing, except it ignores any cached value. In general, the cached value should never be incorrect, unless the database schema is changed after the dataset is created.
DB[:table].columns! # [:column1, :column2, :column3, ...]
Model.columns does the same thing as Dataset#columns, using the model‘s dataset:
Model.columns # [:column1, :column2, :column3, ...]
Sequel::Model offers complete introspection capability for all associations.
You can get an array of association symbols with Model.associations:
Model.associations # [:association1, :association2, ...]
You can get the association reflection for a single association via the Model.association_reflection. Association reflections are subclasses of hash, for ease of use and introspection (and backwards compatibility):
Model.association_reflection(:association1) # {:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}
You can get an array of all association reflections via Model.all_association_reflections:
Model.all_association_reflections # [{:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}, ...]
Finally, you can get a hash of association reflections via Model.association_reflections:
Model.association_reflections # {:association1=>{:name=>:association1, :type=>:many_to_one, :model=>Model, :associated_class=>OtherModel, ...}, ...}