Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Constraint types seems to be missing #1

@pyramation

Description

@pyramation

UPDATE: seems that we need to switch on Enum ConstrType, and create helper methods named based on something like <Type>_<Enum>, e.g. Constraint_CONSTR_FOREIGN or some case() method variation for better readability.

Seems that the polymorphic nature of Constraint via ConstrType has left those names out of the protobuf, for our own implementation:

https://github.com/pganalyze/libpg_query/blob/1ec38940e5c6f09a4c1d17a46d839a881c4f2db7/protobuf/pg_query.proto#L1735

https://github.com/pganalyze/libpg_query/blob/1ec38940e5c6f09a4c1d17a46d839a881c4f2db7/protobuf/pg_query.proto#L2895-L2912

Likely this means that likely we don't need to add them directly, but consider how naming conventions should work in general for these types of scenarios

  • ReferenceConstraint
  • ConstraintStmt
  • ExclusionConstraint

example of ConstraintStmt in pgsql-parser, first you'll see that Constraint is the actual node:

Constraint(node: Constraint, context = {}) {
    const output = [];

    if (node.contype === 'CONSTR_FOREIGN') {
      output.push(this.ReferenceConstraint(node, context));
    } else {
      output.push(this.ConstraintStmt(node, context));
    }
}

the others are technically our own made up names. ConstraintStmt happens to be a generic way to insert certain general onstraint utilties, and even supports other constraint types. Essentially, ReferenceConstraint, ConstraintStmt, ExclusionConstraint are helpers for Constraint:

  ['ConstraintStmt'](node) {
    const output = [];
    const constraint = getConstraintFromConstrType(node.contype);

    if (node.conname) {
      output.push('CONSTRAINT');
      output.push(node.conname);
      if (!node.pktable) {
        output.push(constraint);
      }
    } else if (node.contype === 'CONSTR_IDENTITY') {
      // IDENTITY
      output.push('GENERATED');
      if (node.generated_when == 'a') {
        output.push('ALWAYS AS');
      } else {
        output.push('BY DEFAULT AS');
      }
      output.push('IDENTITY');
      const options = unwrapList(node.options);
      if (options && options.length) {
        output.push('(');
        output.push(this.list(options, ' ', '', 'generated'));
        output.push(')');
      }
    } else if (node.contype === 'CONSTR_GENERATED') {
      output.push('GENERATED');
      if (node.generated_when == 'a') {
        output.push('ALWAYS AS');
      }
    } else {
      output.push(constraint);
    }
    return output.join(' ');
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions