Search in sources :

Example 6 with AbstractParameterHost

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

the class SpOperationHost method getSpaOperation.

public static SpOperationHost getSpaOperation(String dbName, String tableName, List<StoredProcedure> spNames, String operation) throws Exception {
    SpOperationHost host = new SpOperationHost();
    host.exist = true;
    StoredProcedure expectSpa = new StoredProcedure();
    expectSpa.setName(String.format("spA_%s_%s", tableName, operation));
    StoredProcedure expectSp3 = new StoredProcedure();
    expectSp3.setName(String.format("sp3_%s_%s", tableName, operation));
    StoredProcedure currentSp = null;
    int index = -1;
    if ((index = spNames.indexOf(expectSpa)) > -1 && spNames.indexOf(expectSp3) > -1) {
        // spA、sp3都存在
        host.setBasicSpName(expectSpa.getName());
        host.setBatchSpName(expectSp3.getName());
        host.setType("sp3");
        currentSp = spNames.get(index);
    } else if ((index = spNames.indexOf(expectSpa)) > -1 && spNames.indexOf(expectSp3) < 0) {
        // 只存在spA
        host.setBasicSpName(expectSpa.getName());
        host.setType("spA");
        currentSp = spNames.get(index);
    } else if (spNames.indexOf(expectSpa) < 0 && (index = spNames.indexOf(expectSp3)) > -1) {
        // 只存在sp3
        host.setBasicSpName(expectSp3.getName());
        host.setBatchSpName(expectSp3.getName());
        host.setType("sp3");
        currentSp = spNames.get(index);
    } else {
        host.exist = false;
    }
    if (host.exist) {
        List<AbstractParameterHost> params = DbUtils.getSpParams(dbName, currentSp, new JavaSpParamResultSetExtractor(dbName, currentSp.getName()));
        List<JavaParameterHost> realParams = new ArrayList<>();
        for (AbstractParameterHost p : params) {
            realParams.add((JavaParameterHost) p);
        }
        host.parameters = realParams;
    }
    return host;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure) ArrayList(java.util.ArrayList)

Example 7 with AbstractParameterHost

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

the class CSharpSpaOperationHost method getSpaOperation.

public static CSharpSpaOperationHost getSpaOperation(String dbName, String tableName, List<StoredProcedure> spNames, String operation) throws Exception {
    CSharpSpaOperationHost host = new CSharpSpaOperationHost();
    StoredProcedure expectSpa = new StoredProcedure();
    expectSpa.setName(String.format("spA_%s_%s", tableName, operation));
    StoredProcedure expectSp3 = new StoredProcedure();
    expectSp3.setName(String.format("sp3_%s_%s", tableName, operation));
    StoredProcedure currentSp = null;
    int index = -1;
    if ((index = spNames.indexOf(expectSpa)) > 0) {
        host.exist = true;
        host.methodName = expectSpa.getName();
        currentSp = spNames.get(index);
    } else if ((index = spNames.indexOf(expectSp3)) > 0) {
        host.exist = true;
        host.methodName = expectSp3.getName();
        currentSp = spNames.get(index);
    } else {
        host.exist = false;
    }
    if (host.exist) {
        List<AbstractParameterHost> parameters = DbUtils.getSpParams(dbName, currentSp, new CsharpSpParamResultSetExtractor());
        List<CSharpParameterHost> realParams = new ArrayList<CSharpParameterHost>();
        for (AbstractParameterHost _host : parameters) {
            realParams.add((CSharpParameterHost) _host);
        }
        host.setParameters(realParams);
    }
    return host;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure) ArrayList(java.util.ArrayList)

Example 8 with AbstractParameterHost

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

the class CsharpColumnNameResultSetExtractor method extract.

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

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

the class AbstractJavaDataPreparer method buildTableHost.

protected JavaTableHost buildTableHost(CodeGenContext context, GenTaskByTableViewSp tableViewSp, String tableName, DatabaseCategory dbCategory) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    if (!DbUtils.tableExists(tableViewSp.getAllInOneName(), tableName)) {
        throw new Exception(String.format("Table[%s.%s] doesn't exist.", tableViewSp.getAllInOneName(), tableName));
    }
    JavaTableHost tableHost = new JavaTableHost();
    tableHost.setPackageName(ctx.getNamespace());
    tableHost.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
    tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
    tableHost.setTableName(tableName);
    tableHost.setPojoClassName(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), tableName));
    tableHost.setSp(tableViewSp.getCud_by_sp());
    tableHost.setApi_list(tableViewSp.getApi_list());
    tableHost.setUserName(ctx.getUserName());
    // tableHost.setLength(tableViewSp.getLength());
    // 主键及所有列
    List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), tableName);
    List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), tableName, new JavaColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), tableName, dbCategory));
    if (null == allColumnsAbstract) {
        throw new Exception(String.format("The column names of table[%s, %s] is null", tableViewSp.getAllInOneName(), tableName));
    }
    List<JavaParameterHost> allColumns = new ArrayList<>();
    for (AbstractParameterHost h : allColumnsAbstract) {
        allColumns.add((JavaParameterHost) h);
    }
    List<JavaParameterHost> primaryKeys = new ArrayList<>();
    boolean hasIdentity = false;
    String identityColumnName = null;
    for (JavaParameterHost h : allColumns) {
        if (!hasIdentity && h.isIdentity()) {
            hasIdentity = true;
            identityColumnName = h.getName();
        }
        if (primaryKeyNames.contains(h.getName())) {
            h.setPrimary(true);
            primaryKeys.add(h);
        }
    }
    List<GenTaskBySqlBuilder> currentTableBuilders = filterExtraMethods(ctx, tableViewSp.getAllInOneName(), tableName);
    List<JavaMethodHost> methods = buildSqlBuilderMethodHost(allColumns, currentTableBuilders);
    tableHost.setFields(allColumns);
    tableHost.setPrimaryKeys(primaryKeys);
    tableHost.setHasIdentity(hasIdentity);
    tableHost.setIdentityColumnName(identityColumnName);
    tableHost.setMethods(methods);
    if (tableHost.isSp()) {
        tableHost.setSpInsert(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "i"));
        tableHost.setSpUpdate(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "u"));
        tableHost.setSpDelete(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "d"));
    }
    return tableHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) SQLException(java.sql.SQLException) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 10 with AbstractParameterHost

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

the class AbstractJavaDataPreparer method buildSelectMethodHosts.

private List<JavaMethodHost> buildSelectMethodHosts(List<JavaParameterHost> allColumns, List<GenTaskBySqlBuilder> currentTableBuilders) throws Exception {
    List<JavaMethodHost> methods = new ArrayList<>();
    for (GenTaskBySqlBuilder builder : currentTableBuilders) {
        if (!builder.getCrud_type().equals("select")) {
            continue;
        }
        JavaMethodHost method = new JavaMethodHost();
        method.setCrud_type(builder.getCrud_type());
        method.setName(builder.getMethod_name());
        method.setSql(builder.getSql_content());
        method.setScalarType(builder.getScalarType());
        method.setPaging(builder.getPagination());
        method.setComments(builder.getComment());
        method.setField(buildSelectFieldExp(builder));
        method.setTableName(builder.getTable_name());
        String orderBy = builder.getOrderby();
        if (orderBy != null && !orderBy.trim().isEmpty() && orderBy.indexOf("-1,") != 0) {
            String[] str = orderBy.split(",");
            String odyExp = "\"" + str[0] + "\", ";
            odyExp = "asc".equalsIgnoreCase(str[1]) ? odyExp + "true" : odyExp + "false";
            method.setOrderByExp(odyExp);
        }
        // select sql have select field and where condition clause
        List<AbstractParameterHost> paramAbstractHosts = DbUtils.getSelectFieldHosts(builder.getAllInOneName(), builder.getSql_content(), new JavaSelectFieldResultSetExtractor());
        List<JavaParameterHost> paramHosts = new ArrayList<>();
        for (AbstractParameterHost phost : paramAbstractHosts) {
            paramHosts.add((JavaParameterHost) phost);
        }
        method.setFields(paramHosts);
        method.setParameters(buildMethodParameterHost4SqlConditin(builder, allColumns));
        method.setHints(builder.getHints());
        methods.add(method);
    }
    return methods;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost)

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