Class: ActiveFacts::Persistence::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/mapping/rails.rb,
lib/activefacts/persistence/index.rb

Instance Method Summary collapse

Constructor Details

#initialize(uc, on, over, columns, is_primary, is_unique = true) ⇒ Index

An Index arises from a uniqueness constraint and applies to a table, but because the UC may actually be over an object absorbed into the table, we must record that object also. We record the columns it’s over, whether it’s primary (for ‘over’), and whether it’s unique (always, at present)



36
37
38
39
# File 'lib/activefacts/persistence/index.rb', line 36

def initialize(uc, on, over, columns, is_primary, is_unique = true)   #:nodoc:
  @uniqueness_constraint, @on, @over, @columns, @is_primary, @is_unique =
    uc, on, over, columns, is_primary, is_unique
end

Instance Method Details

#abbreviated_column_names(separator = "") ⇒ Object

An array of the names of the columns this index covers, with some lexical truncations.



62
63
64
# File 'lib/activefacts/persistence/index.rb', line 62

def abbreviated_column_names(separator = "")
  columns.map{|column| column.name(separator).sub(/^#{over.name}/,'')}
end

#column_names(separator = "") ⇒ Object

An array of the names of the columns this index covers



57
58
59
# File 'lib/activefacts/persistence/index.rb', line 57

def column_names(separator = "")
  columns.map{|column| column.name(separator)}
end

#columnsObject

Return the array of columns in this index



23
# File 'lib/activefacts/persistence/index.rb', line 23

def columns; @columns; end

#is_primaryObject

Is this index the primary key for this table?



26
# File 'lib/activefacts/persistence/index.rb', line 26

def is_primary; @is_primary; end

#is_uniqueObject

Is this index unique?



29
# File 'lib/activefacts/persistence/index.rb', line 29

def is_unique; @is_unique; end

#nameObject

This name is either the name explicitly assigned (if any) or is constructed to form a unique index name.



47
48
49
50
51
52
53
54
# File 'lib/activefacts/persistence/index.rb', line 47

def name
  uc = @uniqueness_constraint
  r = real_name
  return r if r && r !~ /^(Ex|In)ternalUniquenessConstraint[0-9]+$/
  (uc.is_preferred_identifier ? "PK_" : "IX_") +
    view_name +
    (uc.is_preferred_identifier ? "" : "By"+column_names*"")
end

#onObject

The table that the index is on



16
# File 'lib/activefacts/persistence/index.rb', line 16

def on; @on; end

#overObject

If a non-mandatory reference was absorbed, only the non-nil instances are unique. Return the ObjectType that was absorbed, which might differ from this Index’s table.



20
# File 'lib/activefacts/persistence/index.rb', line 20

def over; @over; end

#rails_nameObject



80
81
82
83
84
85
86
87
88
# File 'lib/activefacts/mapping/rails.rb', line 80

def rails_name
	column_names = columns.map{|c| c.rails_name }
	index_name = "index_#{on.rails_name+'_on_'+column_names*'_'}"
	if index_name.length > 63
	  hash = Digest::SHA1.hexdigest index_name
	  index_name = index_name[0, 53] + '__' + hash[0, 8]
	end
	index_name
end

#real_nameObject

The name that was assigned (perhaps implicitly by NORMA)



42
43
44
# File 'lib/activefacts/persistence/index.rb', line 42

def real_name
  @uniqueness_constraint.name && @uniqueness_constraint.name != '' ? @uniqueness_constraint.name.gsub(' ','') : nil
end

#to_sObject

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/activefacts/persistence/index.rb', line 71

def to_s  #:nodoc:
	if @uniqueness_constraint
	  name = @uniqueness_constraint.name
	  preferred = @uniqueness_constraint.is_preferred_identifier ? " (preferred)" : ""
	else
	  name = "#{@on.name}IsUnique"
	  preferred = !@on.injected_surrogate_role ? " (preferred)" : ""
	end
  colnames = @columns.map(&:name)*", "
  "Index #{name} on #{@on.name} over #{@over.name}(#{colnames})#{preferred}"
end

#uniqueness_constraintObject

The UniquenessConstraint that created this index



13
# File 'lib/activefacts/persistence/index.rb', line 13

def uniqueness_constraint; @uniqueness_constraint; end

#view_nameObject

The name of a view that can be created to enforce uniqueness over non-null key values



67
68
69
# File 'lib/activefacts/persistence/index.rb', line 67

def view_name
  "#{over.name.gsub(' ','')}#{on == over ? "" : "In"+on.name.gsub(' ','')}"
end