Skip to content

Commit 47316e0

Browse files
committed
Merge upstream master and preserve table override lookahead
Signed-off-by: minleejae <mmj9808@gmail.com>
2 parents c958e93 + dac8159 commit 47316e0

16 files changed

Lines changed: 981 additions & 78 deletions

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,61 @@
99
*/
1010
package net.sf.jsqlparser.statement.alter;
1111

12+
import net.sf.jsqlparser.statement.create.table.TableOption;
13+
1214
/**
1315
* Internal subclass for table-level option operations within ALTER TABLE. Handles ENGINE,
1416
* ALGORITHM, LOCK, KEY_BLOCK_SIZE, COMMENT, ENCRYPTION, AUTO_INCREMENT (SET_TABLE_OPTION),
1517
* DISCARD/IMPORT TABLESPACE, DISABLE/ENABLE KEYS.
1618
*/
1719
public class AlterExpressionTableOption extends AlterExpression {
1820

21+
private TableOption structuredTableOption;
22+
23+
public TableOption getStructuredTableOption() {
24+
return structuredTableOption;
25+
}
26+
27+
/** Uses the same option model as CREATE TABLE, with legacy getters as live projections. */
28+
public void setStructuredTableOption(TableOption option) {
29+
structuredTableOption = option;
30+
setOperation(AlterOperation.SET_TABLE_OPTION);
31+
super.setTableOption(null);
32+
super.setUseEqual(false);
33+
}
34+
35+
@Override
36+
public String getTableOption() {
37+
return structuredTableOption == null ? super.getTableOption()
38+
: structuredTableOption.toString();
39+
}
40+
41+
@Override
42+
public void setTableOption(String option) {
43+
structuredTableOption = null;
44+
super.setTableOption(option);
45+
}
46+
47+
@Override
48+
public boolean getUseEqual() {
49+
return structuredTableOption == null ? super.getUseEqual()
50+
: structuredTableOption.isUseEquals();
51+
}
52+
53+
@Override
54+
public void setUseEqual(boolean useEqual) {
55+
if (structuredTableOption != null) {
56+
structuredTableOption.setUseEquals(useEqual);
57+
}
58+
super.setUseEqual(useEqual);
59+
}
60+
1961
@Override
2062
protected void appendBody(StringBuilder b) {
2163
switch (getOperation()) {
64+
case SET_TABLE_OPTION:
65+
b.append(getTableOption());
66+
break;
2267
case COMMENT:
2368
b.append("COMMENT ");
2469
b.append(getCommentText());

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public class CheckConstraint extends NamedConstraint {
2222

2323
private Expression expression;
2424

25-
private Boolean enforced;
26-
2725
private boolean noInherit;
2826

2927
public CheckConstraint() {
@@ -61,11 +59,19 @@ public CheckConstraint withNoInherit(boolean noInherit) {
6159
}
6260

6361
public Boolean getEnforced() {
64-
return enforced;
62+
return getConstraintAttributes() == null ? null : getConstraintAttributes().getEnforced();
6563
}
6664

6765
public void setEnforced(Boolean enforced) {
68-
this.enforced = enforced;
66+
ConstraintAttributes attributes = getConstraintAttributes();
67+
if (attributes == null) {
68+
if (enforced == null) {
69+
return;
70+
}
71+
attributes = new ConstraintAttributes();
72+
setConstraintAttributes(attributes);
73+
}
74+
attributes.setEnforced(enforced);
6975
}
7076

7177
@Override
@@ -81,9 +87,6 @@ public void appendTo(StringBuilder b, Consumer<Expression> expressionPrinter) {
8187
if (noInherit) {
8288
b.append(" NO INHERIT");
8389
}
84-
if (enforced != null) {
85-
b.append(enforced ? " ENFORCED" : " NOT ENFORCED");
86-
}
8790
appendConstraintSuffixTo(b);
8891
appendConstraintAttributesTo(b);
8992
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public enum Initially {
2020
private Boolean deferrable;
2121
private Initially initially;
2222
private boolean notValid;
23+
private Boolean enforced;
2324

2425
public Boolean getDeferrable() {
2526
return deferrable;
@@ -45,13 +46,25 @@ public void setNotValid(boolean notValid) {
4546
this.notValid = notValid;
4647
}
4748

49+
/** Null preserves an omitted ENFORCED clause. */
50+
public Boolean getEnforced() {
51+
return enforced;
52+
}
53+
54+
public void setEnforced(Boolean enforced) {
55+
this.enforced = enforced;
56+
}
57+
4858
public void appendTo(StringBuilder sql) {
4959
if (deferrable != null) {
5060
sql.append(deferrable ? " DEFERRABLE" : " NOT DEFERRABLE");
5161
}
5262
if (initially != null) {
5363
sql.append(" INITIALLY ").append(initially);
5464
}
65+
if (enforced != null) {
66+
sql.append(enforced ? " ENFORCED" : " NOT ENFORCED");
67+
}
5568
if (notValid) {
5669
sql.append(" NOT VALID");
5770
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class CreateTable implements Statement {
3838
private DuplicateHandling duplicateHandling;
3939
private Table likeTable;
4040
private Table cloneTable;
41+
private List<Table> inherits;
4142
private ColDataType ofType;
4243
private boolean selectParenthesis;
4344
private boolean useAsKeyword = true;
@@ -347,6 +348,27 @@ public void setPartitionBound(PartitionBound partitionBound) {
347348
this.partitionBound = partitionBound;
348349
}
349350

351+
/** PostgreSQL parent tables, in declaration order; null if INHERITS is absent. */
352+
public List<Table> getInherits() {
353+
return inherits;
354+
}
355+
356+
public void setInherits(List<Table> inherits) {
357+
this.inherits = inherits;
358+
}
359+
360+
public CreateTable withInherits(List<Table> inherits) {
361+
setInherits(inherits);
362+
return this;
363+
}
364+
365+
/** Shared rendering of the structured parent references. */
366+
public void appendInheritanceTo(StringBuilder builder) {
367+
if (inherits != null) {
368+
builder.append(" INHERITS ").append(PlainSelect.getStringList(inherits, true, true));
369+
}
370+
}
371+
350372
public boolean isSelectParenthesis() {
351373
return selectParenthesis;
352374
}
@@ -369,6 +391,7 @@ public String toString() {
369391
StringBuilder b = new StringBuilder();
370392
appendCreateClause(b);
371393
appendColumnDefinitions(b);
394+
appendInheritanceTo(b);
372395
if (partitionBound != null) {
373396
b.append(" ").append(partitionBound);
374397
}

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import net.sf.jsqlparser.statement.ReferentialAction;
2222
import net.sf.jsqlparser.statement.ReferentialAction.Action;
2323
import net.sf.jsqlparser.statement.ReferentialAction.Type;
24-
import net.sf.jsqlparser.statement.select.PlainSelect;
2524

2625
/** The target and actions of a column- or table-level foreign-key reference. */
2726
public class ForeignKeyReference implements Serializable {
@@ -33,8 +32,35 @@ public enum MatchType {
3332
private Table table;
3433
private List<String> referencedColumnNames;
3534
private MatchType matchType;
35+
private boolean usingPeriod;
36+
private ConstraintAttributes constraintAttributes;
3637
private final Set<ReferentialAction> referentialActions = new LinkedHashSet<>(2);
3738

39+
/**
40+
* Attributes of a column REFERENCES clause; table constraints own their attributes on Index.
41+
*/
42+
public ConstraintAttributes getConstraintAttributes() {
43+
return constraintAttributes;
44+
}
45+
46+
public void setConstraintAttributes(ConstraintAttributes constraintAttributes) {
47+
this.constraintAttributes = constraintAttributes;
48+
}
49+
50+
/** Whether the final explicitly referenced column is prefixed with PERIOD. */
51+
public boolean isUsingPeriod() {
52+
return usingPeriod;
53+
}
54+
55+
public void setUsingPeriod(boolean usingPeriod) {
56+
this.usingPeriod = usingPeriod;
57+
}
58+
59+
public ForeignKeyReference withUsingPeriod(boolean usingPeriod) {
60+
setUsingPeriod(usingPeriod);
61+
return this;
62+
}
63+
3864
public Table getTable() {
3965
return table;
4066
}
@@ -124,12 +150,25 @@ public ForeignKeyReference addReferencedColumnNames(
124150
public String toString() {
125151
StringBuilder builder = new StringBuilder("REFERENCES ").append(table);
126152
if (referencedColumnNames != null) {
127-
builder.append(PlainSelect.getStringList(referencedColumnNames, true, true));
153+
builder.append('(');
154+
for (int i = 0; i < referencedColumnNames.size(); i++) {
155+
if (i > 0) {
156+
builder.append(", ");
157+
}
158+
if (usingPeriod && i == referencedColumnNames.size() - 1) {
159+
builder.append("PERIOD ");
160+
}
161+
builder.append(referencedColumnNames.get(i));
162+
}
163+
builder.append(')');
128164
}
129165
if (matchType != null) {
130166
builder.append(" MATCH ").append(matchType);
131167
}
132168
referentialActions.forEach(builder::append);
169+
if (constraintAttributes != null) {
170+
constraintAttributes.appendTo(builder);
171+
}
133172
return builder.toString();
134173
}
135174
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ public enum NullOrdering {
417417
private SortOrder sortOrder;
418418
private NullOrdering nullOrdering;
419419
private ExclusionOperator exclusionOperator;
420+
private boolean withoutOverlaps;
421+
private boolean period;
420422

421423
public String getExclusionOperator() {
422424
return exclusionOperator == null ? null : exclusionOperator.toString();
@@ -466,6 +468,34 @@ public ColumnParams(Expression expression, List<String> params) {
466468
this.expression = expression;
467469
}
468470

471+
/** Marks the final key of a PostgreSQL temporal PRIMARY KEY or UNIQUE constraint. */
472+
public boolean isWithoutOverlaps() {
473+
return withoutOverlaps;
474+
}
475+
476+
public void setWithoutOverlaps(boolean withoutOverlaps) {
477+
this.withoutOverlaps = withoutOverlaps;
478+
}
479+
480+
public ColumnParams withWithoutOverlaps(boolean withoutOverlaps) {
481+
setWithoutOverlaps(withoutOverlaps);
482+
return this;
483+
}
484+
485+
/** Marks the final referencing column of a temporal foreign key. */
486+
public boolean isPeriod() {
487+
return period;
488+
}
489+
490+
public void setPeriod(boolean period) {
491+
this.period = period;
492+
}
493+
494+
public ColumnParams withPeriod(boolean period) {
495+
setPeriod(period);
496+
return this;
497+
}
498+
469499
public String getColumnName() {
470500
return expression != null ? expression.toString() : columnName;
471501
}
@@ -569,6 +599,9 @@ public String toString() {
569599

570600
/** Renders expression keys through the caller's expression printer. */
571601
public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
602+
if (period) {
603+
builder.append("PERIOD ");
604+
}
572605
if (expression != null) {
573606
if (expressionParenthesized) {
574607
builder.append('(');
@@ -588,6 +621,9 @@ public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrint
588621
if (exclusionOperator != null) {
589622
builder.append(" WITH ").append(exclusionOperator);
590623
}
624+
if (withoutOverlaps) {
625+
builder.append(" WITHOUT OVERLAPS");
626+
}
591627
}
592628

593629
private void appendParams(StringBuilder builder) {

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,30 @@
1313
import net.sf.jsqlparser.expression.Expression;
1414
import net.sf.jsqlparser.schema.Column;
1515

16-
/** PostgreSQL 18 table-level NOT NULL constraint, optionally named and non-inheritable. */
16+
/** PostgreSQL 18 column- or table-level NOT NULL constraint. */
1717
public class NotNullConstraint extends NamedConstraint {
1818
private Column column;
1919
private boolean noInherit;
20+
private boolean columnConstraint;
2021

2122
public NotNullConstraint() {
2223
setType("NOT NULL");
2324
}
2425

26+
/** Whether the target is implicit in the enclosing column definition. */
27+
public boolean isColumnConstraint() {
28+
return columnConstraint;
29+
}
30+
31+
public void setColumnConstraint(boolean columnConstraint) {
32+
this.columnConstraint = columnConstraint;
33+
}
34+
35+
public NotNullConstraint withColumnConstraint(boolean columnConstraint) {
36+
setColumnConstraint(columnConstraint);
37+
return this;
38+
}
39+
2540
public Column getColumn() {
2641
return column;
2742
}
@@ -61,12 +76,15 @@ public NotNullConstraint withConstraintAttributes(ConstraintAttributes attribute
6176

6277
@Override
6378
public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter) {
64-
if (column == null) {
79+
if (!columnConstraint && column == null) {
6580
throw new IllegalStateException("NOT NULL requires a target column");
6681
}
6782
appendConstraintPrefixTo(sql);
68-
sql.append("NOT NULL ");
69-
expressionPrinter.accept(column);
83+
sql.append("NOT NULL");
84+
if (!columnConstraint) {
85+
sql.append(' ');
86+
expressionPrinter.accept(column);
87+
}
7088
if (noInherit) {
7189
sql.append(" NO INHERIT");
7290
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class TableOption implements Serializable {
2222

2323
public enum Kind {
24-
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, OTHER
24+
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY, OTHER
2525
}
2626

2727
private Kind kind = Kind.OTHER;

‎src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
109109
}
110110
});
111111
}
112+
if (table.getInherits() != null) {
113+
table.getInherits().forEach(parent -> accept(parent, tables));
114+
}
112115
accept(table.getTrailingLikeTable(), tables);
113116
accept(table.getPartitionOf(), tables);
114117
visit(table.getPartitioning(), expressions);

‎src/main/java/net/sf/jsqlparser/util/deparser/CreateTableDeParser.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void deParse(CreateTable createTable) {
106106
builder.append(")");
107107
}
108108

109+
createTable.appendInheritanceTo(builder);
109110
if (createTable.getPartitionBound() != null) {
110111
builder.append(' ');
111112
createTable.getPartitionBound().appendTo(builder,

0 commit comments

Comments
 (0)