# File lib/baza/driver/mysql/column.rb, line 92 def primarykey? @data.fetch(:Key) == "PRI" end
class Baza::Driver::Mysql::Column
This class handels every MySQL-column, that can be returned from a table-object.
Attributes
args[R]
name[R]
Public Class Methods
new(args)
click to toggle source
Constructor. Should not be called manually.
# File lib/baza/driver/mysql/column.rb, line 6 def initialize(args) @args = args @data = @args.delete(:data) @name = @data.fetch(:Field) @db = @args.fetch(:db) end
Public Instance Methods
__object_unique_id__()
click to toggle source
Used to validate in Wref::Map.
# File lib/baza/driver/mysql/column.rb, line 14 def __object_unique_id__ @name end
autoincr?()
click to toggle source
Returns true if the column is auto-increasing. Otherwise false.
# File lib/baza/driver/mysql/column.rb, line 97 def autoincr? @data.fetch(:Extra).include?("auto_increment") end
change(data)
click to toggle source
Changes the column properties by the given hash.
# File lib/baza/driver/mysql/column.rb, line 114 def change(data) newdata = data.clone newdata[:name] = name unless newdata.key?(:name) newdata[:type] = type unless newdata.key?(:type) newdata[:maxlength] = maxlength if !newdata.key?(:maxlength) && maxlength newdata[:null] = null? unless newdata.key?(:null) newdata[:default] = default if !newdata.key?(:default) && default newdata.delete(:primarykey) if newdata.key?(:primarykey) drop_add = true if name.to_s != newdata[:name].to_s table.__send__(:remove_column_from_list, self) if drop_add @db.query("ALTER TABLE #{@db.quote_table(table_name)} CHANGE #{@db.quote_column(name)} #{@db.columns.data_sql(newdata)}") @name = newdata[:name].to_s reload table.__send__(:add_column_to_list, self) if drop_add end
comment()
click to toggle source
Returns the comment for the column.
# File lib/baza/driver/mysql/column.rb, line 102 def comment @data.fetch(:Comment) end
create_foreign_key(args)
click to toggle source
# File lib/baza/driver/mysql/column.rb, line 18 def create_foreign_key(args) fk_name = args[:name] fk_name ||= "fk_#{table_name}_#{name}" other_column = args.fetch(:column) other_table = other_column.table sql = " ALTER TABLE #{@db.quote_table(table_name)} ADD CONSTRAINT #{@db.escape_table(fk_name)} FOREIGN KEY (#{@db.escape_table(name)}) REFERENCES #{@db.escape_table(other_table.name)} (#{@db.escape_column(other_column.name)}) " @db.query(sql) true end
default()
click to toggle source
Returns the default value for the column.
# File lib/baza/driver/mysql/column.rb, line 84 def default return nil if (type == :datetime || type == :date) && @data[:Default].to_s.strip.empty? return nil if (type == :int || type == :bigint) && @data[:Default].to_s.strip.empty? return nil unless @data[:Default] @data.fetch(:Default) end
drop()
click to toggle source
Drops the column from the table.
# File lib/baza/driver/mysql/column.rb, line 107 def drop @db.query("ALTER TABLE #{@db.quote_table(table_name)} DROP COLUMN #{@db.quote_column(name)}") table.__send__(:remove_column_from_list, self) nil end
maxlength()
click to toggle source
Returns the maxlength.
# File lib/baza/driver/mysql/column.rb, line 77 def maxlength type unless @maxlength return @maxlength if @maxlength false end
null?()
click to toggle source
Return true if the columns allows null. Otherwise false.
# File lib/baza/driver/mysql/column.rb, line 72 def null? @data[:Null] != "NO" end
primarykey?()
click to toggle source
Returns true if the column is the primary key. Otherwise false.
reload()
click to toggle source
# File lib/baza/driver/mysql/column.rb, line 41 def reload data = @db.query("SHOW FULL COLUMNS FROM #{@db.quote_table(table_name)} WHERE #{@db.quote_column("Field")} = #{@db.quote_value(name)}").fetch raise Baza::Errors::ColumnNotFound unless data @data = data @type = nil end
table_name()
click to toggle source
# File lib/baza/driver/mysql/column.rb, line 37 def table_name @args.fetch(:table_name) end
type()
click to toggle source
Returns the type of the column (integer, varchar etc.).
# File lib/baza/driver/mysql/column.rb, line 49 def type unless @type if (match = @data.fetch(:Type).match(/^([A-z]+)$/)) @maxlength = false @type = match[0].to_sym elsif (match = @data.fetch(:Type).match(/^decimal\((\d+),(\d+)\)$/)) @maxlength = "#{match[1]},#{match[2]}" @type = :decimal elsif (match = @data.fetch(:Type).match(/^enum\((.+)\)$/)) @maxlength = match[1] @type = :enum elsif (match = @data.fetch(:Type).match(/^(.+)\((\d+)\)/)) @maxlength = match[2].to_i @type = match[1].to_sym end raise "Still no type from: '#{@data.fetch(:Type)}'" if @type.to_s.strip.empty? end @type end