Class: Baza::Driver::Pg::Database

Inherits:
Baza::Database show all
Defined in:
lib/baza/driver/pg/database.rb

Constant Summary collapse

CREATE_ALLOWED_KEYS =
[:columns, :indexes, :temp, :return_sql].freeze

Instance Attribute Summary

Attributes inherited from Baza::Database

#db, #driver, #name, #name_was

Instance Method Summary collapse

Methods inherited from Baza::Database

#initialize, #table_exists?, #to_param

Methods included from Baza::DatabaseModelFunctionality

#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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/baza/driver/pg/database.rb', line 54

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

#dropObject



7
8
9
10
# File 'lib/baza/driver/pg/database.rb', line 7

def drop
  @db.query("DROP DATABASE #{@db.sep_database}#{@db.escape_database(name)}#{@db.sep_database}")
  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



12
13
14
15
16
# File 'lib/baza/driver/pg/database.rb', line 12

def table(table_name)
  table = tables(name: table_name).first
  raise Baza::Errors::TableNotFound unless table
  table
end

#tables(args = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/baza/driver/pg/database.rb', line 18

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



47
48
49
50
# File 'lib/baza/driver/pg/database.rb', line 47

def use(&blk)
  @db.with_database(name, &blk)
  self
end