Skip to content

Commit 6e36110

Browse files
committed
Do not convert to hash
1 parent b3d32e3 commit 6e36110

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -496,36 +496,48 @@ def column_definitions(table_name)
496496
sql = column_definitions_sql(database, identifier)
497497

498498
binds = []
499-
nv128 = SQLServer::Type::UnicodeVarchar.new limit: 128
499+
nv128 = SQLServer::Type::UnicodeVarchar.new(limit: 128)
500500
binds << Relation::QueryAttribute.new("TABLE_NAME", identifier.object, nv128)
501501
binds << Relation::QueryAttribute.new("TABLE_SCHEMA", identifier.schema, nv128) unless identifier.schema.blank?
502502
results = internal_exec_query(sql, "SCHEMA", binds)
503503

504+
# columns = []
505+
504506
columns = results.map do |ci|
505-
ci = ci.to_h.symbolize_keys # TODO: Fix so doesnt use hash.
506-
ci[:_type] = ci[:type]
507-
ci[:table_name] = view_tblnm || table_name
508-
ci[:type] = case ci[:type]
507+
col = {
508+
name: ci["name"],
509+
numeric_scale: ci["numeric_scale"],
510+
numeric_precision: ci["numeric_precision"],
511+
datetime_precision: ci["datetime_precision"],
512+
collation: ci["collation"],
513+
ordinal_position: ci["ordinal_position"],
514+
length: ci["length"]
515+
}
516+
517+
# ci = ci.to_h.symbolize_keys # TODO: Fix so doesnt use hash.
518+
original_type = ci['type']
519+
col[:table_name] = view_tblnm || table_name
520+
col[:type] = case ci['type']
509521
when /^bit|image|text|ntext|datetime$/
510-
ci[:type]
522+
ci['type']
511523
when /^datetime2|datetimeoffset$/i
512-
"#{ci[:type]}(#{ci[:datetime_precision]})"
524+
"#{ci['type']}(#{ci['datetime_precision']})"
513525
when /^time$/i
514-
"#{ci[:type]}(#{ci[:datetime_precision]})"
526+
"#{ci['type']}(#{ci['datetime_precision']})"
515527
when /^numeric|decimal$/i
516-
"#{ci[:type]}(#{ci[:numeric_precision]},#{ci[:numeric_scale]})"
528+
"#{ci['type']}(#{ci['numeric_precision']},#{ci['numeric_scale']})"
517529
when /^float|real$/i
518-
"#{ci[:type]}"
530+
"#{ci['type']}"
519531
when /^char|nchar|varchar|nvarchar|binary|varbinary|bigint|int|smallint$/
520-
ci[:length].to_i == -1 ? "#{ci[:type]}(max)" : "#{ci[:type]}(#{ci[:length]})"
532+
ci['length'].to_i == -1 ? "#{ci['type']}(max)" : "#{ci['type']}(#{ci['length']})"
521533
else
522-
ci[:type]
534+
ci['type']
523535
end
524-
ci[:default_value],
525-
ci[:default_function] = begin
526-
default = ci[:default_value]
536+
col[:default_value],
537+
col[:default_function] = begin
538+
default = ci['default_value']
527539
if default.nil? && view_exists
528-
view_column = views_real_column_name(table_name, ci[:name]).downcase
540+
view_column = views_real_column_name(table_name, ci['name']).downcase
529541
default = default_functions[view_column] if view_column.present?
530542
end
531543
case default
@@ -540,28 +552,34 @@ def column_definitions(table_name)
540552
when /CREATE DEFAULT/mi
541553
[nil, nil]
542554
else
543-
type = case ci[:type]
544-
when /smallint|int|bigint/ then ci[:_type]
545-
else ci[:type]
555+
type = case col[:type]
556+
when /smallint|int|bigint/ then original_type
557+
else col[:type]
546558
end
547559
value = default.match(/\A\((.*)\)\Z/m)[1]
548560
value = select_value("SELECT CAST(#{value} AS #{type}) AS value", "SCHEMA")
549561
[value, nil]
550562
end
551563
end
552-
ci[:null] = ci[:is_nullable].to_i == 1
553-
ci.delete(:is_nullable)
554-
ci[:is_primary] = ci[:is_primary].to_i == 1
555-
ci[:is_identity] = ci[:is_identity].to_i == 1 unless [TrueClass, FalseClass].include?(ci[:is_identity].class)
556-
ci
557-
end
564+
col[:null] = ci['is_nullable'].to_i == 1
565+
# ci.delete(:is_nullable)
566+
col[:is_primary] = ci['is_primary'].to_i == 1
558567

559-
# Since Rails 7, it's expected that all adapter raise error when table doesn't exists.
560-
# I'm not aware of the possibility of tables without columns on SQL Server (postgres have those).
561-
# Raise error if the method return an empty array
562-
columns.tap do |result|
563-
raise ActiveRecord::StatementInvalid, "Table '#{table_name}' doesn't exist" if result.empty?
568+
if [TrueClass, FalseClass].include?(ci['is_identity'].class)
569+
col[:is_identity] = ci['is_identity']
570+
else
571+
col[:is_identity] = ci['is_identity'].to_i == 1
572+
end
573+
574+
# col[:is_identity] = ci[:is_identity].to_i == 1 unless [TrueClass, FalseClass].include?(ci[:is_identity].class)
575+
576+
col
577+
# cols << col
564578
end
579+
580+
raise ActiveRecord::StatementInvalid, "Table '#{table_name}' doesn't exist" if columns.empty?
581+
582+
columns
565583
end
566584

567585
def column_definitions_sql(database, identifier)

0 commit comments

Comments
 (0)