class Mystic::SQL::Index

Constants

INDEX_TYPES

Attributes

columns[RW]
name[RW]
opts[RW]
table_name[RW]
type[RW]
unique[RW]

Public Class Methods

new(opts={}) click to toggle source

opts It’s a Hash that represents options

Key => Value (type) :fillfactor => A value in the range 10..100 (Integer) :fastupdate => true/false (TrueClass/FalseClass) :concurrently => true/false (TrueClass/FalseClass) :tablespace => The name of the desired tablespace (String) :buffering => :on/:off/:auto (Symbol) :concurrently => true/false (TrueClass/FalseClass) :where => The conditions for including entries in your index, same as SELECT * FROM table WHERE __ (String)

# File lib/mystic/sql/index.rb, line 33
def initialize opts={}
                          opts.symbolize!
                          raise ArgumentError, "Missing table_name." unless opts.member? :table_name
                          raise ArgumentError, "Indeces need columns or else what's the point?" unless opts.member? :columns
  @name = opts.delete(:name).to_sym if opts.member? :name
  @table_name = opts.delete(:table_name).to_sym
  @type = (opts.delete(:type) || :btree).to_s.downcase.to_sym
                          @unique = opts.delete :unique || false
  @columns = opts.delete(:columns).symbolize rescue []
                          @opts = opts
end

Public Instance Methods

<<(col) click to toggle source

can accept shit other than columns like box(location,location)

# File lib/mystic/sql/index.rb, line 47
def << col
  case col
  when Column then @columns << col.name.to_s
  when String then @columns << col
  else raise ArgumentError, "Column must be a String or a Mystic::SQL::Column" end
end
method_missing(meth, *args, &block) click to toggle source
# File lib/mystic/sql/index.rb, line 54
def method_missing(meth, *args, &block)
        return @opts[meth] if @opts.member? meth
        nil
end
to_s() click to toggle source
# File lib/mystic/sql/index.rb, line 59
def to_s
                  storage_params = opts.subhash :fillfactor, :buffering, :fastupdate
                  
                  sql = []
                  sql << "CREATE"
                  sql << "UNIQUE" if unique
                  sql << "INDEX"
                  sql << "CONCURENTLY" if concurrently
            sql << name unless name.nil?
            sql << "ON #{table_name}"
                  sql << "USING #{type}" if INDEX_TYPES.include? type
                  sql << "(#{columns.map(&:to_s).join ',' })"
                  sql << "WITH (#{storage_params.sqlize})" unless storage_params.empty?
                  sql << "TABLESPACE #{tablespace}" unless tablespace.nil?
                  sql << "WHERE #{where}" unless where.nil?
                  sql*' '
end