Class: ActiveRecord::ConnectionAdapters::TableDefinition
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::TableDefinition
- Defined in:
- lib/active_record/connection_adapters/abstract/schema_definitions.rb
Overview
Represents a SQL table in an abstract way. Columns are stored as ColumnDefinition in the #columns attribute.
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns a ColumnDefinition for the column with name
name. -
#column(name, type, options = {}) ⇒ Object
Instantiates a new column for the table.
-
#initialize(base) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
-
#primary_key(name) ⇒ Object
Appends a primary key definition to the table definition.
-
#to_sql ⇒ Object
Returns a String whose contents are the column definitions concatenated together.
Constructor Details
#initialize(base) ⇒ TableDefinition
Returns a new instance of TableDefinition.
230 231 232 233 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 230 def initialize(base) @columns = [] @base = base end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
228 229 230 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 228 def columns @columns end |
Instance Method Details
#[](name) ⇒ Object
Returns a ColumnDefinition for the column with name name.
242 243 244 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 242 def [](name) @columns.find {|column| column.name.to_s == name.to_s} end |
#column(name, type, options = {}) ⇒ Object
Instantiates a new column for the table.
The type parameter must be one of the following values:
:primary_key, :string, :text,
:integer, :float, :decimal,
:datetime, :timestamp, :time,
:date, :binary, :boolean.
Available options are (none of these exists by default):
- :limit: Requests a maximum column length (:string, :text, :binary or :integer columns only)
- :default: The column's default value. Use nil for NULL.
- :null:
Allows or disallows
NULLvalues in the column. This option could have been named :null_allowed. - :precision: Specifies the precision for a :decimal column.
- :scale: Specifies the scale for a :decimal column.
Please be aware of different RDBMS implementations behavior with :decimal columns:
- The SQL standard says the default scale should be 0, :scale <= :precision, and makes no comments about the requirements of :precision.
- MySQL: :precision [1..63], :scale [0..30]. Default is (10,0).
- PostgreSQL: :precision [1..infinity], :scale [0..infinity]. No default.
- SQLite2: Any :precision and :scale may be used. Internal storage as strings. No default.
- SQLite3: No restrictions on :precision and :scale, but the maximum supported :precision is 16. No default.
- Oracle: :precision [1..38], :scale [-84..127]. Default is (38,0).
- DB2: :precision [1..63], :scale [0..62]. Default unknown.
- Firebird: :precision [1..18], :scale [0..18]. Default (9,0). Internal types NUMERIC and DECIMAL have different storage rules, decimal being better.
- FrontBase?: :precision [1..38], :scale [0..38]. Default (38,0). WARNING Max :precision/:scale for NUMERIC is 19, and DECIMAL is 38.
- SqlServer?: :precision [1..38], :scale [0..38]. Default (38,0).
- Sybase: :precision [1..38], :scale [0..38]. Default (38,0).
- OpenBase?: Documentation unclear. Claims storage in double.
This method returns self.
Examples
Assuming td is an instance of TableDefinition
td.column(:granted, :boolean) #=> granted BOOLEAN
td.column(:picture, :binary, :limit => 2.megabytes) #=> picture BLOB(2097152)
td.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false) #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
def.column(:bill_gates_money, :decimal, :precision => 15, :scale => 2) #=> bill_gates_money DECIMAL(15,2)
def.column(:sensor_reading, :decimal, :precision => 30, :scale => 20) #=> sensor_reading DECIMAL(30,20)
While :scale defaults to zero on most databases, it
probably wouldn't hurt to include it.
def.column(:huge_integer, :decimal, :precision => 30) #=> huge_integer DECIMAL(30)
319 320 321 322 323 324 325 326 327 328 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 319 def column(name, type, = {}) column = self[name] || ColumnDefinition.new(@base, name, type) column.limit = [:limit] || native[type.to_sym][:limit] if [:limit] or native[type.to_sym] column.precision = [:precision] column.scale = [:scale] column.default = [:default] column.null = [:null] @columns << column unless @columns.include? column self end |
#primary_key(name) ⇒ Object
Appends a primary key definition to the table definition. Can be called multiple times, but this is probably not a good idea.
237 238 239 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 237 def primary_key(name) column(name, :primary_key) end |
#to_sql ⇒ Object
Returns a String whose contents are the column definitions concatenated together. This string can then be pre and appended to to generate the final SQL to create the table.
333 334 335 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 333 def to_sql @columns * ', ' end |