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, #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
#expression ⇒ Object (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
455 |
# File 'lib/pg_meta/meta.rb', line 455 def not_null?() !columns.empty? end |
#ruby_expression ⇒ Object
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_h ⇒ Object
469 |
# File 'lib/pg_meta/meta.rb', line 469 def to_h() attrs_to_h(:name, :kind, :expression) end |