Visitar URL original
Separate index and constraint ASTs (breaking API change) by minleejae · Pull Request #2747 · JSQLParser/JSqlParser · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: simplify constraint inspection and index rendering
Signed-off-by: minleejae <mmj9808@gmail.com>
  • Loading branch information
minleejae committed Oct 2, 2026
commit 00a8400cc05496dc209db95a3ce3728259d27493
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,10 @@ public Index getIndex() {
* return OTHER: inspect their ColumnOption nodes for inline constraints.
*/
public ConstraintKind getConstraintKind() {
if (operation == null) {
return ConstraintKind.OTHER;
if (hasExplicitConstraintHeader()) {
return ConstraintKind.fromType(getConstraintType());
}
if (isRenameOperation() && !(constraintType != null && constraintSymbol != null
&& (operation == AlterOperation.ADD || operation == AlterOperation.ALTER))) {
if (operation == null || isRenameOperation()) {
return ConstraintKind.OTHER;
}
switch (operation) {
Expand All @@ -632,36 +631,43 @@ public ConstraintKind getConstraintKind() {
case DROP_CHECK:
return ConstraintKind.CHECK;
case ADD:
case ALTER:
if (constraintType != null && constraintSymbol != null) {
return ConstraintKind.fromType(getConstraintType());
}
if (operation != AlterOperation.ADD || columnName != null
|| colDataTypeList != null || constraintName != null || oldIndex != null
|| commentText != null || columnSetNotNullList != null
|| columnDropNotNullList != null
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
return ConstraintKind.OTHER;
}
if (constraint != null) {
return constraint.getKind() != null ? constraint.getKind()
: ConstraintKind.OTHER;
}
if (index != null) {
return ConstraintKind.OTHER;
}
if (pkColumns != null) {
return ConstraintKind.PRIMARY_KEY;
}
if (ukColumns != null) {
return ConstraintKind.UNIQUE;
}
return fkColumns != null ? ConstraintKind.FOREIGN_KEY : ConstraintKind.OTHER;
return getAddedConstraintKind();
default:
return ConstraintKind.OTHER;
}
}

private boolean hasExplicitConstraintHeader() {
return constraintType != null && constraintSymbol != null
&& (operation == AlterOperation.ADD || operation == AlterOperation.ALTER);
}

private ConstraintKind getAddedConstraintKind() {
if (hasColumnOrNamedTarget()) {
return ConstraintKind.OTHER;
}
if (constraint != null) {
return constraint.getKind() != null ? constraint.getKind() : ConstraintKind.OTHER;
}
if (index != null) {
return ConstraintKind.OTHER;
}
if (pkColumns != null) {
return ConstraintKind.PRIMARY_KEY;
}
if (ukColumns != null) {
return ConstraintKind.UNIQUE;
}
return fkColumns != null ? ConstraintKind.FOREIGN_KEY : ConstraintKind.OTHER;
}

private boolean hasColumnOrNamedTarget() {
return columnName != null || colDataTypeList != null || constraintName != null
|| oldIndex != null || commentText != null || columnSetNotNullList != null
|| columnDropNotNullList != null
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty();
}

/**
* Whether the active ADD body uses the complete table-definition renderer. Legacy constraint
* headers and specialized action bodies use their own renderers. Stored definitions remain
Expand Down Expand Up @@ -967,8 +973,7 @@ public final String toString() {
protected void appendBody(StringBuilder b) {
if (operation == AlterOperation.UNSPECIFIC) {
b.append(optionalSpecifier);
} else if (constraintType != null && constraintSymbol != null
&& (operation == AlterOperation.ALTER || operation == AlterOperation.ADD)) {
} else if (hasExplicitConstraintHeader()) {
toStringConstraintAlter(b);
} else if (operation == AlterOperation.ALTER
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class RelationAlterAction extends AlterExpression {
private boolean statisticsDefault;
private Expression defaultExpression;
private Expression generationExpression;
private Table relation;
private boolean noInherit;
private boolean noDependency;
private List<IndexOption> options;
private List<String> resetOptions;

public Expression getGenerationExpression() {
return generationExpression;
Expand All @@ -40,12 +45,6 @@ public void setGenerationExpression(Expression expression) {
generationExpression = expression;
}

private Table relation;
private boolean noInherit;
private boolean noDependency;
private List<IndexOption> options;
private List<String> resetOptions;

public enum Kind {
RENAME, RENAME_COLUMN, OWNER, SET_SCHEMA, SET_TABLESPACE, SET_ACCESS_METHOD, SET_OPTIONS, RESET_OPTIONS, ALTER_COLUMN, ATTACH_PARTITION, DEPENDS_ON_EXTENSION, VALIDATE_CONSTRAINT, INHERIT, ALTER_CONSTRAINT_INHERIT, REPLICA_IDENTITY, CLUSTER_ON, SET_WITHOUT_CLUSTER, SET_WITHOUT_OIDS, SET_LOGGED, SET_UNLOGGED, OF, NOT_OF, TRIGGER_STATE, RULE_STATE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,9 @@
public class CreateTable implements Statement {

private boolean tableOptionsAfterPartition;

public boolean isTableOptionsAfterPartition() {
return tableOptionsAfterPartition;
}

public void setTableOptionsAfterPartition(boolean tableOptionsAfterPartition) {
this.tableOptionsAfterPartition = tableOptionsAfterPartition;
}

private net.sf.jsqlparser.statement.execute.Execute execute;

/** Prepared statement source of PostgreSQL CREATE TABLE AS EXECUTE. */
public net.sf.jsqlparser.statement.execute.Execute getExecute() {
return execute;
}

public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {
this.execute = execute;
select = null;
selectParenthesis = false;
}

private Table table;
private boolean unlogged = false;

/** The structured SERVER clause, also present in the shared table-options list. */
public ForeignTableOptions getForeignTableOptions() {
if (tableOptions != null) {
for (TableOption option : tableOptions) {
if (option.getForeignTableOptions() != null) {
return option.getForeignTableOptions();
}
}
}
return null;
}

private List<String> createOptionsStrings;
private List<String> tableOptionsStrings;
private List<TableOption> tableOptions;
Expand Down Expand Up @@ -94,6 +60,37 @@ public enum DuplicateHandling {
IGNORE, REPLACE
}

public boolean isTableOptionsAfterPartition() {
return tableOptionsAfterPartition;
}

public void setTableOptionsAfterPartition(boolean tableOptionsAfterPartition) {
this.tableOptionsAfterPartition = tableOptionsAfterPartition;
}

/** Prepared statement source of PostgreSQL CREATE TABLE AS EXECUTE. */
public net.sf.jsqlparser.statement.execute.Execute getExecute() {
return execute;
}

public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {
this.execute = execute;
select = null;
selectParenthesis = false;
}

/** The structured SERVER clause, also present in the shared table-options list. */
public ForeignTableOptions getForeignTableOptions() {
if (tableOptions != null) {
for (TableOption option : tableOptions) {
if (option.getForeignTableOptions() != null) {
return option.getForeignTableOptions();
}
}
}
return null;
}

/** MySQL's duplicate-key handling when creating a table from a query; null if omitted. */
public DuplicateHandling getDuplicateHandling() {
return duplicateHandling;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class ForeignKeyConstraint extends NamedConstraint implements KeyColumnSource {
private List<KeyElement> columns;
private String indexName;
private ForeignKeyReference reference = new ForeignKeyReference();

public ForeignKeyConstraint() {
setType("FOREIGN KEY");
Expand Down Expand Up @@ -49,8 +50,6 @@ public void setIndexName(String value) {
indexName = value;
}

private ForeignKeyReference reference = new ForeignKeyReference();

/** Returns the mutable reference shared by the structured and legacy accessors. */
public ForeignKeyReference getReference() {
return reference;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/net/sf/jsqlparser/statement/create/table/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,15 @@ public String toString() {
}

public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter) {
String spec = PlainSelect.getStringList(getIndexSpec(), false, false);
appendDeclarationTo(sql);
appendColumnsAndSpecTo(sql, expressionPrinter);
appendIndexOptionsTo(sql, expressionPrinter);
if (tailParameters != null && !tailParameters.isEmpty()) {
sql.append(' ').append(PlainSelect.getStringList(tailParameters, false, false));
}
}

private void appendDeclarationTo(StringBuilder sql) {
String keyword = indexKeyword != null && (type == null
|| !type.toUpperCase(java.util.Locale.ROOT)
.endsWith(indexKeyword.toUpperCase(java.util.Locale.ROOT)))
Expand All @@ -225,6 +233,11 @@ public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter)
sql.append(" USING ").append(getUsing());
}
sql.append(nullsDistinctClause()).append(clusteringClause());
}

private void appendColumnsAndSpecTo(StringBuilder sql,
Consumer<Expression> expressionPrinter) {
String spec = PlainSelect.getStringList(getIndexSpec(), false, false);
boolean hasColumns = columns != null && !columns.isEmpty();
if (hasColumns) {
sql.append(' ');
Expand All @@ -233,9 +246,5 @@ public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter)
if (!spec.isEmpty()) {
sql.append(hasColumns ? " " : " ").append(spec);
}
appendIndexOptionsTo(sql, expressionPrinter);
if (tailParameters != null && !tailParameters.isEmpty()) {
sql.append(' ').append(PlainSelect.getStringList(tailParameters, false, false));
}
}
}
Loading