mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修复type不能作为别名 char()方法解析失败的问题
This commit is contained in:
@@ -415,7 +415,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
expr = matchAgainstExpr;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if ("CONVERT".equalsIgnoreCase(ident)) {
|
||||
} else if (("CONVERT".equalsIgnoreCase(ident))||("CHAR".equalsIgnoreCase(ident))) {
|
||||
lexer.nextToken();
|
||||
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
|
||||
|
||||
|
||||
@@ -709,7 +709,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("CONVERT".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (("CONVERT".equalsIgnoreCase(x.getMethodName()))||"CHAR".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (x.getOwner() != null) {
|
||||
x.getOwner().accept(this);
|
||||
print(".");
|
||||
|
||||
@@ -142,6 +142,7 @@ public class SQLParser {
|
||||
case EXCLUSIVE:
|
||||
case MODE:
|
||||
case ADVISE:
|
||||
case TYPE:
|
||||
alias = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
return alias;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.mysql;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
|
||||
import java.util.List;
|
||||
/**
|
||||
*测试char() CHAR(N,... [USING charset_name])
|
||||
*/
|
||||
public class MySqlTest_Char extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT char(888 using utf8)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
assertEquals(output(statementList),"SELECT char(888 USING utf8)");
|
||||
}
|
||||
public void test_1() throws Exception {
|
||||
String sql = "SELECT char('abc8a9b10c' using utf8)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
assertEquals(output(statementList),"SELECT char('abc8a9b10c' USING utf8)");
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
String sql = "SELECT char(12,321,'lq9s9f','abc8a9b10c' using utf8)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
assertEquals(output(statementList),"SELECT char(12, 321, 'lq9s9f', 'abc8a9b10c' USING utf8)");
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
String sql = "SELECT char(12,321,'lq9s9f','abc8a9b10c')";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
assertEquals(output(statementList),"SELECT char(12, 321, 'lq9s9f', 'abc8a9b10c')");
|
||||
}
|
||||
|
||||
public void test_4() throws Exception {
|
||||
String sql = "SELECT CHAR(77,121,83,81,'76' using utf8)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
assertEquals(output(statementList),"SELECT CHAR(77, 121, 83, 81, '76' USING utf8)");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.alibaba.druid.bvt.sql.postgresql.expr;
|
||||
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;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import org.junit.Assert;
|
||||
import java.util.List;
|
||||
/**
|
||||
*测试 TYPE作为别名
|
||||
*/
|
||||
public class AliasTest_Type extends PGTest {
|
||||
public void test_timestamp() throws Exception {
|
||||
String sql = "select column1 as TYPE from table1 where xx=1";
|
||||
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);
|
||||
String mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcUtils.POSTGRESQL);
|
||||
System.out.println(mergedSql);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user