Class: Baza::Driver::Sqlite3::Column
- Defined in:
- lib/baza/driver/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
#after, #inspect, #to_param, #to_s
Methods included from Baza::DatabaseModelFunctionality
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
#args ⇒ Object (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.
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 |
#data ⇒ Object
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 |
#default ⇒ Object
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 |
#drop ⇒ Object
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 |
#maxlength ⇒ Object
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 |
#name ⇒ Object
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.
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.
96 97 98 |
# File 'lib/baza/driver/sqlite3/column.rb', line 96 def primarykey? @data.fetch(:pk).to_i == 1 end |
#reload ⇒ Object
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 |
#table ⇒ Object
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_name ⇒ Object
17 18 19 |
# File 'lib/baza/driver/sqlite3/column.rb', line 17 def table_name @args.fetch(:table_name) end |
#type ⇒ Object
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 |