@@ -496,36 +496,48 @@ def column_definitions(table_name)
496
496
sql = column_definitions_sql ( database , identifier )
497
497
498
498
binds = [ ]
499
- nv128 = SQLServer ::Type ::UnicodeVarchar . new limit : 128
499
+ nv128 = SQLServer ::Type ::UnicodeVarchar . new ( limit : 128 )
500
500
binds << Relation ::QueryAttribute . new ( "TABLE_NAME" , identifier . object , nv128 )
501
501
binds << Relation ::QueryAttribute . new ( "TABLE_SCHEMA" , identifier . schema , nv128 ) unless identifier . schema . blank?
502
502
results = internal_exec_query ( sql , "SCHEMA" , binds )
503
503
504
+ # columns = []
505
+
504
506
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' ]
509
521
when /^bit|image|text|ntext|datetime$/
510
- ci [ : type]
522
+ ci [ ' type' ]
511
523
when /^datetime2|datetimeoffset$/i
512
- "#{ ci [ : type] } (#{ ci [ : datetime_precision] } )"
524
+ "#{ ci [ ' type' ] } (#{ ci [ ' datetime_precision' ] } )"
513
525
when /^time$/i
514
- "#{ ci [ : type] } (#{ ci [ : datetime_precision] } )"
526
+ "#{ ci [ ' type' ] } (#{ ci [ ' datetime_precision' ] } )"
515
527
when /^numeric|decimal$/i
516
- "#{ ci [ : type] } (#{ ci [ : numeric_precision] } ,#{ ci [ : numeric_scale] } )"
528
+ "#{ ci [ ' type' ] } (#{ ci [ ' numeric_precision' ] } ,#{ ci [ ' numeric_scale' ] } )"
517
529
when /^float|real$/i
518
- "#{ ci [ : type] } "
530
+ "#{ ci [ ' type' ] } "
519
531
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' ] } )"
521
533
else
522
- ci [ : type]
534
+ ci [ ' type' ]
523
535
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' ]
527
539
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
529
541
default = default_functions [ view_column ] if view_column . present?
530
542
end
531
543
case default
@@ -540,28 +552,34 @@ def column_definitions(table_name)
540
552
when /CREATE DEFAULT/mi
541
553
[ nil , nil ]
542
554
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 ]
546
558
end
547
559
value = default . match ( /\A \( (.*)\) \Z /m ) [ 1 ]
548
560
value = select_value ( "SELECT CAST(#{ value } AS #{ type } ) AS value" , "SCHEMA" )
549
561
[ value , nil ]
550
562
end
551
563
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
558
567
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
564
578
end
579
+
580
+ raise ActiveRecord ::StatementInvalid , "Table '#{ table_name } ' doesn't exist" if columns . empty?
581
+
582
+ columns
565
583
end
566
584
567
585
def column_definitions_sql ( database , identifier )
0 commit comments