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

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

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

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcSql {

    private final Map parameterValues = new HashMap<>();
    private final SQL sql;

    public interface Clear{
        void clear() throws SQLException;
    }

    public interface CallBack{
       void run(int parameterIndex) throws SQLException;
    }

    public JdbcSql(SQL sql) {
        this.sql = sql;
    }

    public void clearParameters(Clear clear) throws SQLException{
        parameterValues.clear();
        if(clear!=null){
            clear.clear();
        }
    }

    public void put(String sqlKey, Object value,CallBack callBack) throws SQLException {
       int index = sql.getIndex(sqlKey);
       if(index!=0){
           this.put(index,value,callBack);
       }
    }


    public void put(int parameterIndex, Object value,CallBack callBack) throws SQLException {
        //skip remove index
        if(sql.hasRemoveIndex(parameterIndex)){
            return;
        }

        // rebuild index
        List removeIndexes =  sql.getRemoveIndexes();
        for (int index:removeIndexes){
            if(parameterIndex>index){
                parameterIndex--;
            }
        }

        parameterValues.put(parameterIndex,new JdbcValue(value));
        if(callBack!=null){
            callBack.run(parameterIndex);
        }
    }

    public String getExecuteSql(){
        int index = 1;
        StringBuilder builder = new StringBuilder();
        for (char character :sql.getSqlChars()){
            if(character=='?'){
                JdbcValue value = parameterValues.get(index++);
                builder.append(value.strVal());
            }else{
                builder.append(character);
            }
        }
        return builder.toString();
    }

    public String getSql() {
        return sql.getSql();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy