Skip to content

Commit 5154a9f

Browse files
committed
Merge origin/master into fix/mysql-column-type-modifiers
2 parents 5ea41f4 + c919a0c commit 5154a9f

20 files changed

Lines changed: 1206 additions & 32 deletions

src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import net.sf.jsqlparser.parser.feature.Feature;
3131
import net.sf.jsqlparser.statement.Statement;
3232
import net.sf.jsqlparser.statement.Statements;
33+
import net.sf.jsqlparser.statement.create.table.ColDataType;
3334

3435
/**
3536
* Toolfunctions to start and use JSqlParser.
@@ -199,6 +200,61 @@ public static Expression parseExpression(String expression, boolean allowPartial
199200
});
200201
}
201202

203+
/**
204+
* Parses a column data type fragment. The complete input must represent the data type; trailing
205+
* tokens are rejected.
206+
*
207+
* @param columnDataType the column data type fragment to parse
208+
* @return the parsed column data type, or {@code null} for a null or empty input
209+
* @throws JSQLParserException when the input cannot be parsed completely
210+
* @see #parseColDataType(String, Consumer)
211+
*/
212+
public static ColDataType parseColDataType(String columnDataType) throws JSQLParserException {
213+
return parseColDataType(columnDataType, null);
214+
}
215+
216+
/**
217+
* Parses a column data type fragment while allowing the parser to be configured. The complete
218+
* input must represent the data type; trailing tokens are rejected.
219+
*
220+
* @param columnDataType the column data type fragment to parse
221+
* @param consumer parser configuration callback, or {@code null}
222+
* @return the parsed column data type, or {@code null} for a null or empty input
223+
* @throws JSQLParserException when the input cannot be parsed completely
224+
*/
225+
public static ColDataType parseColDataType(String columnDataType,
226+
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
227+
if (columnDataType == null || columnDataType.isEmpty()) {
228+
return null;
229+
}
230+
231+
try {
232+
return parseColDataType(columnDataType, false, consumer);
233+
} catch (JSQLParserException ex) {
234+
return parseColDataType(columnDataType, true, consumer);
235+
}
236+
}
237+
238+
private static ColDataType parseColDataType(String columnDataType, boolean allowComplexParsing,
239+
Consumer<CCJSqlParser> consumer) throws JSQLParserException {
240+
CCJSqlParser parser = newParser(columnDataType)
241+
.withAllowComplexParsing(allowComplexParsing);
242+
if (consumer != null) {
243+
consumer.accept(parser);
244+
}
245+
246+
try {
247+
ColDataType result = parser.ColDataType();
248+
if (parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
249+
throw new JSQLParserException(
250+
"could only parse partial column data type " + result);
251+
}
252+
return result;
253+
} catch (ParseException ex) {
254+
throw new JSQLParserException(columnDataType, ex);
255+
}
256+
}
257+
202258
@SuppressWarnings("PMD.CyclomaticComplexity")
203259
public static Expression parseExpression(String expressionStr, boolean allowPartialParse,
204260
Consumer<CCJSqlParser> consumer) throws JSQLParserException {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ protected boolean isPartitionOperation() {
830830
switch (operation) {
831831
case DISCARD_PARTITION:
832832
case IMPORT_PARTITION:
833+
case ATTACH_PARTITION:
834+
case DETACH_PARTITION:
833835
case TRUNCATE_PARTITION:
834836
case COALESCE_PARTITION:
835837
case REORGANIZE_PARTITION:

src/main/java/net/sf/jsqlparser/statement/alter/AlterExpressionPartition.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,75 @@
99
*/
1010
package net.sf.jsqlparser.statement.alter;
1111

12+
import net.sf.jsqlparser.schema.Table;
13+
import net.sf.jsqlparser.statement.create.table.PartitionBound;
14+
1215
/**
1316
* Internal subclass for partition maintenance operations within ALTER TABLE. Handles TRUNCATE,
1417
* COALESCE, REORGANIZE, EXCHANGE, ANALYZE, CHECK, OPTIMIZE, REBUILD, REPAIR PARTITION, PARTITION
1518
* BY, and REMOVE PARTITIONING.
1619
*/
1720
public class AlterExpressionPartition extends AlterExpression {
1821

22+
public enum DetachMode {
23+
CONCURRENTLY, FINALIZE
24+
}
25+
26+
private Table partitionTable;
27+
private PartitionBound partitionBound;
28+
private DetachMode detachMode;
29+
30+
public Table getPartitionTable() {
31+
return partitionTable;
32+
}
33+
34+
public void setPartitionTable(Table partitionTable) {
35+
this.partitionTable = partitionTable;
36+
}
37+
38+
public PartitionBound getPartitionBound() {
39+
return partitionBound;
40+
}
41+
42+
public void setPartitionBound(PartitionBound partitionBound) {
43+
this.partitionBound = partitionBound;
44+
}
45+
46+
public DetachMode getDetachMode() {
47+
return detachMode;
48+
}
49+
50+
public void setDetachMode(DetachMode detachMode) {
51+
this.detachMode = detachMode;
52+
}
53+
54+
public AlterExpressionPartition withPartitionTable(Table partitionTable) {
55+
setPartitionTable(partitionTable);
56+
return this;
57+
}
58+
59+
public AlterExpressionPartition withPartitionBound(PartitionBound partitionBound) {
60+
setPartitionBound(partitionBound);
61+
return this;
62+
}
63+
64+
public AlterExpressionPartition withDetachMode(DetachMode detachMode) {
65+
setDetachMode(detachMode);
66+
return this;
67+
}
68+
1969
@Override
2070
protected void appendBody(StringBuilder b) {
21-
toStringPartition(b);
71+
if (getOperation() == AlterOperation.ATTACH_PARTITION) {
72+
b.append("ATTACH PARTITION ").append(partitionTable).append(" ")
73+
.append(partitionBound);
74+
} else if (getOperation() == AlterOperation.DETACH_PARTITION) {
75+
b.append("DETACH PARTITION ").append(partitionTable);
76+
if (detachMode != null) {
77+
b.append(" ").append(detachMode);
78+
}
79+
} else {
80+
toStringPartition(b);
81+
}
2282
}
2383
}

src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Locale;
1313

1414
public enum AlterOperation {
15-
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, DISCARD_PARTITION, IMPORT_PARTITION, TRUNCATE_PARTITION, COALESCE_PARTITION, REORGANIZE_PARTITION, EXCHANGE_PARTITION, ANALYZE_PARTITION, CHECK_PARTITION, OPTIMIZE_PARTITION, REBUILD_PARTITION, REPAIR_PARTITION, REMOVE_PARTITIONING, PARTITION_BY, SET_TABLE_OPTION, ENGINE, FORCE, KEY_BLOCK_SIZE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS, ENABLE_ROW_LEVEL_SECURITY, DISABLE_ROW_LEVEL_SECURITY, FORCE_ROW_LEVEL_SECURITY, NO_FORCE_ROW_LEVEL_SECURITY;
15+
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, ATTACH_PARTITION, DETACH_PARTITION, DISCARD_PARTITION, IMPORT_PARTITION, TRUNCATE_PARTITION, COALESCE_PARTITION, REORGANIZE_PARTITION, EXCHANGE_PARTITION, ANALYZE_PARTITION, CHECK_PARTITION, OPTIMIZE_PARTITION, REBUILD_PARTITION, REPAIR_PARTITION, REMOVE_PARTITIONING, PARTITION_BY, SET_TABLE_OPTION, ENGINE, FORCE, KEY_BLOCK_SIZE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS, ENABLE_ROW_LEVEL_SECURITY, DISABLE_ROW_LEVEL_SECURITY, FORCE_ROW_LEVEL_SECURITY, NO_FORCE_ROW_LEVEL_SECURITY;
1616

1717
public static AlterOperation from(String operation) {
1818
return Enum.valueOf(AlterOperation.class, operation.toUpperCase(Locale.ROOT));

src/main/java/net/sf/jsqlparser/statement/create/index/CreateIndex.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import static java.util.stream.Collectors.joining;
1313

1414
import java.util.*;
15+
import net.sf.jsqlparser.expression.Expression;
1516
import net.sf.jsqlparser.schema.*;
1617
import net.sf.jsqlparser.statement.*;
1718
import net.sf.jsqlparser.statement.create.table.*;
19+
import net.sf.jsqlparser.statement.select.PlainSelect;
1820

1921
public class CreateIndex implements Statement {
2022

@@ -25,6 +27,11 @@ public class CreateIndex implements Statement {
2527
private boolean usingIfNotExists = false;
2628
private boolean concurrently;
2729
private boolean only;
30+
private List<String> includeColumns;
31+
private Boolean nullsDistinct;
32+
private List<Index.Option> storageParameters;
33+
private String tableSpace;
34+
private Expression where;
2835

2936
public boolean isIndexTypeBeforeOn() {
3037
return indexTypeBeforeOn;
@@ -59,6 +66,46 @@ public void setOnly(boolean only) {
5966
this.only = only;
6067
}
6168

69+
public List<String> getIncludeColumns() {
70+
return includeColumns;
71+
}
72+
73+
public void setIncludeColumns(List<String> includeColumns) {
74+
this.includeColumns = includeColumns;
75+
}
76+
77+
public Boolean getNullsDistinct() {
78+
return nullsDistinct;
79+
}
80+
81+
public void setNullsDistinct(Boolean nullsDistinct) {
82+
this.nullsDistinct = nullsDistinct;
83+
}
84+
85+
public List<Index.Option> getStorageParameters() {
86+
return storageParameters;
87+
}
88+
89+
public void setStorageParameters(List<Index.Option> storageParameters) {
90+
this.storageParameters = storageParameters;
91+
}
92+
93+
public String getTableSpace() {
94+
return tableSpace;
95+
}
96+
97+
public void setTableSpace(String tableSpace) {
98+
this.tableSpace = tableSpace;
99+
}
100+
101+
public Expression getWhere() {
102+
return where;
103+
}
104+
105+
public void setWhere(Expression where) {
106+
this.where = where;
107+
}
108+
62109
@Override
63110
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
64111
return statementVisitor.visit(this, context);
@@ -135,6 +182,8 @@ public String toString() {
135182

136183
buffer.append(")");
137184

185+
appendPostgreSqlTail(buffer);
186+
138187
if (tailParameters != null) {
139188
for (String param : tailParameters) {
140189
buffer.append(" ").append(param);
@@ -145,6 +194,25 @@ public String toString() {
145194
return buffer.toString();
146195
}
147196

197+
private void appendPostgreSqlTail(StringBuilder buffer) {
198+
if (includeColumns != null) {
199+
buffer.append(" INCLUDE (").append(String.join(", ", includeColumns)).append(")");
200+
}
201+
if (nullsDistinct != null) {
202+
buffer.append(" NULLS ").append(nullsDistinct ? "DISTINCT" : "NOT DISTINCT");
203+
}
204+
if (storageParameters != null) {
205+
buffer.append(" WITH ")
206+
.append(PlainSelect.getStringList(storageParameters, true, true));
207+
}
208+
if (tableSpace != null) {
209+
buffer.append(" TABLESPACE ").append(tableSpace);
210+
}
211+
if (where != null) {
212+
buffer.append(" WHERE ").append(where);
213+
}
214+
}
215+
148216
public CreateIndex withTable(Table table) {
149217
this.setTable(table);
150218
return this;
@@ -169,4 +237,29 @@ public CreateIndex withOnly(boolean only) {
169237
setOnly(only);
170238
return this;
171239
}
240+
241+
public CreateIndex withIncludeColumns(List<String> includeColumns) {
242+
setIncludeColumns(includeColumns);
243+
return this;
244+
}
245+
246+
public CreateIndex withNullsDistinct(Boolean nullsDistinct) {
247+
setNullsDistinct(nullsDistinct);
248+
return this;
249+
}
250+
251+
public CreateIndex withStorageParameters(List<Index.Option> storageParameters) {
252+
setStorageParameters(storageParameters);
253+
return this;
254+
}
255+
256+
public CreateIndex withTableSpace(String tableSpace) {
257+
setTableSpace(tableSpace);
258+
return this;
259+
}
260+
261+
public CreateIndex withWhere(Expression where) {
262+
setWhere(where);
263+
return this;
264+
}
172265
}

src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public enum Signedness {
3535
private List<Integer> arrayData = new ArrayList<Integer>();
3636
private Signedness signedness;
3737
private boolean zerofill;
38+
private Integer precision;
39+
private Integer scale;
3840

3941
public ColDataType() {
4042
// empty constructor
@@ -44,8 +46,10 @@ public ColDataType(String dataType, int precision, int scale) {
4446
this.dataType = dataType;
4547

4648
if (precision >= 0) {
49+
this.precision = precision;
4750
this.dataType += " (" + (precision == Integer.MAX_VALUE ? "MAX" : precision);
4851
if (scale >= 0) {
52+
this.scale = scale;
4953
this.dataType += ", " + scale;
5054
}
5155
this.dataType += ")";
@@ -116,6 +120,32 @@ public void setZerofill(boolean zerofill) {
116120
this.zerofill = zerofill;
117121
}
118122

123+
/**
124+
* The first numeric type parameter, e.g. {@code 255} for {@code VARCHAR(255)} or {@code 10} for
125+
* {@code DECIMAL(10, 2)}. {@code MAX} is reported as {@link Integer#MAX_VALUE}. Returns
126+
* {@code null} when the type carries no numeric parameters, e.g. {@code INT} or
127+
* {@code ENUM('a', 'b')}.
128+
*/
129+
public Integer getPrecision() {
130+
return precision;
131+
}
132+
133+
public void setPrecision(Integer precision) {
134+
this.precision = precision;
135+
}
136+
137+
/**
138+
* The second numeric type parameter, e.g. {@code 2} for {@code DECIMAL(10, 2)}. Returns
139+
* {@code null} when absent.
140+
*/
141+
public Integer getScale() {
142+
return scale;
143+
}
144+
145+
public void setScale(Integer scale) {
146+
this.scale = scale;
147+
}
148+
119149
@Override
120150
public String toString() {
121151
StringBuilder arraySpec = new StringBuilder();
@@ -172,6 +202,16 @@ public ColDataType withZerofill(boolean zerofill) {
172202
return this;
173203
}
174204

205+
public ColDataType withPrecision(Integer precision) {
206+
this.setPrecision(precision);
207+
return this;
208+
}
209+
210+
public ColDataType withScale(Integer scale) {
211+
this.setScale(scale);
212+
return this;
213+
}
214+
175215
public ColDataType addArgumentsStringList(String... argumentsStringList) {
176216
List<String> collection =
177217
Optional.ofNullable(getArgumentsStringList()).orElseGet(ArrayList::new);

0 commit comments

Comments
 (0)