Class: PgMeta::Column
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Default value.
-
#dimensions ⇒ Object
readonly
Number of dimensions if an array, 0 if not.
-
#element_type ⇒ Object
readonly
Element type if type is an array and nil otherwise.
-
#ordinal ⇒ Object
readonly
Ordinal number of the column.
-
#type ⇒ Object
readonly
Type of the column.
Attributes inherited from Node
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare columns by table and ordinal.
-
#array? ⇒ Boolean
True if column is an array.
-
#generated? ⇒ Boolean
True if column is auto generated.
-
#identity? ⇒ Boolean
True if column is an identity column.
-
#initialize(table, name, ordinal, type, element_type, dimensions, default, is_identity, is_generated, is_nullable, is_updatable) ⇒ Column
constructor
A new instance of Column.
-
#kind? ⇒ Boolean
True if column is a single-column reference to another record and is of type varchar or text.
-
#nullable? ⇒ Boolean
True if column is nullable.
-
#primary_key? ⇒ Boolean
True if column is the single primary key of the table and false otherwise.
-
#primary_key_column? ⇒ Boolean
True is column is (part of) the primary key of the table.
-
#reference? ⇒ Boolean
True if column is referencing other records.
-
#references ⇒ Object
List of referential constraints that involve this column.
- #to_h ⇒ Object
-
#unique? ⇒ Boolean
True if column is unique (not that this information is not stored in the Column but in a table constraint).
-
#updatable? ⇒ Boolean
True if column is updatable.
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
#default ⇒ Object (readonly)
Default value
315 316 317 |
# File 'lib/pg_meta/meta.rb', line 315 def default @default end |
#dimensions ⇒ Object (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_type ⇒ Object (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 |
#ordinal ⇒ Object (readonly)
Ordinal number of the column
303 304 305 |
# File 'lib/pg_meta/meta.rb', line 303 def ordinal @ordinal end |
#type ⇒ Object (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
330 |
# File 'lib/pg_meta/meta.rb', line 330 def array?() !@element_type.nil? end |
#generated? ⇒ Boolean
True if column is auto generated
321 |
# File 'lib/pg_meta/meta.rb', line 321 def generated?() @is_generated end |
#identity? ⇒ Boolean
True if column is an identity column
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
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
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
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
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
350 351 352 |
# File 'lib/pg_meta/meta.rb', line 350 def reference?() @reference ||= !references.empty? end |
#references ⇒ Object
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_h ⇒ Object
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)
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
327 |
# File 'lib/pg_meta/meta.rb', line 327 def updatable?() @is_updatable end |