improved sql parser.

This commit is contained in:
wenshao
2017-08-30 17:17:06 +08:00
parent 956edd618d
commit eb3ae06885
3 changed files with 94 additions and 4 deletions
+15 -1
View File
@@ -10,7 +10,7 @@
-->
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3-preview_23</version>
<version>1.1.3-preview_43</version>
<packaging>jar</packaging>
<name>druid</name>
@@ -556,6 +556,13 @@
<version>22.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<scope>provided</scope>
</dependency>
<!--
test dependences
@@ -700,5 +707,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -319,16 +319,26 @@ public class OracleStatementParser extends SQLStatementParser {
}
if (lexer.token() == Token.LPAREN) {
char ch = lexer.current();
int bp = lexer.bp();
Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
int parenCount = 0;
while (lexer.token() == Token.LPAREN) {
savePoint = lexer.mark();
lexer.nextToken();
parenCount++;
}
if (lexer.token() == Token.SELECT) {
lexer.reset(bp, ch, Token.LPAREN);
lexer.reset(savePoint);
SQLStatement stmt = parseSelect();
stmt.setParent(parent);
statementList.add(stmt);
for (int i = 0; i < parenCount; ++i) {
accept(Token.RPAREN);
}
continue;
} else {
throw new ParserException("TODO : " + lexer.info());
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2017 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 OracleSelectTest76_brace extends OracleTest {
public void test_0() throws Exception {
String sql = //
"((SELECT *\n" +
"FROM emp\n" +
"WHERE empno = 111));"; //
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());
assertEquals(1, visitor.getTables().size());
// assertEquals(2, visitor.getColumns().size());
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM emp\n" +
"WHERE empno = 111;", 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")));
}
}