Class: Baza::Driver::Sqlite3::Column

Inherits:
Column
  • Object
show all
Defined in:
lib/baza/driver/sqlite3/column.rb

Overview

This class handels all the SQLite3-columns.

Direct Known Subclasses

Baza::Driver::Sqlite3Java::Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Column

#after, #inspect, #to_param, #to_s

Methods included from Baza::DatabaseModelFunctionality

#model_name, #to_model

Constructor Details

#initialize(args) ⇒ Column

Constructor. This should not be called manually.



6
7
8
9
10
# File 'lib/baza/driver/sqlite3/column.rb', line 6

def initialize(args)
  @args = args
  @data = args.fetch(:data)
  @db = @args.fetch(:db)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/baza/driver/sqlite3/column.rb', line 3

def args
  @args
end

Instance Method Details

#autoincr?Boolean

Returns true if the column is auto-increasing.

Returns:

  • (Boolean)


101
102
103
# File 'lib/baza/driver/sqlite3/column.rb', line 101

def autoincr?
  primarykey? && @data.fetch(:type).casecmp("integer").zero?
end

#change(data) ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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

#dataObject

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



27
28
29
30
31
32
33
34
35
36
37
# 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

#defaultObject

Returns the default value of the column.



84
85
86
87
88
89
90
91
92
93
# 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

#dropObject

Drops the column from the table.



106
107
108
# File 'lib/baza/driver/sqlite3/column.rb', line 106

def drop
  table.copy(drops: name)
end

#maxlengthObject

Returns the maxlength of the column.



77
78
79
80
81
# File 'lib/baza/driver/sqlite3/column.rb', line 77

def maxlength
  type unless @maxlength.nil?
  return @maxlength if @maxlength
  false
end

#nameObject

Returns the name of the column.



13
14
15
# File 'lib/baza/driver/sqlite3/column.rb', line 13

def name
  @data.fetch(:name)
end

#null?Boolean

Returns true if the column allows null. Otherwise false.

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/baza/driver/sqlite3/column.rb', line 71

def null?
  return false if @data.fetch(:notnull).to_i == 1
  true
end

#primarykey?Boolean

Returns true if the column is the primary key.

Returns:

  • (Boolean)


96
97
98
# File 'lib/baza/driver/sqlite3/column.rb', line 96

def primarykey?
  @data.fetch(:pk).to_i == 1
end

#reloadObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/baza/driver/sqlite3/column.rb', line 110

def reload
  @db.q("PRAGMA table_info(`#{@db.escape_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

#tableObject

Returns the columns table-object.



22
23
24
# File 'lib/baza/driver/sqlite3/column.rb', line 22

def table
  @db.tables[table_name]
end

#table_nameObject



17
18
19
# File 'lib/baza/driver/sqlite3/column.rb', line 17

def table_name
  @args.fetch(:table_name)
end

#typeObject

Returns the type of the column.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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