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_s, #to_yaml, #uid

Constructor Details

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

Returns a new instance of Table.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/pg_meta/meta.rb', line 238

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



220
221
222
# File 'lib/pg_meta/meta.rb', line 220

def check_constraints
  @check_constraints
end

#columnsObject (readonly)

Hash of columns



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

def columns
  @columns
end

#constraintsObject (readonly)

Hash of all constraints



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

def constraints
  @constraints
end

#primary_key_columnsObject (readonly)

List of primary key columns

Note: Assigned by PrimaryKeyConstraint#initialize



205
206
207
# File 'lib/pg_meta/meta.rb', line 205

def primary_key_columns
  @primary_key_columns
end

#primary_key_constraintsObject (readonly)

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



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

def primary_key_constraints
  @primary_key_constraints
end

#referential_constraintsObject (readonly)

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



224
225
226
# File 'lib/pg_meta/meta.rb', line 224

def referential_constraints
  @referential_constraints
end

#triggersObject (readonly)

Hash of triggers. Maps from trigger name to Trigger object



227
228
229
# File 'lib/pg_meta/meta.rb', line 227

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?



216
217
218
# File 'lib/pg_meta/meta.rb', line 216

def unique_constraints
  @unique_constraints
end

Instance Method Details

#[](name) ⇒ Object

Lookup column by name



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

def [](name) = @columns[name]

#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



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

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



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

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

#insertable?Boolean

True if the table/view is insertable

Returns:

  • (Boolean)


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

def insertable?() @is_insertable end

#materialized?Boolean

True iff table is a materialized view

Returns:

  • (Boolean)


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

def materialized?() false end

#primary_key_columnObject

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



190
191
192
193
194
195
196
197
# File 'lib/pg_meta/meta.rb', line 190

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)


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

def table?() true end

#to_hObject



256
257
258
259
260
261
# File 'lib/pg_meta/meta.rb', line 256

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)


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

def typed?() @is_typed end

#view?Boolean

True iff table is a view

Returns:

  • (Boolean)


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

def view?() !table? end