org.onetwo.common.db.parser.SqlInfixVarConditionExpr Maven / Gradle / Ivy
package org.onetwo.common.db.parser;
import java.util.ArrayList;
import java.util.List;
import org.onetwo.common.db.sqlext.ExtQueryUtils;
import org.onetwo.common.db.sqlext.SQLKeys;
import org.onetwo.common.exception.BaseException;
import org.onetwo.common.lexer.AbstractParser.JTokenValue;
import org.onetwo.common.lexer.AbstractParser.JTokenValueCollection;
@SuppressWarnings("unchecked")
public class SqlInfixVarConditionExpr extends AbstractSqlVarObject implements SqlVarObject {
private SqlInfixConditionExpr expr;
private boolean rightVar = true;
public SqlInfixVarConditionExpr() {
super();
}
public SqlInfixVarConditionExpr(JTokenValueCollection left, SqlTokenKey operator, JTokenValueCollection right, boolean rightVar) {
super();
this.expr = new SqlInfixConditionExpr(left, operator, right);
this.rightVar = rightVar;
JTokenValueCollection varIds = null;
if(isRightVar()){
varIds = right;
}else{
varIds = left;
}
setNamed(varIds.contains(SqlTokenKey.VARNAME));
if(isNamed()){
JTokenValue tv = varIds.getTokenValue(SqlTokenKey.VARNAME);
if(tv==null)
throw new BaseException("not found var name in : " + varIds);
varname = tv.getValue().substring(1);
}else{
varname = SqlTokenKey.QUESTION.getName();
}
}
public boolean isRightVar() {
return rightVar;
}
public String getLeftString(){
return getLeftString(1);
}
protected String getLeftString(int varCount){
if(!isNamed()){
// return getLeft().getValues("");
// return getLeft().getVauesWithReplace(" ", SqlTokenKey.QUESTION, getActualPlaceHolder(varCount));
return SqlParserUtils.toSqlWithReplace(expr.getLeft(), SqlTokenKey.QUESTION, varCount);
}
if(isRightVar()){
// return getLeft().getValues("");
return SqlParserUtils.toFragmentSql(expr.getLeft());
}else{
// return getLeft().getVauesWithReplace(" ", SqlTokenKey.VARNAME, getActualPlaceHolder(varCount));
return SqlParserUtils.toSqlWithReplace(expr.getLeft(), SqlTokenKey.VARNAME, varCount);
}
}
protected String getRightString(int varCount){
if(!isNamed()){
// return getRight().getValues("");
// return getRight().getVauesWithReplace(" ", SqlTokenKey.QUESTION, getActualPlaceHolder(varCount));
return SqlParserUtils.toSqlWithReplace(expr.getRight(), SqlTokenKey.QUESTION, varCount);
}
if(isRightVar()){
// return getRight().getVauesWithReplace(" ", SqlTokenKey.VARNAME, getActualPlaceHolder(varCount));
return SqlParserUtils.toSqlWithReplace(expr.getRight(), SqlTokenKey.VARNAME, varCount);
}else{
// return getRight().getValues("");
return SqlParserUtils.toFragmentSql(expr.getRight());
}
}
// @Override
public boolean isInfix() {
return true;
}
// @Override
public String toJdbcSql(int varCount){
StringBuilder sql = new StringBuilder();
sql.append(getLeftString(varCount)).append(expr.getOperatorString()).append(" ").append(getRightString(varCount));
return sql.toString();
}
@Override
public String parseSql(SqlCondition condition){
StringBuilder sql = new StringBuilder();
if (condition.isMutiValue()) {
sql.append("( ");
int vindex = 0;
List> values = new ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy