Search in sources :

Example 21 with PartitionInfo

use of com.cubrid.common.core.common.model.PartitionInfo in project cubrid-manager by CUBRID.

the class ConstraintComparator method getSchemaDDL.

/**
	 * Return DDL of a schema
	 *
	 * @param schemaInfo SchemaInfo the given reference of a SChemaInfo object
	 * @param isContainIndex boolean whether include the index DDL
	 * @param isVirtual boolean whether be a virtual table
	 * @return String a string indicates a instance of SchemaInfo
	 */
public String getSchemaDDL(SchemaInfo schemaInfo, boolean isContainIndex, boolean isVirtual) {
    StringBuffer ddlBuffer = new StringBuffer();
    ddlBuffer.append("CREATE TABLE ");
    final String tableName = schemaInfo.getClassname().toLowerCase();
    if (null == tableName || tableName.equals("")) {
        ddlBuffer.append("<class_name>");
    } else {
        ddlBuffer.append(QuerySyntax.escapeKeyword(tableName));
    }
    List<String> slist = schemaInfo.getSuperClasses();
    if (!slist.isEmpty()) {
        ddlBuffer.append(StringUtil.NEWLINE).append("\t\t UNDER ");
        for (int i = 0; i < slist.size(); i++) {
            if (i != 0) {
                ddlBuffer.append(",");
            }
            ddlBuffer.append(QuerySyntax.escapeKeyword(slist.get(i).toLowerCase()));
        }
    }
    boolean attrBegin = false;
    int count = 0;
    // class attribute
    List<DBAttribute> clist = schemaInfo.getClassAttributes();
    if (!clist.isEmpty()) {
        for (int i = 0; i < clist.size(); i++) {
            DBAttribute classAttr = clist.get(i);
            String inherit = classAttr.getInherit();
            if (inherit.equalsIgnoreCase(schemaInfo.getClassname())) {
                if (count == 0) {
                    ddlBuffer.append(StringUtil.NEWLINE);
                    attrBegin = true;
                    ddlBuffer.append("CLASS ATTRIBUTE(").append(StringUtil.NEWLINE);
                } else {
                    ddlBuffer.append(",").append(StringUtil.NEWLINE);
                }
                ddlBuffer.append(getClassAttributeDDL(classAttr, isVirtual));
                count++;
            }
        }
        if (attrBegin) {
            ddlBuffer.append(StringUtil.NEWLINE).append(")").append(StringUtil.NEWLINE);
        }
    }
    // instance attribute
    List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
    Constraint pk = schemaInfo.getPK(newSupers);
    List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
    count = 0;
    attrBegin = false;
    List<DBAttribute> nlist = schemaInfo.getAttributes();
    if (!nlist.isEmpty()) {
        for (int i = 0; i < nlist.size(); i++) {
            DBAttribute instanceAttr = nlist.get(i);
            String inherit = instanceAttr.getInherit();
            String className = schemaInfo.getClassname();
            if (StringUtil.isEqualIgnoreCase(inherit, className)) {
                if (count == 0) {
                    if (!attrBegin) {
                        ddlBuffer.append("(").append(StringUtil.NEWLINE);
                        attrBegin = true;
                    }
                } else {
                    ddlBuffer.append(",").append(StringUtil.NEWLINE);
                }
                ddlBuffer.append(getInstanceAttributeDDL(instanceAttr, pkAttributes, schemaInfo, isVirtual));
                count++;
            }
        }
    }
    // constraint
    List<Constraint> constraintList = new ArrayList<Constraint>();
    if (isContainIndex) {
        constraintList = schemaInfo.getConstraints();
    }
    /*Sort the constaints first*/
    Collections.sort(constraintList, new ConstraintComparator(tableName));
    if (!constraintList.isEmpty()) {
        for (int i = 0; i < constraintList.size(); i++) {
            Constraint constraint = constraintList.get(i);
            List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
            if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
                String contraintDDL = getContraintDDL(tableName, constraint);
                if (StringUtil.isNotEmpty(contraintDDL)) {
                    ddlBuffer.append(",").append(StringUtil.NEWLINE).append(contraintDDL);
                }
            }
        }
    }
    if (count > 0) {
        ddlBuffer.append(StringUtil.NEWLINE).append(")");
    }
    boolean supportCharset = CompatibleUtil.isSupportCreateDBByCharset(databaseInfo);
    if ((isVirtual || supportCharset) && !StringUtil.isEmpty(schemaInfo.getCollation())) {
        ddlBuffer.append(" COLLATE ").append(schemaInfo.getCollation()).append(" ");
    }
    //reuse OID
    if (schemaInfo.isReuseOid()) {
        ddlBuffer.append(" REUSE_OID ");
    }
    String resolutionDDL = getResolutionsDDL(schemaInfo.getClassResolutions(), schemaInfo.getResolutions());
    ddlBuffer.append(resolutionDDL);
    ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
    if (!constraintList.isEmpty()) {
        for (int i = 0; i < constraintList.size(); i++) {
            Constraint constraint = constraintList.get(i);
            List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
            if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
                String type = constraint.getType();
                if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE INDEX".equals(type) || "REVERSE UNIQUE".equals(type)) {
                    String indexDDL = getCreateIndexDDL(tableName, constraint);
                    if (StringUtil.isNotEmpty(indexDDL)) {
                        ddlBuffer.append(indexDDL);
                        ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
                    }
                }
            }
        }
    }
    // partition DDL
    List<PartitionInfo> partitionInfoList = schemaInfo.getPartitionList();
    String transformToPartitionDDL = getTransformToPartitionDDL(partitionInfoList);
    if (transformToPartitionDDL != null) {
        ddlBuffer.append(transformToPartitionDDL);
        ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 22 with PartitionInfo

use of com.cubrid.common.core.common.model.PartitionInfo in project cubrid-manager by CUBRID.

the class GetPartitionedClassListTask method getPartitionItemList.

/**
	 * Returning of partitioning sub-table meta information
	 * 
	 * @param tableName String partitioned table name
	 * @return List<PartitionInfo>
	 */
public List<PartitionInfo> getPartitionItemList(String tableName) {
    List<PartitionInfo> result = new ArrayList<PartitionInfo>();
    try {
        if (errorMsg != null && errorMsg.trim().length() > 0) {
            return result;
        }
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return result;
        }
        String sql = "SELECT class_name, partition_name, partition_class_name, partition_type, partition_expr, partition_values FROM db_partition WHERE class_name='" + tableName.trim().toLowerCase() + "'";
        // [TOOLS-2425]Support shard broker
        sql = databaseInfo.wrapShardQuery(sql);
        connection.setAutoCommit(false);
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sql);
        String exprDataType = null;
        while (rs.next()) {
            String className = rs.getString("class_name");
            String partitionName = rs.getString("partition_name");
            String partitionClassName = rs.getString("partition_class_name");
            String partitionExpr = rs.getString("partition_expr");
            PartitionType partitionType = null;
            String partitionTypeStr = rs.getString("partition_type");
            if (partitionTypeStr.equalsIgnoreCase("HASH")) {
                partitionType = PartitionType.HASH;
            } else if (partitionTypeStr.equalsIgnoreCase("LIST")) {
                partitionType = PartitionType.LIST;
            } else if (partitionTypeStr.equalsIgnoreCase("RANGE")) {
                partitionType = PartitionType.RANGE;
            }
            List<String> partitionValues = new ArrayList<String>();
            if (partitionType != PartitionType.HASH) {
                Object obj = rs.getObject("partition_values");
                if (obj == null) {
                    continue;
                }
                Object[] arr = (Object[]) obj;
                for (int i = 0, len = arr.length; i < len; i++) {
                    if (arr[i] == null) {
                        partitionValues.add(null);
                    } else {
                        partitionValues.add(arr[i].toString());
                    }
                }
            }
            PartitionInfo partitionItem = new PartitionInfo(className, partitionName, partitionClassName, partitionType, partitionExpr, partitionValues, -1);
            if (exprDataType == null && partitionExpr != null && partitionExpr.trim().length() > 0) {
                exprDataType = getExprDataType(className, partitionExpr);
            }
            partitionItem.setPartitionExprType(exprDataType);
            result.add(partitionItem);
        }
        // counting rows
        int len = result.size();
        if (len > 0) {
            QueryUtil.freeQuery(stmt, rs);
            stmt = null;
            rs = null;
            StringBuilder qry = new StringBuilder();
            qry.append("SELECT ");
            for (int i = 0; i < len; i++) {
                qry.append(" SUM(DECODE(code, 'p").append(i + 1).append("', cnt, 0)) ");
                if (i < len - 1) {
                    qry.append(" , ");
                }
            }
            qry.append(" FROM ( ");
            for (int i = 0; i < len; i++) {
                PartitionInfo item = result.get(i);
                qry.append(" SELECT 'p").append(i + 1).append("' AS code, COUNT(*) AS cnt ");
                qry.append(" from ").append(item.getPartitionClassName());
                if (i < len - 1) {
                    qry.append(" UNION ALL ");
                }
            }
            qry.append(" ) t");
            stmt = connection.createStatement();
            rs = stmt.executeQuery(qry.toString());
            if (rs.next()) {
                for (int i = 0; i < len; i++) {
                    PartitionInfo item = result.get(i);
                    item.setRows(rs.getInt(i + 1));
                }
            }
        }
    } catch (SQLException e) {
        errorMsg = e.getMessage();
    } finally {
        finish();
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) PartitionType(com.cubrid.common.core.common.model.PartitionType)

