Class: Baza::Driver::Mysql::Column
- Defined in:
- lib/baza/driver/mysql/column.rb
Overview
This class handels every MySQL-column, that can be returned from a table-object.
Direct Known Subclasses
Baza::Driver::Mysql2::Column, Baza::Driver::MysqlJava::Column
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#__object_unique_id__ ⇒ Object
Used to validate in Wref::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.
- #create_foreign_key(args) ⇒ Object
-
#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.
-
#null? ⇒ Boolean
Return true if the columns allows null.
-
#primarykey? ⇒ Boolean
Returns true if the column is the primary key.
- #reload ⇒ Object
- #table_name ⇒ Object
-
#type ⇒ Object
Returns the type of the column (integer, varchar etc.).
Methods inherited from Column
#after, #data, #inspect, #table, #to_param, #to_s
Methods included from Baza::DatabaseModelFunctionality
Constructor Details
#initialize(args) ⇒ Column
Constructor. Should not be called manually.
6 7 8 9 10 11 |
# File 'lib/baza/driver/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
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/baza/driver/mysql/column.rb', line 3 def args @args end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/baza/driver/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/driver/mysql/column.rb', line 14 def __object_unique_id__ @name end |
#autoincr? ⇒ Boolean
Returns true if the column is auto-increasing. Otherwise false.
97 98 99 |
# File 'lib/baza/driver/mysql/column.rb', line 97 def autoincr? @data.fetch(:Extra).include?("auto_increment") end |
#change(data) ⇒ Object
Changes the column properties by the given hash.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/baza/driver/mysql/column.rb', line 114 def change(data) 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 #{@db.quote_table(table_name)} CHANGE #{@db.quote_column(name)} #{@db.columns.data_sql(newdata)}") @name = newdata[:name].to_s reload table.__send__(:add_column_to_list, self) if drop_add end |
#comment ⇒ Object
Returns the comment for the column.
102 103 104 |
# File 'lib/baza/driver/mysql/column.rb', line 102 def comment @data.fetch(:Comment) end |
#create_foreign_key(args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/baza/driver/mysql/column.rb', line 18 def create_foreign_key(args) fk_name = args[:name] fk_name ||= "fk_#{table_name}_#{name}" other_column = args.fetch(:column) other_table = other_column.table sql = " ALTER TABLE #{@db.quote_table(table_name)} ADD CONSTRAINT #{@db.escape_table(fk_name)} FOREIGN KEY (#{@db.escape_table(name)}) REFERENCES #{@db.escape_table(other_table.name)} (#{@db.escape_column(other_column.name)}) " @db.query(sql) true end |
#default ⇒ Object
Returns the default value for the column.
84 85 86 87 88 89 |
# File 'lib/baza/driver/mysql/column.rb', line 84 def default return nil if (type == :datetime || type == :date) && @data[:Default].to_s.strip.empty? return nil if (type == :int || type == :bigint) && @data[:Default].to_s.strip.empty? return nil unless @data[:Default] @data.fetch(:Default) end |
#drop ⇒ Object
Drops the column from the table.
107 108 109 110 111 |
# File 'lib/baza/driver/mysql/column.rb', line 107 def drop @db.query("ALTER TABLE #{@db.quote_table(table_name)} DROP COLUMN #{@db.quote_column(name)}") table.__send__(:remove_column_from_list, self) nil end |
#maxlength ⇒ Object
Returns the maxlength.
77 78 79 80 81 |
# File 'lib/baza/driver/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.
72 73 74 |
# File 'lib/baza/driver/mysql/column.rb', line 72 def null? @data[:Null] != "NO" end |
#primarykey? ⇒ Boolean
Returns true if the column is the primary key. Otherwise false.
92 93 94 |
# File 'lib/baza/driver/mysql/column.rb', line 92 def primarykey? @data.fetch(:Key) == "PRI" end |
#reload ⇒ Object
41 42 43 44 45 46 |
# File 'lib/baza/driver/mysql/column.rb', line 41 def reload data = @db.query("SHOW FULL COLUMNS FROM #{@db.quote_table(table_name)} WHERE #{@db.quote_column("Field")} = #{@db.quote_value(name)}").fetch raise Baza::Errors::ColumnNotFound unless data @data = data @type = nil end |
#table_name ⇒ Object
37 38 39 |
# File 'lib/baza/driver/mysql/column.rb', line 37 def table_name @args.fetch(:table_name) end |
#type ⇒ Object
Returns the type of the column (integer, varchar etc.).
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/baza/driver/mysql/column.rb', line 49 def type unless @type if (match = @data.fetch(:Type).match(/^([A-z]+)$/)) @maxlength = false @type = match[0].to_sym elsif (match = @data.fetch(:Type).match(/^decimal\((\d+),(\d+)\)$/)) @maxlength = "#{match[1]},#{match[2]}" @type = :decimal elsif (match = @data.fetch(:Type).match(/^enum\((.+)\)$/)) @maxlength = match[1] @type = :enum elsif (match = @data.fetch(:Type).match(/^(.+)\((\d+)\)/)) @maxlength = match[2].to_i @type = match[1].to_sym end raise "Still no type from: '#{@data.fetch(:Type)}'" if @type.to_s.strip.empty? end @type end |