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

com.codingapi.springboot.permission.analyzer.CurdSelectSqlAnalyzer Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.codingapi.springboot.permission.analyzer;

import java.util.List;
import java.util.regex.Pattern;

/**
 * @author lorne
 * @since 1.0.0
 */
public class CurdSelectSqlAnalyzer extends BaseAnalyzerFilter implements SqlAnalyzerFilter{

    //Pattern
    private static final Pattern EQUAL_PATTERN = Pattern.compile("([a-zA-Z0-9_\\.]*\\s*[=]\\s*[?])");

    private static final Pattern CURD_SELECT_PATTERN = Pattern.compile("(^\\s*(select).*)",Pattern.CASE_INSENSITIVE);


    @Override
    public boolean match(SQL sql) {
        return match(CURD_SELECT_PATTERN,sql.getSql());
    }

    @Override
    public void doFilter(SQL sql) {
        List values = find(EQUAL_PATTERN,sql.getSql());
        if (values.size() > 0) {
            for (int i = 0; i < values.size(); i++) {
                String value = values.get(i);
                String[] kvs = value.split("=");
                sql.put(kvs[0].trim(), i+1);
            }
        }
    }

    @Override
    public String delete(String key, String sql) {
        //for example : update t_demo set name = ?, age = ? where id = ? , delete name will get result:update t_demo set age = ? where id = ?
        sql = sql.replaceFirst(String.format("\\s*%s\\s*=\\s*\\?\\s*[,]?",key), "");
        return sql;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy