Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@

public class ColDataType implements Serializable {

public enum Signedness {
SIGNED, UNSIGNED
}

private String dataType;
private List<String> argumentsStringList;
private String characterSet;
private IntervalQualifier intervalQualifier;
private List<Integer> arrayData = new ArrayList<Integer>();
private Signedness signedness;
private boolean zerofill;
private Integer precision;
private Integer scale;

Expand Down Expand Up @@ -98,6 +104,22 @@ public void setArrayData(List<Integer> arrayData) {
this.arrayData = arrayData;
}

public Signedness getSignedness() {
return signedness;
}

public void setSignedness(Signedness signedness) {
this.signedness = signedness;
}

public boolean isZerofill() {
return zerofill;
}

public void setZerofill(boolean zerofill) {
this.zerofill = zerofill;
}

/**
* The first numeric type parameter, e.g. {@code 255} for {@code VARCHAR(255)} or {@code 10} for
* {@code DECIMAL(10, 2)}. {@code MAX} is reported as {@link Integer#MAX_VALUE}. Returns
Expand Down Expand Up @@ -139,6 +161,8 @@ public String toString() {
+ (argumentsStringList != null
? " " + PlainSelect.getStringList(argumentsStringList, true, true)
: "")
+ (signedness != null ? " " + signedness : "")
+ (zerofill ? " ZEROFILL" : "")
+ arraySpec.toString()
+ (characterSet != null ? " CHARACTER SET " + characterSet : "");
}
Expand Down Expand Up @@ -168,6 +192,16 @@ public ColDataType withArrayData(List<Integer> arrayData) {
return this;
}

public ColDataType withSignedness(Signedness signedness) {
setSignedness(signedness);
return this;
}

public ColDataType withZerofill(boolean zerofill) {
setZerofill(zerofill);
return this;
}

public ColDataType withPrecision(Integer precision) {
this.setPrecision(precision);
return this;
Expand Down Expand Up @@ -218,7 +252,9 @@ public final boolean equals(Object o) {
&& Objects.equals(argumentsStringList, that.argumentsStringList)
&& Objects.equals(characterSet, that.characterSet)
&& Objects.equals(intervalQualifier, that.intervalQualifier)
&& Objects.equals(arrayData, that.arrayData);
&& Objects.equals(arrayData, that.arrayData)
&& signedness == that.signedness
&& zerofill == that.zerofill;
}

@Override
Expand All @@ -228,6 +264,8 @@ public int hashCode() {
result = 31 * result + Objects.hashCode(characterSet);
result = 31 * result + Objects.hashCode(intervalQualifier);
result = 31 * result + Objects.hashCode(arrayData);
result = 31 * result + Objects.hashCode(signedness);
result = 31 * result + Boolean.hashCode(zerofill);
return result;
}
}
12 changes: 9 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,7 @@ String NonReservedWord() :
| tk=<K_XSINIL:"XSINIL">
| tk=<K_YAML:"YAML">
| tk=<K_YES:"YES">
| tk=<K_ZEROFILL:"ZEROFILL">
| tk=<K_ZONE:"ZONE">
)
{ return tk.image; }
Expand Down Expand Up @@ -11794,10 +11795,10 @@ ColDataType DataType():
zonePrecision = tk.kind == DT_ZONE ? zonedTypePrecision(tk.image) : null;
}
(
// MySQL seems to allow: INT UNSIGNED. Do not consume CHARACTER when it starts
// the trailing CHARACTER SET clause of a character type.
// Signedness is parsed by ColDataType after optional precision/scale. Do not
// consume CHARACTER when it starts the trailing CHARACTER SET clause.
LOOKAHEAD(2, { getToken(1).kind != K_CHARACTER || getToken(2).kind != K_SET })
( tk = <DATA_TYPE> | tk = <K_SIGNED> | tk = <K_UNSIGNED>
( tk = <DATA_TYPE>
| tk=<K_CHARACTER> | tk=<K_BIT> | tk=<K_BYTES> | tk=<K_BINARY> | tk=<K_BOOLEAN>
| tk=<K_CHAR> | tk=<K_JSON> | tk=<K_STRING> ) { type += " " + tk.image; }
)*
Expand Down Expand Up @@ -11926,6 +11927,11 @@ ColDataType ColDataType():
)*
")"
]
[ LOOKAHEAD(2)
( tk=<K_SIGNED> { colDataType.setSignedness(ColDataType.Signedness.SIGNED); }
| tk=<K_UNSIGNED> { colDataType.setSignedness(ColDataType.Signedness.UNSIGNED); } )
]
[ LOOKAHEAD(2) <K_ZEROFILL> { colDataType.setZerofill(true); } ]
[ LOOKAHEAD(2) ( LOOKAHEAD(2) "[" {tk=null;} [ tk=<S_LONG> ] { array.add(tk!=null?Integer.valueOf(tk.image):null); } "]" )+ { colDataType.setArrayData(array); } ]
[ LOOKAHEAD(2) <K_CHARACTER> <K_SET> (tk=<S_IDENTIFIER> | tk=<K_BINARY>) { colDataType.setCharacterSet(tk.image); } ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColDataType.Signedness;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.table.ExcludeConstraint;
Expand Down Expand Up @@ -1255,6 +1256,28 @@ void testUniqueIndexIssue1893() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE t (a int, INDEX idx (a))", true);
}

@Test
void testMySqlColumnTypeModifiers() throws JSQLParserException {
String sql = "CREATE TABLE t (a INT UNSIGNED, b INT (11) UNSIGNED ZEROFILL, "
+ "c DECIMAL (10, 2) SIGNED NOT NULL)";
CreateTable createTable =
(CreateTable) assertSqlCanBeParsedAndDeparsed(sql, true);

ColDataType first = createTable.getColumnDefinitions().get(0).getColDataType();
assertEquals("INT", first.getDataType());
assertEquals(Signedness.UNSIGNED, first.getSignedness());
assertFalse(first.isZerofill());

ColDataType second = createTable.getColumnDefinitions().get(1).getColDataType();
assertEquals(Signedness.UNSIGNED, second.getSignedness());
assertTrue(second.isZerofill());

ColDataType third = createTable.getColumnDefinitions().get(2).getColDataType();
assertEquals(Signedness.SIGNED, third.getSignedness());
assertEquals(List.of("NOT", "NULL"),
createTable.getColumnDefinitions().get(2).getColumnSpecs());
}

@Test
void testMySqlConstraintSymbolAndIndexNameIssue1570() throws JSQLParserException {
String uniqueSql = "CREATE TABLE table1 (col1 INT, col2 INT UNIQUE, "
Expand Down
Loading