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_param, #to_s

Methods included from Baza::DatabaseModelFunctionality

#model_name, #to_model

Constructor Details

#initialize(args) ⇒ Column

Constructor. Should not be called manually.



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

def initialize(args)
  @args = args
  @data = @args.delete(:data)
  @name = @data.fetch(:Field)
  @db = @args.fetch(: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.



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

def __object_unique_id__
  @name
end

#autoincr?Boolean

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

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/baza/drivers/mysql/column.rb', line 98

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

#change(data) ⇒ Object

Changes the column properties by the given hash.



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

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

#commentObject

Returns the comment for the column.



104
105
106
# File 'lib/baza/drivers/mysql/column.rb', line 104

def comment
  @data.fetch(:Comment)
end

#dataObject

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



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

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

#defaultObject

Returns the default value for the column.



84
85
86
87
88
89
# File 'lib/baza/drivers/mysql/column.rb', line 84

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

#dropObject

Drops the column from the table.



109
110
111
112
113
# File 'lib/baza/drivers/mysql/column.rb', line 109

def drop
  @db.query("ALTER TABLE `#{@db.escape_table(table_name)}` DROP COLUMN `#{@db.escape_column(name)}`")
  table.__send__(:remove_column_from_list, self)
  nil
end

#maxlengthObject

Returns the maxlength.



77
78
79
80
81
# File 'lib/baza/drivers/mysql/column.rb', line 77

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

#null?Boolean

Return true if the columns allows null. Otherwise false.

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/baza/drivers/mysql/column.rb', line 71

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

#primarykey?Boolean

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

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/baza/drivers/mysql/column.rb', line 92

def primarykey?
  return true if @data.fetch(:Key) == "PRI"
  false
end

#reloadObject



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

def reload
  data = @db.query("SHOW FULL COLUMNS FROM `#{@db.escape_table(table_name)}` WHERE `Field` = '#{@db.esc(name)}'").fetch
  raise Baza::Errors::ColumnNotFound unless data
  @data = data
  @type = nil
end

#tableObject

Returns the table-object that this column belongs to.



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

def table
  @db.tables[table_name]
end

#table_nameObject



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

def table_name
  @args.fetch(:table_name)
end

#typeObject

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



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

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

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

  @type
end