Class: Baza::Driver::Pg::Column
- Inherits:
-
Column
- Object
- Column
- Baza::Driver::Pg::Column
show all
- Defined in:
- lib/baza/driver/pg/column.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Column
#after, #data, #inspect, #table, #to_param, #to_s
#model_name, #to_model
Constructor Details
#initialize(args) ⇒ Column
Returns a new instance of Column.
4
5
6
7
8
|
# File 'lib/baza/driver/pg/column.rb', line 4
def initialize(args)
@db = args.fetch(:db)
@data = args.fetch(:data)
@name = @data.fetch(:column_name)
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
2
3
4
|
# File 'lib/baza/driver/pg/column.rb', line 2
def name
@name
end
|
Instance Method Details
#autoincr? ⇒ Boolean
40
41
42
|
# File 'lib/baza/driver/pg/column.rb', line 40
def autoincr?
!@data.fetch(:column_default).to_s.match(/\Anextval\('#{Regexp.escape(table_name)}_#{Regexp.escape(name)}_seq'::regclass\)\Z/).nil?
end
|
#change(data) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
|
# File 'lib/baza/driver/pg/column.rb', line 60
def change(data)
if data.key?(:name) && data.fetch(:name).to_s != name
@db.query("#{alter_table_sql} RENAME #{col_escaped} TO #{@db.sep_col}#{@db.escape_column(data.fetch(:name))}#{@db.sep_col}")
@name = data.fetch(:name).to_s
end
change_type = true if data.key?(:type) && data.fetch(:type).to_s != type.to_s
change_type = true if data.key?(:maxlength) && data.fetch(:maxlength) != maxlength
if change_type
type = data[:type].to_s || type.to_s
if type == "int"
using = " USING (trim(#{name})::integer)"
else
using = ""
end
@db.query("#{alter_column_sql} TYPE #{data.fetch(:type)}#{using}")
@type = nil
changed = true
end
if data.key?(:null) && data.fetch(:null) != null?
if data.fetch(:null)
@db.query("#{alter_column_sql} DROP NOT NULL")
else
@db.query("#{alter_column_sql} ADD NOT NULL")
end
changed = true
end
if data.key?(:default) && data.fetch(:default) != default
if data.fetch(:default).nil?
@db.query("#{alter_column_sql} DROP DEFAULT")
else
default = "'#{@db.esc(data.fetch(:default))}'"
@db.query("#{alter_column_sql} SET DEFAULT #{default}")
end
changed = true
end
reload if changed
self
end
|
#default ⇒ Object
44
45
46
47
|
# File 'lib/baza/driver/pg/column.rb', line 44
def default
return nil if autoincr?
@data.fetch(:column_default)
end
|
#drop ⇒ Object
49
50
51
52
|
# File 'lib/baza/driver/pg/column.rb', line 49
def drop
@db.query("ALTER TABLE #{@db.sep_table}#{@db.escape_table(table_name)}#{@db.sep_table} DROP COLUMN #{@db.sep_col}#{@db.escape_column(name)}#{@db.sep_col}")
nil
end
|
#maxlength ⇒ Object
28
29
30
|
# File 'lib/baza/driver/pg/column.rb', line 28
def maxlength
@data.fetch(:character_maximum_length)
end
|
#null? ⇒ Boolean
32
33
34
|
# File 'lib/baza/driver/pg/column.rb', line 32
def null?
@data.fetch(:is_nullable) == "YES"
end
|
#primarykey? ⇒ Boolean
36
37
38
|
# File 'lib/baza/driver/pg/column.rb', line 36
def primarykey?
autoincr?
end
|
#reload ⇒ Object
54
55
56
57
58
|
# File 'lib/baza/driver/pg/column.rb', line 54
def reload
data = @db.single([:information_schema, :columns], table_name: table_name, column_name: name)
raise Baza::Errors::ColumnNotFound unless data
@data = data
end
|
#table_name ⇒ Object
10
11
12
|
# File 'lib/baza/driver/pg/column.rb', line 10
def table_name
@data.fetch(:table_name)
end
|
#type ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/baza/driver/pg/column.rb', line 14
def type
unless @type
type = @data.fetch(:udt_name)
if type == "int4"
@type = :int
else
@type = type.to_sym
end
end
@type
end
|