java 自定义 ? 占位符处理sql 拼接

package cn.nokia.rso.qz.core.sql;

public class SqlBuilder {
    StringBuffer sb;
    int _p_size = 0;

    public static final String TOKEN = "?";
    public static final String TOKEN_REPLACE = "$";

    public SqlBuilder(String sql) {
        init(sql);
    }


    void init(String sql) {
        sb = new StringBuffer(sql);
        int p_index = 1;
        int index;
        while ((index = sb.indexOf(TOKEN)) > 0) {
            sb.replace(index, index + 1, TOKEN_REPLACE + p_index++);
        }
    }

    public SqlBuilder setColumn(int index, String parameter) {
        int _index = sb.indexOf(TOKEN_REPLACE + index);
        sb.replace(_index, _index + 2, parameter);
        return this;
    }

    public SqlBuilder setString(int index, String parameter) {
        int _index = sb.indexOf(TOKEN_REPLACE + index);
        sb.replace(_index, _index + 2, "'" + parameter + "'");
        return this;
    }

    public SqlBuilder setNumber(int index, Integer parameter) {
        int _index = sb.indexOf(TOKEN_REPLACE + index);
        sb.replace(_index, _index + 2, String.valueOf(parameter));
        return this;
    }

    public SqlBuilder setNumber(int index, Double parameter) {
        int _index = sb.indexOf(TOKEN_REPLACE + index);
        sb.replace(_index, _index + 2, String.valueOf(parameter));
        return this;
    }


    public SqlBuilder setDate(int index, String parameter) {
        int _index = sb.indexOf(TOKEN_REPLACE + index);
        sb.replace(_index, _index + 2, "'" + parameter + "'");
        return this;
    }

    @Override
    public String toString() {
        return sb.toString();
    }
}

你可能感兴趣的:(java)