Class: Mkxms::Mssql::ClrFunction

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

Defined Under Namespace

Classes: ResultTable

Constant Summary collapse

SQL_OBJECT_TYPE =
'FUNCTION'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::InitializedAttributes

attr_init

Methods included from Utils::SchemaQualifiedName

#qualified_name

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

Constructor Details

#initialize(attrs) ⇒ ClrFunction

Returns a new instance of ClrFunction.



38
39
40
41
42
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 38

def initialize(attrs)
  @schema = attrs['schema']
  @name = attrs['name']
  @execute_as = attrs['execute-as']
end

Instance Attribute Details

#clr_implObject

Returns the value of attribute clr_impl.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def clr_impl
  @clr_impl
end

#execute_asObject

Returns the value of attribute execute_as.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def execute_as
  @execute_as
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def name
  @name
end

#result_tableObject

Returns the value of attribute result_table.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def result_table
  @result_table
end

#returnsObject

Returns the value of attribute returns.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def returns
  @returns
end

#schemaObject

Returns the value of attribute schema.



44
45
46
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 44

def schema
  @schema
end

Instance Method Details

#param_properties_sqlObject



82
83
84
85
86
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 82

def param_properties_sql
  params.map do |param|
    subitem_extended_properties_sql(param)
  end
end

#procedure_def_sqlObject



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
76
77
78
79
80
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 51

def procedure_def_sql
  [[].tap do |lines|
    lines << "CREATE FUNCTION [{filename}] ("
    lines << params.map do |param|
      "  #{param.name} #{param.type_spec}".tap do |param_spec|
        param_spec << " = #{param.default_value}" if param.default_value
      end
    end.join(",\n")
    lines << ")"
    case 
    when returns
      lines << "RETURNS #{returns.type_spec}"
    when result_table
      lines << "RETURNS TABLE ("
      lines << result_table.columns.map do |col|
        "  #{col.name} #{col.type_spec}"
      end.join(",\n")
      lines << ")"
    else
      raise RuntimeError.new("Function return not defined")
    end
    case execute_as
    when "OWNER"
      lines << "WITH EXECUTE AS OWNER"
    when String
      lines << "WITH EXECUTE AS '#{Utils.unquoted_name execute_as}'"
    end
    lines << "AS EXTERNAL NAME #{clr_impl.full_specifier};"
  end.join("\n")]
end

#result_column_properties_sqlObject



88
89
90
91
92
93
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 88

def result_column_properties_sql
  return [] unless result_table
  result_table.columns.map do |col|
    subitem_extended_properties_sql(col)
  end
end

#to_sqlObject



47
48
49
# File 'lib/mkxms/mssql/clr_function_handler.rb', line 47

def to_sql
  (procedure_def_sql + extended_properties_sql + param_properties_sql + result_column_properties_sql).join("\n")
end