Skip to content
Browse files

push SQL generation inside the schema creation object

  • Loading branch information...
1 parent a80bcc3 commit 14d7dc0811fc946ffb63ceed7e0389ed14b50800 @tenderlove tenderlove committed Mar 15, 2013
View
6 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -49,12 +49,16 @@ class TableDefinition
# An array of ColumnDefinition objects, representing the column changes
# that have been defined.
attr_accessor :columns, :indexes
+ attr_reader :name, :temporary, :options
- def initialize(types)
+ def initialize(types, name, temporary, options)
@columns = []
@columns_hash = {}
@indexes = {}
@native = types
+ @temporary = temporary
+ @options = options
+ @name = name
end
# Appends a primary key definition to the table definition.
View
12 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -171,7 +171,7 @@ def column_exists?(table_name, column_name, type = nil, options = {})
#
# See also TableDefinition#column for details on how to create columns.
def create_table(table_name, options = {})
- td = create_table_definition
+ td = create_table_definition table_name, options[:temporary], options[:options]
unless options[:id] == false
pk = options.fetch(:primary_key) {
@@ -187,11 +187,7 @@ def create_table(table_name, options = {})
drop_table(table_name, options)
end
- create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
- create_sql << "#{quote_table_name(table_name)} ("
- create_sql << schema_creation.accept(td)
- create_sql << ") #{options[:options]}"
- execute create_sql
+ execute schema_creation.accept td
td.indexes.each_pair { |c,o| add_index table_name, c, o }
end
@@ -829,8 +825,8 @@ def rename_column_indexes(table_name, column_name, new_column_name)
end
private
- def create_table_definition
- TableDefinition.new native_database_types
+ def create_table_definition(name, temporary, options)
+ TableDefinition.new native_database_types, name, temporary, options
end
def update_table_definition(table_name, base)
View
10 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -125,13 +125,21 @@ def visit_ColumnDefinition(o)
end
def visit_TableDefinition(o)
- o.columns.map { |c| accept c }.join ', '
+ create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE "
+ create_sql << "#{quote_table_name(o.name)} ("
+ create_sql << o.columns.map { |c| accept c }.join(', ')
+ create_sql << ") #{o.options}"
+ create_sql
end
def quote_column_name(name)
@conn.quote_column_name name
end
+ def quote_table_name(name)
+ @conn.quote_table_name name
+ end
+
def type_to_sql(type, limit, precision, scale)
@conn.type_to_sql type.to_sym, limit, precision, scale
end
View
4 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -900,8 +900,8 @@ def extract_table_ref_from_insert_sql(sql)
$1.strip if $1
end
- def create_table_definition
- TableDefinition.new native_database_types
+ def create_table_definition(name, temporary, options)
+ TableDefinition.new native_database_types, name, temporary, options
end
def update_table_definition(table_name, base)

0 comments on commit 14d7dc0

Please sign in to comment.
Something went wrong with that request. Please try again.