Class: SQL::TableModifier
- Inherits:
-
Object
- Object
- SQL::TableModifier
- Defined in:
- lib/sql.rb
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
- #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.
62 63 64 65 66 67 68 69 70 |
# File 'lib/sql.rb', line 62 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.
60 61 62 |
# File 'lib/sql.rb', line 60 def adapter @adapter end |
#opts ⇒ Object
Returns the value of attribute opts.
60 61 62 |
# File 'lib/sql.rb', line 60 def opts @opts end |
#statements ⇒ Object
Returns the value of attribute statements.
60 61 62 |
# File 'lib/sql.rb', line 60 def statements @statements end |
#table_name ⇒ Object
Returns the value of attribute table_name.
60 61 62 |
# File 'lib/sql.rb', line 60 def table_name @table_name end |
Instance Method Details
#add_column(name, type, opts = {}) ⇒ Object
72 73 74 75 |
# File 'lib/sql.rb', line 72 def add_column(name, type, opts = {}) column = SQL::TableCreator::Column.new(@adapter, name, type, opts) @statements << "ALTER TABLE #{quoted_table_name} ADD COLUMN #{column.to_sql}" end |
#change_column(name, type, opts = {}) ⇒ Object
94 95 96 97 |
# File 'lib/sql.rb', line 94 def change_column(name, type, opts = {}) # raise NotImplemented for SQLite3 @statements << "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(name)} TYPE #{type}" end |
#drop_column(name) ⇒ Object Also known as: drop_columns
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sql.rb', line 77 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. # Just write the sql yourself. 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
99 100 101 |
# File 'lib/sql.rb', line 99 def quote_column_name(name) @adapter.send(:quote_column_name, name.to_s) end |
#quoted_table_name ⇒ Object
103 104 105 |
# File 'lib/sql.rb', line 103 def quoted_table_name @adapter.send(:quote_table_name, table_name) end |
#rename_column(name, new_name, opts = {}) ⇒ Object
89 90 91 92 |
# File 'lib/sql.rb', line 89 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 |