Class: Baza::Driver::Pg::Table
- Inherits:
-
Table
- Object
- Table
- Baza::Driver::Pg::Table
show all
- Defined in:
- lib/baza/driver/pg/table.rb
Instance Attribute Summary collapse
Attributes inherited from Table
#db
Instance Method Summary
collapse
Methods inherited from Table
#<=>, #create_columns, #insert, #inspect, #row, #rows, #to_param, #to_s, #upsert_duplicate_key
#model_name, #to_model
Constructor Details
#initialize(args) ⇒ Table
Returns a new instance of Table.
4
5
6
7
8
|
# File 'lib/baza/driver/pg/table.rb', line 4
def initialize(args)
@db = args.fetch(:driver).db
@data = args.fetch(:data)
@name = @data.fetch(:table_name)
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
2
3
4
|
# File 'lib/baza/driver/pg/table.rb', line 2
def name
@name
end
|
Instance Method Details
#clone(newname, _args = {}) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/baza/driver/pg/table.rb', line 197
def clone(newname, _args = {})
raise "Invalid name." if newname.to_s.strip.empty?
columns_list = []
indexes_list = []
columns do |column|
columns_list << column.data
end
indexes do |index|
data = index.data
data.delete(:name)
indexes_list << data
end
@db.tables.create(newname, columns: columns_list, indexes: indexes_list)
clone_insert_from_original_table(newname, columns_list)
@db.tables[newname]
end
|
#column(name) ⇒ Object
68
69
70
71
72
|
# File 'lib/baza/driver/pg/table.rb', line 68
def column(name)
column = columns(name: name).first
raise Baza::Errors::ColumnNotFound, "Column not found: #{name}" unless column
column
end
|
#columns(args = {}) ⇒ Object
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
|
# File 'lib/baza/driver/pg/table.rb', line 42
def columns(args = {})
where_args = {
table_catalog: @db.opts[:db],
table_name: name,
table_schema: "public"
}
where_args[:column_name] = args.fetch(:name) if args[:name]
columns_list = [] unless block_given?
@db.select([:information_schema, :columns], where_args) do |column_data|
column = Baza::Driver::Pg::Column.new(
db: @db,
data: column_data
)
if columns_list
columns_list << column
else
yield column
end
end
columns_list
end
|
#create_indexes(index_list, args = {}) ⇒ Object
162
163
164
|
# File 'lib/baza/driver/pg/table.rb', line 162
def create_indexes(index_list, args = {})
db.indexes.create_index(index_list, args.merge(table_name: name))
end
|
#data ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/baza/driver/pg/table.rb', line 16
def data
ret = {
name: name,
columns: [],
indexes: []
}
columns do |column|
ret[:columns] << column.data
end
indexes do |index|
ret[:indexes] << index.data
end
ret
end
|
#database_name ⇒ Object
34
35
36
|
# File 'lib/baza/driver/pg/table.rb', line 34
def database_name
@data.fetch(:table_catalog)
end
|
#drop ⇒ Object
10
11
12
13
14
|
# File 'lib/baza/driver/pg/table.rb', line 10
def drop
@db.with_database(database_name) do
@db.query("DROP TABLE \"#{@db.escape_table(name)}\"")
end
end
|
#foreign_key(name) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/baza/driver/pg/table.rb', line 122
def foreign_key(name)
foreign_keys(name: name) do |foreign_key|
return foreign_key
end
raise Baza::Errors::ForeignKeyNotFound, "Foreign key not found: #{name}"
end
|
#foreign_keys(args = {}) ⇒ 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
|
# File 'lib/baza/driver/pg/table.rb', line 79
def foreign_keys(args = {})
sql = "
SELECT
tc.constraint_name,
tc.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON
tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE
constraint_type = 'FOREIGN KEY' AND
tc.table_name = '#{@db.escape(name)}'
"
sql << " AND tc.constraint_name = '#{@db.escape(args.fetch(:name))}'" if args[:name]
result = [] unless block_given?
@db.query(sql) do |data|
foreign_key = Baza::Driver::Pg::ForeignKey.new(
db: @db,
data: data
)
if block_given?
yield foreign_key
else
result << foreign_key
end
end
result
end
|
#index(name) ⇒ Object
156
157
158
159
160
|
# File 'lib/baza/driver/pg/table.rb', line 156
def index(name)
index = indexes(name: name).first
raise Baza::Errors::IndexNotFound unless index
index
end
|
#indexes(args = {}) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/baza/driver/pg/table.rb', line 130
def indexes(args = {})
where_args = {
tablename: name
}
where_args[:indexname] = args.fetch(:name) if args[:name]
indexes_list = [] unless block_given?
@db.select(:pg_indexes, where_args) do |index_data|
index = Baza::Driver::Pg::Index.new(
db: @db,
data: index_data
)
next if index.primary?
if indexes_list
indexes_list << index
else
yield index
end
end
indexes_list
end
|
#native? ⇒ Boolean
38
39
40
|
# File 'lib/baza/driver/pg/table.rb', line 38
def native?
false
end
|
#optimize ⇒ Object
192
193
194
195
|
# File 'lib/baza/driver/pg/table.rb', line 192
def optimize
@db.query("VACUUM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}")
self
end
|
#reload ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/baza/driver/pg/table.rb', line 172
def reload
where_args = {
table_catalog: @db.opts.fetch(:db),
table_schema: "public",
table_name: name
}
data = @db.single([:information_schema, :tables], where_args)
raise Baza::Errors::TableNotFound unless data
@data = data
self
end
|
#rename(new_name) ⇒ Object
166
167
168
169
170
|
# File 'lib/baza/driver/pg/table.rb', line 166
def rename(new_name)
@db.query("ALTER TABLE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} RENAME TO #{@db.sep_table}#{@db.escape_table(new_name)}#{@db.sep_table}")
@name = new_name.to_s
self
end
|
#rows_count ⇒ Object
185
186
187
188
189
190
|
# File 'lib/baza/driver/pg/table.rb', line 185
def rows_count
@db.databases.with_database(database_name) do
sql = "SELECT COUNT(*) AS count FROM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}"
return @db.query(sql).fetch.fetch(:count).to_i
end
end
|
#truncate ⇒ Object
74
75
76
77
|
# File 'lib/baza/driver/pg/table.rb', line 74
def truncate
@db.query("TRUNCATE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} RESTART IDENTITY")
self
end
|