Class: PgMeta::CheckConstraint
- Inherits:
-
Constraint
- Object
- Node
- Constraint
- PgMeta::CheckConstraint
- Defined in:
- lib/pg_meta/meta.rb
Overview
Note that #columns is always empty for check constraints
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
SQL check expression.
Attributes inherited from Constraint
Attributes inherited from Node
Instance Method Summary collapse
-
#initialize(table, name, expression) ⇒ CheckConstraint
constructor
A new instance of CheckConstraint.
-
#not_null? ⇒ Boolean
True if this is a not-null check constraint.
-
#ruby_expression ⇒ Object
Half-baked SQL-to-ruby expression transpiler.
- #to_h ⇒ Object
Methods inherited from Constraint
Methods inherited from Node
#dump, #dump_value, #guid, #inspect, #sid, #to_s, #to_yaml, #uid
Constructor Details
#initialize(table, name, expression) ⇒ CheckConstraint
Returns a new instance of CheckConstraint.
478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/pg_meta/meta.rb', line 478 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
#expression ⇒ Object (readonly)
SQL check expression
465 466 467 |
# File 'lib/pg_meta/meta.rb', line 465 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
476 |
# File 'lib/pg_meta/meta.rb', line 476 def not_null?() !columns.empty? end |
#ruby_expression ⇒ Object
Half-baked SQL-to-ruby expression transpiler
468 469 470 |
# File 'lib/pg_meta/meta.rb', line 468 def ruby_expression # Very simple @ruby ||= sql.sub(/\((.*)\)/, "\\1").gsub(/\((\w+) IS NOT NULL\)/, "!\\1.nil?").gsub(/ OR /, " || ") end |
#to_h ⇒ Object
490 |
# File 'lib/pg_meta/meta.rb', line 490 def to_h() attrs_to_h(:name, :kind, :expression) end |