Search in sources :

Example 96 with SchemaInfo

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

the class SchemaAlterDDLTest method indexTest1.

//add indexes, then drop them
private void indexTest1() {
    changeList = new SchemaChangeManager(databaseInfo, false);
    ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaInfo newSchema = sup3.clone();
    Constraint constraint = null;
    String indexType = null;
    String indexName = "";
    String tableName = newSchema.getClassname();
    //add unique
    constraint = new Constraint(false);
    indexType = "UNIQUE";
    constraint.setType(indexType);
    constraint.addAttribute("integer");
    constraint.addRule("integer DESC");
    constraint.addAttribute("bigint");
    constraint.addRule("bigint ASC");
    if (indexName.equals("")) {
        //$NON-NLS-1$
        indexName = constraint.getDefaultName(tableName);
    }
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add index
    constraint = new Constraint(false);
    indexType = "INDEX";
    constraint.setType(indexType);
    constraint.addAttribute("numeric1");
    constraint.addRule("numeric1 ASC");
    constraint.addAttribute("numeric2");
    constraint.addRule("numeric2 DESC");
    constraint.addAttribute("float");
    constraint.addRule("float ASC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add reverse unique
    constraint = new Constraint(false);
    indexType = "REVERSE UNIQUE";
    constraint.setType(indexType);
    constraint.addAttribute("numeric2");
    constraint.addRule("numeric2 DESC");
    constraint.addAttribute("float");
    constraint.addRule("float DESC");
    constraint.addAttribute("setint");
    constraint.addRule("setint DESC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add reverse index
    constraint = new Constraint(false);
    indexType = "REVERSE INDEX";
    constraint.setType(indexType);
    constraint.addAttribute("integer");
    constraint.addRule("integer DESC");
    constraint.addAttribute("bigint");
    constraint.addRule("bigint DESC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    String expected = "CREATE UNIQUE INDEX u_sup3_integer_d_bigint ON sup3([integer] DESC,[bigint]);" + StringUtil.NEWLINE;
    expected += "CREATE INDEX i_sup3_numeric1_numeric2_d_float ON sup3(numeric1,numeric2 DESC,[float]);" + StringUtil.NEWLINE;
    expected += "CREATE REVERSE UNIQUE INDEX ru_sup3_numeric2_float_setint ON sup3(numeric2 DESC,[float] DESC,setint DESC);" + StringUtil.NEWLINE;
    expected += "CREATE REVERSE INDEX ri_sup3_integer_bigint ON sup3([integer] DESC,[bigint] DESC);";
    String alterDDL = ddl.getAlterDDL(sup3, newSchema);
    assertEquals(expected, alterDDL.trim());
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 97 with SchemaInfo

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

the class SchemaDDLTest method testGetDDL.

public void testGetDDL() throws Exception {
    boolean success = createTestTable();
    assertTrue(success);
    SchemaInfo sup1 = databaseInfo.getSchemaInfo("sup1");
    SchemaInfo sup2 = databaseInfo.getSchemaInfo("sup2");
    SchemaInfo schema = databaseInfo.getSchemaInfo(testTableName);
    SchemaChangeManager changeList = new SchemaChangeManager(databaseInfo, false);
    SchemaDDL ddl = new SchemaDDL(changeList, databaseInfo);
    String retSQL = ddl.getSchemaDDL(schema);
    assertNotSame(trimSQL(createTestTableSQL), trimSQL(retSQL));
    retSQL = ddl.getSchemaDDL(sup1);
    trimSQL(createSuperSQL1);
    trimSQL(retSQL);
    retSQL = ddl.getSchemaDDL(sup2);
    assertNotSame(trimSQL(createSuperSQL2), trimSQL(retSQL));
    SchemaInfo newSchema = schema.clone();
    assertTrue(newSchema.equals(schema));
    newSchema.toString();
    ddl.getPKsDDL(sup1);
    ddl.getFKsDDL(sup1);
}
Also used : SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 98 with SchemaInfo

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

the class SuperClassUtil method addAttributes2StatisticMap.

/**
	 * add super classes' attribute to map for statistic
	 * 
	 * @param columnSchemaMap Map<String, List<SchemaInfo>>
	 * @param superslist List<SchemaInfo>
	 * @param isClassAttr boolean
	 */
private static void addAttributes2StatisticMap(Map<String, List<SchemaInfo>> columnSchemaMap, List<SchemaInfo> superslist, boolean isClassAttr) {
    for (int i = 0; i < superslist.size(); i++) {
        SchemaInfo superSchema = superslist.get(i);
        List<DBAttribute> list = null;
        if (isClassAttr) {
            list = superSchema.getClassAttributes();
        } else {
            list = superSchema.getAttributes();
        }
        addAttributes2StatisticMap(columnSchemaMap, superSchema, list, isClassAttr);
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 99 with SchemaInfo

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

the class SuperClassUtil method computingAttributeList.

/**
	 * computing which attributes will be remained
	 * 
	 * @param database DatabaseInfo
	 * @param newSchemaInfo SchemaInfo
	 * @param resolutions List<DBResolution>
	 * @param columnInheritSchemaMap Map<String, List<SchemaInfo>>
	 * @param isClassAttr boolean
	 * @return boolean
	 */
public static boolean computingAttributeList(DatabaseInfo database, SchemaInfo newSchemaInfo, List<DBResolution> resolutions, Map<String, List<SchemaInfo>> columnInheritSchemaMap, boolean isClassAttr) {
    for (Iterator<Entry<String, List<SchemaInfo>>> i = columnInheritSchemaMap.entrySet().iterator(); i.hasNext(); ) {
        Entry<String, List<SchemaInfo>> entry = i.next();
        String columnName = entry.getKey();
        List<SchemaInfo> schemaList = entry.getValue();
        if (schemaList.size() > 1) {
            //there should be a resolution
            List<NewAttribute> attrList = new ArrayList<NewAttribute>();
            //local attribute has the highest priority
            NewAttribute localAttr = null;
            for (SchemaInfo schema : schemaList) {
                DBAttribute attr = schema.getDBAttributeByName(columnName, isClassAttr);
                if (attr == null) {
                    continue;
                }
                if (attr.getInherit().equals(newSchemaInfo.getClassname())) {
                    localAttr = new NewAttribute(attr, schema);
                } else {
                    attrList.add(new NewAttribute(attr, schema));
                }
            }
            /**
				 * compute: <li>if local attribute exists, check whether it is
				 * the lowest in the class hierarchy <li>otherwise, return the
				 * lowest attributes, one or more in a list
				 */
            List<NewAttribute> lowestAttrList;
            try {
                lowestAttrList = getLowestAttributes(attrList, localAttr, database);
            } catch (Exception e) {
                LOGGER.error("", e);
                return false;
            }
            int size = lowestAttrList.size();
            if (size == 1) {
                //there is only one
                schemaList.clear();
                schemaList.add(lowestAttrList.get(0).schema);
            } else {
                //select one
                schemaList.clear();
                boolean found = false;
                for (int j = size - 1; j >= 0; j--) {
                    NewAttribute attr = lowestAttrList.get(j);
                    String column = attr.attr.getName();
                    String table = attr.schema.getClassname();
                    DBResolution r = getResolution(resolutions, column, table);
                    if (r != null && (r.getAlias() == null || "".equals(r.getAlias()))) {
                        found = true;
                        schemaList.add(attr.schema);
                    }
                }
                if (!found) {
                    schemaList.add(lowestAttrList.get(0).schema);
                }
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) Entry(java.util.Map.Entry) DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 100 with SchemaInfo

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

the class SuperClassUtil method resetAttribute.

/**
	 * when super classes change, attributes of a schema changes, not only the
	 * number of attributes, but also the order of attributes
	 * 
	 * @param newSchemaInfo SchemaInfo
	 * @param schemalist List<SchemaInfo>
	 * @param resolutions List<DBResolution>
	 * @param localAttributes List<DBAttribute>
	 * @param columnInheritSchemaMap Map<String, List<SchemaInfo>>
	 * @param isClassAttr boolean
	 */
private static void resetAttribute(SchemaInfo newSchemaInfo, List<SchemaInfo> schemalist, List<DBResolution> resolutions, List<DBAttribute> localAttributes, Map<String, List<SchemaInfo>> columnInheritSchemaMap, boolean isClassAttr) {
    //reorder attributes		
    List<DBAttribute> newAttrList = new ArrayList<DBAttribute>();
    //add inherit attributes at first
    for (int j = 0; j < schemalist.size(); j++) {
        SchemaInfo schema = schemalist.get(j);
        List<DBAttribute> attrList = null;
        if (isClassAttr) {
            attrList = schema.getClassAttributes();
        } else {
            attrList = schema.getAttributes();
        }
        for (int i = 0; i < attrList.size(); i++) {
            DBAttribute attr = attrList.get(i);
            String columnName = attr.getName();
            List<SchemaInfo> list = columnInheritSchemaMap.get(columnName);
            /**
				 * <li>if it is in statistic map, it should be remained; <li>if
				 * it is not in, but it has an alias name, it should be remained
				 * too;
				 */
            if (list.contains(schema)) {
                newAttrList.add(attr);
            } else {
                String tableName = schema.getClassname();
                DBResolution r = getResolution(resolutions, columnName, tableName);
                if (r != null && r.getAlias() != null && !r.getAlias().equals("")) {
                    //modify attr name
                    attr.setName(r.getAlias());
                    newAttrList.add(attr);
                }
            }
        }
    }
    //at last, add local attributes 		
    newAttrList.addAll(localAttributes);
    if (isClassAttr) {
        newSchemaInfo.setClassAttributes(newAttrList);
    } else {
        newSchemaInfo.setAttributes(newAttrList);
    }
}
Also used : DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)136 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)57 Constraint (com.cubrid.common.core.common.model.Constraint)56 ArrayList (java.util.ArrayList)47 HashMap (java.util.HashMap)15 List (java.util.List)15 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)11 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)11 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)10 Connection (java.sql.Connection)10 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)9 SQLException (java.sql.SQLException)9 TableItem (org.eclipse.swt.widgets.TableItem)9 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)8 SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)8 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)7 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 DBResolution (com.cubrid.common.core.common.model.DBResolution)6 Map (java.util.Map)5