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;
}
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());
}
Aggregations