Class: KnjDB_sqlite3::Columns::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb

Overview

This class handels all the SQLite3-columns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Column

Constructor. This should not be called manually.



52
53
54
55
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 52

def initialize(args)
  @args = args
  @db = @args[:db]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



49
50
51
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 49

def args
  @args
end

Instance Method Details

#autoincr?Boolean

Returns true if the column is auto-increasing.

Returns:

  • (Boolean)


147
148
149
150
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 147

def autoincr?
  return true if @args[:data][:pk].to_i == 1 and @args[:data][:type].to_s == "integer"
  return false
end

#change(data) ⇒ Object

Changes data on the column. Like the name, type, maxlength or whatever.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 158

def change(data)
  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")
  newdata["primarykey"] = self.primarykey? if !newdata.key?("primarykey")
  
  @type = nil
  @maxlength = nil
  
  new_table = self.table.copy(
    "alter_columns" => {
      self.name.to_s => newdata
    }
  )
end

#dataObject

Returns the data of the column as a hash in knjdb-format.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 68

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 of the column.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 126

def default
  def_val = @args[:data][:dflt_value]
  if def_val.to_s.slice(0..0) == "'"
    def_val = def_val.to_s.slice(0)
  end
  
  if def_val.to_s.slice(-1..-1) == "'"
    def_val = def_val.to_s.slice(0, def_val.length - 1)
  end
  
  return false if @args[:data][:dflt_value].to_s.length == 0
  return def_val
end

#dropObject

Drops the column from the table.



153
154
155
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 153

def drop
  self.table.copy("drops" => self.name)
end

#maxlengthObject

Returns the maxlength of the column.



119
120
121
122
123
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 119

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

#nameObject

Returns the name of the column.



58
59
60
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 58

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

#null?Boolean

Returns true if the column allows null. Otherwise false.

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 113

def null?
  return false if @args[:data][:notnull].to_i == 1
  return true
end

#primarykey?Boolean

Returns true if the column is the primary key.

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 141

def primarykey?
  return false if @args[:data][:pk].to_i == 0
  return true
end

#tableObject

Returns the columns table-object.



63
64
65
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 63

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

#typeObject

Returns the type of the column.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_columns.rb', line 81

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]
      type = match[1].to_sym
    elsif @args[:data].key?(:type) and @args[:data][:type].to_s == ""
      #A type can actually be empty in SQLite... Wtf?
      return @args[:data][:type]
    end
    
    if type == :integer
      @type = :int
    else
      @type = type
    end
    
    raise "Still not type? (#{@args[:data]})" if @type.to_s.strip.length <= 0
  end
  
  return @type
end