mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support TimeStamp Literal.
This commit is contained in:
@@ -35,6 +35,11 @@ public class SQLTimestampExpr extends SQLExprImpl implements SQLValuableExpr {
|
||||
|
||||
}
|
||||
|
||||
public SQLTimestampExpr(String literal){
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
|
||||
public SQLTimestampExpr clone() {
|
||||
SQLTimestampExpr x = new SQLTimestampExpr();
|
||||
x.literal = literal;
|
||||
|
||||
@@ -323,6 +323,12 @@ public class SQLExprParser extends SQLParser {
|
||||
SQLDateExpr dateExpr = new SQLDateExpr();
|
||||
dateExpr.setLiteral(literal);
|
||||
sqlExpr = dateExpr;
|
||||
} else if (hash_lower == FnvHash.Constants.TIMESTAMP
|
||||
&& (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.VARIANT)
|
||||
&& !JdbcConstants.ORACLE.equals(dbType)) {
|
||||
SQLTimestampExpr dateExpr = new SQLTimestampExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
sqlExpr = dateExpr;
|
||||
} else if (JdbcConstants.MYSQL.equalsIgnoreCase(dbType) && ident.startsWith("0x") && (ident.length() % 2) == 0) {
|
||||
sqlExpr = new SQLHexExpr(ident.substring(2));
|
||||
} else {
|
||||
|
||||
@@ -97,6 +97,9 @@ public final class ExportParameterVisitorUtils {
|
||||
} else if (param instanceof SQLHexExpr) {
|
||||
value = ((SQLHexExpr) param).toBytes();
|
||||
replace = true;
|
||||
} else if (param instanceof SQLTimestampExpr || param instanceof SQLDateExpr) {
|
||||
value = ((SQLTimestampExpr) param).getValue();
|
||||
replace = true;
|
||||
} else if (param instanceof SQLListExpr) {
|
||||
SQLListExpr list = ((SQLListExpr) param);
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.visitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
|
||||
@@ -287,4 +288,18 @@ public class ParameterizedOutputVisitorUtils {
|
||||
|
||||
return new SQLASTOutputVisitor(out, true);
|
||||
}
|
||||
|
||||
public static String restore(String sql, String dbType, List<Object> parameters) {
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
|
||||
visitor.setInputParameters(parameters);
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4278,6 +4278,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLTimestampExpr x) {
|
||||
if (this.parameterized) {
|
||||
print('?');
|
||||
incrementReplaceCunt();
|
||||
|
||||
if(this.parameters != null){
|
||||
ExportParameterVisitorUtils.exportParameter(this.parameters, x);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
print0(ucase ? "TIMESTAMP " : "timestamp ");
|
||||
|
||||
if (x.isWithTimeZone()) {
|
||||
@@ -5537,6 +5547,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public boolean visit(SQLDateExpr x) {
|
||||
if (this.parameterized) {
|
||||
print('?');
|
||||
incrementReplaceCunt();
|
||||
|
||||
if(this.parameters != null){
|
||||
ExportParameterVisitorUtils.exportParameter(this.parameters, x);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLExpr literal = x.getLiteral();
|
||||
print0(ucase ? "DATE " : "date ");
|
||||
printExpr(literal);
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.insert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlInsertTest_25_time extends MysqlTest {
|
||||
|
||||
public void test_insert() throws Exception {
|
||||
String sql = "INSERT INTO DB1.TB2 (col1, col2, col3) VALUES(1, Timestamp '2019-01-01:12:12:21', '3')";
|
||||
|
||||
{
|
||||
List<Object> outParameters = new ArrayList<Object>();
|
||||
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, outParameters);
|
||||
assertEquals("INSERT INTO DB1.TB2(col1, col2, col3)\n" +
|
||||
"VALUES (?, ?, ?)", psql);
|
||||
|
||||
assertEquals(3, outParameters.size());
|
||||
|
||||
String rsql = ParameterizedOutputVisitorUtils.restore(psql, JdbcConstants.MYSQL, outParameters);
|
||||
assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" +
|
||||
"VALUES (1, '2019-01-01:12:12:21', '3')", rsql);
|
||||
}
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
|
||||
|
||||
assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" +
|
||||
"VALUES (1, TIMESTAMP '2019-01-01:12:12:21', '3')", SQLUtils.toMySqlString(insertStmt));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user