improve sql parser for informix skip/first clause.

This commit is contained in:
wenshao
2016-10-17 19:22:47 +08:00
parent 4f612ec2e0
commit 82cdd301c4
15 changed files with 359 additions and 31 deletions
@@ -386,7 +386,7 @@ public class SQLUtils {
return new OdpsOutputVisitor(out);
}
return new SQLASTOutputVisitor(out);
return new SQLASTOutputVisitor(out, dbType);
}
@Deprecated
@@ -38,6 +38,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
protected boolean noWait = false;
protected SQLExpr waitTime;
protected SQLExpr first;
protected SQLExpr offset;
public SQLSelectQueryBlock(){
@@ -161,6 +162,17 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.first = first;
}
public SQLExpr getOffset() {
return offset;
}
public void setOffset(SQLExpr offset) {
if (offset != null) {
offset.setParent(this);
}
this.offset = offset;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -54,6 +54,7 @@ import com.alibaba.druid.sql.dialect.odps.ast.OdpsShowStatisticStmt;
import com.alibaba.druid.sql.dialect.odps.ast.OdpsStatisticClause;
import com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.JdbcConstants;
public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVisitor {
@@ -77,7 +78,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
public OdpsOutputVisitor(Appendable appender){
super(appender);
super(appender, JdbcConstants.ODPS);
}
public boolean visit(OdpsCreateTableStatement x) {
@@ -18,18 +18,14 @@ package com.alibaba.druid.sql.dialect.oracle.parser;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUnionOperator;
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause.AsOfFlashbackQueryClause;
@@ -82,8 +78,15 @@ public class OracleSelectParser extends SQLSelectParser {
withSubquery(select);
select.setQuery(query());
select.setOrderBy(this.parseOrderBy());
SQLSelectQuery query = query();
select.setQuery(query);
SQLOrderBy orderBy = this.parseOrderBy();
select.setOrderBy(orderBy);
if (orderBy != null && query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
parseFetchClause(queryBlock);
}
if (lexer.token() == (Token.FOR)) {
lexer.nextToken();
@@ -344,7 +344,8 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
println();
}
x.getQuery().accept(this);
SQLSelectQuery query = x.getQuery();
query.accept(this);
if (x.getRestriction() != null) {
print(' ');
@@ -356,9 +357,18 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
x.getForUpdate().accept(this);
}
if (x.getOrderBy() != null) {
println();
x.getOrderBy().accept(this);
SQLOrderBy orderBy = x.getOrderBy();
if (orderBy != null) {
boolean hasFirst = false;
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
hasFirst = queryBlock.getFirst() != null;
}
if (!hasFirst) {
println();
orderBy.accept(this);
}
}
return false;
@@ -164,6 +164,8 @@ public class SQLServerSelectParser extends SQLSelectParser {
parseGroupBy(queryBlock);
parseFetchClause(queryBlock);
return queryRest(queryBlock);
}
@@ -93,6 +93,8 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
x.getGroupBy().accept(this);
}
printFetchFirst(x);
return false;
}
@@ -70,7 +70,7 @@ public class SQLParserUtils {
return new PhoenixStatementParser(sql);
}
return new SQLStatementParser(sql);
return new SQLStatementParser(sql, dbType);
}
public static SQLExprParser createExprParser(String sql, String dbType) {
@@ -173,6 +173,20 @@ public class SQLSelectParser extends SQLParser {
lexer.nextToken();
}
if (JdbcConstants.INFORMIX.equals(dbType)) {
if (identifierEquals("SKIP")) {
lexer.nextToken();
SQLExpr offset = this.exprParser.primary();
queryBlock.setOffset(offset);
}
if (identifierEquals("FIRST")) {
lexer.nextToken();
SQLExpr first = this.exprParser.primary();
queryBlock.setFirst(first);
}
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
@@ -541,6 +555,15 @@ public class SQLSelectParser extends SQLParser {
}
public void parseFetchClause(SQLSelectQueryBlock queryBlock) {
if (identifierEquals("OFFSET") || lexer.token() == Token.OFFSET) {
lexer.nextToken();
SQLExpr offset = this.exprParser.primary();
queryBlock.setOffset(offset);
if (identifierEquals("ROW") || identifierEquals("ROWS")) {
lexer.nextToken();
}
}
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
if (lexer.token() == Token.FIRST) {
@@ -60,6 +60,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
this.appender = appender;
}
public SQLASTOutputVisitor(Appendable appender, String dbType){
this.appender = appender;
this.dbType = dbType;
}
public SQLASTOutputVisitor(Appendable appender, boolean parameterized){
this.appender = appender;
this.parameterized = parameterized;
@@ -753,6 +758,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print0(ucase ? "SELECT " : "select ");
if (JdbcConstants.INFORMIX.equals(dbType)) {
printFetchFirst(x);
}
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print0(ucase ? "ALL " : "all ");
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
@@ -787,28 +796,51 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
x.getOrderBy().accept(this);
}
printFetchFirst(x);
if (!JdbcConstants.INFORMIX.equals(dbType)) {
printFetchFirst(x);
}
return false;
}
protected void printFetchFirst(SQLSelectQueryBlock x) {
if (x.getFirst() != null) {
//order by 语句必须在FETCH FIRST ROWS ONLY之前
SQLObject parent= x.getParent();
if(parent instanceof SQLSelect) {
SQLOrderBy orderBy = ((SQLSelect) parent).getOrderBy();
if (orderBy!=null&&orderBy.getItems().size() > 0) {
println();
print0(ucase ? "ORDER BY " : "order by ");
printAndAccept(orderBy.getItems(), ", ");
((SQLSelect) parent).setOrderBy(null);
SQLExpr offset = x.getOffset();
SQLExpr first = x.getFirst();
boolean informix = JdbcConstants.INFORMIX.equals(dbType);
if (first != null) {
if (informix) {
if (offset != null) {
print0(ucase ? "SKIP " : "skip ");
offset.accept(this);
}
print0(ucase ? " FIRST " : " first ");
first.accept(this);
print(' ');
} else {
//order by 语句必须在FETCH FIRST ROWS ONLY之前
SQLObject parent = x.getParent();
if (parent instanceof SQLSelect) {
SQLOrderBy orderBy = ((SQLSelect) parent).getOrderBy();
if (orderBy != null && orderBy.getItems().size() > 0) {
println();
print0(ucase ? "ORDER BY " : "order by ");
printAndAccept(orderBy.getItems(), ", ");
}
}
println();
if (offset != null) {
print0(ucase ? "OFFSET " : "offset ");
offset.accept(this);
print0(ucase ? " ROWS " : " rows ");
}
print0(ucase ? "FETCH FIRST " : "fetch first ");
first.accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
}
println();
print0(ucase ? "FETCH FIRST " : "fetch first ");
x.getFirst().accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
}
}
@@ -0,0 +1,55 @@
/*
* 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.informix;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;
import java.util.List;
public class InformixSelectTest extends TestCase {
public void test_0() throws Exception {
String sql = "select skip 500 first 500 * from pub_menu ";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.INFORMIX);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals("SELECT SKIP 500 FIRST 500 *\n" +
"FROM pub_menu", SQLUtils.toSQLString(stmt, JdbcConstants.INFORMIX));
Assert.assertEquals("select skip 500 first 500 *\n" +
"from pub_menu", SQLUtils.toSQLString(stmt, JdbcConstants.INFORMIX, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getTables().size());
}
}
@@ -54,7 +54,7 @@ public class MySqlReplaceTest extends MysqlTest {
assertEquals(0, visitor.getConditions().size());
assertTrue(visitor.containsTable("t1"));
assertTrue(visitor.containsColumn("t1", "id"));
assertTrue(visitor.containsColumn("t1", "name"));
}
@@ -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.odps;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;
import java.util.List;
public class OdpsSelectTest18 extends TestCase {
public void test_select() throws Exception {
String sql = "select a, b, c from tableA where a in ( select a from tableB limit 10 )";//
Assert.assertEquals("SELECT a\n" +
"\t, b\n" +
"\t, c\n" +
"FROM tableA\n" +
"WHERE a IN (SELECT a\n" +
"\tFROM tableB\n" +
"\tLIMIT 10)", SQLUtils.formatOdps(sql));
Assert.assertEquals("select a\n" +
"\t, b\n" +
"\t, c\n" +
"from tableA\n" +
"where a in (select a\n" +
"\tfrom tableB\n" +
"\tlimit 10)", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
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(2, visitor.getTables().size());
Assert.assertEquals(4, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getConditions().size());
// Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
}
@@ -0,0 +1,74 @@
/*
* 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.oracle.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest59 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"SELECT * FROM atable ORDER BY acolumn DESC OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(2, visitor.getColumns().size());
{
String text = SQLUtils.toOracleString(stmt);
Assert.assertEquals("SELECT *\n" +
"FROM atable\n" +
"ORDER BY acolumn DESC\n" +
"OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY", text);
}
{
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *\n" +
"from atable\n" +
"order by acolumn desc\n" +
"offset 0 rows fetch first 10 rows only", text);
}
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
}
@@ -0,0 +1,48 @@
/*
* 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.sqlserver;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import junit.framework.TestCase;
import org.junit.Assert;
public class SQLServerSelectTest23 extends TestCase {
public void test_simple() throws Exception {
String sql = //
"SELECT * FROM T FETCH FIRST 10 ROWS ONLY"; //
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
{
String text = SQLUtils.toSQLServerString(stmt);
Assert.assertEquals("SELECT *\n" +
"FROM T\n" +
"FETCH FIRST 10 ROWS ONLY", text);
}
{
String text = SQLUtils.toSQLServerString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *\n" +
"from T\n" +
"fetch first 10 rows only", text);
}
}
}