Class: KnjDB_mysql::Columns::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Column

Constructor. Should not be called manually.



40
41
42
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 40

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



37
38
39
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 37

def args
  @args
end

Instance Method Details

#__object_unique_id__Object

Used to validate in Knj::Wrap_map.



45
46
47
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 45

def __object_unique_id__
  return @args[:data][:Field]
end

#autoincr?Boolean

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

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 123

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

#change(data) ⇒ Object

Changes the column properties by the given hash.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 140

def change(data)
  col_escaped = "`#{@args[:db].esc_col(self.name)}`"
  table_escape = "`#{@args[:db].esc_table(self.table.name)}`"
  newdata = data.clone
  
  newdata["name"] = self.name if !newdata.key?("name")
  newdata["type"] = self.type if !newdata.key?("type")
  newdata["maxlength"] = self.maxlength if !newdata.key?("maxlength") and self.maxlength
  newdata["null"] = self.null? if !newdata.key?("null")
  newdata["default"] = self.default if !newdata.key?("default") and self.default
  newdata.delete("primarykey") if newdata.key?("primarykey")
  
  type_s = newdata["type"].to_s
  @args[:db].query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@args[:db].cols.data_sql(newdata)}")
end

#commentObject

Returns the comment for the column.



129
130
131
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 129

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

#dataObject

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 60

def data
  return {
    "type" => self.type,
    "name" => self.name,
    "null" => self.null?,
    "maxlength" => self.maxlength,
    "default" => self.default,
    "primarykey" => self.primarykey?,
    "autoincr" => self.autoincr?
  }
end

#defaultObject

Returns the default value for the column.



109
110
111
112
113
114
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 109

def default
  return false if (self.type == "datetime" or self.type == "date") and @args[:data][:Default].to_s.strip.length <= 0
  return false if (self.type == "int" or self.type == "bigint") and @args[:data][:Default].to_s.strip.length <= 0
  return false if !@args[:data][:Default]
  return @args[:data][:Default]
end

#dropObject

Drops the column from the table.



134
135
136
137
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 134

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

#maxlengthObject

Returns the maxlength.



102
103
104
105
106
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 102

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

#nameObject

Returns the name of the column.



50
51
52
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 50

def name
  return @args[:data][:Field]
end

#null?Boolean

Return true if the columns allows null. Otherwise false.

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 96

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)


117
118
119
120
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 117

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

#tableObject

Returns the table-object that this column belongs to.



55
56
57
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 55

def table
  return @args[:db].tables[@args[:table_name]]
end

#typeObject

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 73

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.length <= 0
  end
  
  return @type
end