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



393
394
395
396
397
398
399
400
401
402
# File 'lib/pg_meta/meta.rb', line 393

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



336
337
338
# File 'lib/pg_meta/meta.rb', line 336

def default
  @default
end

#dimensionsObject (readonly)

Number of dimensions if an array, 0 if not



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

def dimensions
  @dimensions
end

#element_typeObject (readonly)

Element type if type is an array and nil otherwise



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

def element_type
  @element_type
end

#ordinalObject (readonly)

Ordinal number of the column



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

def ordinal
  @ordinal
end

#typeObject (readonly)

Type of the column



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

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’?)



411
412
413
414
415
416
417
# File 'lib/pg_meta/meta.rb', line 411

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)


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

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

#generated?Boolean

True if column is auto generated

Returns:

  • (Boolean)


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

def generated?() @is_generated end

#identity?Boolean

True if column is an identity column

Returns:

  • (Boolean)


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

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)


377
378
379
380
381
382
# File 'lib/pg_meta/meta.rb', line 377

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)


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

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)


361
362
363
364
# File 'lib/pg_meta/meta.rb', line 361

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)


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

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)


371
372
373
# File 'lib/pg_meta/meta.rb', line 371

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

#referencesObject

List of referential constraints that involve this column



385
386
387
388
389
390
391
# File 'lib/pg_meta/meta.rb', line 385

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

#sidObject



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

def sid()
  @sid ||= "#{table.name}.#{name}"
end

#to_hObject



404
405
406
407
408
# File 'lib/pg_meta/meta.rb', line 404

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)


355
356
357
# File 'lib/pg_meta/meta.rb', line 355

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

#updatable?Boolean

True if column is updatable

Returns:

  • (Boolean)


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

def updatable?() @is_updatable end