# File lib/baza/driver/sqlite3/column.rb, line 96 def primarykey? @data.fetch(:pk).to_i == 1 end
class Baza::Driver::Sqlite3::Column
This class handels all the SQLite3-columns.
Attributes
args[R]
Public Class Methods
new(args)
click to toggle source
Constructor. This should not be called manually.
# File lib/baza/driver/sqlite3/column.rb, line 6 def initialize(args) @args = args @data = args.fetch(:data) @db = @args.fetch(:db) end
Public Instance Methods
autoincr?()
click to toggle source
Returns true if the column is auto-increasing.
# File lib/baza/driver/sqlite3/column.rb, line 101 def autoincr? primarykey? && @data.fetch(:type).casecmp("integer").zero? end
change(data)
click to toggle source
Changes data on the column. Like the name, type, maxlength or whatever.
# File lib/baza/driver/sqlite3/column.rb, line 122 def change(data) newdata = data.clone newdata[:name] = name unless newdata.key?(:name) newdata[:type] = type unless newdata.key?(:type) newdata[:maxlength] = maxlength unless newdata.key?(:maxlength) && maxlength newdata[:null] = null? unless newdata.key?(:null) newdata[:default] = default unless newdata.key?(:default) newdata[:primarykey] = primarykey? unless newdata.key?(:primarykey) @type = nil @maxlength = nil table.copy( alter_columns: { name => newdata } ) @data[:name] = newdata.fetch(:name).to_s reload end
data()
click to toggle source
Returns the data of the column as a hash in knjdb-format.
# File lib/baza/driver/sqlite3/column.rb, line 27 def data { type: type, name: name, null: null?, maxlength: maxlength, default: default, primarykey: primarykey?, autoincr: autoincr? } end
default()
click to toggle source
Returns the default value of the column.
# File lib/baza/driver/sqlite3/column.rb, line 84 def default def_val = @data.fetch(:dflt_value) if def_val && (match = def_val.match(/\A'(.*)'\Z/)) return match[1] end return nil if @data.fetch(:dflt_value).to_s.empty? def_val end
drop()
click to toggle source
Drops the column from the table.
# File lib/baza/driver/sqlite3/column.rb, line 106 def drop table.copy(drops: name) end
maxlength()
click to toggle source
Returns the maxlength of the column.
# File lib/baza/driver/sqlite3/column.rb, line 77 def maxlength type unless @maxlength.nil? return @maxlength if @maxlength false end
name()
click to toggle source
Returns the name of the column.
# File lib/baza/driver/sqlite3/column.rb, line 13 def name @data.fetch(:name) end
null?()
click to toggle source
Returns true if the column allows null. Otherwise false.
# File lib/baza/driver/sqlite3/column.rb, line 71 def null? return false if @data.fetch(:notnull).to_i == 1 true end
primarykey?()
click to toggle source
Returns true if the column is the primary key.
reload()
click to toggle source
# File lib/baza/driver/sqlite3/column.rb, line 110 def reload @db.q("PRAGMA table_info(#{@db.quote_table(table_name)})") do |data| next unless data.fetch(:name) == name @data = data @type = nil return nil end raise Baza::Errors::ColumnNotFound, "Could not find data for column: #{table_name}.#{name}" end
table()
click to toggle source
Returns the columns table-object.
# File lib/baza/driver/sqlite3/column.rb, line 22 def table @db.tables[table_name] end
table_name()
click to toggle source
# File lib/baza/driver/sqlite3/column.rb, line 17 def table_name @args.fetch(:table_name) end
type()
click to toggle source
Returns the type of the column.
# File lib/baza/driver/sqlite3/column.rb, line 40 def type unless @type if (match = @data.fetch(:type).match(/^([A-z]+)$/)) @maxlength = false type = match[0].downcase.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] type = match[1].to_sym elsif @data.key?(:type) && @data.fetch(:type).to_s == "" return @data[:type] # A type can actually be empty in SQLite... Wtf? end if type == :integer @type = :int else @type = type end raise "Still not type? (#{@data})" if @type.to_s.strip.empty? end @type end