Search in sources :

Example 11 with AbstractParameterHost

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

the class CSharpDataPreparerOfTableViewSpProcessor method buildViewHost.

private CSharpTableHost buildViewHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, DatabaseCategory dbCategory, String view) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    if (!DbUtils.viewExists(tableViewSp.getAllInOneName(), view)) {
        throw new Exception(String.format("视图 %s 不存在,请编辑DAO再生成", view));
    }
    List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), view, new CsharpColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), view, dbCategory));
    List<CSharpParameterHost> allColumns = new ArrayList<>();
    for (AbstractParameterHost h : allColumnsAbstract) {
        allColumns.add((CSharpParameterHost) h);
    }
    CSharpTableHost tableHost = new CSharpTableHost();
    tableHost.setNameSpace(ctx.getNamespace());
    tableHost.setDatabaseCategory(dbCategory);
    tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
    tableHost.setTableName(view);
    tableHost.setClassName(CommonUtils.normalizeVariable(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), view)));
    tableHost.setTable(false);
    tableHost.setSpa(false);
    tableHost.setColumns(allColumns);
    tableHost.setHasPagination(tableViewSp.getPagination());
    return tableHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) SQLException(java.sql.SQLException)

Example 12 with AbstractParameterHost

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

the class AbstractCSharpDataPreparer method buildTableHost.

protected CSharpTableHost buildTableHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, String table, DatabaseCategory dbCategory, List<StoredProcedure> allSpNames) throws Exception {
    CSharpCodeGenContext ctx = (CSharpCodeGenContext) codeGenCtx;
    if (!DbUtils.tableExists(tableViewSp.getAllInOneName(), table))
        throw new Exception(String.format("表 %s 不存在,请编辑DAO再生成", table));
    // 主键及所有列
    List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), table, new CsharpColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), table, dbCategory));
    List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), table);
    List<CSharpParameterHost> allColumns = new ArrayList<>();
    for (AbstractParameterHost h : allColumnsAbstract) {
        allColumns.add((CSharpParameterHost) h);
    }
    List<CSharpParameterHost> primaryKeys = new ArrayList<>();
    for (CSharpParameterHost h : allColumns) {
        if (primaryKeyNames.contains(h.getName())) {
            h.setPrimary(true);
            primaryKeys.add(h);
        }
    }
    Queue<GenTaskBySqlBuilder> _sqlBuilders = ctx.getSqlBuilders();
    List<GenTaskBySqlBuilder> currentTableBuilders = filterExtraMethods(_sqlBuilders, tableViewSp.getAllInOneName(), table);
    List<CSharpMethodHost> methods = buildSqlBuilderMethodHost(allColumns, currentTableBuilders);
    CSharpTableHost tableHost = new CSharpTableHost();
    tableHost.setExtraMethods(methods);
    tableHost.setNameSpace(ctx.getNamespace());
    tableHost.setDatabaseCategory(dbCategory);
    tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
    tableHost.setTableName(table);
    tableHost.setClassName(CommonUtils.normalizeVariable(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), table)));
    tableHost.setTable(true);
    tableHost.setSpa(tableViewSp.getCud_by_sp());
    // SP方式增删改
    if (tableHost.isSpa()) {
        tableHost.setSpaInsert(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "i"));
        tableHost.setSpaUpdate(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "u"));
        tableHost.setSpaDelete(CSharpSpaOperationHost.getSpaOperation(tableViewSp.getAllInOneName(), table, allSpNames, "d"));
    }
    tableHost.setPrimaryKeys(primaryKeys);
    tableHost.setColumns(allColumns);
    tableHost.setHasPagination(tableViewSp.getPagination());
    StoredProcedure expectSptI = new StoredProcedure();
    expectSptI.setName(String.format("spT_%s_i", table));
    StoredProcedure expectSptU = new StoredProcedure();
    expectSptU.setName(String.format("spT_%s_u", table));
    StoredProcedure expectSptD = new StoredProcedure();
    expectSptD.setName(String.format("spT_%s_d", table));
    tableHost.setHasSptI(allSpNames.contains(expectSptI));
    tableHost.setHasSptU(allSpNames.contains(expectSptU));
    tableHost.setHasSptD(allSpNames.contains(expectSptD));
    tableHost.setHasSpt(tableHost.isHasSptI() || tableHost.isHasSptU() || tableHost.isHasSptD());
    tableHost.setApi_list(tableViewSp.getApi_list());
    return tableHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) CSharpCodeGenContext(com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext) SQLException(java.sql.SQLException) GenTaskBySqlBuilder(com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure)

Example 13 with AbstractParameterHost

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

the class CsharpSelectFieldResultSetExtractor method extract.

@Override
public List<AbstractParameterHost> extract(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 14 with AbstractParameterHost

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

the class JavaSelectFieldResultSetExtractor method extract.

@Override
public List<AbstractParameterHost> extract(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) {
            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 15 with AbstractParameterHost

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

the class JavaColumnNameResultSetExtractor method extractData.

@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
    List<AbstractParameterHost> allColumns = new ArrayList<>();
    Map<String, Integer> columnSqlType = DbUtils.getColumnSqlType(allInOneName, tableName);
    Map<String, Class<?>> typeMapper = DbUtils.getSqlType2JavaTypeMaper(allInOneName, tableName);
    Map<String, String> columnComment;
    try {
        columnComment = DbUtils.getSqlserverColumnComment(allInOneName, tableName);
    } catch (Exception e) {
        throw new SQLException(e.getMessage(), e);
    }
    if (columnSqlType != null && columnSqlType.size() > 0) {
        while (rs.next()) {
            JavaParameterHost host = new JavaParameterHost();
            String typeName = rs.getString(TYPE_NAME);
            String columnName = rs.getString(COLUMN_NAME);
            host.setName(columnName);
            host.setSqlType(columnSqlType.get(host.getName()));
            Class<?> javaClass = null;
            if (null != typeMapper && typeMapper.containsKey(host.getName())) {
                javaClass = typeMapper.get(host.getName());
            } else {
                javaClass = Consts.jdbcSqlTypeToJavaClass.get(host.getSqlType());
            }
            if (null == javaClass) {
                if (null != typeName && typeName.equalsIgnoreCase("sql_variant")) {
                    log.fatal(String.format("The sql_variant is not support by java.[%s, %s, %s, %s, %s]", host.getName(), allInOneName, tableName, host.getSqlType(), javaClass));
                    return null;
                } else if (null != typeName && typeName.equalsIgnoreCase("datetimeoffset")) {
                    javaClass = DateTimeOffset.class;
                } else {
                    log.fatal(String.format("The java type cant be mapped.[%s, %s, %s, %s, %s]", host.getName(), allInOneName, tableName, host.getSqlType(), javaClass));
                    return null;
                }
            }
            host.setJavaClass(javaClass);
            host.setIndex(rs.getInt(ORDINAL_POSITION));
            host.setIdentity(rs.getString(IS_AUTOINCREMENT).equalsIgnoreCase("YES"));
            String remarks = rs.getString(REMARKS);
            if (remarks == null) {
                String description = columnComment.get(columnName.toLowerCase());
                remarks = description == null ? "" : description;
            }
            host.setComment(remarks.replace("\n", " "));
            host.setDefaultValue(rs.getString(COLUMN_DEF));
            host.setDbCategory(dbCategory);
            int dataType = rs.getInt(DATA_TYPE);
            host.setDataType(dataType);
            allColumns.add(host);
        }
    }
    return allColumns;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DateTimeOffset(microsoft.sql.DateTimeOffset) SQLException(java.sql.SQLException)

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