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
59
60
61
|
# File 'lib/baza/driver/pg/column.rb', line 59
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/baza/driver/pg/column.rb', line 79
def change(data)
if data.key?(:name) && data.fetch(:name).to_s != name
@db.query("#{alter_table_sql} RENAME #{@db.quote_column(name)} TO #{@db.quote_column(data.fetch(:name))}")
@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
|
#create_foreign_key(args) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/baza/driver/pg/column.rb', line 14
def create_foreign_key(args)
fk_name = args[:name]
fk_name ||= "fk_#{table_name}_#{name}"
other_column = args.fetch(:column)
other_table = other_column.table
sql = "
ALTER TABLE #{@db.quote_table(table_name)}
ADD CONSTRAINT #{@db.escape_table(fk_name)}
FOREIGN KEY (#{@db.escape_table(name)})
REFERENCES #{@db.escape_table(other_table.name)} (#{@db.escape_column(other_column.name)})
"
@db.query(sql)
true
end
|
#default ⇒ Object
63
64
65
66
|
# File 'lib/baza/driver/pg/column.rb', line 63
def default
return nil if autoincr?
@data.fetch(:column_default)
end
|
#drop ⇒ Object
68
69
70
71
|
# File 'lib/baza/driver/pg/column.rb', line 68
def drop
@db.query("ALTER TABLE #{@db.quote_table(table_name)} DROP COLUMN #{@db.quote_column(name)}")
nil
end
|
#maxlength ⇒ Object
47
48
49
|
# File 'lib/baza/driver/pg/column.rb', line 47
def maxlength
@data.fetch(:character_maximum_length)
end
|
#null? ⇒ Boolean
51
52
53
|
# File 'lib/baza/driver/pg/column.rb', line 51
def null?
@data.fetch(:is_nullable) == "YES"
end
|
#primarykey? ⇒ Boolean
55
56
57
|
# File 'lib/baza/driver/pg/column.rb', line 55
def primarykey?
autoincr?
end
|
#reload ⇒ Object
73
74
75
76
77
|
# File 'lib/baza/driver/pg/column.rb', line 73
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
@_table_name ||= @data.fetch(:table_name)
end
|
#type ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/baza/driver/pg/column.rb', line 33
def type
unless @type
type = @data.fetch(:udt_name)
if type == "int4"
@type = :int
else
@type = type.to_sym
end
end
@type
end
|