Class: SQL::TableModifier
- Defined in:
- lib/mack-data_mapper/dm_patches/migrations.rb,
lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb
Overview
Sqlite3
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#statements ⇒ Object
Returns the value of attribute statements.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#add_column(name, type, opts = {}) ⇒ Object
:nodoc:.
- #build_type(name, type_class, options = @opts) ⇒ Object
- #change_column(name, type, opts = {}) ⇒ Object
- #drop_column(name) ⇒ Object (also: #drop_columns)
-
#initialize(adapter, table_name, opts = {}, &block) ⇒ TableModifier
constructor
A new instance of TableModifier.
- #quote_column_name(name) ⇒ Object
- #quoted_table_name ⇒ Object
- #rename_column(name, new_name, opts = {}) ⇒ Object
Constructor Details
#initialize(adapter, table_name, opts = {}, &block) ⇒ TableModifier
Returns a new instance of TableModifier.
5 6 7 8 9 10 11 12 13 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 5 def initialize(adapter, table_name, opts = {}, &block) @adapter = adapter @table_name = table_name.to_s @opts = (opts) @statements = [] self.instance_eval &block end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
3 4 5 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 3 def adapter @adapter end |
#opts ⇒ Object
Returns the value of attribute opts.
3 4 5 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 3 def opts @opts end |
#statements ⇒ Object
Returns the value of attribute statements.
3 4 5 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 3 def statements @statements end |
#table_name ⇒ Object
Returns the value of attribute table_name.
3 4 5 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 3 def table_name @table_name end |
Instance Method Details
#add_column(name, type, opts = {}) ⇒ Object
:nodoc:
24 25 26 27 |
# File 'lib/mack-data_mapper/dm_patches/migrations.rb', line 24 def add_column(name, type, opts = {}) column = SQL::TableCreator::Column.new(@adapter, name, build_type(name, type), opts) @statements << "ALTER TABLE #{quoted_table_name} ADD COLUMN #{column.to_sql}" end |
#build_type(name, type_class, options = @opts) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/mack-data_mapper/dm_patches/migrations.rb', line 34 def build_type(name, type_class, = @opts) schema = {:name => name.to_s, :quote_column_name => quote_column_name(name)}.merge() schema[:serial?] ||= schema[:serial] schema[:nullable?] ||= schema[:nullable] schema = @adapter.class.type_map[type_class].merge(schema) @adapter.property_schema_statement(schema) end |
#change_column(name, type, opts = {}) ⇒ Object
29 30 31 32 |
# File 'lib/mack-data_mapper/dm_patches/migrations.rb', line 29 def change_column(name, type, opts = {}) # raise NotImplemented for SQLite3 @statements << "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(name)} TYPE #{build_type(name, type, opts).gsub(quote_column_name(name), '').gsub('NOT NULL', '')}" end |
#drop_column(name) ⇒ Object Also known as: drop_columns
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 20 def drop_column(name) # raise NotImplemented for SQLite3. Can't ALTER TABLE, need to copy table. # We'd have to inspect it, and we can't, since we aren't executing any queries yet. # TODO instead of building the SQL queries when executing the block, create AddColumn, # AlterColumn and DropColumn objects that get #to_sql'd if name.is_a?(Array) name.each{ |n| drop_column(n) } else @statements << "ALTER TABLE #{quoted_table_name} DROP COLUMN #{quote_column_name(name)}" end end |
#quote_column_name(name) ⇒ Object
43 44 45 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 43 def quote_column_name(name) @adapter.send(:quote_column_name, name.to_s) end |
#quoted_table_name ⇒ Object
47 48 49 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 47 def quoted_table_name @adapter.send(:quote_table_name, table_name) end |
#rename_column(name, new_name, opts = {}) ⇒ Object
33 34 35 36 |
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb', line 33 def rename_column(name, new_name, opts = {}) # raise NotImplemented for SQLite3 @statements << "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(name)} TO #{quote_column_name(new_name)}" end |