use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class DbUtils method getActualParameters.
private static List<Parameter> getActualParameters(final String sql, List<Parameter> parameters) {
List<Parameter> list = new ArrayList<>();
Matcher matcher = pattern.matcher(sql);
while (matcher.find()) {
String parameter = matcher.group();
Parameter p = new Parameter();
p.setName(parameter.substring(1));
list.add(p);
}
Map<String, Parameter> map = new HashMap<>();
if (parameters != null && parameters.size() > 0) {
for (Parameter p : parameters) {
String name = p.getName();
if (!map.containsKey(name)) {
map.put(name, p);
}
}
}
for (Parameter p : list) {
String name = p.getName();
Parameter temp = map.get(name);
if (temp != null) {
p.setType(temp.getType());
}
}
return list;
}
use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class DbUtils method testAQuerySql.
public static List<AbstractParameterHost> testAQuerySql(String allInOneName, String sql, String params, DalResultSetExtractor<List<AbstractParameterHost>> extractor) throws Exception {
List<AbstractParameterHost> result = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DataSourceUtil.getConnection(allInOneName);
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);
}
}
}
Boolean containsQuestionMark = sql.indexOf("?") > -1;
String temp1 = sql;
if (!containsQuestionMark) {
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("(?) "));
}
temp1 = temp.replaceAll(expression, "?");
}
preparedStatement = connection.prepareStatement(temp1);
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);
}
}
ResultSet rs = preparedStatement.executeQuery();
result = extractor.extract(rs);
} catch (Throwable e) {
throw e;
} finally {
ResourceUtils.close(resultSet);
ResourceUtils.close(preparedStatement);
ResourceUtils.close(connection);
}
return result;
}
use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class GenTaskByFreeSqlResource method getValues.
private String[] getValues(List<Parameter> list) {
if (list == null || list.size() == 0) {
return new String[0];
}
String[] array = new String[list.size()];
int index = 0;
for (Parameter p : list) {
array[index] = p.getValue();
index++;
}
return array;
}
use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class GenTaskByFreeSqlResource method getTypes.
private int[] getTypes(List<Parameter> list) {
if (list == null || list.size() == 0) {
return new int[0];
}
int[] array = new int[list.size()];
int index = 0;
for (Parameter p : list) {
array[index] = p.getType();
index++;
}
return array;
}
use of com.ctrip.platform.dal.daogen.entity.Parameter in project dal by ctripcorp.
the class GenTaskByFreeSqlResource method validateSQL.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("sqlValidate")
public Status validateSQL(@FormParam("db_name") String set_name, @FormParam("crud_type") String crud_type, @FormParam("sql_content") String sql_content, @FormParam("params") String params, @FormParam("pagination") boolean pagination, @FormParam("mockValues") String mockValues, @FormParam("mode_type") String modeType) {
Status status = Status.OK();
Boolean containsQuestionMark = sql_content.indexOf("?") > -1;
try {
Map<String, Parameter> map = new LinkedHashMap<>();
List<Parameter> list = new ArrayList<>();
if (!containsQuestionMark) {
Matcher matcher = pattern.matcher(sql_content);
while (matcher.find()) {
String parameter = matcher.group();
Parameter p = new Parameter();
// trim @
p.setName(parameter.substring(1));
list.add(p);
}
}
String[] values = mockValues.split(";");
String[] parameters = params.split(";");
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].isEmpty()) {
continue;
}
String[] array = parameters[i].split(",");
if (array != null && array.length > 0) {
String name = array[0];
if (name.isEmpty()) {
continue;
}
int type = Integer.valueOf(array[1]);
if (!map.containsKey(name)) {
Parameter p = new Parameter();
p.setType(type);
p.setValue(values[i]);
map.put(name, p);
}
}
}
}
// C#
if (list.size() > 0) {
for (Parameter p : list) {
String name = p.getName();
Parameter temp = map.get(name);
if (temp != null) {
p.setType(temp.getType());
p.setValue(temp.getValue());
}
}
} else {
// Java
for (Map.Entry<String, Parameter> entry : map.entrySet()) {
Parameter p = new Parameter();
Parameter temp = entry.getValue();
p.setType(temp.getType());
p.setValue(temp.getValue());
list.add(p);
}
}
if (!containsQuestionMark) {
sql_content = sql_content.replaceAll(expression, "?");
}
int[] sqlTypes = getTypes(list);
values = getValues(list);
String dbName = AllInOneNameUtils.getAllInOneName(set_name, modeType);
ValidateResult validResult = null;
String resultPrefix = "The affected rows is ";
if (pagination && "select".equalsIgnoreCase(crud_type)) {
sql_content = SqlBuilder.pagingQuerySql(sql_content, DbUtils.getDatabaseCategory(dbName), CurrentLanguage.Java);
sql_content = String.format(sql_content, 1, 2);
}
if ("select".equalsIgnoreCase(crud_type)) {
validResult = SQLValidation.queryValidate(dbName, sql_content, sqlTypes, values);
resultPrefix = "The result count is ";
} else {
validResult = SQLValidation.updateValidate(dbName, sql_content, sqlTypes, values);
}
if (validResult != null && validResult.isPassed()) {
status.setInfo(resultPrefix + validResult.getAffectRows());
status.setExplanJson(validResult.getMessage());
} else {
status = Status.ERROR();
status.setInfo(validResult.getMessage());
}
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
status = Status.ERROR();
status.setInfo(e.getMessage());
}
return status;
}
Aggregations