improved sql parser. for issue #1775

This commit is contained in:
wenshao
2017-06-14 10:01:07 +08:00
parent 820d0a2e1a
commit e170df7025
18 changed files with 370 additions and 206 deletions
@@ -24,8 +24,8 @@ public class MySqlSetCharSetStatement extends MySqlStatementImpl {
private String collate;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
// visitor.visit(this);
// visitor.endVisit(this);
}
public String getCharSet() {
@@ -24,8 +24,8 @@ public class MySqlSetNamesStatement extends MySqlStatementImpl {
private String collate;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
// visitor.visit(this);
// visitor.endVisit(this);
}
public String getCharSet() {
@@ -690,7 +690,37 @@ public class MySqlExprParser extends SQLExprParser {
}
if ("NAMES".equalsIgnoreCase(ident)) {
// skip
String charset = lexer.stringVal();
boolean chars = false;
if (lexer.token() == Token.IDENTIFIER) {
lexer.nextToken();
} else if (lexer.token() == Token.DEFAULT) {
charset = "DEFAULT";
lexer.nextToken();
} else {
chars = true;
accept(Token.LITERAL_CHARS);
}
if (identifierEquals("COLLATE")) {
MySqlCharExpr charsetExpr = new MySqlCharExpr(charset);
lexer.nextToken();
String collate = lexer.stringVal();
lexer.nextToken();
charsetExpr.setCollate(collate);
item.setValue(charsetExpr);
} else {
item.setValue(chars
? new SQLCharExpr(charset)
: new SQLIdentifierExpr(charset)
);
}
item.setTarget(var);
return item;
} else if ("CHARACTER".equalsIgnoreCase(ident)) {
var = new SQLIdentifierExpr("CHARACTER SET");
accept(Token.SET);
@@ -71,8 +71,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
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.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
@@ -2333,48 +2331,48 @@ public class MySqlStatementParser extends SQLStatementParser {
}
return stmt;
} else if (identifierEquals("NAMES")) {
lexer.nextToken();
MySqlSetNamesStatement stmt = new MySqlSetNamesStatement();
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
stmt.setDefault(true);
} else {
String charSet = lexer.stringVal();
stmt.setCharSet(charSet);
lexer.nextToken();
if (identifierEquals(COLLATE2)) {
lexer.nextToken();
String collate = lexer.stringVal();
stmt.setCollate(collate);
lexer.nextToken();
}
}
return stmt;
} else if (identifierEquals(CHARACTER)) {
lexer.nextToken();
accept(Token.SET);
MySqlSetCharSetStatement stmt = new MySqlSetCharSetStatement();
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
stmt.setDefault(true);
} else {
String charSet = lexer.stringVal();
stmt.setCharSet(charSet);
lexer.nextToken();
if (identifierEquals(COLLATE2)) {
lexer.nextToken();
String collate = lexer.stringVal();
stmt.setCollate(collate);
lexer.nextToken();
}
}
return stmt;
// } else if (identifierEquals("NAMES")) {
// lexer.nextToken();
//
// MySqlSetNamesStatement stmt = new MySqlSetNamesStatement();
// if (lexer.token() == Token.DEFAULT) {
// lexer.nextToken();
// stmt.setDefault(true);
// } else {
// String charSet = lexer.stringVal();
// stmt.setCharSet(charSet);
// lexer.nextToken();
// if (identifierEquals(COLLATE2)) {
// lexer.nextToken();
//
// String collate = lexer.stringVal();
// stmt.setCollate(collate);
// lexer.nextToken();
// }
// }
// return stmt;
// } else if (identifierEquals(CHARACTER)) {
// lexer.nextToken();
//
// accept(Token.SET);
//
// MySqlSetCharSetStatement stmt = new MySqlSetCharSetStatement();
// if (lexer.token() == Token.DEFAULT) {
// lexer.nextToken();
// stmt.setDefault(true);
// } else {
// String charSet = lexer.stringVal();
// stmt.setCharSet(charSet);
// lexer.nextToken();
// if (identifierEquals(COLLATE2)) {
// lexer.nextToken();
//
// String collate = lexer.stringVal();
// stmt.setCollate(collate);
// lexer.nextToken();
// }
// }
// return stmt;
} else {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
@@ -69,8 +69,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.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
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.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
@@ -257,14 +255,6 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
void endVisit(MySqlSetTransactionStatement x);
boolean visit(MySqlSetNamesStatement x);
void endVisit(MySqlSetNamesStatement x);
boolean visit(MySqlSetCharSetStatement x);
void endVisit(MySqlSetCharSetStatement x);
boolean visit(MySqlShowBinaryLogsStatement x);
void endVisit(MySqlShowBinaryLogsStatement x);
@@ -71,8 +71,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.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
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.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
@@ -443,26 +441,6 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
}
@Override
public boolean visit(MySqlSetNamesStatement x) {
return true;
}
@Override
public void endVisit(MySqlSetNamesStatement x) {
}
@Override
public boolean visit(MySqlSetCharSetStatement x) {
return true;
}
@Override
public void endVisit(MySqlSetCharSetStatement x) {
}
@Override
public boolean visit(MySqlShowAuthorsStatement x) {
return true;
@@ -79,8 +79,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.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
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.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
@@ -1769,46 +1767,35 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlSetTransactionStatement x) {
}
//
// @Override
// public boolean visit(MySqlSetNamesStatement x) {
// print0(ucase ? "SET NAMES " : "set names ");
// if (x.isDefault()) {
// print0(ucase ? "DEFAULT" : "default");
// } else {
// print0(x.getCharSet());
// if (x.getCollate() != null) {
// print0(ucase ? " COLLATE " : " collate ");
// print0(x.getCollate());
// }
// }
// return false;
// }
@Override
public boolean visit(MySqlSetNamesStatement x) {
print0(ucase ? "SET NAMES " : "set names ");
if (x.isDefault()) {
print0(ucase ? "DEFAULT" : "default");
} else {
print0(x.getCharSet());
if (x.getCollate() != null) {
print0(ucase ? " COLLATE " : " collate ");
print0(x.getCollate());
}
}
return false;
}
@Override
public void endVisit(MySqlSetNamesStatement x) {
}
@Override
public boolean visit(MySqlSetCharSetStatement x) {
print0(ucase ? "SET CHARACTER SET " : "set character set ");
if (x.isDefault()) {
print0(ucase ? "DEFAULT" : "default");
} else {
print0(x.getCharSet());
if (x.getCollate() != null) {
print0(ucase ? " COLLATE " : " collate ");
print0(x.getCollate());
}
}
return false;
}
@Override
public void endVisit(MySqlSetCharSetStatement x) {
}
// public boolean visit(MySqlSetCharSetStatement x) {
// print0(ucase ? "SET CHARACTER SET " : "set character set ");
// if (x.isDefault()) {
// print0(ucase ? "DEFAULT" : "default");
// } else {
// print0(x.getCharSet());
// if (x.getCollate() != null) {
// print0(ucase ? " COLLATE " : " collate ");
// print0(x.getCollate());
// }
// }
// return false;
// }
@Override
public void endVisit(MySqlShowAuthorsStatement x) {
@@ -2938,9 +2925,24 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public boolean visit(SQLAssignItem x) {
x.getTarget().accept(this);
if (!"NAMES".equalsIgnoreCase(x.getTarget().toString())) {
String tagetString = x.getTarget().toString();
boolean mysqlSpecial = false;
if (JdbcConstants.MYSQL.equals(dbType)) {
mysqlSpecial = "NAMES".equalsIgnoreCase(tagetString)
|| "CHARACTER SET".equalsIgnoreCase(tagetString)
|| "CHARSET".equalsIgnoreCase(tagetString);
}
if (!mysqlSpecial) {
x.getTarget().accept(this);
print0(" = ");
} else {
print0(ucase ? tagetString.toUpperCase() : tagetString.toLowerCase());
print(' ');
}
x.getValue().accept(this);
return false;
@@ -86,8 +86,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.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
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.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
@@ -587,26 +585,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
}
@Override
public boolean visit(MySqlSetNamesStatement x) {
return false;
}
@Override
public void endVisit(MySqlSetNamesStatement x) {
}
@Override
public boolean visit(MySqlSetCharSetStatement x) {
return false;
}
@Override
public void endVisit(MySqlSetCharSetStatement x) {
}
@Override
public boolean visit(MySqlShowAuthorsStatement x) {
return false;
@@ -48,8 +48,6 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
private OracleStorageClause storage;
private OracleLobStorageClause lobStorage;
private boolean organizationIndex = false;
private Integer pctfree;
private Integer pctused;
private Integer initrans;
@@ -628,10 +628,10 @@ public class OracleSelectParser extends SQLSelectParser {
private void parseTableSourceQueryTableExpr(OracleSelectTableReference tableReference) {
tableReference.setExpr(this.exprParser.expr());
{
FlashbackQueryClause clause = flashback();
tableReference.setFlashback(clause);
}
// {
// FlashbackQueryClause clause = flashback();
// tableReference.setFlashback(clause);
// }
if (identifierEquals("SAMPLE")) {
lexer.nextToken();
@@ -726,40 +726,31 @@ public class OracleSelectParser extends SQLSelectParser {
}
private FlashbackQueryClause flashback() {
if (lexer.token() == Token.AS) {
accept(Token.OF);
if (identifierEquals("SCN")) {
AsOfFlashbackQueryClause clause = new AsOfFlashbackQueryClause();
clause.setType(AsOfFlashbackQueryClause.Type.SCN);
lexer.nextToken();
}
clause.setExpr(exprParser.expr());
if (lexer.token() == Token.OF) {
return clause;
} else if (identifierEquals("SNAPSHOT")) {
lexer.nextToken();
accept(Token.LPAREN);
AsOfSnapshotClause clause = new AsOfSnapshotClause();
clause.setExpr(this.expr());
accept(Token.RPAREN);
if (identifierEquals("SCN")) {
AsOfFlashbackQueryClause clause = new AsOfFlashbackQueryClause();
clause.setType(AsOfFlashbackQueryClause.Type.SCN);
lexer.nextToken();
clause.setExpr(exprParser.expr());
return clause;
} else if (identifierEquals("SNAPSHOT")) {
lexer.nextToken();
accept(Token.LPAREN);
AsOfSnapshotClause clause = new AsOfSnapshotClause();
clause.setExpr(this.expr());
accept(Token.RPAREN);
return clause;
} else {
AsOfFlashbackQueryClause clause = new AsOfFlashbackQueryClause();
acceptIdentifier("TIMESTAMP");
clause.setType(AsOfFlashbackQueryClause.Type.TIMESTAMP);
clause.setExpr(exprParser.expr());
return clause;
}
return clause;
} else {
AsOfFlashbackQueryClause clause = new AsOfFlashbackQueryClause();
acceptIdentifier("TIMESTAMP");
clause.setType(AsOfFlashbackQueryClause.Type.TIMESTAMP);
clause.setExpr(exprParser.expr());
return clause;
}
return null;
}
protected SQLTableSource primaryTableSourceRest(SQLTableSource tableSource) {
@@ -784,9 +775,10 @@ public class OracleSelectParser extends SQLSelectParser {
if (lexer.token() == Token.OF) {
tableSource.setFlashback(flashback());
return parseTableSourceRest(tableSource);
}
tableSource.setAlias(tableAlias());
tableSource.setAlias(tableAlias(true));
} else if ((tableSource.getAlias() == null) || (tableSource.getAlias().length() == 0)) {
if (lexer.token() != Token.LEFT && lexer.token() != Token.RIGHT && lexer.token() != Token.FULL) {
tableSource.setAlias(tableAlias());
@@ -61,12 +61,30 @@ public class SQLParser {
}
protected String tableAlias() {
if (lexer.token() == Token.CONNECT || lexer.token() == Token.START) {
return tableAlias(false);
}
protected String tableAlias(boolean must) {
final Token token = lexer.token();
if (token == Token.CONNECT
|| token == Token.START
|| token == Token.SELECT
|| token == Token.FROM
|| token == Token.WHERE) {
if (must) {
throw new ParserException("illegal alias. " + lexer.info());
}
return null;
}
if (identifierEquals("START") || identifierEquals("CONNECT")) {
return null;
if (token == Token.IDENTIFIER) {
String ident = lexer.stringVal;
if (ident.equalsIgnoreCase("START") || ident.equalsIgnoreCase("CONNECT")) {
if (must) {
throw new ParserException("illegal alias. " + lexer.info());
}
return null;
}
}
return this.as();
@@ -59,8 +59,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
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.MySqlShowGrantsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
@@ -2453,9 +2451,7 @@ public class WallVisitorUtils {
allow = config.isDropTableAllow();
denyMessage = "drop table not allow";
errorCode = ErrorCode.DROP_TABLE_NOT_ALLOW;
} else if (x instanceof MySqlSetCharSetStatement //
|| x instanceof MySqlSetNamesStatement //
|| x instanceof SQLSetStatement //
} else if (x instanceof SQLSetStatement //
|| x instanceof SQLServerSetStatement) {
allow = config.isSetAllow();
denyMessage = "set not allow";
@@ -2571,8 +2567,7 @@ public class WallVisitorUtils {
List<SQLStatement> statementList = parser.parseStatementList();
if (statementList != null && statementList.size() > 0) {
SQLStatement statement = statementList.get(0);
if (statement instanceof SQLSetStatement || statement instanceof MySqlSetCharSetStatement
|| statement instanceof MySqlSetNamesStatement) {
if (statement instanceof SQLSetStatement) {
isWhite = true;
}
}
@@ -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);
}
}