Search in sources :

Example 6 with SqlParamsModel

use of org.sagacity.sqltoy.config.model.SqlParamsModel in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method convertParamsToNamed.

/**
 * update 2020-08-15 增强对非条件参数?的判断处理
 *
 * @todo sql中替换?为:sagParamName+i形式,便于查询处理(主要针对分页和取随机记录的查询)
 * @param sql
 * @param startIndex
 * @return
 */
public static SqlParamsModel convertParamsToNamed(String sql, int startIndex) {
    SqlParamsModel sqlParam = new SqlParamsModel();
    if (sql == null || sql.trim().equals("")) {
        return sqlParam;
    }
    // 以?号对字符串进行切割,并忽视'' 和"" 之间的
    String[] strs = StringUtil.splitExcludeSymMark(sql, SqlConfigParseUtils.ARG_NAME, QuesFilters);
    int size = strs.length;
    if (size == 1) {
        sqlParam.setSql(sql);
        return sqlParam;
    }
    String preName = SqlToyConstants.DEFAULT_PARAM_NAME;
    StringBuilder result = new StringBuilder();
    String[] paramsName = new String[size - 1];
    int index;
    for (int i = 0; i < size - 1; i++) {
        index = i + startIndex + 1;
        result.append(strs[i]).append(":" + preName + index);
        paramsName[i] = preName + index;
    }
    result.append(strs[size - 1]);
    sqlParam.setSql(result.toString());
    sqlParam.setParamsName(paramsName);
    sqlParam.setParamCnt(size - 1);
    return sqlParam;
}
Also used : SqlParamsModel(org.sagacity.sqltoy.config.model.SqlParamsModel)

Example 7 with SqlParamsModel

use of org.sagacity.sqltoy.config.model.SqlParamsModel in project sagacity-sqltoy by chenrenfei.

the class DialectUtilsTest method testConvertQuesToNamed.

@Test
public void testConvertQuesToNamed() {
    String sql = "select *,' and ? ' from table where a=' ? ' and b in(?,?,?) and c like ? and d>? and m<\"?\") or a>?";
    SqlParamsModel unifySqlParams = DialectUtils.convertParamsToNamed(sql, 0);
    System.err.println(unifySqlParams.getSql());
    System.err.println("paramCnt=" + unifySqlParams.getParamCnt());
    for (String s : unifySqlParams.getParamsName()) {
        System.err.println("param=" + s);
    }
    sql = "select *,' and ? ' from table where a=' ? ' ";
    unifySqlParams = DialectUtils.convertParamsToNamed(sql, 0);
    System.err.println(unifySqlParams.getSql());
    System.err.println("paramCnt=" + unifySqlParams.getParamCnt());
// sql = "select * from table where a=' ? ' and b is ? and c like ? and m between ? and ?";
// unifySqlParams = DialectUtils.convertParamsToNamed(sql, 0);
// System.err.println(unifySqlParams.getSql());
// System.err.println("paramCnt=" + unifySqlParams.getParamCnt());
// 
// sql = "update table set a.nnn=?,xxx=?_ where t.xxx=\\?";
// unifySqlParams = DialectUtils.convertParamsToNamed(sql, 0);
// System.err.println(unifySqlParams.getSql());
// System.err.println("paramCnt=" + unifySqlParams.getParamCnt());
// //
// sql = "update table set a.nnn=?,xxx=? where t.xxx=?;";
// unifySqlParams = DialectUtils.convertParamsToNamed(sql, 0);
// System.err.println(unifySqlParams.getSql());
// System.err.println("paramCnt=" + unifySqlParams.getParamCnt());
}
Also used : SqlParamsModel(org.sagacity.sqltoy.config.model.SqlParamsModel) Test(org.junit.jupiter.api.Test)

Aggregations

SqlParamsModel (org.sagacity.sqltoy.config.model.SqlParamsModel)7 ArrayList (java.util.ArrayList)3 Connection (java.sql.Connection)2 List (java.util.List)2 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)2 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)2 Matcher (java.util.regex.Matcher)1 Test (org.junit.jupiter.api.Test)1 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)1 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)1 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)1 BaseException (org.sagacity.sqltoy.exception.BaseException)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)1