Skip to content

Commit 00a8400

Browse files
committed
refactor: simplify constraint inspection and index rendering
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 11ee3df commit 00a8400

5 files changed

Lines changed: 87 additions & 78 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,10 @@ public Index getIndex() {
614614
* return OTHER: inspect their ColumnOption nodes for inline constraints.
615615
*/
616616
public ConstraintKind getConstraintKind() {
617-
if (operation == null) {
618-
return ConstraintKind.OTHER;
617+
if (hasExplicitConstraintHeader()) {
618+
return ConstraintKind.fromType(getConstraintType());
619619
}
620-
if (isRenameOperation() && !(constraintType != null && constraintSymbol != null
621-
&& (operation == AlterOperation.ADD || operation == AlterOperation.ALTER))) {
620+
if (operation == null || isRenameOperation()) {
622621
return ConstraintKind.OTHER;
623622
}
624623
switch (operation) {
@@ -632,36 +631,43 @@ public ConstraintKind getConstraintKind() {
632631
case DROP_CHECK:
633632
return ConstraintKind.CHECK;
634633
case ADD:
635-
case ALTER:
636-
if (constraintType != null && constraintSymbol != null) {
637-
return ConstraintKind.fromType(getConstraintType());
638-
}
639-
if (operation != AlterOperation.ADD || columnName != null
640-
|| colDataTypeList != null || constraintName != null || oldIndex != null
641-
|| commentText != null || columnSetNotNullList != null
642-
|| columnDropNotNullList != null
643-
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
644-
return ConstraintKind.OTHER;
645-
}
646-
if (constraint != null) {
647-
return constraint.getKind() != null ? constraint.getKind()
648-
: ConstraintKind.OTHER;
649-
}
650-
if (index != null) {
651-
return ConstraintKind.OTHER;
652-
}
653-
if (pkColumns != null) {
654-
return ConstraintKind.PRIMARY_KEY;
655-
}
656-
if (ukColumns != null) {
657-
return ConstraintKind.UNIQUE;
658-
}
659-
return fkColumns != null ? ConstraintKind.FOREIGN_KEY : ConstraintKind.OTHER;
634+
return getAddedConstraintKind();
660635
default:
661636
return ConstraintKind.OTHER;
662637
}
663638
}
664639

640+
private boolean hasExplicitConstraintHeader() {
641+
return constraintType != null && constraintSymbol != null
642+
&& (operation == AlterOperation.ADD || operation == AlterOperation.ALTER);
643+
}
644+
645+
private ConstraintKind getAddedConstraintKind() {
646+
if (hasColumnOrNamedTarget()) {
647+
return ConstraintKind.OTHER;
648+
}
649+
if (constraint != null) {
650+
return constraint.getKind() != null ? constraint.getKind() : ConstraintKind.OTHER;
651+
}
652+
if (index != null) {
653+
return ConstraintKind.OTHER;
654+
}
655+
if (pkColumns != null) {
656+
return ConstraintKind.PRIMARY_KEY;
657+
}
658+
if (ukColumns != null) {
659+
return ConstraintKind.UNIQUE;
660+
}
661+
return fkColumns != null ? ConstraintKind.FOREIGN_KEY : ConstraintKind.OTHER;
662+
}
663+
664+
private boolean hasColumnOrNamedTarget() {
665+
return columnName != null || colDataTypeList != null || constraintName != null
666+
|| oldIndex != null || commentText != null || columnSetNotNullList != null
667+
|| columnDropNotNullList != null
668+
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty();
669+
}
670+
665671
/**
666672
* Whether the active ADD body uses the complete table-definition renderer. Legacy constraint
667673
* headers and specialized action bodies use their own renderers. Stored definitions remain
@@ -967,8 +973,7 @@ public final String toString() {
967973
protected void appendBody(StringBuilder b) {
968974
if (operation == AlterOperation.UNSPECIFIC) {
969975
b.append(optionalSpecifier);
970-
} else if (constraintType != null && constraintSymbol != null
971-
&& (operation == AlterOperation.ALTER || operation == AlterOperation.ADD)) {
976+
} else if (hasExplicitConstraintHeader()) {
972977
toStringConstraintAlter(b);
973978
} else if (operation == AlterOperation.ALTER
974979
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()

‎src/main/java/net/sf/jsqlparser/statement/alter/RelationAlterAction.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class RelationAlterAction extends AlterExpression {
3131
private boolean statisticsDefault;
3232
private Expression defaultExpression;
3333
private Expression generationExpression;
34+
private Table relation;
35+
private boolean noInherit;
36+
private boolean noDependency;
37+
private List<IndexOption> options;
38+
private List<String> resetOptions;
3439

3540
public Expression getGenerationExpression() {
3641
return generationExpression;
@@ -40,12 +45,6 @@ public void setGenerationExpression(Expression expression) {
4045
generationExpression = expression;
4146
}
4247

43-
private Table relation;
44-
private boolean noInherit;
45-
private boolean noDependency;
46-
private List<IndexOption> options;
47-
private List<String> resetOptions;
48-
4948
public enum Kind {
5049
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
5150
}

‎src/main/java/net/sf/jsqlparser/statement/create/table/CreateTable.java‎

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,9 @@
2626
public class CreateTable implements Statement {
2727

2828
private boolean tableOptionsAfterPartition;
29-
30-
public boolean isTableOptionsAfterPartition() {
31-
return tableOptionsAfterPartition;
32-
}
33-
34-
public void setTableOptionsAfterPartition(boolean tableOptionsAfterPartition) {
35-
this.tableOptionsAfterPartition = tableOptionsAfterPartition;
36-
}
37-
3829
private net.sf.jsqlparser.statement.execute.Execute execute;
39-
40-
/** Prepared statement source of PostgreSQL CREATE TABLE AS EXECUTE. */
41-
public net.sf.jsqlparser.statement.execute.Execute getExecute() {
42-
return execute;
43-
}
44-
45-
public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {
46-
this.execute = execute;
47-
select = null;
48-
selectParenthesis = false;
49-
}
50-
5130
private Table table;
5231
private boolean unlogged = false;
53-
54-
/** The structured SERVER clause, also present in the shared table-options list. */
55-
public ForeignTableOptions getForeignTableOptions() {
56-
if (tableOptions != null) {
57-
for (TableOption option : tableOptions) {
58-
if (option.getForeignTableOptions() != null) {
59-
return option.getForeignTableOptions();
60-
}
61-
}
62-
}
63-
return null;
64-
}
65-
6632
private List<String> createOptionsStrings;
6733
private List<String> tableOptionsStrings;
6834
private List<TableOption> tableOptions;
@@ -94,6 +60,37 @@ public enum DuplicateHandling {
9460
IGNORE, REPLACE
9561
}
9662

63+
public boolean isTableOptionsAfterPartition() {
64+
return tableOptionsAfterPartition;
65+
}
66+
67+
public void setTableOptionsAfterPartition(boolean tableOptionsAfterPartition) {
68+
this.tableOptionsAfterPartition = tableOptionsAfterPartition;
69+
}
70+
71+
/** Prepared statement source of PostgreSQL CREATE TABLE AS EXECUTE. */
72+
public net.sf.jsqlparser.statement.execute.Execute getExecute() {
73+
return execute;
74+
}
75+
76+
public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {
77+
this.execute = execute;
78+
select = null;
79+
selectParenthesis = false;
80+
}
81+
82+
/** The structured SERVER clause, also present in the shared table-options list. */
83+
public ForeignTableOptions getForeignTableOptions() {
84+
if (tableOptions != null) {
85+
for (TableOption option : tableOptions) {
86+
if (option.getForeignTableOptions() != null) {
87+
return option.getForeignTableOptions();
88+
}
89+
}
90+
}
91+
return null;
92+
}
93+
9794
/** MySQL's duplicate-key handling when creating a table from a query; null if omitted. */
9895
public DuplicateHandling getDuplicateHandling() {
9996
return duplicateHandling;

‎src/main/java/net/sf/jsqlparser/statement/create/table/ForeignKeyConstraint.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class ForeignKeyConstraint extends NamedConstraint implements KeyColumnSource {
2222
private List<KeyElement> columns;
2323
private String indexName;
24+
private ForeignKeyReference reference = new ForeignKeyReference();
2425

2526
public ForeignKeyConstraint() {
2627
setType("FOREIGN KEY");
@@ -49,8 +50,6 @@ public void setIndexName(String value) {
4950
indexName = value;
5051
}
5152

52-
private ForeignKeyReference reference = new ForeignKeyReference();
53-
5453
/** Returns the mutable reference shared by the structured and legacy accessors. */
5554
public ForeignKeyReference getReference() {
5655
return reference;

‎src/main/java/net/sf/jsqlparser/statement/create/table/Index.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ public String toString() {
211211
}
212212

213213
public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter) {
214-
String spec = PlainSelect.getStringList(getIndexSpec(), false, false);
214+
appendDeclarationTo(sql);
215+
appendColumnsAndSpecTo(sql, expressionPrinter);
216+
appendIndexOptionsTo(sql, expressionPrinter);
217+
if (tailParameters != null && !tailParameters.isEmpty()) {
218+
sql.append(' ').append(PlainSelect.getStringList(tailParameters, false, false));
219+
}
220+
}
221+
222+
private void appendDeclarationTo(StringBuilder sql) {
215223
String keyword = indexKeyword != null && (type == null
216224
|| !type.toUpperCase(java.util.Locale.ROOT)
217225
.endsWith(indexKeyword.toUpperCase(java.util.Locale.ROOT)))
@@ -225,6 +233,11 @@ public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter)
225233
sql.append(" USING ").append(getUsing());
226234
}
227235
sql.append(nullsDistinctClause()).append(clusteringClause());
236+
}
237+
238+
private void appendColumnsAndSpecTo(StringBuilder sql,
239+
Consumer<Expression> expressionPrinter) {
240+
String spec = PlainSelect.getStringList(getIndexSpec(), false, false);
228241
boolean hasColumns = columns != null && !columns.isEmpty();
229242
if (hasColumns) {
230243
sql.append(' ');
@@ -233,9 +246,5 @@ public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter)
233246
if (!spec.isEmpty()) {
234247
sql.append(hasColumns ? " " : " ").append(spec);
235248
}
236-
appendIndexOptionsTo(sql, expressionPrinter);
237-
if (tailParameters != null && !tailParameters.isEmpty()) {
238-
sql.append(' ').append(PlainSelect.getStringList(tailParameters, false, false));
239-
}
240249
}
241250
}

0 commit comments

Comments
 (0)