Skip to content

Commit

Permalink
Support additional Postgres column types for alter table statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Story committed Aug 30, 2016
1 parent 03a344d commit 42181b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_SOME:"SOME">
| <K_FULL:"FULL">
| <K_WITH:"WITH">
| <K_WITHOUT:"WITHOUT">
| <K_TABLE:"TABLE">
| <K_VIEW:"VIEW">
| <K_WHERE:"WHERE">
Expand Down Expand Up @@ -195,6 +196,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_REFERENCES:"REFERENCES">
| <K_CHECK:"CHECK">
| <K_CHARACTER:"CHARACTER">
| <K_BIT:"BIT">
| <K_VARYING:"VARYING">
| <K_START:"START">
| <K_CONNECT:"CONNECT">
Expand Down Expand Up @@ -246,6 +248,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_IGNORE : "IGNORE">
| <K_SEMI : "SEMI">
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
| <K_ZONE:"ZONE">
| <K_TIME_KEY_EXPR : ( "CURRENT_TIMESTAMP" | "CURRENT_TIME" | "CURRENT_DATE" ) ( "()" )?>
| <K_DOUBLE : "DOUBLE">
| <K_PRECISION : "PRECISION">
Expand All @@ -265,6 +268,11 @@ TOKEN : /* Operators */
| <OP_NOTEQUALSBANG: "!=">
}

TOKEN : /* Date/Time with time zones */
{
<DT_ZONE: <K_DATETIMELITERAL> <WHITESPACE> (<K_WITH> | <K_WITHOUT>) <WHITESPACE> "TIME" <WHITESPACE> <K_ZONE>>
}

TOKEN : /* Numeric Constants */
{
< S_DOUBLE: ((<S_LONG>)? "." <S_LONG> ( ["e","E"] (["+", "-"])? <S_LONG>)?
Expand Down Expand Up @@ -2818,9 +2826,9 @@ ColDataType ColDataType():
List<Integer> array = new ArrayList<Integer>();
}
{
( tk=<K_CHARACTER> [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
( (tk=<K_CHARACTER> | tk=<K_BIT>) [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
| tk=<K_DOUBLE> [tk2=<K_PRECISION>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> | tk=<K_INTERVAL> ) { colDataType.setDataType(tk.image); } )
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> | tk=<K_XML> | tk=<K_INTERVAL> | tk=<DT_ZONE> ) { colDataType.setDataType(tk.image); })

[LOOKAHEAD(2) "(" ( (tk=<S_LONG> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER> ) { argumentsStringList.add(tk.image); } ["," {/*argumentsStringList.add(",");*/}] )* ")"]
[( "[" {tk=null;} [ tk=<S_LONG> ] { array.add(tk!=null?Integer.valueOf(tk.image):null); } "]" )+ { colDataType.setArrayData(array); } ]
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,24 @@ public void testAlterTableAddColumn4() throws JSQLParserException {
assertEquals("integer", col2DataTypes.get(0).getColDataType().toString());
}

public void testAlterTableAddColumnWithZone() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp without time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date with time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date without time zone");

Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
Alter alter = (Alter) stmt;
List<AlterExpression> alterExps = alter.getAlterExpressions();
AlterExpression col1Exp = alterExps.get(0);
List<ColumnDataType> col1DataTypes = col1Exp.getColDataTypeList();
assertEquals("timestamp with time zone", col1DataTypes.get(0).getColDataType().toString());
}

public void testAlterTableAddColumnKeywordTypes() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 xml");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 interval");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 bit varying");
}

}

0 comments on commit 42181b0

Please sign in to comment.