class Mystic::SQL::Table
Attributes
columns[RW]
indeces[RW]
name[R]
operations[RW]
opts[RW]
Public Class Methods
alter(opts={})
click to toggle source
# File lib/mystic/sql/table.rb, line 16 def self.alter opts={} new false, opts end
create(opts={})
click to toggle source
# File lib/mystic/sql/table.rb, line 12 def self.create opts={} new true, opts end
new(is_create=true, opts={})
click to toggle source
# 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
Public Instance Methods
<<(obj)
click to toggle source
# 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={})
click to toggle source
Column
DSL
# 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?()
click to toggle source
# File lib/mystic/sql/table.rb, line 31 def create? @is_create end
drop_columns(*col_names)
click to toggle source
# 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)
click to toggle source
Operation DSL
# 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={})
click to toggle source
# 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)
click to toggle source
# 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
method_missing(meth, *args, &block)
click to toggle source
Calls superclass method
# 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
rename(newname)
click to toggle source
# 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)
click to toggle source
# 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_s()
click to toggle source
# 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