Class: Baza::Driver::Pg::Table

Inherits:
Table
  • Object
show all
Defined in:
lib/baza/driver/pg/table.rb

Instance Attribute Summary collapse

Attributes inherited from Table

#db

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Table

#<=>, #create_columns, #insert, #inspect, #row, #rows, #rows_count, #to_param, #to_s, #upsert_duplicate_key

Methods included from Baza::DatabaseModelFunctionality

#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

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/baza/driver/pg/table.rb', line 2

def name
  @name
end

Class Method Details

.create_indexes(index_list, args = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/baza/driver/pg/table.rb', line 97

def self.create_indexes(index_list, args = {})
  db = args.fetch(:db)
  sqls = Baza::Driver::Pg::CreateIndexSqlCreator.new(db: db, indexes: index_list, create_args: args).sqls

  unless args[:return_sql]
    db.transaction do
      sqls.each do |sql|
        db.query(sql)
      end
    end
  end

  sqls if args[:return_sql]
end

Instance Method Details

#clone(newname, _args = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/baza/driver/pg/table.rb', line 136

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



50
51
52
53
54
# File 'lib/baza/driver/pg/table.rb', line 50

def column(name)
  column = columns(name: name).first
  raise Baza::Errors::ColumnNotFound unless column
  column
end

#columns(args = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/baza/driver/pg/table.rb', line 24

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



93
94
95
# File 'lib/baza/driver/pg/table.rb', line 93

def create_indexes(index_list, args = {})
  Baza::Driver::Pg::Table.create_indexes(index_list, args.merge(table_name: name, db: @db))
end

#database_nameObject



16
17
18
# File 'lib/baza/driver/pg/table.rb', line 16

def database_name
  @data.fetch(:table_catalog)
end

#dropObject



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

#index(name) ⇒ Object



87
88
89
90
91
# File 'lib/baza/driver/pg/table.rb', line 87

def index(name)
  index = indexes(name: name).first
  raise Baza::Errors::IndexNotFound unless index
  index
end

#indexes(args = {}) ⇒ Object



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
# File 'lib/baza/driver/pg/table.rb', line 61

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/baza/driver/pg/table.rb', line 20

def native?
  false
end

#optimizeObject



131
132
133
134
# File 'lib/baza/driver/pg/table.rb', line 131

def optimize
  @db.query("VACUUM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}")
  self
end

#reloadObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/baza/driver/pg/table.rb', line 118

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



112
113
114
115
116
# File 'lib/baza/driver/pg/table.rb', line 112

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

#truncateObject



56
57
58
59
# File 'lib/baza/driver/pg/table.rb', line 56

def truncate
  @db.query("TRUNCATE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} RESTART IDENTITY")
  self
end