@@ -68,15 +68,15 @@ def columns(table_name)
68
68
return [ ] if table_name . blank?
69
69
70
70
column_definitions ( table_name ) . map do |ci |
71
- sqlserver_options = ci . slice ( :ordinal_position , :is_primary , :is_identity , :table_name )
72
- sql_type_metadata = fetch_type_metadata ( ci [ ' type' ] , sqlserver_options )
71
+ sqlserver_options = ci . slice :ordinal_position , :is_primary , :is_identity , :table_name
72
+ sql_type_metadata = fetch_type_metadata ci [ : type] , sqlserver_options
73
73
new_column (
74
- ci [ ' name' ] ,
75
- ci [ ' default_value' ] ,
74
+ ci [ : name] ,
75
+ ci [ : default_value] ,
76
76
sql_type_metadata ,
77
- ci [ ' null' ] ,
78
- ci [ ' default_function' ] ,
79
- ci [ ' collation' ] ,
77
+ ci [ : null] ,
78
+ ci [ : default_function] ,
79
+ ci [ : collation] ,
80
80
nil ,
81
81
sqlserver_options
82
82
)
@@ -480,16 +480,16 @@ def initialize_native_database_types
480
480
end
481
481
482
482
def column_definitions ( table_name )
483
- identifier = database_prefix_identifier ( table_name )
484
- database = identifier . fully_qualified_database_quoted
483
+ identifier = database_prefix_identifier ( table_name )
484
+ database = identifier . fully_qualified_database_quoted
485
485
view_exists = view_exists? ( table_name )
486
- view_table_name = view_table_name ( table_name ) if view_exists
486
+ view_tblnm = view_table_name ( table_name ) if view_exists
487
487
488
488
if view_exists
489
489
sql = <<~SQL
490
490
SELECT LOWER(c.COLUMN_NAME) AS [name], c.COLUMN_DEFAULT AS [default]
491
491
FROM #{ database } .INFORMATION_SCHEMA.COLUMNS c
492
- WHERE c.TABLE_NAME = #{ quote ( view_table_name ) }
492
+ WHERE c.TABLE_NAME = #{ quote ( view_tblnm ) }
493
493
SQL
494
494
results = internal_exec_query ( sql , "SCHEMA" )
495
495
default_functions = results . each . with_object ( { } ) { |row , out | out [ row [ "name" ] ] = row [ "default" ] } . compact
@@ -504,29 +504,30 @@ def column_definitions(table_name)
504
504
results = internal_exec_query ( sql , "SCHEMA" , binds )
505
505
506
506
columns = results . map do |ci |
507
- ci [ '_type' ] = ci [ 'type' ]
508
- ci [ 'table_name' ] = view_table_name || table_name
509
- ci [ 'type' ] = case ci [ 'type' ]
507
+ ci = ci . to_h . symbolize_keys
508
+ ci [ :_type ] = ci [ :type ]
509
+ ci [ :table_name ] = view_tblnm || table_name
510
+ ci [ :type ] = case ci [ :type ]
510
511
when /^bit|image|text|ntext|datetime$/
511
- ci [ ' type' ]
512
+ ci [ : type]
512
513
when /^datetime2|datetimeoffset$/i
513
- "#{ ci [ ' type' ] } (#{ ci [ ' datetime_precision' ] } )"
514
+ "#{ ci [ : type] } (#{ ci [ : datetime_precision] } )"
514
515
when /^time$/i
515
- "#{ ci [ ' type' ] } (#{ ci [ ' datetime_precision' ] } )"
516
+ "#{ ci [ : type] } (#{ ci [ : datetime_precision] } )"
516
517
when /^numeric|decimal$/i
517
- "#{ ci [ ' type' ] } (#{ ci [ ' numeric_precision' ] } ,#{ ci [ ' numeric_scale' ] } )"
518
+ "#{ ci [ : type] } (#{ ci [ : numeric_precision] } ,#{ ci [ : numeric_scale] } )"
518
519
when /^float|real$/i
519
- "#{ ci [ ' type' ] } "
520
+ "#{ ci [ : type] } "
520
521
when /^char|nchar|varchar|nvarchar|binary|varbinary|bigint|int|smallint$/
521
- ci [ ' length' ] . to_i == -1 ? "#{ ci [ ' type' ] } (max)" : "#{ ci [ ' type' ] } (#{ ci [ ' length' ] } )"
522
+ ci [ : length] . to_i == -1 ? "#{ ci [ : type] } (max)" : "#{ ci [ : type] } (#{ ci [ : length] } )"
522
523
else
523
- ci [ ' type' ]
524
+ ci [ : type]
524
525
end
525
- ci [ ' default_value' ] ,
526
- ci [ ' default_function' ] = begin
527
- default = ci [ ' default_value' ]
526
+ ci [ : default_value] ,
527
+ ci [ : default_function] = begin
528
+ default = ci [ : default_value]
528
529
if default . nil? && view_exists
529
- view_column = views_real_column_name ( table_name , ci [ ' name' ] ) . downcase
530
+ view_column = views_real_column_name ( table_name , ci [ : name] ) . downcase
530
531
default = default_functions [ view_column ] if view_column . present?
531
532
end
532
533
case default
@@ -541,19 +542,19 @@ def column_definitions(table_name)
541
542
when /CREATE DEFAULT/mi
542
543
[ nil , nil ]
543
544
else
544
- type = case ci [ ' type' ]
545
- when /smallint|int|bigint/ then ci [ ' _type' ]
546
- else ci [ ' type' ]
545
+ type = case ci [ : type]
546
+ when /smallint|int|bigint/ then ci [ : _type]
547
+ else ci [ : type]
547
548
end
548
549
value = default . match ( /\A \( (.*)\) \Z /m ) [ 1 ]
549
550
value = select_value ( "SELECT CAST(#{ value } AS #{ type } ) AS value" , "SCHEMA" )
550
551
[ value , nil ]
551
552
end
552
553
end
553
- ci [ ' null' ] = ci [ ' is_nullable' ] . to_i == 1
554
+ ci [ : null] = ci [ : is_nullable] . to_i == 1
554
555
ci . delete ( :is_nullable )
555
- ci [ ' is_primary' ] = ci [ ' is_primary' ] . to_i == 1
556
- ci [ ' is_identity' ] = ci [ ' is_identity' ] . to_i == 1 unless [ TrueClass , FalseClass ] . include? ( ci [ ' is_identity' ] . class )
556
+ ci [ : is_primary] = ci [ : is_primary] . to_i == 1
557
+ ci [ : is_identity] = ci [ : is_identity] . to_i == 1 unless [ TrueClass , FalseClass ] . include? ( ci [ : is_identity] . class )
557
558
ci
558
559
end
559
560
@@ -705,20 +706,21 @@ def view_table_name(table_name)
705
706
706
707
def view_information ( table_name )
707
708
@view_information ||= { }
708
-
709
709
@view_information [ table_name ] ||= begin
710
710
identifier = SQLServer ::Utils . extract_identifiers ( table_name )
711
711
information_query_table = identifier . database . present? ? "[#{ identifier . database } ].[INFORMATION_SCHEMA].[VIEWS]" : "[INFORMATION_SCHEMA].[VIEWS]"
712
-
713
- view_info = select_one ( "SELECT * FROM #{ information_query_table } WITH (NOLOCK) WHERE TABLE_NAME = #{ quote ( identifier . object ) } " , "SCHEMA" )
714
-
715
- if view_info && view_info [ 'VIEW_DEFINITION' ] . blank? || view_info [ 'VIEW_DEFINITION' ] . length == 4000
716
- view_info [ 'VIEW_DEFINITION' ] = begin
717
- select_values ( "EXEC sp_helptext #{ identifier . object_quoted } " , "SCHEMA" ) . join
718
- rescue
719
- warn "No view definition found, possible permissions problem.\n Please run GRANT VIEW DEFINITION TO your_user;"
720
- nil
721
- end
712
+ view_info = select_one "SELECT * FROM #{ information_query_table } WITH (NOLOCK) WHERE TABLE_NAME = #{ quote ( identifier . object ) } " , "SCHEMA"
713
+
714
+ if view_info
715
+ view_info = view_info . to_h . with_indifferent_access
716
+ if view_info [ :VIEW_DEFINITION ] . blank? || view_info [ :VIEW_DEFINITION ] . length == 4000
717
+ view_info [ :VIEW_DEFINITION ] = begin
718
+ select_values ( "EXEC sp_helptext #{ identifier . object_quoted } " , "SCHEMA" ) . join
719
+ rescue
720
+ warn "No view definition found, possible permissions problem.\n Please run GRANT VIEW DEFINITION TO your_user;"
721
+ nil
722
+ end
723
+ end
722
724
end
723
725
724
726
view_info
0 commit comments