Class: Mkxms::Mssql::KeylikeConstraint

Inherits:
Object
  • Object
show all
Extended by:
Utils::FlagsQueries
Includes:
ExtendedProperties, Property::Hosting
Defined in:
lib/mkxms/mssql/keylike_constraint_helper.rb

Direct Known Subclasses

PrimaryKey, UniqueConstraint

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::FlagsQueries

flags_query

Methods included from ExtendedProperties

#extended_properties

Methods included from Property::Hosting

#extended_properties_sql

Constructor Details

#initialize(attrs) ⇒ KeylikeConstraint

Returns a new instance of KeylikeConstraint.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 12

def initialize(attrs)
  @schema = attrs['schema']
  @table = attrs['table']
  @name = attrs['name']
  @stored_on = attrs['stored-on']
  @fill_factor = attrs['fill-factor']
  
  @flags = []
  @flags << :clustered if attrs['clustered']
  @flags << :paddedd if attrs['padded']
  @flags << :row_locks_ok unless attrs['no-row-locks']
  @flags << :page_locks_ok unless attrs['no-page-locks']
  
  @columns = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



29
30
31
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 29

def columns
  @columns
end

#fill_factorObject

Returns the value of attribute fill_factor.



28
29
30
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 28

def fill_factor
  @fill_factor
end

#flagsObject (readonly)

Returns the value of attribute flags.



29
30
31
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 29

def flags
  @flags
end

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 28

def name
  @name
end

#schemaObject

Returns the value of attribute schema.



28
29
30
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 28

def schema
  @schema
end

#stored_onObject

Returns the value of attribute stored_on.



28
29
30
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 28

def stored_on
  @stored_on
end

#tableObject

Returns the value of attribute table.



28
29
30
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 28

def table
  @table
end

Instance Method Details

#property_subject_identifiersObject



63
64
65
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 63

def property_subject_identifiers
  @prop_subj_id ||= ['SCHEMA', schema, 'TABLE', table, 'CONSTRAINT', name].map {|s| Utils::unquoted_name(s)}
end

#qualified_nameObject



59
60
61
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 59

def qualified_name
  "#@schema.#@name" if @name
end

#qualified_tableObject



55
56
57
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 55

def qualified_table
  "#@schema.#@table"
end

#to_sqlObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 32

def to_sql
  "ALTER TABLE #@schema.#@table ADD #{"CONSTRAINT #@name " if @name}" +
  "#{self.sql_constraint_type} #{'NON' unless clustered?}CLUSTERED (\n" +
  '    ' + columns.map {|c| c.to_sql}.join(", ") +
  "\n)" +
  with_clause_sql +
  # TODO: Handle partitioned constraints
  "#{" ON #@stored_on" if @stored_on}" +
  ";" +
  (name ? extended_properties_sql.joined_on_new_lines : '')
end

#with_clause_sqlObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/mkxms/mssql/keylike_constraint_helper.rb', line 44

def with_clause_sql
  options = []
  options << 'PAD_INDEX = ON' if padded?
  options << "FILLFACTOR = #@fill_factor" if fill_factor
  options << 'ALLOW_ROW_LOCKS = OFF' unless row_locks_ok?
  options << 'ALLOW_PAGE_LOCKS = OFF' unless page_locks_ok?
  
  return '' if options.empty?
  return " WITH (\n#{options.join ", "}\n)"
end