sql parser improved support db2 truncate syntax. for issue #1324

This commit is contained in:
高铁
2016-07-09 14:33:16 +08:00
parent e9334f09e2
commit 9ca288e3d8
9 changed files with 465 additions and 8 deletions
@@ -24,20 +24,28 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLTruncateStatement extends SQLStatementImpl {
protected List<SQLExprTableSource> tableSources = new ArrayList<SQLExprTableSource>(2);
protected List<SQLExprTableSource> tableSources = new ArrayList<SQLExprTableSource>(2);
private boolean purgeSnapshotLog = false;
private boolean purgeSnapshotLog = false;
private boolean only;
private Boolean restartIdentity;
private Boolean cascade;
public SQLTruncateStatement() {
// db2
private boolean dropStorage = false;
private boolean reuseStorage = false;
private boolean immediate = false;
private boolean ignoreDeleteTriggers = false;
private boolean restrictWhenDeleteTriggers = false;
private boolean continueIdentity = false;
public SQLTruncateStatement(){
}
public SQLTruncateStatement(String dbType) {
super (dbType);
public SQLTruncateStatement(String dbType){
super(dbType);
}
public List<SQLExprTableSource> getTableSources() {
@@ -93,4 +101,53 @@ public class SQLTruncateStatement extends SQLStatementImpl {
public void setCascade(Boolean cascade) {
this.cascade = cascade;
}
public boolean isDropStorage() {
return dropStorage;
}
public void setDropStorage(boolean dropStorage) {
this.dropStorage = dropStorage;
}
public boolean isReuseStorage() {
return reuseStorage;
}
public void setReuseStorage(boolean reuseStorage) {
this.reuseStorage = reuseStorage;
}
public boolean isImmediate() {
return immediate;
}
public void setImmediate(boolean immediate) {
this.immediate = immediate;
}
public boolean isIgnoreDeleteTriggers() {
return ignoreDeleteTriggers;
}
public void setIgnoreDeleteTriggers(boolean ignoreDeleteTriggers) {
this.ignoreDeleteTriggers = ignoreDeleteTriggers;
}
public boolean isRestrictWhenDeleteTriggers() {
return restrictWhenDeleteTriggers;
}
public void setRestrictWhenDeleteTriggers(boolean restrictWhenDeleteTriggers) {
this.restrictWhenDeleteTriggers = restrictWhenDeleteTriggers;
}
public boolean isContinueIdentity() {
return continueIdentity;
}
public void setContinueIdentity(boolean continueIdentity) {
this.continueIdentity = continueIdentity;
}
}
@@ -38,6 +38,8 @@ public class DB2Lexer extends Lexer {
map.put("OPTIMIZE", Token.OPTIMIZE);
map.put("OF", Token.OF);
map.put("CONCAT", Token.CONCAT);
map.put("CONTINUE", Token.CONTINUE);
map.put("IDENTITY", Token.IDENTITY);
DEFAULT_DB2_KEYWORDS = new Keywords(map);
}
@@ -1430,6 +1430,49 @@ public class SQLStatementParser extends SQLParser {
stmt.setCascade(Boolean.FALSE);
continue;
}
if (lexer.token() == Token.DROP) {
lexer.nextToken();
acceptIdentifier("STORAGE");
stmt.setDropStorage(true);
continue;
}
if (identifierEquals("REUSE")) {
lexer.nextToken();
acceptIdentifier("STORAGE");
stmt.setReuseStorage(true);
continue;
}
if (identifierEquals("IGNORE")) {
lexer.nextToken();
accept(Token.DELETE);
acceptIdentifier("TRIGGERS");
stmt.setIgnoreDeleteTriggers(true);
continue;
}
if (identifierEquals("RESTRICT")) {
lexer.nextToken();
accept(Token.WHEN);
accept(Token.DELETE);
acceptIdentifier("TRIGGERS");
stmt.setRestrictWhenDeleteTriggers(true);
continue;
}
if (lexer.token() == Token.CONTINUE) {
lexer.nextToken();
accept(Token.IDENTITY);
continue;
}
if (identifierEquals("IMMEDIATE")) {
lexer.nextToken();
stmt.setImmediate(true);
continue;
}
break;
}
@@ -1449,6 +1449,31 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
public boolean visit(SQLTruncateStatement x) {
print0(ucase ? "TRUNCATE TABLE " : "truncate table ");
printAndAccept(x.getTableSources(), ", ");
if (x.isDropStorage()) {
print0(ucase ? " DROP STORAGE" : " drop storage");
}
if (x.isReuseStorage()) {
print0(ucase ? " REUSE STORAGE" : " reuse storage");
}
if (x.isIgnoreDeleteTriggers()) {
print0(ucase ? " IGNORE DELETE TRIGGERS" : " ignore delete triggers");
}
if (x.isRestrictWhenDeleteTriggers()) {
print0(ucase ? " RESTRICT WHEN DELETE TRIGGERS" : " restrict when delete triggers");
}
if (x.isContinueIdentity()) {
print0(ucase ? " CONTINUE IDENTITY" : " continue identity");
}
if (x.isImmediate()) {
print0(ucase ? " IMMEDIATE" : " immediate");
}
return false;
}
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.db2;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
public class DB2TruncateTest extends DB2Test {
public void test_0() throws Exception {
String sql = "TRUNCATE TASK_AICSFSM_QUERY_BATCH_TEMP IMMEDIATE";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("TASK_AICSFSM_QUERY_BATCH_TEMP")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("TRUNCATE TABLE TASK_AICSFSM_QUERY_BATCH_TEMP IMMEDIATE", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("truncate table TASK_AICSFSM_QUERY_BATCH_TEMP immediate", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.db2;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
public class DB2TruncateTest2 extends DB2Test {
public void test_0() throws Exception {
String sql = "TRUNCATE TABLE INVENTORY IGNORE DELETE TRIGGERS DROP STORAGE IMMEDIATE";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("INVENTORY")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("TRUNCATE TABLE INVENTORY DROP STORAGE IGNORE DELETE TRIGGERS IMMEDIATE", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("truncate table INVENTORY drop storage ignore delete triggers immediate", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.db2;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
public class DB2TruncateTest3 extends DB2Test {
public void test_0() throws Exception {
String sql = "TRUNCATE TABLE INVENTORY REUSE STORAGE RESTRICT WHEN DELETE TRIGGERS";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("INVENTORY")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("TRUNCATE TABLE INVENTORY REUSE STORAGE RESTRICT WHEN DELETE TRIGGERS", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("truncate table INVENTORY reuse storage restrict when delete triggers", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.db2;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
public class DB2TruncateTest4 extends DB2Test {
public void test_0() throws Exception {
String sql = "TRUNCATE TABLE INVENTORY REUSE STORAGE IGNORE DELETE TRIGGERS IMMEDIATE";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("INVENTORY")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("TRUNCATE TABLE INVENTORY REUSE STORAGE IGNORE DELETE TRIGGERS IMMEDIATE", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("truncate table INVENTORY reuse storage ignore delete triggers immediate", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.db2;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
public class DB2TruncateTest5 extends DB2Test {
public void test_0() throws Exception {
String sql = "TRUNCATE TABLE INVENTORY REUSE STORAGE IGNORE DELETE TRIGGERS CONTINUE IDENTITY IMMEDIATE";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("INVENTORY")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("TRUNCATE TABLE INVENTORY REUSE STORAGE IGNORE DELETE TRIGGERS IMMEDIATE", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("truncate table INVENTORY reuse storage ignore delete triggers immediate", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}