mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for sql parser
This commit is contained in:
@@ -55,7 +55,7 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
|
||||
private static final long serialVersionUID = 1L;
|
||||
protected SQLIdentifierExpr name;
|
||||
protected final List<SQLName> columns = new ArrayList<SQLName>();
|
||||
protected SQLSelectQuery subQuery;
|
||||
protected SQLSelect subQuery;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
@@ -75,11 +75,11 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public SQLSelectQuery getSubQuery() {
|
||||
public SQLSelect getSubQuery() {
|
||||
return subQuery;
|
||||
}
|
||||
|
||||
public void setSubQuery(SQLSelectQuery subQuery) {
|
||||
public void setSubQuery(SQLSelect subQuery) {
|
||||
this.subQuery = subQuery;
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ public class OracleSelectParser extends SQLSelectParser {
|
||||
|
||||
accept(Token.AS);
|
||||
accept(Token.LPAREN);
|
||||
entry.setSubQuery(query());
|
||||
entry.setSubQuery(select());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (identifierEquals("SEARCH")) {
|
||||
|
||||
@@ -123,7 +123,7 @@ public class SQLParser {
|
||||
} else {
|
||||
setErrorEndPos(lexer.pos());
|
||||
throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " "
|
||||
+ lexer.stringVal());
|
||||
+ lexer.stringVal() + ", pos " + this.lexer.pos());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
|
||||
accept(Token.AS);
|
||||
accept(Token.LPAREN);
|
||||
entry.setSubQuery(query());
|
||||
entry.setSubQuery(select());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
withQueryClause.getEntries().add(entry);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.filter.wall;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.wall.WallUtils;
|
||||
|
||||
/**
|
||||
* SQLServerWallTest
|
||||
*
|
||||
* @author RaymondXiu
|
||||
* @version 1.0, 2012-3-18
|
||||
* @see
|
||||
*/
|
||||
public class MySqlWallTest3 extends TestCase {
|
||||
|
||||
public void test_stuff() throws Exception {
|
||||
Assert.assertTrue(WallUtils.isValidateMySql(//
|
||||
"SELECT COUNT(p.id) FROM TB_PRO p" + //
|
||||
" INNER JOIN TB_Db b ON b.id =p.dbid " + //
|
||||
" WHERE p.index_status='0' AND p.sc=?"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
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 com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class OracleSelectTest40 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"WITH a AS (" + //
|
||||
" SELECT TO_CHAR(csl.create_time,'yyyyMMdd') create_time, cwl.client_key ck, csl.src_id src_id " + //
|
||||
" FROM xxxx csl, xxxx cwl " + //
|
||||
" WHERE 1 =1 AND csl.src_id = cwl.src_id AND csl.curr_url = cwl.curr_url " + //
|
||||
" AND TO_CHAR(csl.create_time,'yyyyMMdd') BETWEEN ? " + //
|
||||
" AND ? AND cwl.day = TO_CHAR(csl.create_time,'yyyyMMdd') " + //
|
||||
" GROUP BY cwl.client_key, csl.src_id, csl.create_time ORDER BY csl.src_id )" + //
|
||||
", b AS (" + //
|
||||
" SELECT itn.buyerpaytime, itn.esc_orderid, itn.oldck, " + //
|
||||
" MAX(con.PAYED_AMOUNT) gmv, MAX(con.buyer_onlyid) buyer " + //
|
||||
" FROM xxxx itn, xxxx don, xxxx con " + //
|
||||
" WHERE don.esc_orderid = itn.esc_orderid " + //
|
||||
" AND con.esc_orderid = itn.esc_orderid " + //
|
||||
" AND don.order_status IN (4,5,6,7,8) AND itn.buyerpaytime BETWEEN ? AND ? " + //
|
||||
" GROUP BY itn.esc_orderid, itn.oldck, itn.buyerpaytime ) " + //
|
||||
"SELECT MAX(cos.location) AS position , a.src_id AS srcid , " + //
|
||||
" COUNT(DISTINCT b.esc_orderid) AS orders , SUM(b.gmv) AS uvGmv, " + //
|
||||
" COUNT(DISTINCT buyer) AS buyers FROM a, b, cp_operate_statistics cos " + //
|
||||
"WHERE a.ck = b.oldck(+) AND TO_CHAR(cos.day,'yyyyMMdd') = a.create_time " + //
|
||||
" AND a.create_time = b.buyerpaytime AND a.src_id = cos.src_id GROUP BY a.src_id ORDER BY a.src_id"; //
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.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(2, visitor.getTables().size());
|
||||
|
||||
Assert.assertEquals(15, visitor.getColumns().size());
|
||||
|
||||
// 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,57 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.postgresql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
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;
|
||||
|
||||
public class PGSelectTest9 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT res.restaurant_id, row_number() " + //
|
||||
"over (order by ST_Distance(res.restaurant_point,GeomFromEWKT(?))*111.12 ) as row_id " + //
|
||||
"FROM restaurant res where res.city_id = ? and res.is_delete =false and res.restaurant_id <1000000 limit 10";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
Assert.assertEquals(4, visitor.getColumns().size());
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
}
|
||||
}
|
||||
|
||||
// select categoryId , offerIds from cnres.function_select_get_spt_p4p_offer_list (' 1031918 , 1031919 , 1037004 ') as
|
||||
// a(categoryId numeric,offerIds character varying(4000))
|
||||
// select memberId , offerIds from cnres.function_select_get_seller_hot_offer_list('\'gzyyd168\'') as a(memberId
|
||||
// character varying(20),offerIds character varying(4000))
|
||||
|
||||
Reference in New Issue
Block a user