Class: Mkxms::Mssql::AdoptionScriptWriter::CheckConstraintAdoptionChecks

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(cnstr, error_sql_proc) ⇒ CheckConstraintAdoptionChecks

Returns a new instance of CheckConstraintAdoptionChecks.



850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 850

def initialize(cnstr, error_sql_proc)
  super()
  
  @cnstr = cnstr
  @cnstr_id = "check constraint%s on #{cnstr.qualified_table}" % [
    cnstr.name ? cnstr.name + " " : ""
  ]
  @error_sql_proc = error_sql_proc
  
  @schema_name = unquoted_identifier cnstr.schema
  @table_name = unquoted_identifier cnstr.table
  @cnstr_name = unquoted_identifier(cnstr.name) if cnstr.name
  
  if cnstr.name
    add_named_constraint_tests
  else
    add_unnamed_constraint_tests
  end
end

Instance Attribute Details

#cnstrObject (readonly)

Returns the value of attribute cnstr.



870
871
872
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 870

def cnstr
  @cnstr
end

#cnstr_idObject (readonly)

Returns the value of attribute cnstr_id.



870
871
872
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 870

def cnstr_id
  @cnstr_id
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



870
871
872
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 870

def schema_name
  @schema_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



870
871
872
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 870

def table_name
  @table_name
end

Instance Method Details

#add_named_constraint_testsObject



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 876

def add_named_constraint_tests
  dsl {
    puts "IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT * FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.name = #{strlit cnstr_name}
      }
    end
    puts "BEGIN"
    indented {
      puts error_sql "#{cnstr_id.capitalize} does not exist."
    }
    puts "END ELSE IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT * FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.parent_object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.name = #{strlit cnstr_name}
        AND cc.definition = #{strlit cnstr.definition}
      }
    end
    puts "BEGIN"
    indented {
      puts error_sql "#{cnstr_id.capitalize} does not have expected definition."
    }
    puts "END;"
  }
end

#add_unnamed_constraint_testsObject



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 911

def add_unnamed_constraint_tests
  dsl {
    puts "IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT cc.object_id
        FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.parent_object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.definition = #{strlit cnstr.definition}
      }
    end
    puts "BEGIN".."END;" do
      puts error_sql "Expected #{cnstr_id} does not exist."
    end
  }
end

#error_sql(s) ⇒ Object



872
873
874
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 872

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