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.
190 191 192 193 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 190 def initialize(base) @columns = [] @base = base end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
188 189 190 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 188 def columns @columns end |
Instance Method Details
#[](name) ⇒ Object
Returns a ColumnDefinition for the column with name name
.
202 203 204 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 202 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
, :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. You cannot explicitely set the default value toNULL
. Simply leave off this option if you want aNULL
default value. -
:null
: Allows or disallowsNULL
values in the column. This option could have been named:null_allowed
.
This method returns self
.
Examples
# Assuming def is an instance of TableDefinition
def.column(:granted, :boolean)
#=> granted BOOLEAN
def.column(:picture, :binary, :limit => 2.megabytes)
#=> picture BLOB(2097152)
def.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false)
#=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
237 238 239 240 241 242 243 244 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 237 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.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.
197 198 199 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 197 def primary_key(name) column(name, native[: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.
249 250 251 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 249 def to_sql @columns * ', ' end |