Class: Mkxms::Mssql::AdoptionScriptWriter::StatisticsAdoptionChecks

Inherits:
IndentedStringBuilder show all
Includes:
SqlStringManipulators
Defined in:
lib/mkxms/mssql/adoption_script_writer.rb

Constant Summary

Constants included from SqlStringManipulators

SqlStringManipulators::MSSQL

Constants inherited from IndentedStringBuilder

IndentedStringBuilder::NAMED_SUBSTITUTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlStringManipulators

#bit_test, #boolean_desc, #dedent, #stresc, #strlit, #unquoted_identifier

Methods inherited from IndentedStringBuilder

dsl, #dsl, #each, #indented, #puts, #to_s

Constructor Details

#initialize(statistics, error_sql_proc) ⇒ StatisticsAdoptionChecks

Returns a new instance of StatisticsAdoptionChecks.



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1071

def initialize(statistics, error_sql_proc)
  super()
  
  @statistics = statistics
  @error_sql_proc = error_sql_proc
  
  @stats_id = "statistics #{statistics.name} on #{statistics.qualified_relation}"
  
  dsl {
    puts "IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT * FROM sys.stats so
        INNER JOIN sys.objects rel ON so.object_id = rel.object_id
        INNER JOIN sys.schemas s ON rel.schema_id = s.schema_id
        WHERE s.name = #{strlit(unquoted_identifier statistics.schema)}
        AND rel.name = #{strlit(unquoted_identifier statistics.relation)}
        AND so.name = #{strlit(unquoted_identifier statistics.name)}
      }
    end
    puts "BEGIN"
    indented {
      puts error_sql "#{stats_id.capitalize} does not exist."
    }
    puts "END ELSE BEGIN"
    indented {
      # Check column sequence
      QueryCursor.new(
        dedent(%Q{
          SELECT c.name
          FROM sys.stats so
          JOIN sys.stats_columns sc
            ON so.object_id = sc.object_id
            AND so.stats_id = sc.stats_id
          JOIN sys.columns c
            ON sc.object_id = c.object_id
            AND sc.column_id = c.column_id
          JOIN sys.objects rel ON so.object_id = rel.object_id
          JOIN sys.schemas s ON rel.schema_id = s.schema_id
          WHERE s.name = #{strlit(unquoted_identifier statistics.schema)}
          AND rel.name = #{strlit(unquoted_identifier statistics.relation)}
          AND so.name = #{strlit(unquoted_identifier statistics.name)}
          ORDER BY sc.stats_column_id
        }),
        "@column_name SYSNAME",
        output_to: self
      ).expectations(
        on_extra: ->{puts error_sql "#{stats_id.capitalize} has one or more unexpected columns."},
      ) do |test|
        statistics.columns.each.with_index do |col_name, i|
          test.row(
            on_missing: ->{puts error_sql "#{stats_id.capitalize} is missing #{col_name}."},
          ) {
            puts "IF QUOTENAME(@column_name) <> #{strlit col_name}"
            puts "BEGIN".."END" do
              puts error_sql "Expected #{col_name} as column #{i + 1} of #{stats_id}."
            end
          }
        end
      end
    }
    puts "END;"
  }
end

Instance Attribute Details

#stats_idObject (readonly)

Returns the value of attribute stats_id.



1135
1136
1137
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1135

def stats_id
  @stats_id
end

Instance Method Details

#error_sql(s) ⇒ Object



1137
1138
1139
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1137

def error_sql(s)
  @error_sql_proc.call(s)
end