Search in sources :

Example 6 with Parameter

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;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Parameter(com.ctrip.platform.dal.daogen.entity.Parameter)

Aggregations

Parameter (com.ctrip.platform.dal.daogen.entity.Parameter)6 ArrayList (java.util.ArrayList)4 Matcher (java.util.regex.Matcher)4 Status (com.ctrip.platform.dal.daogen.domain.Status)1 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)1 ValidateResult (com.ctrip.platform.dal.daogen.sql.validate.ValidateResult)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1