Search in sources :

Example 1 with AbstractParameterHost

use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.

the class CsharpGivenSqlResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> hosts = new ArrayList<>();
    if (rs == null) {
        return hosts;
    }
    ResultSetMetaData metaData = rs.getMetaData();
    int count = metaData.getColumnCount();
    for (int i = 1; i <= count; i++) {
        CSharpParameterHost host = new CSharpParameterHost();
        String columnName = metaData.getColumnLabel(i);
        host.setName(columnName);
        String typeName = metaData.getColumnTypeName(i);
        boolean isUnsigned = DbUtils.isColumnUnsigned(typeName);
        int dataType = metaData.getColumnType(i);
        int length = metaData.getColumnDisplaySize(i);
        // 特殊处理
        DbType dbType = DbUtils.getDotNetDbType(typeName, dataType, length, isUnsigned, dbCategory);
        host.setDbType(dbType);
        String type = DbType.getCSharpType(host.getDbType());
        host.setType(type);
        host.setIdentity(false);
        host.setNullable(metaData.isNullable(i) == 1 ? true : false);
        host.setPrimary(false);
        host.setLength(length);
        host.setValueType(Consts.CSharpValueTypes.contains(host.getType()));
        hosts.add(host);
    }
    return hosts;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) ArrayList(java.util.ArrayList) DbType(com.ctrip.platform.dal.daogen.enums.DbType)

Example 2 with AbstractParameterHost

use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.

the class CsharpSelectFieldResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    ResultSetMetaData rsMeta = rs.getMetaData();
    List<AbstractParameterHost> hosts = new ArrayList<>();
    for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
        CSharpParameterHost pHost = new CSharpParameterHost();
        pHost.setName(rsMeta.getColumnLabel(i));
        pHost.setDbType(DbType.getDbTypeFromJdbcType(rsMeta.getColumnType(i)));
        pHost.setType(DbType.getCSharpType(pHost.getDbType()));
        pHost.setIdentity(false);
        pHost.setNullable(rsMeta.isNullable(i) == 1 ? true : false);
        pHost.setValueType(Consts.CSharpValueTypes.contains(pHost.getType()));
        pHost.setPrimary(false);
        pHost.setLength(rsMeta.getColumnDisplaySize(i));
        hosts.add(pHost);
    }
    return hosts;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) ArrayList(java.util.ArrayList)

Example 3 with AbstractParameterHost

use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.

the class JavaGivenSqlResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    List<AbstractParameterHost> paramHosts = new ArrayList<>();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        JavaParameterHost paramHost = new JavaParameterHost();
        paramHost.setName(rsmd.getColumnLabel(i));
        paramHost.setSqlType(rsmd.getColumnType(i));
        //paramHost.setJavaClass(Consts.jdbcSqlTypeToJavaClass.get(paramHost.getSqlType()));
        Class<?> javaClass = null;
        try {
            javaClass = Class.forName(rsmd.getColumnClassName(i));
        } catch (Exception e) {
            e.printStackTrace();
            javaClass = Consts.jdbcSqlTypeToJavaClass.get(paramHost.getSqlType());
        }
        paramHost.setJavaClass(javaClass);
        paramHost.setIdentity(false);
        paramHost.setNullable(rsmd.isNullable(i) == 1 ? true : false);
        paramHost.setPrimary(false);
        paramHost.setLength(rsmd.getColumnDisplaySize(i));
        paramHosts.add(paramHost);
    }
    return paramHosts;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException)

Example 4 with AbstractParameterHost

use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.

the class DbUtils method getSelectFieldHosts.

public static List<AbstractParameterHost> getSelectFieldHosts(String allInOneName, String sql, DalResultSetExtractor<List<AbstractParameterHost>> extractor) throws Exception {
    List<AbstractParameterHost> list = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    StatementParameters parameters = new StatementParameters();
    DalHints hints = DalHints.createIfAbsent(null);
    String testSql = sql;
    int whereIndex = StringUtils.indexOfIgnoreCase(testSql, "where");
    if (whereIndex > 0)
        testSql = sql.substring(0, whereIndex);
    if (isMySqlServer(allInOneName)) {
        testSql = testSql + " limit 1";
    } else {
        testSql = testSql.replace("select", "select top(1)");
    }
    try {
        connection = DataSourceUtil.getConnection(allInOneName);
        preparedStatement = statementCreator.createPreparedStatement(connection, testSql, parameters, hints);
        resultSet = preparedStatement.executeQuery();
        list = extractor.extract(resultSet);
    } catch (Throwable e) {
        throw e;
    } finally {
        ResourceUtils.close(resultSet);
        ResourceUtils.close(preparedStatement);
        ResourceUtils.close(connection);
    }
    return list;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 5 with AbstractParameterHost

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

Aggregations

AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)28 ArrayList (java.util.ArrayList)16 SQLException (java.sql.SQLException)13 ResultSetMetaData (java.sql.ResultSetMetaData)8 DbType (com.ctrip.platform.dal.daogen.enums.DbType)6 StoredProcedure (com.ctrip.platform.dal.daogen.domain.StoredProcedure)5 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)3 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)3 JavaParameterHost (com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)3 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)2 DatabaseCategory (com.ctrip.platform.dal.daogen.enums.DatabaseCategory)2 Matcher (java.util.regex.Matcher)2 DalHints (com.ctrip.platform.dal.dao.DalHints)1 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 Parameter (com.ctrip.platform.dal.daogen.entity.Parameter)1 JavaColumnNameResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaColumnNameResultSetExtractor)1 JavaGivenSqlResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaGivenSqlResultSetExtractor)1 JavaMethodHost (com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)1 JavaSelectFieldResultSetExtractor (com.ctrip.platform.dal.daogen.host.java.JavaSelectFieldResultSetExtractor)1 HashMap (java.util.HashMap)1