use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class DbUtils method testUpdateSql.
public static int testUpdateSql(String allInOneName, String sql, String params) throws Exception {
int result;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DataSourceUtil.getConnection(allInOneName);
connection.setAutoCommit(false);
List<Parameter> list = new ArrayList<>();
String[] parameters = params.split(";");
if (parameters != null && parameters.length > 0) {
for (String p : parameters) {
if (p.isEmpty()) {
continue;
}
String[] tuple = p.split(",");
if (tuple != null && tuple.length > 0) {
Parameter parameter = new Parameter();
parameter.setName(tuple[0]);
parameter.setType(Integer.valueOf(tuple[1]));
list.add(parameter);
}
}
}
Matcher matcher = pattern.matcher(sql);
// Match C# parameters
if (matcher.find()) {
list = getActualParameters(sql, list);
}
Matcher m = inRegxPattern.matcher(sql);
String temp = sql;
while (m.find()) {
temp = temp.replace(m.group(1), String.format("(?) "));
}
String replacedSql = temp.replaceAll(expression, "?");
preparedStatement = connection.prepareStatement(replacedSql);
int index = 0;
for (Parameter parameter : list) {
String name = parameter.getName();
int type = parameter.getType();
try {
index = Integer.valueOf(name);
} catch (NumberFormatException ex) {
index++;
}
if (type == 10001) {
preparedStatement.setObject(index, mockATest(type), Types.BINARY);
} else {
preparedStatement.setObject(index, mockATest(type), type);
}
}
result = preparedStatement.executeUpdate();
} catch (Throwable e) {
throw e;
} finally {
ResourceUtils.close(preparedStatement);
ResourceUtils.rollback(connection);
ResourceUtils.close(connection);
}
return result;
}
Aggregations