Skip to content

Commit fefe4ac

Browse files
authored
Merge pull request #328 from wrobstory/postgres-types
Support additional Postgres column types for alter table statements
2 parents 21a0df9 + 42181b0 commit fefe4ac

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
146146
| <K_SOME:"SOME">
147147
| <K_FULL:"FULL">
148148
| <K_WITH:"WITH">
149+
| <K_WITHOUT:"WITHOUT">
149150
| <K_TABLE:"TABLE">
150151
| <K_VIEW:"VIEW">
151152
| <K_WHERE:"WHERE">
@@ -195,6 +196,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
195196
| <K_REFERENCES:"REFERENCES">
196197
| <K_CHECK:"CHECK">
197198
| <K_CHARACTER:"CHARACTER">
199+
| <K_BIT:"BIT">
198200
| <K_VARYING:"VARYING">
199201
| <K_START:"START">
200202
| <K_CONNECT:"CONNECT">
@@ -246,6 +248,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
246248
| <K_IGNORE : "IGNORE">
247249
| <K_SEMI : "SEMI">
248250
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
251+
| <K_ZONE:"ZONE">
249252
| <K_TIME_KEY_EXPR : ( "CURRENT_TIMESTAMP" | "CURRENT_TIME" | "CURRENT_DATE" ) ( "()" )?>
250253
| <K_DOUBLE : "DOUBLE">
251254
| <K_PRECISION : "PRECISION">
@@ -265,6 +268,11 @@ TOKEN : /* Operators */
265268
| <OP_NOTEQUALSBANG: "!=">
266269
}
267270

271+
TOKEN : /* Date/Time with time zones */
272+
{
273+
<DT_ZONE: <K_DATETIMELITERAL> <WHITESPACE> (<K_WITH> | <K_WITHOUT>) <WHITESPACE> "TIME" <WHITESPACE> <K_ZONE>>
274+
}
275+
268276
TOKEN : /* Numeric Constants */
269277
{
270278
< S_DOUBLE: ((<S_LONG>)? "." <S_LONG> ( ["e","E"] (["+", "-"])? <S_LONG>)?
@@ -2818,9 +2826,9 @@ ColDataType ColDataType():
28182826
List<Integer> array = new ArrayList<Integer>();
28192827
}
28202828
{
2821-
( tk=<K_CHARACTER> [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
2829+
( (tk=<K_CHARACTER> | tk=<K_BIT>) [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
28222830
| tk=<K_DOUBLE> [tk2=<K_PRECISION>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
2823-
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> | tk=<K_INTERVAL> ) { colDataType.setDataType(tk.image); } )
2831+
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> | tk=<K_XML> | tk=<K_INTERVAL> | tk=<DT_ZONE> ) { colDataType.setDataType(tk.image); })
28242832

28252833
[LOOKAHEAD(2) "(" ( (tk=<S_LONG> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER> ) { argumentsStringList.add(tk.image); } ["," {/*argumentsStringList.add(",");*/}] )* ")"]
28262834
[( "[" {tk=null;} [ tk=<S_LONG> ] { array.add(tk!=null?Integer.valueOf(tk.image):null); } "]" )+ { colDataType.setArrayData(array); } ]

src/test/java/net/sf/jsqlparser/test/alter/AlterTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,24 @@ public void testAlterTableAddColumn4() throws JSQLParserException {
109109
assertEquals("integer", col2DataTypes.get(0).getColDataType().toString());
110110
}
111111

112+
public void testAlterTableAddColumnWithZone() throws JSQLParserException {
113+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
114+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp without time zone");
115+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date with time zone");
116+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date without time zone");
117+
118+
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
119+
Alter alter = (Alter) stmt;
120+
List<AlterExpression> alterExps = alter.getAlterExpressions();
121+
AlterExpression col1Exp = alterExps.get(0);
122+
List<ColumnDataType> col1DataTypes = col1Exp.getColDataTypeList();
123+
assertEquals("timestamp with time zone", col1DataTypes.get(0).getColDataType().toString());
124+
}
125+
126+
public void testAlterTableAddColumnKeywordTypes() throws JSQLParserException {
127+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 xml");
128+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 interval");
129+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 bit varying");
130+
}
131+
112132
}

0 commit comments

Comments
 (0)