Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint types seems to be missing #1

Open
3 tasks
pyramation opened this issue Mar 30, 2024 · 0 comments
Open
3 tasks

Constraint types seems to be missing #1

pyramation opened this issue Mar 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@pyramation
Copy link
Contributor

pyramation commented Mar 30, 2024

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(' ');
  }
@pyramation pyramation changed the title ConstraintStmt seems to be missing Constraint types seems to be missing Mar 30, 2024
@pyramation pyramation added the bug Something isn't working label Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant