Class: Baza::Driver::Sqlite3::Column
- Defined in:
- lib/baza/drivers/sqlite3/column.rb
Overview
This class handels all the SQLite3-columns.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
-
#autoincr? ⇒ Boolean
Returns true if the column is auto-increasing.
-
#change(data) ⇒ Object
Changes data on the column.
-
#data ⇒ Object
Returns the data of the column as a hash in knjdb-format.
-
#default ⇒ Object
Returns the default value of the column.
-
#drop ⇒ Object
Drops the column from the table.
-
#initialize(args) ⇒ Column
constructor
Constructor.
-
#maxlength ⇒ Object
Returns the maxlength of the column.
-
#name ⇒ Object
Returns the name of the column.
-
#null? ⇒ Boolean
Returns true if the column allows null.
-
#primarykey? ⇒ Boolean
Returns true if the column is the primary key.
- #reload ⇒ Object
-
#table ⇒ Object
Returns the columns table-object.
- #table_name ⇒ Object
-
#type ⇒ Object
Returns the type of the column.
Methods inherited from Column
Constructor Details
#initialize(args) ⇒ Column
Constructor. This should not be called manually.
6 7 8 9 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 6 def initialize(args) @args = args @db = @args[:db] end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 3 def args @args end |
Instance Method Details
#autoincr? ⇒ Boolean
Returns true if the column is auto-increasing.
102 103 104 105 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 102 def autoincr? return true if @args[:data][:pk].to_i == 1 && @args[:data][:type].to_sym == :integer return false end |
#change(data) ⇒ Object
Changes data on the column. Like the name, type, maxlength or whatever.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 124 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 new_table = table.copy( alter_columns: { name.to_sym => newdata } ) @args[:data][:name] = newdata[:name].to_s reload end |
#data ⇒ Object
Returns the data of the column as a hash in knjdb-format.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 26 def data return { type: type, name: name, null: null?, maxlength: maxlength, default: default, primarykey: primarykey?, autoincr: autoincr? } end |
#default ⇒ Object
Returns the default value of the column.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 84 def default def_val = @args[:data][:dflt_value] if def_val && match = def_val.match(/\A'(.*)'\Z/) return match[1] end return false if @args[:data][:dflt_value].to_s.empty? return def_val end |
#drop ⇒ Object
Drops the column from the table.
108 109 110 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 108 def drop table.copy(drops: name) end |
#maxlength ⇒ Object
Returns the maxlength of the column.
77 78 79 80 81 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 77 def maxlength self.type if !@maxlength return @maxlength if @maxlength return false end |
#name ⇒ Object
Returns the name of the column.
12 13 14 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 12 def name return @args[:data][:name].to_sym end |
#null? ⇒ Boolean
Returns true if the column allows null. Otherwise false.
71 72 73 74 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 71 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.
96 97 98 99 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 96 def primarykey? return false if @args[:data][:pk].to_i == 0 return true end |
#reload ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 112 def reload @db.q("PRAGMA table_info(`#{@db.esc_table(table_name)}`)") do |data| next unless data[:name] == @args[:data][:name] @args[:data] = data @type = nil return end raise "Could not find data for column: #{table_name}.#{name}" end |
#table ⇒ Object
Returns the columns table-object.
21 22 23 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 21 def table return @db.tables[table_name] end |
#table_name ⇒ Object
16 17 18 |
# File 'lib/baza/drivers/sqlite3/column.rb', line 16 def table_name @args[:table_name] end |
#type ⇒ Object
Returns the type of the column.
39 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/drivers/sqlite3/column.rb', line 39 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.empty? end return @type end |