Class: PgMeta::Table

Inherits:
Node
  • Object
show all
Defined in:
lib/pg_meta/meta.rb

Direct Known Subclasses

View

Instance Attribute Summary collapse

Attributes inherited from Node

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from Node

#dump, #dump_value, #guid, #inspect, #to_yaml, #uid

Constructor Details

#initialize(schema, name, is_insertable, is_typed) ⇒ Table

Returns a new instance of Table.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/pg_meta/meta.rb', line 221

def initialize(schema, name, is_insertable, is_typed)
  super(schema, name)
  @is_insertable = is_insertable
  @is_typed = is_typed
  @columns = {}
  @primary_key_column = :undefined
  @primary_key_columns = []
  @constraints = {}
  @primary_key_constraints = []
  @unique_constraints = {}
  @check_constraints = {}
  @referential_constraints = {}
  @triggers = {}
  @depending_tables_hash = {}
  @depending_views_hash = {}
  schema.tables[name] = self
end

Instance Attribute Details

#check_constraintsObject (readonly)

Hash of check constraints. Maps from constraint name to CheckConstraint object object



203
204
205
# File 'lib/pg_meta/meta.rb', line 203

def check_constraints
  @check_constraints
end

#columnsObject (readonly)

Hash of columns



173
174
175
# File 'lib/pg_meta/meta.rb', line 173

def columns
  @columns
end

#constraintsObject (readonly)

Hash of all constraints



191
192
193
# File 'lib/pg_meta/meta.rb', line 191

def constraints
  @constraints
end

#primary_key_columnsObject (readonly)

List of primary key columns

Note: Assigned by PrimaryKeyConstraint#initialize



188
189
190
# File 'lib/pg_meta/meta.rb', line 188

def primary_key_columns
  @primary_key_columns
end

#primary_key_constraintsObject (readonly)

List of primary key constraints (there is only one element)



194
195
196
# File 'lib/pg_meta/meta.rb', line 194

def primary_key_constraints
  @primary_key_constraints
end

#referential_constraintsObject (readonly)

Hash of referential constraints. Maps from constraint name to ReferentialConstraint object



207
208
209
# File 'lib/pg_meta/meta.rb', line 207

def referential_constraints
  @referential_constraints
end

#triggersObject (readonly)

Hash of triggers. Maps from trigger name to Trigger object



210
211
212
# File 'lib/pg_meta/meta.rb', line 210

def triggers
  @triggers
end

#unique_constraintsObject (readonly)

Hash of unique constraints. Maps from constraint name to UniqueConstraint object. Note that because constraint names are unpredictable, you’ll most often use +unqiue_constraints.values?



199
200
201
# File 'lib/pg_meta/meta.rb', line 199

def unique_constraints
  @unique_constraints
end

Instance Method Details

#depending_tablesObject

List of tables that directly or indirectly depends on this table. Note that the tables are sorted by name to make testing in rspec easier



214
# File 'lib/pg_meta/meta.rb', line 214

def depending_tables() @depending_tables ||= @depending_tables_hash.keys.sort_by(&:uid) end

#depending_viewsObject

List of views that directly or indirectly depends on this table. This is the opposite of View#defining_tables. Note that the tables are sorted by name to make testing in rspec easier



219
# File 'lib/pg_meta/meta.rb', line 219

def depending_views() @depending_views ||= @depending_views_hash.keys.sort_by(&:uid) end

#insertable?Boolean

True if the table/view is insertable

Returns:

  • (Boolean)


167
# File 'lib/pg_meta/meta.rb', line 167

def insertable?() @is_insertable end

#materialized?Boolean

True iff table is a materialized view

Returns:

  • (Boolean)


164
# File 'lib/pg_meta/meta.rb', line 164

def materialized?() false end

#primary_key_columnObject

The primary key column. nil if the table has multiple primary key columns



176
177
178
179
180
181
182
183
# File 'lib/pg_meta/meta.rb', line 176

def primary_key_column
  return @primary_key_column if @primary_key_column != :undefined
  if primary_key_columns.size == 1
    @primary_key_column = primary_key_columns.first
  else
    @primary_key_column = nil
  end
end

#table?Boolean

True iff table is a real table and not a view

Returns:

  • (Boolean)


158
# File 'lib/pg_meta/meta.rb', line 158

def table?() true end

#to_hObject



239
240
241
242
243
244
# File 'lib/pg_meta/meta.rb', line 239

def to_h
  attrs_to_h(
      :name, :table?, :view?, :materialized?, :insertable?, :typed?,
      :columns, :primary_key_columns, :constraints,
      :referential_constraints, :depending_tables, :depending_views, :triggers)
end

#typed?Boolean

True if the table/view is typed

Returns:

  • (Boolean)


170
# File 'lib/pg_meta/meta.rb', line 170

def typed?() @is_typed end

#view?Boolean

True iff table is a view

Returns:

  • (Boolean)


161
# File 'lib/pg_meta/meta.rb', line 161

def view?() !table? end