Example 23 with PartitionInfo

use of com.cubrid.common.core.common.model.PartitionInfo in project cubrid-manager by CUBRID.

the class ConstraintComparator method getCoalescePartitionDDL.

/**
	 * Get coalesce partition DDL
	 *
	 * @param oldPartList List<PartitionInfo> the given list that includes the
	 *        old instance of PartitionInfo
	 * @param newPartList List<PartitionInfo> the given list that includes the
	 *        new instance of PartitionInfo
	 * @return String a string that indicates the info of coalesce partition DDL
	 */
public String getCoalescePartitionDDL(List<PartitionInfo> oldPartList, List<PartitionInfo> newPartList) {
    PartitionInfo newPart = newPartList.get(0);
    if (newPart.getPartitionType() == PartitionType.HASH) {
        int partCnt = oldPartList.size() - newPartList.size();
        return "ALTER TABLE " + QuerySyntax.escapeKeyword(newPart.getClassName()) + " COALESCE PARTITION " + partCnt;
    }
    // TODO: HASH coalesce to merge as the number means that number is. UI and the means were different note ...
    StringBuilder ddl = new StringBuilder();
    ddl.append("ALTER TABLE ").append(QuerySyntax.escapeKeyword(newPart.getClassName())).append(" REORGANIZE PARTITION ");
    for (int i = 0, len = oldPartList.size(); i < len; i++) {
        PartitionInfo inf = oldPartList.get(i);
        if (i > 0) {
            ddl.append(", ");
        }
        ddl.append(inf.getPartitionName());
    }
    ddl.append(StringUtil.NEWLINE);
    ddl.append(" INTO (PARTITION ").append(QuerySyntax.escapeKeyword(newPart.getPartitionName()));
    ddl.append(" VALUES ");
    if (newPart.getPartitionType() == PartitionType.LIST) {
        ddl.append("IN (");
        ddl.append(newPart.getPartitionValuesString(true));
        ddl.append(")");
    } else if (newPart.getPartitionType() == PartitionType.RANGE) {
        ddl.append("LESS THAN (");
        ddl.append(newPart.getPartitionValues().get(1) == null ? "MAXVALUE" : newPart.getPartitionValues().get(1));
        ddl.append(")");
    }
    ddl.append(")");
    return ddl.toString();
}
Also used : PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) Constraint(com.cubrid.common.core.common.model.Constraint)

Aggregations

PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)23 Constraint (com.cubrid.common.core.common.model.Constraint)11 ArrayList (java.util.ArrayList)9 PartitionType (com.cubrid.common.core.common.model.PartitionType)8 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)7 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)5 DBResolution (com.cubrid.common.core.common.model.DBResolution)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TableItem (org.eclipse.swt.widgets.TableItem)2 DBMethod (com.cubrid.common.core.common.model.DBMethod)1 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)1 DataCompare (com.cubrid.common.ui.compare.data.model.DataCompare)1 CreatePartitionWizard (com.cubrid.common.ui.cubrid.table.control.CreatePartitionWizard)1 RangePartitionComparator (com.cubrid.common.ui.cubrid.table.control.RangePartitionComparator)1 CMWizardDialog (com.cubrid.common.ui.spi.dialog.CMWizardDialog)1 CommonTaskExec (com.cubrid.common.ui.spi.progress.CommonTaskExec)1 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)1 TaskExecutor (com.cubrid.common.ui.spi.progress.TaskExecutor)1 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)1