Class: PgMeta::Column

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

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(table, name, ordinal, type, element_type, dimensions, default, is_identity, is_generated, is_nullable, is_updatable) ⇒ Column

Returns a new instance of Column.



372
373
374
375
376
377
378
379
380
381
# File 'lib/pg_meta/meta.rb', line 372

def initialize(
    table, name, ordinal, type, element_type, dimensions, default, 
    is_identity, is_generated, is_nullable, is_updatable)
  super(table, name)
  @type, @element_type, @dimensions, @ordinal, @default, @is_identity, 
      @is_generated, @is_nullable, @is_updatable =
        type, element_type, dimensions, ordinal, default, is_identity, 
        is_generated, is_nullable, is_updatable
  table.columns[name] = self
end

Instance Attribute Details

#defaultObject (readonly)

Default value



315
316
317
# File 'lib/pg_meta/meta.rb', line 315

def default
  @default
end

#dimensionsObject (readonly)

Number of dimensions if an array, 0 if not



312
313
314
# File 'lib/pg_meta/meta.rb', line 312

def dimensions
  @dimensions
end

#element_typeObject (readonly)

Element type if type is an array and nil otherwise



309
310
311
# File 'lib/pg_meta/meta.rb', line 309

def element_type
  @element_type
end

#ordinalObject (readonly)

Ordinal number of the column



303
304
305
# File 'lib/pg_meta/meta.rb', line 303

def ordinal
  @ordinal
end

#typeObject (readonly)

Type of the column



306
307
308
# File 'lib/pg_meta/meta.rb', line 306

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object

Compare columns by table and ordinal. FIXME: Compare by schema too (and what’s with the ‘super’?)



390
391
392
393
394
395
396
# File 'lib/pg_meta/meta.rb', line 390

def <=>(other)
  if other.is_a?(Column) && table == other.table
    ordinal <=> other.ordinal
  else
    super
  end
end

#array?Boolean

True if column is an array

Returns:

  • (Boolean)


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

def array?() !@element_type.nil? end

#generated?Boolean

True if column is auto generated

Returns:

  • (Boolean)


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

def generated?() @is_generated end

#identity?Boolean

True if column is an identity column

Returns:

  • (Boolean)


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

def identity?() @is_identity end

#kind?Boolean

True if column is a single-column reference to another record and is of type varchar or text

Returns:

  • (Boolean)


356
357
358
359
360
361
# File 'lib/pg_meta/meta.rb', line 356

def kind?() 
  @kind ||= 
      references.size == 1 && 
      references.first.referencing_columns.size == 1 &&
      !references.first.referenced_columns.first.primary_key?
end

#nullable?Boolean

True if column is nullable

Returns:

  • (Boolean)


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

def nullable?() @is_nullable end

#primary_key?Boolean

True if column is the single primary key of the table and false otherwise. Always nil for tables with multiple primary keys

Returns:

  • (Boolean)


340
341
342
343
# File 'lib/pg_meta/meta.rb', line 340

def primary_key?()
  return nil if table.primary_key_columns.size != 1
  table.table? && self == table.primary_key_column || false
end

#primary_key_column?Boolean

True is column is (part of) the primary key of the table

Returns:

  • (Boolean)


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

def primary_key_column?() table.table? && table.primary_key_columns.include?(self) end

#reference?Boolean

True if column is referencing other records. This includes columns in multi-column references

Returns:

  • (Boolean)


350
351
352
# File 'lib/pg_meta/meta.rb', line 350

def reference?()
  @reference ||= !references.empty?
end

#referencesObject

List of referential constraints that involve this column



364
365
366
367
368
369
370
# File 'lib/pg_meta/meta.rb', line 364

def references
  @references ||= begin
    table.referential_constraints.values.select { |constraint|
      constraint.referencing_columns.include?(self)
    }
  end
end

#to_hObject



383
384
385
386
387
# File 'lib/pg_meta/meta.rb', line 383

def to_h()
  attrs_to_h(
      :name, :ordinal, :type, :element_type, :dimensions, :default, :identity?, :generated?,
      :nullable?, :updatable?, :primary_key?, :reference?, :kind?)
end

#unique?Boolean

True if column is unique (not that this information is not stored in the Column but in a table constraint)

Returns:

  • (Boolean)


334
335
336
# File 'lib/pg_meta/meta.rb', line 334

def unique?()
  table.unique_constraints.values.any? { |constraint| constraint.columns == [self] }
end

#updatable?Boolean

True if column is updatable

Returns:

  • (Boolean)


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

def updatable?() @is_updatable end