Class: Mkxms::Mssql::ClrType

Inherits:
Object
  • Object
show all
Includes:
ExtendedProperties, Property::Hosting, Property::SchemaScoped, Utils::SchemaQualifiedName
Defined in:
lib/mkxms/mssql/clr_type_handler.rb

Constant Summary collapse

RaiserrorSource =
Utils::RaiserrorWriter.new("%s: Missing or misconfigured CLR type %s")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExtendedProperties

#extended_properties

Methods included from Property::Hosting

#extended_properties_sql

Methods included from Property::SchemaScoped

#property_subject_identifiers, #subitem_extended_properties_sql

Methods included from Utils::SchemaQualifiedName

#qualified_name

Constructor Details

#initialize(schema, name, assembly, clr_class) ⇒ ClrType

Returns a new instance of ClrType.



12
13
14
15
16
17
18
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 12

def initialize(schema, name, assembly, clr_class)
  @schema = schema
  @name = name
  @assembly = assembly
  @clr_class = clr_class
  @warning_stmt = RaiserrorSource.next_statement("WARNING".sql_quoted, qualified_name.sql_quoted, severity: :warning)
end

Instance Attribute Details

#assemblyObject

Returns the value of attribute assembly.



21
22
23
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 21

def assembly
  @assembly
end

#clr_classObject

Returns the value of attribute clr_class.



21
22
23
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 21

def clr_class
  @clr_class
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 20

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



20
21
22
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 20

def schema
  @schema
end

#warning_stmtObject (readonly)

Returns the value of attribute warning_stmt.



20
21
22
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 20

def warning_stmt
  @warning_stmt
end

Class Method Details

.setup_sqlObject



23
24
25
26
27
28
29
30
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 23

def self.setup_sql
  [].tap do |s|
    s << "IF NOT EXISTS (SELECT * FROM sys.tables t WHERE t.object_id = OBJECT_ID(N'xmigra.ignored_clr_types'))"
    s << "    CREATE TABLE xmigra.ignored_clr_types ([schema] SYSNAME, name SYSNAME, CONSTRAINT PK_ignored_clr_types PRIMARY KEY ([schema], name));"
    
    s << "" # Give a newline at the end
  end.join("\n")
end

Instance Method Details

#to_sqlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mkxms/mssql/clr_type_handler.rb', line 32

def to_sql
  [].tap do |s|
    s << "IF NOT EXISTS ("
    s << "  SELECT t.assembly_qualified_name"
    s << "  FROM sys.assembly_types t"
    s << "  JOIN sys.schemas s ON t.schema_id = s.schema_id"
    s << "  WHERE QUOTENAME(s.name) = #{schema.sql_quoted}"
    s << "  AND QUOTENAME(t.name) = #{name.sql_quoted}"
    s << "  UNION ALL"
    s << "  SELECT N''"
    s << "  FROM xmigra.ignored_clr_types t"
    s << "  WHERE t.[schema] = #{schema.sql_quoted}"
    s << "  AND t.name = #{name.sql_quoted}"
    s << ") BEGIN"
    s << "  CREATE TYPE #{schema}.#{name} EXTERNAL NAME #{assembly}.#{clr_class};"
    s.concat(extended_properties_sql.map {|s| "  " + s})
    s << "END"
    
    s << "IF NOT EXISTS ("
    s << "  SELECT CONCAT(s.name, N'.', t.name) as clr_type, QUOTENAME(asm.name) as assembly, QUOTENAME(t.assembly_class) as clr_class"
    s << "  FROM sys.assembly_types t"
    s << "  JOIN sys.schemas s ON t.schema_id = s.schema_id"
    s << "  JOIN sys.assemblies asm ON t.assembly_id = asm.assembly_id"
    s << "  WHERE QUOTENAME(s.name) = #{schema.sql_quoted}"
    s << "  AND QUOTENAME(t.name) = #{name.sql_quoted}"
    s << "  -- #{warning_stmt.error_marker} Run the query up to this point for CLR type configuration --"
    cols = [
      ["assembly", assembly],
      ["clr_class", clr_class],
    ].map {|t, v| [t.ljust(v.length), v.ljust(t.length)]}
    s << ("  --                  " + cols.map {|e| e[0]}.join('   ') + ' --')
    s << ("  -- Expected values: " + cols.map {|e| e[1]}.join('   ') + ' --')
    s << "  AND QUOTENAME(asm.name) = #{assembly.sql_quoted}"
    s << "  AND QUOTENAME(t.assembly_class) = #{clr_class.sql_quoted}"
    s << "  UNION ALL"
    s << "  SELECT CONCAT(t.[schema], N'.', t.name), NULL, NULL"
    s << "  FROM xmigra.ignored_clr_types t"
    s << "  WHERE t.[schema] = #{schema.sql_quoted}"
    s << "  AND t.name = #{name.sql_quoted}"
    s << ") #{warning_stmt};"
    
    s << "" # Give a newline at the end
  end
end