Module: Sequel::Schema::SQL
- Included in:
- Database
- Defined in:
- lib/sequel/schema/schema_sql.rb
Constant Summary collapse
- RESTRICT =
'RESTRICT'.freeze
- CASCADE =
'CASCADE'.freeze
- NO_ACTION =
'NO ACTION'.freeze
- SET_NULL =
'SET NULL'.freeze
- SET_DEFAULT =
'SET DEFAULT'.freeze
- AUTOINCREMENT =
'AUTOINCREMENT'.freeze
- COMMA_SEPARATOR =
', '.freeze
- UNIQUE =
' UNIQUE'.freeze
- NOT_NULL =
' NOT NULL'.freeze
- PRIMARY_KEY =
' PRIMARY KEY'.freeze
- TYPES =
Hash.new {|h, k| k}
- UNDERSCORE =
'_'.freeze
Instance Method Summary collapse
- #alter_table_sql(table, op) ⇒ Object
- #alter_table_sql_list(table, operations) ⇒ Object
- #auto_increment_sql ⇒ Object
- #column_definition_sql(column) ⇒ Object
- #column_list_sql(columns) ⇒ Object
- #create_table_sql_list(name, columns, indexes = nil) ⇒ Object
- #default_index_name(table_name, columns) ⇒ Object
- #drop_table_sql(name) ⇒ Object
- #index_definition_sql(table_name, index) ⇒ Object
- #index_list_sql_list(table_name, indexes) ⇒ Object
- #literal(v) ⇒ Object
- #on_delete_clause(action) ⇒ Object
- #rename_table_sql(name, new_name) ⇒ Object
- #schema_utility_dataset ⇒ Object
Instance Method Details
#alter_table_sql(table, op) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/sequel/schema/schema_sql.rb', line 108 def alter_table_sql(table, op) case op[:op] when :add_column "ALTER TABLE #{table} ADD COLUMN #{column_definition_sql(op)}" when :drop_column "ALTER TABLE #{table} DROP COLUMN #{literal(op[:name])}" when :rename_column "ALTER TABLE #{table} RENAME COLUMN #{literal(op[:name])} TO #{literal(op[:new_name])}" when :set_column_type "ALTER TABLE #{table} ALTER COLUMN #{literal(op[:name])} TYPE #{op[:type]}" when :set_column_default "ALTER TABLE #{table} ALTER COLUMN #{literal(op[:name])} SET DEFAULT #{literal(op[:default])}" when :add_index index_definition_sql(table, op) when :drop_index "DROP INDEX #{default_index_name(table, op[:columns])}" else raise Error, "Unsupported ALTER TABLE operation" end end |
#alter_table_sql_list(table, operations) ⇒ Object
104 105 106 |
# File 'lib/sequel/schema/schema_sql.rb', line 104 def alter_table_sql_list(table, operations) operations.map {|op| alter_table_sql(table, op)} end |
#auto_increment_sql ⇒ Object
27 28 29 |
# File 'lib/sequel/schema/schema_sql.rb', line 27 def auto_increment_sql AUTOINCREMENT end |
#column_definition_sql(column) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sequel/schema/schema_sql.rb', line 47 def column_definition_sql(column) sql = "#{literal(column[:name].to_sym)} #{TYPES[column[:type]]}" column[:size] ||= 255 if column[:type] == :varchar elements = column[:size] || column[:elements] sql << "(#{literal(elements)})" if elements sql << UNIQUE if column[:unique] sql << NOT_NULL if column[:null] == false sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default) sql << PRIMARY_KEY if column[:primary_key] if column[:table] sql << " REFERENCES #{column[:table]}" sql << "(#{column[:key]})" if column[:key] end sql << " ON DELETE #{on_delete_clause(column[:on_delete])}" if column[:on_delete] sql << " #{auto_increment_sql}" if column[:auto_increment] sql end |
#column_list_sql(columns) ⇒ Object
65 66 67 |
# File 'lib/sequel/schema/schema_sql.rb', line 65 def column_list_sql(columns) columns.map {|c| column_definition_sql(c)}.join(COMMA_SEPARATOR) end |
#create_table_sql_list(name, columns, indexes = nil) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/sequel/schema/schema_sql.rb', line 88 def create_table_sql_list(name, columns, indexes = nil) sql = ["CREATE TABLE #{name} (#{column_list_sql(columns)})"] if indexes && !indexes.empty? sql.concat(index_list_sql_list(name, indexes)) end sql end |
#default_index_name(table_name, columns) ⇒ Object
71 72 73 |
# File 'lib/sequel/schema/schema_sql.rb', line 71 def default_index_name(table_name, columns) "#{table_name}_#{columns.join(UNDERSCORE)}_index" end |
#drop_table_sql(name) ⇒ Object
96 97 98 |
# File 'lib/sequel/schema/schema_sql.rb', line 96 def drop_table_sql(name) "DROP TABLE #{name}" end |
#index_definition_sql(table_name, index) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/sequel/schema/schema_sql.rb', line 75 def index_definition_sql(table_name, index) index_name = index[:name] || default_index_name(table_name, index[:columns]) if index[:unique] "CREATE UNIQUE INDEX #{index_name} ON #{table_name} (#{literal(index[:columns])})" else "CREATE INDEX #{index_name} ON #{table_name} (#{literal(index[:columns])})" end end |
#index_list_sql_list(table_name, indexes) ⇒ Object
84 85 86 |
# File 'lib/sequel/schema/schema_sql.rb', line 84 def index_list_sql_list(table_name, indexes) indexes.map {|i| index_definition_sql(table_name, i)} end |
#literal(v) ⇒ Object
43 44 45 |
# File 'lib/sequel/schema/schema_sql.rb', line 43 def literal(v) schema_utility_dataset.literal(v) end |
#on_delete_clause(action) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sequel/schema/schema_sql.rb', line 10 def on_delete_clause(action) case action when :restrict RESTRICT when :cascade CASCADE when :set_null SET_NULL when :set_default SET_DEFAULT else NO_ACTION end end |
#rename_table_sql(name, new_name) ⇒ Object
100 101 102 |
# File 'lib/sequel/schema/schema_sql.rb', line 100 def rename_table_sql(name, new_name) "ALTER TABLE #{name} RENAME TO #{new_name}" end |
#schema_utility_dataset ⇒ Object
39 40 41 |
# File 'lib/sequel/schema/schema_sql.rb', line 39 def schema_utility_dataset @schema_utility_dataset ||= dataset end |