Skip to content

Commit 36ef289

Browse files
committed
Merge upstream master and preserve temporal foreign key attributes
Signed-off-by: minleejae <mmj9808@gmail.com>
2 parents 33f7635 + 63d65ed commit 36ef289

17 files changed

Lines changed: 1031 additions & 90 deletions

‎src/main/java/net/sf/jsqlparser/schema/Sequence.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public Sequence addParameters(Collection<? extends Parameter> parameters) {
187187
* The available parameters to a sequence
188188
*/
189189
public enum ParameterType {
190-
INCREMENT_BY, INCREMENT, START_WITH, START, RESTART_WITH, MAXVALUE, NOMAXVALUE, MINVALUE, NOMINVALUE, CYCLE, NOCYCLE, CACHE, NOCACHE, ORDER, NOORDER, KEEP, NOKEEP, SESSION, GLOBAL, NO_MINVALUE, NO_MAXVALUE, NO_CYCLE;
190+
INCREMENT_BY, INCREMENT, START_WITH, START, RESTART_WITH, MAXVALUE, NOMAXVALUE, MINVALUE, NOMINVALUE, CYCLE, NOCYCLE, CACHE, NOCACHE, ORDER, NOORDER, KEEP, NOKEEP, SESSION, GLOBAL, NO_MINVALUE, NO_MAXVALUE, NO_CYCLE, SEQUENCE_NAME;
191191

192192
public static ParameterType from(String type) {
193193
return Enum.valueOf(ParameterType.class, type.toUpperCase(Locale.ROOT));
@@ -201,6 +201,7 @@ public static class Parameter implements java.io.Serializable {
201201

202202
private final ParameterType option;
203203
private Long value;
204+
private Sequence sequenceName;
204205

205206
public Parameter(ParameterType option) {
206207
this.option = option;
@@ -214,6 +215,20 @@ public ParameterType getOption() {
214215
return option;
215216
}
216217

218+
/** The sequence named by an identity declaration's SEQUENCE NAME option. */
219+
public Sequence getSequenceName() {
220+
return sequenceName;
221+
}
222+
223+
public void setSequenceName(Sequence sequenceName) {
224+
this.sequenceName = sequenceName;
225+
}
226+
227+
public Parameter withSequenceName(Sequence sequenceName) {
228+
setSequenceName(sequenceName);
229+
return this;
230+
}
231+
217232
@Override
218233
public String toString() {
219234
return formatParameter();
@@ -225,6 +240,8 @@ public void setValue(Long value) {
225240

226241
public String formatParameter() {
227242
switch (option) {
243+
case SEQUENCE_NAME:
244+
return "SEQUENCE NAME " + sequenceName.getFullyQualifiedName();
228245
case INCREMENT_BY:
229246
return prefix("INCREMENT BY");
230247
case INCREMENT:

‎src/main/java/net/sf/jsqlparser/statement/ReferentialAction.java‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
import java.io.Serializable;
1313
import java.util.Locale;
14+
import java.util.List;
15+
import java.util.Objects;
16+
import net.sf.jsqlparser.statement.select.PlainSelect;
1417

1518
public class ReferentialAction implements Serializable {
1619

1720
private Type type;
1821
private Action action;
22+
private List<String> columnNames;
1923

2024
public ReferentialAction() {
2125
// default constructor
@@ -52,6 +56,20 @@ public ReferentialAction withAction(Action action) {
5256
return this;
5357
}
5458

59+
/** Columns affected by PostgreSQL ON DELETE SET NULL or SET DEFAULT; null means all columns. */
60+
public List<String> getColumnNames() {
61+
return columnNames;
62+
}
63+
64+
public void setColumnNames(List<String> columnNames) {
65+
this.columnNames = columnNames;
66+
}
67+
68+
public ReferentialAction withColumnNames(List<String> columnNames) {
69+
setColumnNames(columnNames);
70+
return this;
71+
}
72+
5573
@Override
5674
public int hashCode() {
5775
final int prime = 31;
@@ -64,7 +82,9 @@ public int hashCode() {
6482
@Override
6583
public String toString() {
6684
return " ON " + getType().name() + " " +
67-
getAction().getAction();
85+
getAction().getAction()
86+
+ (columnNames == null ? ""
87+
: " " + PlainSelect.getStringList(columnNames, true, true));
6888
}
6989

7090
@Override
@@ -79,12 +99,8 @@ public boolean equals(Object obj) {
7999
return false;
80100
}
81101
ReferentialAction other = (ReferentialAction) obj;
82-
// if (action != other.action) {
83-
// return false;
84-
// }
85-
// if (type != other.type) {
86-
// return false;
87-
return action == other.action && type == other.type;
102+
return action == other.action && type == other.type
103+
&& Objects.equals(columnNames, other.columnNames);
88104
}
89105

90106
public enum Type {

‎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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public class CreateTable implements Statement {
3535
private List<Index> indexes;
3636
private List<TableElement> tableElements;
3737
private Select select;
38+
private DuplicateHandling duplicateHandling;
3839
private Table likeTable;
3940
private Table cloneTable;
4041
private ColDataType ofType;
4142
private boolean selectParenthesis;
4243
private boolean useAsKeyword = true;
44+
private Boolean withData;
4345
private boolean ifNotExists = false;
4446
private boolean orReplace = false;
4547
private TablePartitioning partitioning;
@@ -50,6 +52,19 @@ public class CreateTable implements Statement {
5052

5153
private SpannerInterleaveIn interleaveIn = null;
5254

55+
public enum DuplicateHandling {
56+
IGNORE, REPLACE
57+
}
58+
59+
/** MySQL's duplicate-key handling when creating a table from a query; null if omitted. */
60+
public DuplicateHandling getDuplicateHandling() {
61+
return duplicateHandling;
62+
}
63+
64+
public void setDuplicateHandling(DuplicateHandling duplicateHandling) {
65+
this.duplicateHandling = duplicateHandling;
66+
}
67+
5368
@Override
5469
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
5570
return statementVisitor.visit(this, context);
@@ -204,9 +219,21 @@ public void setUseAsKeyword(boolean useAsKeyword) {
204219
this.useAsKeyword = useAsKeyword;
205220
}
206221

222+
/** Returns null for omission, true for WITH DATA, and false for WITH NO DATA. */
223+
public Boolean getWithData() {
224+
return withData;
225+
}
226+
227+
public void setWithData(Boolean withData) {
228+
this.withData = withData;
229+
}
230+
207231
public StringBuilder appendSelectTo(StringBuilder builder,
208232
java.util.function.Consumer<Select> selectRenderer) {
209233
if (select != null) {
234+
if (duplicateHandling != null) {
235+
builder.append(' ').append(duplicateHandling);
236+
}
210237
builder.append(useAsKeyword ? " AS " : " ");
211238
if (selectParenthesis) {
212239
builder.append("(");
@@ -215,6 +242,9 @@ public StringBuilder appendSelectTo(StringBuilder builder,
215242
if (selectParenthesis) {
216243
builder.append(")");
217244
}
245+
if (withData != null) {
246+
builder.append(withData ? " WITH DATA" : " WITH NO DATA");
247+
}
218248
}
219249
return builder;
220250
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ public enum MatchType {
3333
private List<String> referencedColumnNames;
3434
private MatchType matchType;
3535
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+
3850
/** Whether the final explicitly referenced column is prefixed with PERIOD. */
3951
public boolean isUsingPeriod() {
4052
return usingPeriod;
@@ -154,6 +166,9 @@ public String toString() {
154166
builder.append(" MATCH ").append(matchType);
155167
}
156168
referentialActions.forEach(builder::append);
169+
if (constraintAttributes != null) {
170+
constraintAttributes.appendTo(builder);
171+
}
157172
return builder.toString();
158173
}
159174
}

‎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;

0 commit comments

Comments
 (0)