Class: Baza::Driver::Pg::Database
Constant Summary
collapse
- CREATE_ALLOWED_KEYS =
[:columns, :indexes, :temp, :return_sql].freeze
Instance Attribute Summary
#db, #driver, #name, #name_was
Instance Method Summary
collapse
#import_file!, #initialize, #table_exists?, #to_param
#model_name, #to_model
Constructor Details
This class inherits a constructor from Baza::Database
Instance Method Details
#create_table(table_name, data, args = nil) ⇒ Object
Creates a new table by the given name and data.
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
|
# File 'lib/baza/driver/pg/database.rb', line 65
def create_table(table_name, data, args = nil)
table_name = table_name.to_s
raise "No columns was given for '#{name}'." if !data[:columns] || data[:columns].empty?
sql = "CREATE"
sql << " TEMPORARY" if data[:temp]
sql << " TABLE #{db.sep_table}#{@db.escape_table(table_name)}#{db.sep_table} ("
first = true
data.fetch(:columns).each do |col_data|
sql << ", " unless first
first = false if first
col_data.delete(:after) if col_data[:after]
sql << @db.columns.data_sql(col_data)
end
sql << ")"
use { @db.query(sql) } if !args || !args[:return_sql]
if data[:indexes] && !data[:indexes].empty?
table = @db.tables[table_name]
table.create_indexes(data.fetch(:indexes))
end
return [sql] if args && args[:return_sql]
end
|
#drop ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/baza/driver/pg/database.rb', line 7
def drop
other_db = @db.databases.list.find { |database| database.name != @db.current_database }
@db.clone_conn(db: other_db.name) do |cloned_conn|
cloned_conn.query("REVOKE CONNECT ON DATABASE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database} FROM public")
cloned_conn.query("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = #{@db.sep_val}#{@db.esc(name)}#{@db.sep_val} AND pid != pg_backend_pid()")
cloned_conn.query("DROP DATABASE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database}")
end
self
end
|
#rename(new_name) ⇒ Object
93
94
95
96
97
|
# File 'lib/baza/driver/pg/database.rb', line 93
def rename(new_name)
@db.query("ALTER DATABASE #{@db.sep_database}#{@db.escape_database(name_was)}#{@db.sep_database} RENAME TO #{@db.sep_database}#{@db.escape_database(new_name)}#{@db.sep_database}")
@name = new_name.to_s
self
end
|
#save! ⇒ Object
2
3
4
5
|
# File 'lib/baza/driver/pg/database.rb', line 2
def save!
rename(name) unless name.to_s == name_was
self
end
|
#table(table_name) ⇒ Object
23
24
25
26
27
|
# File 'lib/baza/driver/pg/database.rb', line 23
def table(table_name)
table = tables(name: table_name).first
raise Baza::Errors::TableNotFound unless table
table
end
|
#tables(args = {}) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/baza/driver/pg/database.rb', line 29
def tables(args = {})
tables_list = [] unless block_given?
where_args = {
table_catalog: name,
table_schema: "public"
}
where_args[:table_name] = args.fetch(:name) if args[:name]
use do
@db.select([:information_schema, :tables], where_args, orderby: :table_name) do |table_data|
table = Baza::Driver::Pg::Table.new(
driver: @db.driver,
data: table_data
)
next if table.native?
if tables_list
tables_list << table
else
yield table
end
end
end
tables_list
end
|
#use(&blk) ⇒ Object
58
59
60
61
|
# File 'lib/baza/driver/pg/database.rb', line 58
def use(&blk)
@db.with_database(name, &blk)
self
end
|