Class: KnjDB_mysql::Columns::Column
- Defined in:
- lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
- #autoincr? ⇒ Boolean
- #change(data) ⇒ Object
- #comment ⇒ Object
- #data ⇒ Object
- #default ⇒ Object
- #drop ⇒ Object
-
#initialize(args) ⇒ Column
constructor
A new instance of Column.
- #maxlength ⇒ Object
- #name ⇒ Object
- #null? ⇒ Boolean
- #primarykey? ⇒ Boolean
- #table ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(args) ⇒ Column
Returns a new instance of Column.
38 39 40 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 38 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
#autoincr? ⇒ Boolean
105 106 107 108 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 105 def autoincr? return true if @args[:data][:Extra].index("auto_increment") != nil return false end |
#change(data) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 118 def change(data) esc_col = @args[:driver].escape_col col_escaped = "#{esc_col}#{@args[:db].esc_col(self.name)}#{esc_col}" table_escape = "#{@args[:driver].escape_table}#{@args[:driver].esc_table(self.table.name)}#{@args[:driver].escape_table}" 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
110 111 112 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 110 def comment return @args[:data][:Comment] end |
#data ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 50 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
93 94 95 96 97 98 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 93 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
114 115 116 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 114 def drop @args[:db].query("ALTER TABLE `#{@args[:table].name}` DROP COLUMN `#{self.name}`") end |
#maxlength ⇒ Object
87 88 89 90 91 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 87 def maxlength self.type return @maxlength if @maxlength return false end |
#name ⇒ Object
42 43 44 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 42 def name return @args[:data][:Field] end |
#null? ⇒ Boolean
82 83 84 85 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 82 def null? return false if @args[:data][:Null] == "NO" return true end |
#primarykey? ⇒ Boolean
100 101 102 103 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 100 def primarykey? return false if @args[:data][:pk].to_i == 0 return true end |
#table ⇒ Object
46 47 48 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 46 def table return @args[:db].tables[@args[:table_name]] end |
#type ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb', line 62 def type if !@type if match = @args[:data][:Type].match(/^([A-z]+)$/) @maxlength = false @type = match[0] 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] @type = match[1] end end return @type end |