Class: PgMeta::CheckConstraint

Inherits:
Constraint show all
Defined in:
lib/pg_meta/meta.rb

Overview

Note that #columns is always empty for check constraints

Instance Attribute Summary collapse

Attributes inherited from Constraint

#columns

Attributes inherited from Node

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from Constraint

#column, #kind

Methods inherited from Node

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

Constructor Details

#initialize(table, name, expression) ⇒ CheckConstraint

Returns a new instance of CheckConstraint.



457
458
459
460
461
462
463
464
465
466
467
# File 'lib/pg_meta/meta.rb', line 457

def initialize(table, name, expression)
  super(table, name, [])
  @expression = expression
  table.check_constraints[name] = self
  if @expression =~ /^\(\((\S+) IS NOT NULL\)\)$/
    columns = [table.columns[$1]]
  else
    columns = []
  end
  super(table, name, columns)
end

Instance Attribute Details

#expressionObject (readonly)

SQL check expression



444
445
446
# File 'lib/pg_meta/meta.rb', line 444

def expression
  @expression
end

Instance Method Details

#not_null?Boolean

True if this is a not-null check constraint. In that case, #columns will contain the column which is otherwise empty for CheckConstraint objects. Note that the column is not registered directly in Postgres meta tables and have to be parsed from the expression

Returns:

  • (Boolean)


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

def not_null?() !columns.empty? end

#ruby_expressionObject

Half-baked SQL-to-ruby expression transpiler



447
448
449
# File 'lib/pg_meta/meta.rb', line 447

def ruby_expression # Very simple
  @ruby ||= sql.sub(/\((.*)\)/, "\\1").gsub(/\((\w+) IS NOT NULL\)/, "!\\1.nil?").gsub(/ OR /, " || ")
end

#to_hObject



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

def to_h() attrs_to_h(:name, :kind, :expression) end