Skip to content

Separate index and constraint ASTs (breaking API change) - #2747

Merged
manticore-projects merged 5 commits into
JSQLParser:masterfrom
minleejae:refactor/constraint-inspection-20261002
Oct 11, 2026
Merged

manticore-projects merged 5 commits into
JSQLParser:masterfrom
minleejae:refactor/constraint-inspection-20261002

Conversation

@minleejae

@minleejae minleejae commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Make indexes and logical constraints distinct AST concepts so callers can inspect and edit CREATE TABLE and ALTER TABLE definitions without treating every constraint as an index. Preserve declared SQL and share only the components the definitions actually have in common.

Current design (As-is)

Before this change, table constraints inherit Index. CHECK and DEFAULT therefore expose irrelevant key/index options, and getIndexes() mixes physical index declarations with PRIMARY KEY, UNIQUE, FOREIGN KEY and other constraints. Inline constraints and ALTER payloads require different inspection paths, while some editing helpers retain duplicate state.

Target design (To-be)

This PR implements the following refactoring:

  • Separate Index from an independent abstract NamedConstraint base with ConstraintKind. PRIMARY KEY/UNIQUE use KeyConstraint; ForeignKeyIndex becomes ForeignKeyConstraint. Index.Kind contains only index classifications.
  • Share KeyElement, IndexOption and IndexOptions through composition where appropriate. CHECK, DEFAULT and NOT NULL have no irrelevant key/index fields. ConstraintUsingIndex references an existing index without manufacturing a backing index or key list.
  • Give CREATE TABLE separate mutable getIndexes() and getTableConstraints() views. getConstraints() combines inline/table declarations in source order and retains editable source nodes and owning columns.
  • Give ALTER separate constraint/index accessors and canonical key definitions for PK/UK editing. Constraint renames expose source/target names. Named DROP CONSTRAINT does not guess its target's kind. Ordinary column DEFAULT remains an option; explicit DefaultConstraint nodes remain constraints.
  • Adapt visitors, validators and both SQL renderers. Preserve active ALTER payload precedence, builder declarations and permissive index suffix order. MySQL table UNIQUE KEY/INDEX remain constraints; standalone CREATE UNIQUE INDEX remains an index statement.

This is a breaking API change. usage.rst documents replacement types/accessors, extracted shared components and CREATE/ALTER migration examples. The refactoring does not infer catalogue objects or implicit supporting indexes.

Validation

  • Updated against master 40ed289b; the latest merge commit is 9be36363. GitHub CI passes for this revision: Codacy, grammar conflicts, and Gradle/Maven checks on Linux, macOS and Windows.
  • Fresh Java 17 Gradle check passes: 9,913 tests, 9,888 passed, 25 skipped, zero failures/errors. Grammar, Checkstyle, PMD, SpotBugs, Spotless and JaCoCo coverage checks pass using cached dependencies and an upstream/master Spotless baseline.
  • The merged build reparses 882 DDL fixtures previously executed on MySQL 8.4.11/PostgreSQL 18.6. All 875 supported cases retain identical AST inspection and toString/deparser output; seven existing parse gaps remain. The only failure-message changes are generated grammar source locations. No fresh live DB replay is claimed.
  • Upstream regression tests now exercise the extracted index-option types and dedicated constraint rename APIs. Column-action tests preserve the shared renderer's explicit operation changes while checking that stale constraint fields do not change classification.

Signed-off-by: minleejae <mmj9808@gmail.com>
Signed-off-by: minleejae <mmj9808@gmail.com>
Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae minleejae changed the title Expose unified constraint inspection for CREATE and ALTER Separate index and constraint ASTs (breaking API change) Oct 2, 2026
Signed-off-by: minleejae <mmj9808@gmail.com>
@manticore-projects

Copy link
Copy Markdown
Contributor

Please resolve conflicts.

Preserve the merged canonical index options, foreign-key state, and shared ALTER rendering alongside the separate constraint AST. Migrate new upstream tests to the constraint API and verify stale-field classification with operation-aware column rendering.

Signed-off-by: minleejae <mmj9808@gmail.com>
@manticore-projects
manticore-projects merged commit 8d12a5d into JSQLParser:master Oct 11, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you!

minleejae added a commit to minleejae/JSqlParser that referenced this pull request Oct 11, 2026
Delegate legacy FK accessors to the structured constraint, remove copied grammar state and the fallback renderer, and validate the current reference. Preserve quoted name parts and protect existing non-FK definitions.

This follow-up is based on PR JSQLParser#2747 at 9be3636.

Signed-off-by: minleejae <mmj9808@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants