Search in sources :

Example 16 with AbstractParameterHost

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

the class JavaSelectFieldResultSetExtractor 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++) {
        JavaParameterHost paramHost = new JavaParameterHost();
        paramHost.setName(rsMeta.getColumnLabel(i));
        paramHost.setSqlType(rsMeta.getColumnType(i));
        Class<?> javaClass = null;
        try {
            javaClass = Class.forName(rsMeta.getColumnClassName(i));
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
            javaClass = Consts.jdbcSqlTypeToJavaClass.get(paramHost.getSqlType());
        }
        paramHost.setJavaClass(javaClass);
        paramHost.setIdentity(false);
        paramHost.setNullable(rsMeta.isNullable(i) == 1 ? true : false);
        paramHost.setPrimary(false);
        paramHost.setLength(rsMeta.getColumnDisplaySize(i));
        hosts.add(paramHost);
    }
    return hosts;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException)

Example 17 with AbstractParameterHost

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

the class CsharpColumnNameResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> allColumns = new ArrayList<>();
    if (rs == null) {
        return allColumns;
    }
    Map<String, String> columnComment;
    try {
        columnComment = DbUtils.getSqlserverColumnComment(allInOneName, tableName);
    } catch (Exception e) {
        throw new SQLException(e.getMessage(), e);
    }
    while (rs.next()) {
        CSharpParameterHost host = new CSharpParameterHost();
        String columnName = rs.getString(COLUMN_NAME);
        host.setName(columnName);
        String typeName = rs.getString(TYPE_NAME);
        boolean isUnsigned = DbUtils.isColumnUnsigned(typeName);
        int dataType = rs.getInt(DATA_TYPE);
        host.setDataType(dataType);
        int length = rs.getInt(COLUMN_SIZE);
        // 特殊处理
        DbType dbType = DbUtils.getDotNetDbType(typeName, dataType, length, isUnsigned, dbCategory);
        host.setDbType(dbType);
        String remark = rs.getString(REMARKS);
        if (remark == null) {
            String description = columnComment.get(columnName.toLowerCase());
            remark = description == null ? "" : description;
        }
        host.setComment(remark.replace("\n", " "));
        String type = DbType.getCSharpType(host.getDbType());
        host.setType(type);
        host.setIdentity(rs.getString(IS_AUTOINCREMENT).equalsIgnoreCase("YES"));
        host.setNullable(rs.getShort(NULLABLE) == DatabaseMetaData.columnNullable);
        host.setValueType(Consts.CSharpValueTypes.contains(host.getType()));
        // 仅获取String类型的长度
        if ("string".equalsIgnoreCase(host.getType()))
            host.setLength(length);
        host.setDefaultValue(rs.getString(COLUMN_DEF));
        host.setDbCategory(dbCategory);
        allColumns.add(host);
    }
    return allColumns;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DbType(com.ctrip.platform.dal.daogen.enums.DbType)

Example 18 with AbstractParameterHost

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

the class CsharpSpParamResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> parameters = new ArrayList<>();
    while (rs.next()) {
        int paramMode = rs.getShort("COLUMN_TYPE");
        if (!DbUtils.validMode.contains(paramMode)) {
            continue;
        }
        CSharpParameterHost host = new CSharpParameterHost();
        DbType dbType = DbUtils.getDotNetDbType(rs.getString("TYPE_NAME"), rs.getInt("DATA_TYPE"), rs.getInt("LENGTH"), false, null);
        host.setDbType(dbType);
        host.setNullable(rs.getShort("NULLABLE") == DatabaseMetaData.columnNullable);
        if (paramMode == DatabaseMetaData.procedureColumnIn) {
            host.setDirection(ParameterDirection.Input);
        } else if (paramMode == DatabaseMetaData.procedureColumnInOut) {
            host.setDirection(ParameterDirection.InputOutput);
        } else {
            host.setDirection(ParameterDirection.Output);
        }
        host.setName(rs.getString("COLUMN_NAME"));
        host.setType(DbType.getCSharpType(host.getDbType()));
        host.setNullable(rs.getShort("NULLABLE") == DatabaseMetaData.columnNullable);
        if (host.getType() == null) {
            host.setType("string");
            host.setDbType(DbType.AnsiString);
        }
        parameters.add(host);
    }
    return parameters;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) ArrayList(java.util.ArrayList) DbType(com.ctrip.platform.dal.daogen.enums.DbType)

