mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. for issue #1775
This commit is contained in:
@@ -26,8 +26,6 @@ import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
|
||||
@@ -181,7 +179,7 @@ public class DALParserTest extends TestCase {
|
||||
public void test_setNames() throws Exception {
|
||||
String sql = "SET names default ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlSetNamesStatement set = (MySqlSetNamesStatement) parser.parseStatementList().get(0);
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET NAMES DEFAULT", output);
|
||||
@@ -190,16 +188,16 @@ public class DALParserTest extends TestCase {
|
||||
public void test_setNames_1() throws Exception {
|
||||
String sql = "SET NAMEs 'utf8' collatE \"latin1_danish_ci\" ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlSetNamesStatement set = (MySqlSetNamesStatement) parser.parseStatementList().get(0);
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET NAMES utf8 COLLATE latin1_danish_ci", output);
|
||||
Assert.assertEquals("SET NAMES 'utf8' COLLATE latin1_danish_ci", output);
|
||||
}
|
||||
|
||||
public void test_setNames_2() throws Exception {
|
||||
String sql = "SET NAMEs utf8 ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlSetNamesStatement set = (MySqlSetNamesStatement) parser.parseStatementList().get(0);
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET NAMES utf8", output);
|
||||
@@ -208,16 +206,16 @@ public class DALParserTest extends TestCase {
|
||||
public void test_setCharSet() throws Exception {
|
||||
String sql = "SET CHARACTEr SEt 'utf8' ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlSetCharSetStatement set = (MySqlSetCharSetStatement) parser.parseStatementList().get(0);
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET CHARACTER SET utf8", output);
|
||||
Assert.assertEquals("SET CHARACTER SET 'utf8'", output);
|
||||
}
|
||||
|
||||
public void test_setCharSet_1() throws Exception {
|
||||
String sql = "SET CHARACTEr SEt DEFaULT ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlSetCharSetStatement set = (MySqlSetCharSetStatement) parser.parseStatementList().get(0);
|
||||
SQLStatement set = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(set);
|
||||
Assert.assertEquals("SET CHARACTER SET DEFAULT", output);
|
||||
|
||||
@@ -35,8 +35,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlResetStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
|
||||
@@ -119,9 +117,7 @@ public class MySqlASTVisitorAdapterTest extends TestCase {
|
||||
new MySqlOutFileExpr().accept(adapter);
|
||||
new MySqlUpdateStatement().accept(adapter);
|
||||
new MySqlSetTransactionStatement().accept(adapter);
|
||||
new MySqlSetNamesStatement().accept(adapter);
|
||||
new MySqlShowMasterLogsStatement().accept(adapter);
|
||||
new MySqlSetCharSetStatement().accept(adapter);
|
||||
new MySqlShowAuthorsStatement().accept(adapter);
|
||||
new MySqlShowCollationStatement().accept(adapter);
|
||||
new MySqlShowBinLogEventsStatement().accept(adapter);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.mysql;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSetTest_2 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "set character set utf8, names utf8mb4;";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
// print(stmtList);
|
||||
|
||||
Assert.assertEquals(1, stmtList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
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(0, visitor.getTables().size());
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
String text = SQLUtils.toMySqlString(stmt);
|
||||
|
||||
Assert.assertEquals("SET CHARACTER SET utf8, NAMES utf8mb4;", text);
|
||||
}
|
||||
}
|
||||
@@ -60,13 +60,13 @@ public class OracleMergeTest2 extends OracleTest {
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.create;
|
||||
|
||||
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 com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleCreateTableTest68 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
" CREATE TABLE \"JWGZPT\".\"A\" \n" +
|
||||
" ( \"XM\" VARCHAR2(100), \n" +
|
||||
" \"SFZH\" VARCHAR2(20), \n" +
|
||||
" \"GZDW\" VARCHAR2(200)\n" +
|
||||
" ) SEGMENT CREATION IMMEDIATE \n" +
|
||||
" PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 \n" +
|
||||
" NOCOMPRESS LOGGING\n" +
|
||||
" STORAGE(INITIAL 81920 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645\n" +
|
||||
" PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1\n" +
|
||||
" BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)\n" +
|
||||
" TABLESPACE \"TBS_JWGZPT\" ";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
assertEquals("CREATE TABLE \"JWGZPT\".\"A\" (\n" +
|
||||
"\t\"XM\" VARCHAR2(100),\n" +
|
||||
"\t\"SFZH\" VARCHAR2(20),\n" +
|
||||
"\t\"GZDW\" VARCHAR2(200)\n" +
|
||||
")\n" +
|
||||
"PCTFREE 10\n" +
|
||||
"PCTUSED 40\n" +
|
||||
"INITRANS 1\n" +
|
||||
"MAXTRANS 255\n" +
|
||||
"NOCOMPRESS\n" +
|
||||
"LOGGING\n" +
|
||||
"TABLESPACE \"TBS_JWGZPT\"\n" +
|
||||
"STORAGE (\n" +
|
||||
"\tINITIAL 81920\n" +
|
||||
"\tNEXT 1048576\n" +
|
||||
"\tMINEXTENTS 1\n" +
|
||||
"\tMAXEXTENTS 2147483645\n" +
|
||||
"\tPCTINCREASE 0\n" +
|
||||
"\tFREELISTS 1\n" +
|
||||
"\tFREELIST GROUPS 1\n" +
|
||||
"\tBUFFER_POOL DEFAULT\n" +
|
||||
"\tFLASH_CACHE DEFAULT\n" +
|
||||
"\tCELL_FLASH_CACHE DEFAULT\n" +
|
||||
")",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
|
||||
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(3, visitor.getColumns().size());
|
||||
|
||||
assertTrue(visitor.getColumns().contains(new TableStat.Column("JWGZPT.A", "XM")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleSelectTest65_error extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"select 1 from hhh as select 1"; //
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
} catch (ParserException ex) {
|
||||
error = ex;
|
||||
}
|
||||
assertNotNull(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user