mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.sql.builder;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 09/07/2017.
|
||||
*/
|
||||
public class FunctionBuilder {
|
||||
private final String dbType;
|
||||
|
||||
public FunctionBuilder(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
// for character function
|
||||
public SQLMethodInvokeExpr length(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("length", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr lower(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("lower", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr upper(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("upper", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr substr(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("substr", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr ltrim(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("ltrim", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr rtrim(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("rtrim", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr trim(SQLExpr expr) {
|
||||
return new SQLMethodInvokeExpr("trim", null, expr);
|
||||
}
|
||||
|
||||
public SQLMethodInvokeExpr ifnull(SQLExpr expr1, SQLExpr expr2) {
|
||||
if (JdbcConstants.ALIYUN_ADS.equals(dbType)
|
||||
|| JdbcConstants.PRESTO.equals(dbType)
|
||||
|| JdbcConstants.ODPS.equals(dbType)) {
|
||||
return new SQLMethodInvokeExpr("coalesce", null, expr1, expr2);
|
||||
}
|
||||
|
||||
if (JdbcConstants.ORACLE.equals(dbType)) {
|
||||
return new SQLMethodInvokeExpr("nvl", null, expr1, expr2);
|
||||
}
|
||||
|
||||
if (JdbcConstants.SQL_SERVER.equals(dbType)) {
|
||||
return new SQLMethodInvokeExpr("isnull", null, expr1, expr2);
|
||||
}
|
||||
|
||||
return new SQLMethodInvokeExpr("ifnull", null, expr1, expr2);
|
||||
}
|
||||
}
|
||||
@@ -194,11 +194,7 @@ public class SQLSelectBuilderImpl implements SQLSelectBuilder {
|
||||
@Override
|
||||
public SQLSelectBuilderImpl whereAnd(String expr) {
|
||||
SQLSelectQueryBlock queryBlock = getQueryBlock();
|
||||
|
||||
SQLExpr exprObj = SQLUtils.toSQLExpr(expr, dbType);
|
||||
SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, exprObj, false,
|
||||
queryBlock.getWhere());
|
||||
queryBlock.setWhere(newCondition);
|
||||
queryBlock.addWhere(SQLUtils.toSQLExpr(expr, dbType));
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -222,86 +218,9 @@ public class SQLSelectBuilderImpl implements SQLSelectBuilder {
|
||||
|
||||
@Override
|
||||
public SQLSelectBuilderImpl limit(int rowCount, int offset) {
|
||||
SQLSelectQueryBlock queryBlock = getQueryBlock();
|
||||
|
||||
if (queryBlock instanceof MySqlSelectQueryBlock) {
|
||||
MySqlSelectQueryBlock mySqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
|
||||
|
||||
SQLLimit limit = new SQLLimit();
|
||||
limit.setRowCount(new SQLIntegerExpr(rowCount));
|
||||
if (offset > 0) {
|
||||
limit.setOffset(new SQLIntegerExpr(offset));
|
||||
}
|
||||
|
||||
mySqlQueryBlock.setLimit(limit);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (queryBlock instanceof SQLServerSelectQueryBlock) {
|
||||
SQLServerSelectQueryBlock sqlserverQueryBlock = (SQLServerSelectQueryBlock) queryBlock;
|
||||
if (offset <= 0) {
|
||||
SQLServerTop top = new SQLServerTop();
|
||||
top.setExpr(new SQLIntegerExpr(rowCount));
|
||||
sqlserverQueryBlock.setTop(top);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("not support offset");
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (queryBlock instanceof PGSelectQueryBlock) {
|
||||
PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
|
||||
SQLLimit limit = new SQLLimit();
|
||||
if (offset > 0) {
|
||||
limit.setOffset(new SQLIntegerExpr(offset));
|
||||
}
|
||||
limit.setRowCount(new SQLIntegerExpr(rowCount));
|
||||
pgQueryBlock.setLimit(limit);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (queryBlock instanceof DB2SelectQueryBlock) {
|
||||
DB2SelectQueryBlock db2QueryBlock = (DB2SelectQueryBlock) queryBlock;
|
||||
if (offset <= 0) {
|
||||
SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
|
||||
db2QueryBlock.setFirst(rowCountExpr);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("not support offset");
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (queryBlock instanceof OracleSelectQueryBlock) {
|
||||
OracleSelectQueryBlock oracleQueryBlock = (OracleSelectQueryBlock) queryBlock;
|
||||
if (offset <= 0) {
|
||||
SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
|
||||
SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, rowCountExpr, false,
|
||||
oracleQueryBlock.getWhere());
|
||||
queryBlock.setWhere(newCondition);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("not support offset");
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
if (queryBlock instanceof OdpsSelectQueryBlock) {
|
||||
OdpsSelectQueryBlock odpsQueryBlock = (OdpsSelectQueryBlock) queryBlock;
|
||||
|
||||
if (offset > 0) {
|
||||
throw new UnsupportedOperationException("not support offset");
|
||||
}
|
||||
|
||||
odpsQueryBlock.setLimit(new SQLLimit(new SQLIntegerExpr(rowCount)));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException();
|
||||
getQueryBlock()
|
||||
.limit(rowCount, offset);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected SQLSelectQueryBlock getQueryBlock() {
|
||||
|
||||
Reference in New Issue
Block a user