Example 19 with AbstractParameterHost

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

the class GenTaskBySqlBuilderResource method getTableColumnSqlType.

/**
 * @param set_name
 * @param table_name
 * @return <column alias, sqltype>
 */
private Map<String, Integer> getTableColumnSqlType(String set_name, String table_name, String modeType) throws Exception {
    String dbName = AllInOneNameUtils.getAllInOneName(set_name, modeType);
    DatabaseCategory dbCategory = DbUtils.getDatabaseCategory(dbName);
    List<AbstractParameterHost> paramsHost = DbUtils.getAllColumnNames(dbName, table_name, new JavaColumnNameResultSetExtractor(dbName, table_name, dbCategory));
    Map<String, Integer> map = new HashMap<>();
    if (paramsHost != null) {
        for (int i = 0; i < paramsHost.size(); i++) {
            JavaParameterHost paramHost = (JavaParameterHost) paramsHost.get(i);
            map.put(paramHost.getAlias().toLowerCase(), paramHost.getSqlType());
        }
    }
    return map;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) DatabaseCategory(com.ctrip.platform.dal.daogen.enums.DatabaseCategory) JavaColumnNameResultSetExtractor(com.ctrip.platform.dal.daogen.host.java.JavaColumnNameResultSetExtractor) HashMap(java.util.HashMap) JavaParameterHost(com.ctrip.platform.dal.daogen.host.java.JavaParameterHost)

Example 20 with AbstractParameterHost

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

the class JavaDataPreparerOfTableViewSpProcessor method buildSpHost.

private SpHost buildSpHost(CodeGenContext context, GenTaskByTableViewSp tableViewSp, String spName) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    String schema = "dbo";
    String realSpName = spName;
    if (spName.contains(".")) {
        String[] splitSp = StringUtils.split(spName, '.');
        schema = splitSp[0];
        realSpName = splitSp[1];
    }
    StoredProcedure sp = new StoredProcedure();
    sp.setSchema(schema);
    sp.setName(realSpName);
    if (!DbUtils.spExists(tableViewSp.getAllInOneName(), sp)) {
        throw new Exception(String.format("The store procedure[%s, %s] doesn't exist, pls check", tableViewSp.getAllInOneName(), sp.getName()));
    }
    SpHost host = new SpHost();
    String className = realSpName.replace("_", "");
    className = getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), className);
    host.setPackageName(ctx.getNamespace());
    host.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
    host.setDbName(tableViewSp.getDatabaseSetName());
    host.setPojoClassName(className);
    host.setSpName(spName);
    // host.setLength(tableViewSp.getLength());
    List<AbstractParameterHost> params = DbUtils.getSpParams(tableViewSp.getAllInOneName(), sp, new JavaSpParamResultSetExtractor(tableViewSp.getAllInOneName(), sp.getName()));
    List<JavaParameterHost> realParams = new ArrayList<>();
    String callParams = "";
    if (params == null) {
        throw new Exception(String.format("The sp[%s, %s] parameters is null", tableViewSp.getAllInOneName(), sp.getName()));
    }
    for (AbstractParameterHost p : params) {
        callParams += "?,";
        realParams.add((JavaParameterHost) p);
    }
    host.setCallParameters(StringUtils.removeEnd(callParams, ","));
    host.setFields(realParams);
    return host;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure) SQLException(java.sql.SQLException) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

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