All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.easy.query.gauss.db.func.GaussDBLikeSQLFunction Maven / Gradle / Ivy

The newest version!
package com.easy.query.gauss.db.func;

import com.easy.query.core.enums.SQLLikeEnum;
import com.easy.query.core.expression.parser.core.available.TableAvailable;
import com.easy.query.core.func.column.ColumnExpression;
import com.easy.query.core.func.column.ColumnFuncValueExpression;
import com.easy.query.core.func.def.AbstractExpressionSQLFunction;

import java.util.List;

/**
 * create time 2024/3/11 20:50
 * 文件说明
 *
 * @author xuejiaming
 */
public class GaussDBLikeSQLFunction extends AbstractExpressionSQLFunction {
    private final List columnExpressions;
    private final SQLLikeEnum sqlLikeEnum;

    public GaussDBLikeSQLFunction(List columnExpressions, SQLLikeEnum sqlLikeEnum) {

        this.columnExpressions = columnExpressions;
        this.sqlLikeEnum = sqlLikeEnum;
    }

    @Override
    public String sqlSegment(TableAvailable defaultTable) {
        if(columnExpressions.size()!=2){
            throw new IllegalArgumentException("bank arguments != 1");
        }
        ColumnExpression columnExpression = columnExpressions.get(1);
        if(columnExpression instanceof ColumnFuncValueExpression){
            ColumnFuncValueExpression columnFuncValueExpression = (ColumnFuncValueExpression) columnExpression;
            Object value = columnFuncValueExpression.getValue();
            if(value!=null){
                String valueString = value.toString();
                if(valueString.contains("%")){
                    if(sqlLikeEnum==SQLLikeEnum.LIKE_PERCENT_RIGHT){
                        return "STRPOS({1},{0}) = 1";
                    }
                    if(sqlLikeEnum==SQLLikeEnum.LIKE_PERCENT_LEFT){
                        return "STRPOS({1},{0}) = CHAR_LENGTH({1})";
                    }
                    return "STRPOS({1},{0}) > 0";
                }
            }
        }
        if(sqlLikeEnum==SQLLikeEnum.LIKE_PERCENT_RIGHT){
            return "{0} LIKE (CONCAT(({1})::TEXT , '%'))";
        }
        if(sqlLikeEnum==SQLLikeEnum.LIKE_PERCENT_LEFT){
            return "{0} LIKE (CONCAT('%' , ({1})::TEXT))";
        }
        return "{0} LIKE (CONCAT('%' , ({1})::TEXT , '%'))";
    }

    @Override
    public int paramMarks() {
        return columnExpressions.size();
    }

    @Override
    protected List getColumnExpressions() {
        return columnExpressions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy