Skip to content
Open
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;

public ColDataType() {
// empty constructor
Expand Down Expand Up @@ -94,6 +100,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;
}

@Override
public String toString() {
StringBuilder arraySpec = new StringBuilder();
Expand All @@ -109,6 +131,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 @@ -138,6 +162,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 addArgumentsStringList(String... argumentsStringList) {
List<String> collection =
Optional.ofNullable(getArgumentsStringList()).orElseGet(ArrayList::new);
Expand Down Expand Up @@ -178,7 +212,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 @@ -188,6 +224,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 @@ -1815,6 +1815,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 @@ -11683,10 +11684,10 @@ ColDataType DataType():
| 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; }
(
// 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 @@ -11806,6 +11807,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 @@ -32,6 +32,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 @@ -1247,4 +1248,26 @@ void testUniqueIndexIssue1893() throws JSQLParserException {
// A plain INDEX must still parse unchanged.
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());
}
}
Loading