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.



1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1699

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.



1763
1764
1765
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1763

def stats_id
  @stats_id
end

Instance Method Details

#error_sql(s) ⇒ Object



1765
1766
1767
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1765

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