Class: Mystic::SQL::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/mystic/sql/index.rb

Constant Summary collapse

INDEX_TYPES =
[
  :btree,
  :hash,
  :gist,
  :spgist,
  :gin
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Index

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)

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



54
55
56
57
# File 'lib/mystic/sql/index.rb', line 54

def method_missing(meth, *args, &block)
	return @opts[meth] if @opts.member? meth
	nil
end

Instance Attribute Details

#columnsObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def columns
  @columns
end

#nameObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def name
  @name
end

#optsObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def opts
  @opts
end

#table_nameObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def table_name
  @table_name
end

#typeObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def type
  @type
end

#uniqueObject

Symbol or string



6
7
8
# File 'lib/mystic/sql/index.rb', line 6

def unique
  @unique
end

Instance Method Details

#<<(col) ⇒ Object

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



47
48
49
50
51
52
# 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

#to_sObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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