Class: KnjDB_mysql::Columns::Column
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
-
#__object_unique_id__ ⇒ Object
Used to validate in Knj::Wrap_map.
-
#autoincr? ⇒ Boolean
Returns true if the column is auto-increasing.
-
#change(data) ⇒ Object
Changes the column properties by the given hash.
-
#comment ⇒ Object
Returns the comment for the column.
-
#data ⇒ Object
Returns all data of the column in the knjdb-format.
-
#default ⇒ Object
Returns the default value for the column.
-
#drop ⇒ Object
Drops the column from the table.
-
#initialize(args) ⇒ Column
constructor
Constructor.
-
#maxlength ⇒ Object
Returns the maxlength.
-
#name ⇒ Object
Returns the name of the column.
-
#null? ⇒ Boolean
Return true if the columns allows null.
-
#primarykey? ⇒ Boolean
Returns true if the column is the primary key.
-
#table ⇒ Object
Returns the table-object that this column belongs to.
-
#type ⇒ Object
Returns the type of the column (integer, varchar etc.).
Constructor Details
#initialize(args) ⇒ Column
Constructor. Should not be called manually.
39 40 41 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 39 def initialize(args) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
36 37 38 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 36 def args @args end |
Instance Method Details
#__object_unique_id__ ⇒ Object
Used to validate in Knj::Wrap_map.
44 45 46 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 44 def __object_unique_id__ return @args[:data][:Field] end |
#autoincr? ⇒ Boolean
Returns true if the column is auto-increasing. Otherwise false.
122 123 124 125 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 122 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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 139 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 |
#comment ⇒ Object
Returns the comment for the column.
128 129 130 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 128 def comment return @args[:data][:Comment] end |
#data ⇒ Object
Returns all data of the column in the knjdb-format.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 59 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 |
#default ⇒ Object
Returns the default value for the column.
108 109 110 111 112 113 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 108 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 |
#drop ⇒ Object
Drops the column from the table.
133 134 135 136 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 133 def drop @args[:db].query("ALTER TABLE `#{@args[:table_name]}` DROP COLUMN `#{self.name}`") return nil end |
#maxlength ⇒ Object
Returns the maxlength.
101 102 103 104 105 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 101 def maxlength self.type if !@maxlength return @maxlength if @maxlength return false end |
#name ⇒ Object
Returns the name of the column.
49 50 51 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 49 def name return @args[:data][:Field] end |
#null? ⇒ Boolean
Return true if the columns allows null. Otherwise false.
95 96 97 98 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 95 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.
116 117 118 119 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 116 def primarykey? return true if @args[:data][:Key] == "PRI" return false end |
#table ⇒ Object
Returns the table-object that this column belongs to.
54 55 56 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 54 def table return @args[:db].tables[@args[:table_name]] end |
#type ⇒ Object
Returns the type of the column (integer, varchar etc.).
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 72 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 |