Class: Mystic::SQL::Table

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(is_create = true, opts = {}) ⇒ Table

Returns a new instance of Table.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
# File 'lib/mystic/sql/table.rb', line 20

def initialize is_create=true, opts={}
				@is_create = is_create
				@opts = opts.symbolize
  @columns = []
  @indeces = []
  @operations = []
				
  @name = @opts.delete(:name).to_s
  raise ArgumentError, "Argument 'name' is invalid." if @name.empty?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



116
117
118
119
# File 'lib/mystic/sql/table.rb', line 116

def method_missing meth, *args, &block
				return column args[0], meth.to_s, args[1] if args.count > 0
				super
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/mystic/sql/table.rb', line 7

def columns
  @columns
end

#indecesObject

Returns the value of attribute indeces.



7
8
9
# File 'lib/mystic/sql/table.rb', line 7

def indeces
  @indeces
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#operationsObject

Returns the value of attribute operations.



7
8
9
# File 'lib/mystic/sql/table.rb', line 7

def operations
  @operations
end

#optsObject

Returns the value of attribute opts.



7
8
9
# File 'lib/mystic/sql/table.rb', line 7

def opts
  @opts
end

Class Method Details

.alter(opts = {}) ⇒ Object



16
17
18
# File 'lib/mystic/sql/table.rb', line 16

def self.alter opts={}
	new false, opts
end

.create(opts = {}) ⇒ Object



12
13
14
# File 'lib/mystic/sql/table.rb', line 12

def self.create opts={}
	new true, opts
end

Instance Method Details

#<<(obj) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/mystic/sql/table.rb', line 35

def << obj
  case obj
  when Column then @columns << obj
  when Index then @indeces << obj
  when Operation then @operations << obj
  when String then @operations << obj
  else raise ArgumentError, "Argument is not a Mystic::SQL::Column, Mystic::SQL::Operation, or Mystic::SQL::Index." end
end

#column(col_name, kind, opts = {}) ⇒ Object

Column DSL



92
93
94
95
96
97
# File 'lib/mystic/sql/table.rb', line 92

def column col_name, kind, opts={}
  self << Mystic::SQL::Column.new({
    :name => col_name,
    :kind => kind.to_sym
  }.merge(opts || {}))
end

#create?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mystic/sql/table.rb', line 31

def create?
	@is_create
end

#drop_columns(*col_names) ⇒ Object

Raises:



83
84
85
86
# File 'lib/mystic/sql/table.rb', line 83

def drop_columns *col_names
				raise Mystic::SQL::Error, "Cannot drop a column(s) on a table that doesn't exist." if create?
  self << "ALTER TABLE #{table_name} #{col_names.map { |c| "DROP COLUMN #{c.to_s}" }*', ' }"
end

#drop_index(idx_name) ⇒ Object

Operation DSL

Raises:



67
68
69
70
# File 'lib/mystic/sql/table.rb', line 67

def drop_index idx_name
				raise Mystic::SQL::Error, "Cannot drop an index on a table that doesn't exist." if create?
  self << "DROP INDEX #{idx_name}"
end

#geometry(col_name, kind, srid, opts = {}) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/mystic/sql/table.rb', line 99

def geometry col_name, kind, srid, opts={}
				self << Mystic::SQL::Column.new({
    :name => col_name,
					:kind => :geometry,
    :geom_kind => kind,
    :geom_srid => srid
  }.merge(opts || {}))
end

#index(*cols) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/mystic/sql/table.rb', line 108

def index *cols
  opts = cols.delete_at -1 if cols.last.is_a? Hash
  opts ||= {}
  opts[:columns] = cols
				opts[:table_name] = @name
  self << Mystic::SQL::Index.new(opts)
end

#rename(newname) ⇒ Object

Raises:



77
78
79
80
81
# File 'lib/mystic/sql/table.rb', line 77

def rename newname
				raise Mystic::SQL::Error, "Cannot rename a table that doesn't exist." if create?
  self << "ALTER TABLE #{table_name} RENAME TO #{newname}"
  self.name = newname
end

#rename_column(oldname, newname) ⇒ Object

Raises:



72
73
74
75
# File 'lib/mystic/sql/table.rb', line 72

def rename_column oldname, newname
				raise Mystic::SQL::Error, "Cannot rename a column on a table that doesn't exist." if create?
  self << "ALTER TABLE #{table_name} RENAME COLUMN #{old_name} TO #{new_name}"
end

#to_sObject

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mystic/sql/table.rb', line 44

def to_s
  raise ArgumentError, "Table cannot have zero columns." if @columns.empty?
  			sql = []
			
  			if create?
tbl = []
tbl << "CREATE TABLE #{name} (#{columns.map(&:to_s)*","})"
tbl << "INHERITS #{opts[:inherits]}" if opts[:inherits]
tbl << "TABLESPACE #{opts[:tablespace]}" if opts[:tablespace]
sql << tbl*' '
  			else
sql << "ALTER TABLE #{name} #{columns.map{ |c| "ADD COLUMN #{c.to_s}" }*', ' }"
  			end

  			sql.push(*indeces.map(&:to_s)) unless indeces.empty?
 sql.push(*operations.map(&:to_s)) unless operations.empty?
  			sql*'; '
end