Class: Baza::Driver::Mysql::Column

Inherits:
Column
  • Object
show all
Defined in:
lib/baza/drivers/mysql/column.rb

Overview

This class handels every MySQL-column, that can be returned from a table-object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Column

#inspect, #to_s

Constructor Details

#initialize(args) ⇒ Column

Constructor. Should not be called manually.



6
7
8
9
10
# File 'lib/baza/drivers/mysql/column.rb', line 6

def initialize(args)
  @args = args
  @name = @args[:data][:Field].to_sym
  @db = @args[:db]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/baza/drivers/mysql/column.rb', line 3

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/baza/drivers/mysql/column.rb', line 3

def name
  @name
end

Instance Method Details

#__object_unique_id__Object

Used to validate in Wref::Map.



13
14
15
# File 'lib/baza/drivers/mysql/column.rb', line 13

def __object_unique_id__
  return @name
end

#autoincr?Boolean

Returns true if the column is auto-increasing. Otherwise false.

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/baza/drivers/mysql/column.rb', line 95

def autoincr?
  return true if @args[:data][:Extra].include?("auto_increment")
  return false
end

#change(data) ⇒ Object

Changes the column properties by the given hash.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/baza/drivers/mysql/column.rb', line 113

def change(data)
  col_escaped = "`#{@db.esc_col(name)}`"
  table_escape = "`#{@db.esc_table(table_name)}`"
  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] = self.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 #{table_escape} CHANGE #{col_escaped} #{@args[:db].cols.data_sql(newdata)}")
  @name = newdata[:name].to_sym
  reload
  table.__send__(:add_column_to_list, self) if drop_add
end

#commentObject

Returns the comment for the column.



101
102
103
# File 'lib/baza/drivers/mysql/column.rb', line 101

def comment
  return @args[:data][:Comment]
end

#dataObject

Returns all data of the column in the knjdb-format.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/baza/drivers/mysql/column.rb', line 27

def data
  return {
    type: type,
    name: name,
    null: null?,
    maxlength: maxlength,
    default: default,
    primarykey: primarykey?,
    autoincr: autoincr?
  }
end

#defaultObject

Returns the default value for the column.



81
82
83
84
85
86
# File 'lib/baza/drivers/mysql/column.rb', line 81

def default
  return false if (self.type == :datetime || self.type == :date) && @args[:data][:Default].to_s.strip.empty?
  return false if (self.type == :int || self.type == :bigint) && @args[:data][:Default].to_s.strip.empty?
  return false if !@args[:data][:Default]
  return @args[:data][:Default]
end

#dropObject

Drops the column from the table.



106
107
108
109
110
# File 'lib/baza/drivers/mysql/column.rb', line 106

def drop
  @args[:db].query("ALTER TABLE `#{@db.esc_table(@args[:table_name])}` DROP COLUMN `#{@db.esc_col(self.name)}`")
  table.__send__(:remove_column_from_list, self)
  return nil
end

#maxlengthObject

Returns the maxlength.



74
75
76
77
78
# File 'lib/baza/drivers/mysql/column.rb', line 74

def maxlength
  self.type unless @maxlength
  return @maxlength if @maxlength
  return false
end

#null?Boolean

Return true if the columns allows null. Otherwise false.

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/baza/drivers/mysql/column.rb', line 68

def null?
  return false if @args[:data][:Null] == "NO"
  return true
end

#primarykey?Boolean

Returns true if the column is the primary key. Otherwise false.

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/baza/drivers/mysql/column.rb', line 89

def primarykey?
  return true if @args[:data][:Key] == "PRI"
  return false
end

#reloadObject



39
40
41
42
# File 'lib/baza/drivers/mysql/column.rb', line 39

def reload
  @args[:data] = @db.query("SHOW FULL COLUMNS FROM `#{@db.esc_table(table_name)}` WHERE `Field` = '#{@db.esc(name)}'").fetch
  @type = nil
end

#tableObject

Returns the table-object that this column belongs to.



22
23
24
# File 'lib/baza/drivers/mysql/column.rb', line 22

def table
  return @db.tables[table_name]
end

#table_nameObject



17
18
19
# File 'lib/baza/drivers/mysql/column.rb', line 17

def table_name
  @args[:table_name]
end

#typeObject

Returns the type of the column (integer, varchar etc.).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/baza/drivers/mysql/column.rb', line 45

def type
  if !@type
    if match = @args[:data][:Type].match(/^([A-z]+)$/)
      @maxlength = false
      @type = match[0].to_sym
    elsif match = @args[:data][:Type].match(/^decimal\((\d+),(\d+)\)$/)
      @maxlength = "#{match[1]},#{match[2]}"
      @type = :decimal
    elsif match = @args[:data][:Type].match(/^enum\((.+)\)$/)
      @maxlength = match[1]
      @type = :enum
    elsif match = @args[:data][:Type].match(/^(.+)\((\d+)\)/)
      @maxlength = match[2].to_i
      @type = match[1].to_sym
    end

    raise "Still not type from: '#{@args[:data][:Type]}'." if @type.to_s.strip.empty?
  end

  return @type
end