Search in sources :

Example 16 with Constraint

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

the class GetSchemaTaskTest method innerTestGetSchemaTask.

public void innerTestGetSchemaTask() {
    try {
        if (!createTestTable()) {
            dropTestTable();
            //test again
            if (!createTestTable()) {
                fail();
            }
        }
    } catch (Exception e) {
        fail();
    }
    testTableName = testTableName.toLowerCase();
    GetSchemaTask task = new GetSchemaTask(databaseInfo, testTableName);
    task.execute();
    SchemaInfo jSchema = task.getSchema();
    DBAttribute attr = null;
    SerialInfo auto = null;
    //"smallint" smallint AUTO_INCREMENT PRIMARY KEY UNIQUE,
    attr = jSchema.getAttributes().get(0);
    assertFalse(attr.isClassAttribute());
    assertEquals("smallint", attr.getName());
    assertEquals("smallint", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNotNull(auto);
    assertEquals("1", auto.getMinValue());
    assertEquals("1", auto.getIncrementValue());
    //"smallint2" smallint,
    attr = jSchema.getAttributes().get(1);
    assertFalse(attr.isClassAttribute());
    assertEquals("smallint2", attr.getName());
    assertEquals("smallint", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"integer" integer AUTO_INCREMENT(2,1),
    attr = jSchema.getAttributes().get(2);
    assertFalse(attr.isClassAttribute());
    assertEquals("integer", attr.getName());
    assertEquals("integer", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNotNull(auto);
    assertEquals("2", auto.getMinValue());
    assertEquals("1", auto.getIncrementValue());
    //"bigint" bigint,
    attr = jSchema.getAttributes().get(3);
    assertFalse(attr.isClassAttribute());
    assertEquals("bigint", attr.getName());
    assertEquals("bigint", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"numeric1" numeric(15,0) AUTO_INCREMENT UNIQUE,
    attr = jSchema.getAttributes().get(4);
    assertFalse(attr.isClassAttribute());
    assertEquals("numeric1", attr.getName());
    assertEquals("numeric(15,0)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNotNull(auto);
    assertEquals("1", auto.getMinValue());
    assertEquals("1", auto.getIncrementValue());
    //"numeric2" numeric(17,2) DEFAULT 12.50,
    attr = jSchema.getAttributes().get(5);
    assertFalse(attr.isClassAttribute());
    assertEquals("numeric2", attr.getName());
    assertEquals("numeric(17,2)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("12.50", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"float" float DEFAULT 12 NOT NULL,
    attr = jSchema.getAttributes().get(6);
    assertFalse(attr.isClassAttribute());
    assertEquals("float", attr.getName());
    assertEquals("float", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("12", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"date" date DEFAULT DATE'12/12/2009',
    attr = jSchema.getAttributes().get(7);
    assertFalse(attr.isClassAttribute());
    assertEquals("date", attr.getName());
    assertEquals("date", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("2009/12/12", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"datetime" datetime DEFAULT DATETIME'2009-12-12 12:12:12.333',
    attr = jSchema.getAttributes().get(8);
    assertFalse(attr.isClassAttribute());
    assertEquals("datetime", attr.getName());
    assertEquals("datetime", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("2009-12-12 12:12:12.333", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"timestamp" timestamp DEFAULT TIMESTAMP'12/01/2009 03:07:51',
    attr = jSchema.getAttributes().get(9);
    assertFalse(attr.isClassAttribute());
    assertEquals("timestamp", attr.getName());
    assertEquals("timestamp", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("2009-12-01 03:07:51", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"time" time DEFAULT TIME'00:00:12',
    attr = jSchema.getAttributes().get(10);
    assertFalse(attr.isClassAttribute());
    assertEquals("time", attr.getName());
    assertEquals("time", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    //assertEquals("AM 12:00:12", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"char" character(10) DEFAULT 'aa        ',
    attr = jSchema.getAttributes().get(11);
    assertFalse(attr.isClassAttribute());
    assertEquals("char", attr.getName());
    assertEquals("character(10)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("'aa        '", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"varchar" character varying(10) DEFAULT '77',
    attr = jSchema.getAttributes().get(12);
    assertFalse(attr.isClassAttribute());
    assertEquals("varchar", attr.getName());
    assertEquals("character varying(10)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("'77'", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"setint" set_of(smallint) DEFAULT {1, 2},
    attr = jSchema.getAttributes().get(13);
    assertFalse(attr.isClassAttribute());
    assertEquals("setint", attr.getName());
    assertEquals("set_of(smallint)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("{1, 2}", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"multisetchar" multiset_of(character varying(10)) DEFAULT {'12', '13'},
    attr = jSchema.getAttributes().get(14);
    assertFalse(attr.isClassAttribute());
    assertEquals("multisetchar", attr.getName());
    assertEquals("multiset_of(character varying(10))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertEquals("{'12', '13'}", attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"shared" character varying(1073741823) SHARED 'sharedvalue'
    attr = jSchema.getAttributes().get(15);
    assertFalse(attr.isClassAttribute());
    assertEquals("shared", attr.getName());
    assertEquals("character varying(1073741823)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertTrue(attr.isShared());
    assertEquals("'sharedvalue'", attr.getSharedValue());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"bit" bit(1),
    attr = jSchema.getAttributes().get(16);
    assertFalse(attr.isClassAttribute());
    assertEquals("bit", attr.getName());
    assertEquals("bit(1)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"bitv" bit varying(100),
    attr = jSchema.getAttributes().get(17);
    assertFalse(attr.isClassAttribute());
    assertEquals("bitv", attr.getName());
    assertEquals("bit varying(100)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"nchar" national character(1),
    attr = jSchema.getAttributes().get(18);
    assertFalse(attr.isClassAttribute());
    assertEquals("nchar", attr.getName());
    assertEquals("national character(1)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //"varnchar" national character varying(100)
    attr = jSchema.getAttributes().get(19);
    assertFalse(attr.isClassAttribute());
    assertEquals("varnchar", attr.getName());
    assertEquals("national character varying(100)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"sequence_numeric" sequence_of(numeric(15,1)),
    attr = jSchema.getAttributes().get(20);
    assertFalse(attr.isClassAttribute());
    assertEquals("sequence_numeric", attr.getName());
    assertEquals("sequence_of(numeric(15,1))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"set_bigint" set_of(bigint),		
    attr = jSchema.getAttributes().get(21);
    assertFalse(attr.isClassAttribute());
    assertEquals("set_bigint", attr.getName());
    assertEquals("set_of(bigint)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"multiset_float" multiset_of(float),	
    attr = jSchema.getAttributes().get(22);
    assertFalse(attr.isClassAttribute());
    assertEquals("multiset_float", attr.getName());
    assertEquals("multiset_of(float)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"sequence_nchar" sequence_of(national character(2)),
    attr = jSchema.getAttributes().get(23);
    assertFalse(attr.isClassAttribute());
    assertEquals("sequence_nchar", attr.getName());
    assertEquals("sequence_of(national character(2))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"set_vnchar" set_of(national character varying(100)),
    attr = jSchema.getAttributes().get(24);
    assertFalse(attr.isClassAttribute());
    assertEquals("set_vnchar", attr.getName());
    assertEquals("set_of(national character varying(100))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"set_time" set_of(time),		
    attr = jSchema.getAttributes().get(25);
    assertFalse(attr.isClassAttribute());
    assertEquals("set_time", attr.getName());
    assertEquals("set_of(time)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"multiset_date" multiset_of(date),		
    attr = jSchema.getAttributes().get(26);
    assertFalse(attr.isClassAttribute());
    assertEquals("multiset_date", attr.getName());
    assertEquals("multiset_of(date)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"sequence_datetime" sequence_of(datetime),
    attr = jSchema.getAttributes().get(27);
    assertFalse(attr.isClassAttribute());
    assertEquals("sequence_datetime", attr.getName());
    assertEquals("sequence_of(datetime)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"set_timestamp" set_of(timestamp),		
    attr = jSchema.getAttributes().get(28);
    assertFalse(attr.isClassAttribute());
    assertEquals("set_timestamp", attr.getName());
    assertEquals("set_of(timestamp)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"multiset_monetary" multiset_of(monetary),
    attr = jSchema.getAttributes().get(29);
    assertFalse(attr.isClassAttribute());
    assertEquals("multiset_monetary", attr.getName());
    assertEquals("multiset_of(monetary)", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"sequence_bit" sequence_of(bit(2)),
    attr = jSchema.getAttributes().get(30);
    assertFalse(attr.isClassAttribute());
    assertEquals("sequence_bit", attr.getName());
    assertEquals("sequence_of(bit(2))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    //		"set_varbit" set_of(bit varying(120))
    attr = jSchema.getAttributes().get(31);
    assertFalse(attr.isClassAttribute());
    assertEquals("set_varbit", attr.getName());
    assertEquals("set_of(bit varying(120))", attr.getType());
    assertEquals(testTableName, attr.getInherit());
    assertNull(attr.getDefault());
    auto = attr.getAutoIncrement();
    assertNull(auto);
    List<String> ruleList = new ArrayList<String>();
    List<String> attrList = new ArrayList<String>();
    String constraintName = null;
    Constraint constraint = null;
    //CREATE  UNIQUE INDEX ON "testgetrecordcounttask"("numeric2" DESC,"float" ASC,"date" DESC);
    attrList.clear();
    ruleList.clear();
    attrList.add("numeric2");
    attrList.add("float");
    attrList.add("date");
    ruleList.add("numeric2 DESC");
    ruleList.add("float ASC");
    ruleList.add("date DESC");
    constraintName = ConstraintNamingUtil.getUniqueName(testTableName, ruleList);
    constraint = jSchema.getConstraintByName(constraintName);
    assertNotNull(constraint);
    assertEquals(attrList, constraint.getAttributes());
    assertEquals(ruleList, constraint.getRules());
    //CREATE  REVERSE UNIQUE INDEX ON "testgetrecordcounttask"("integer" DESC,"bigint" ASC);
    attrList.clear();
    ruleList.clear();
    attrList.add("integer");
    attrList.add("bigint");
    ruleList.add("integer DESC");
    ruleList.add("bigint ASC");
    constraintName = ConstraintNamingUtil.getReverseUniqueName(testTableName, attrList);
    constraint = jSchema.getConstraintByName(constraintName);
    assertNotNull(constraint);
    assertEquals(attrList, constraint.getAttributes());
    assertNotSame(ruleList, constraint.getRules());
    assertEquals("integer DESC", constraint.getRules().get(0));
    assertEquals("bigint DESC", constraint.getRules().get(1));
    //CREATE  INDEX ON "testgetrecordcounttask"("smallint" DESC,"smallint2" DESC);
    attrList.clear();
    ruleList.clear();
    attrList.add("smallint");
    attrList.add("smallint2");
    ruleList.add("smallint DESC");
    ruleList.add("smallint2 DESC");
    constraintName = ConstraintNamingUtil.getIndexName(testTableName, ruleList);
    constraint = jSchema.getConstraintByName(constraintName);
    assertNotNull(constraint);
    assertEquals(attrList, constraint.getAttributes());
    assertEquals(ruleList, constraint.getRules());
    //CREATE  REVERSE INDEX ON "testgetrecordcounttask"("numeric1" ASC,"numeric2" DESC);
    attrList.clear();
    ruleList.clear();
    attrList.add("numeric1");
    attrList.add("numeric2");
    ruleList.add("numeric1 ASC");
    ruleList.add("numeric2 DESC");
    constraintName = ConstraintNamingUtil.getReverseIndexName(testTableName, attrList);
    constraint = jSchema.getConstraintByName(constraintName);
    assertNotNull(constraint);
    assertEquals(attrList, constraint.getAttributes());
    assertEquals("numeric1 DESC", constraint.getRules().get(0));
    assertEquals("numeric2 DESC", constraint.getRules().get(1));
    task.setErrorMsg("errorMsg");
    task.execute();
    task.setErrorMsg(null);
    task.execute();
    task.cancel();
    task.execute();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 17 with Constraint

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

the class SchemaAlterDDLTest method fkTest1.

// add a new fk, then delete it
private void fkTest1() {
    changeList = new SchemaChangeManager(databaseInfo, false);
    ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaInfo newSchema = sup3.clone();
    Constraint fk = null;
    String foreignTable = "sup2";
    String deleteRule = "RESTRICT";
    String updateRule = "RESTRICT";
    fk = new Constraint(false);
    fk.setType(Constraint.ConstraintType.FOREIGNKEY.getText());
    fk.addAttribute("numeric1");
    fk.addAttribute("numeric2");
    String fkName = "";
    if (fkName.equals("")) {
        //$NON-NLS-1$
        fkName = ConstraintNamingUtil.getFKName(newSchema.getClassname(), fk.getAttributes());
    }
    fk.setName(fkName);
    //$NON-NLS-1$		
    fk.addRule("REFERENCES " + foreignTable);
    fk.addRule("ON DELETE " + deleteRule);
    fk.addRule("ON UPDATE " + updateRule);
    fk.addRule("ON CACHE OBJECT " + "cachedtable");
    newSchema.addConstraint(fk);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, fk.getName(), SchemeInnerType.TYPE_FK));
    String expected = "ALTER TABLE sup3 ADD FOREIGN KEY (numeric1,numeric2) " + "REFERENCES sup2(numeric1,numeric2) " + "ON DELETE RESTRICT ON UPDATE RESTRICT ON CACHE OBJECT cachedtable;";
    String alterDDL = ddl.getAlterDDL(sup3, newSchema);
    assertEquals(expected, alterDDL.trim());
    //drop this new added fk		
    newSchema.removeFKConstraint(fkName);
    changeList.addSchemeChangeLog(new SchemaChangeLog(fkName, null, SchemeInnerType.TYPE_FK));
    expected = "";
    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 18 with Constraint

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

the class SchemaDDLTest method testGetAlterDDL2.

public void testGetAlterDDL2() throws Exception {
    boolean success = createTestTable();
    assertTrue(success);
    SchemaChangeManager changeLogMgr = new SchemaChangeManager(databaseInfo, false);
    SchemaInfo schema = databaseInfo.getSchemaInfo(testTableName);
    SchemaInfo alteredschema = schema.clone();
    alteredschema.setClassname("alteredName");
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(schema.getClassname(), alteredschema.getClassname(), SchemeInnerType.TYPE_SCHEMA));
    /*Change supper*/
    schema.setSuperClasses(new ArrayList<String>());
    alteredschema.setSuperClasses(new ArrayList<String>());
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(schema.getClassname(), alteredschema.getClassname(), SchemeInnerType.TYPE_SUPER_TABLE));
    /*Remove attr*/
    alteredschema.removeDBAttributeByName("gender", false);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog("gender", null, SchemeInnerType.TYPE_ATTRIBUTE));
    /*Change position*/
    DBAttribute charAttr = alteredschema.getDBAttributeByName("char", false);
    alteredschema.getAttributes().remove(charAttr);
    alteredschema.addAttribute(charAttr);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(charAttr.getName(), charAttr.getName(), SchemeInnerType.TYPE_POSITION));
    /*Change attr*/
    DBAttribute attr1 = alteredschema.getDBAttributeByName("nation_code", false);
    alteredschema.removeDBAttributeByName("nation_code", false);
    attr1.setName("nation_code2");
    attr1.setDefault("c");
    attr1.setNotNull(true);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog("nation_code", "nation_code2", SchemeInnerType.TYPE_ATTRIBUTE));
    alteredschema.addAttribute(attr1);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog("nation_code", "nation_code2", SchemeInnerType.TYPE_POSITION));
    /*Add attr*/
    DBAttribute newAttr = new DBAttribute();
    newAttr.setName("newAttr");
    newAttr.setDefault("0");
    newAttr.setNotNull(false);
    newAttr.setType("String");
    newAttr.setUnique(false);
    newAttr.setInherit(alteredschema.getClassname());
    /*Add pk attr*/
    DBAttribute pkAttr = new DBAttribute();
    pkAttr.setName("pkAttr");
    pkAttr.setNotNull(true);
    pkAttr.setType("Integer");
    pkAttr.setUnique(true);
    pkAttr.setInherit(alteredschema.getClassname());
    pkAttr.setAutoIncrement(new SerialInfo("pk", "dba", "0", "1", "999999", "0", true, "0", "0", alteredschema.getClassname(), pkAttr.getName()));
    alteredschema.addAttribute(newAttr);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(null, newAttr.getName(), SchemeInnerType.TYPE_ATTRIBUTE));
    /*Add index*/
    Constraint index = new Constraint(false);
    index.setType(Constraint.ConstraintType.INDEX.getText());
    index.addAttribute("newAttr");
    index.addRule("newAttr_index DESC");
    index.setName(ConstraintNamingUtil.getIndexName(alteredschema.getClassname(), index.getRules()));
    alteredschema.addConstraint(index);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(null, index.getDefaultName(alteredschema.getClassname()) + "$" + index.getName(), SchemeInnerType.TYPE_INDEX));
    /*Remove index*/
    Constraint removeConstraint = alteredschema.getConstraints().get(0);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(removeConstraint.getDefaultName(alteredschema.getClassname()) + "$" + removeConstraint.getName(), null, SchemeInnerType.TYPE_INDEX));
    /*Add pk*/
    Constraint pk = new Constraint(false);
    pk.addAttribute(pkAttr.getName());
    pk.setName(ConstraintNamingUtil.getPKName(alteredschema.getClassname(), pk.getAttributes()));
    pk.setType(Constraint.ConstraintType.PRIMARYKEY.getText());
    pk.addRule("pkAttr_pk ASC");
    alteredschema.addConstraint(pk);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(null, pk.getDefaultName(alteredschema.getClassname()) + "$" + pk.getName(), SchemeInnerType.TYPE_INDEX));
    /*Remove fk*/
    Constraint fk2 = new Constraint(false);
    fk2.setType(Constraint.ConstraintType.FOREIGNKEY.getText());
    fk2.addAttribute(pkAttr.getName());
    fk2.addRule("REFERENCES " + testTableName);
    fk2.addRule("ON DELETE RESTRICT ON UPDATE RESTRICT");
    String fkName2 = ConstraintNamingUtil.getFKName(schema.getClassname(), fk2.getAttributes());
    fk2.setName(fkName2);
    schema.addConstraint(fk2);
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(fk2.getName(), null, SchemeInnerType.TYPE_FK));
    /*Alter fk*/
    Constraint alterFK = alteredschema.getFKConstraints().get(0);
    String oldFKName = alterFK.getName();
    alterFK.setName(ConstraintNamingUtil.getFKName(schema.getClassname(), fk2.getAttributes()) + "temp");
    changeLogMgr.addSchemeChangeLog(new SchemaChangeLog(oldFKName, alterFK.getName(), SchemeInnerType.TYPE_FK));
    SchemaDDL schemaDDL = new SchemaDDL(changeLogMgr, databaseInfo);
    assertNotNull(schemaDDL.getAlterDDL(schema, alteredschema));
//		schemaDDL = new SchemaDDL(changeLogMgr, databaseInfo831);
//		assertNotNull(schemaDDL.getAlterDDL(schema, alteredschema));
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 19 with Constraint

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

the class TableModelTest method testModelConstraint.

public void testModelConstraint() {
    Constraint bean = new Constraint(false);
    bean.setName("name");
    assertEquals(bean.getName(), "name");
    bean.setType("type");
    assertEquals(bean.getType(), "type");
    bean.setKeyCount(8);
    assertEquals(bean.getKeyCount(), 8);
    bean.addAttribute("Attr");
    assertEquals(bean.getAttributes().size(), 1);
    bean.getReferencedTable();
    bean.getRules();
    bean.getDefaultName("a");
    bean.getClassAttributes();
    bean.addRule("r");
    bean.addClassAttribute("addClassAttribute");
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Example 20 with Constraint

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

the class SubAttribute method getConstraintInfo.

/**
	 * Get constraint information
	 * 
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getConstraintInfo(SchemaInfo schemaInfo) throws SQLException {
    if (schemaInfo == null) {
        return;
    }
    Map<String, Map<String, String>> foreignKeys = getForeignKeyInfo();
    String sql = "SELECT index_name, is_unique, is_reverse, is_primary_key, is_foreign_key, key_count" + " FROM db_index WHERE class_name=? ORDER BY index_name";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    Map<String, String> constraint2Unique = new HashMap<String, String>();
    try {
        stmt = connection.prepareStatement(sql);
        ((PreparedStatement) stmt).setString(1, tableName);
        rs = ((PreparedStatement) stmt).executeQuery();
        while (rs.next()) {
            String constraintName = rs.getString("index_name");
            String pk = rs.getString("is_primary_key");
            String fk = rs.getString("is_foreign_key");
            String unique = rs.getString("is_unique");
            String reverse = rs.getString("is_reverse");
            int keyCount = rs.getInt("key_count");
            Constraint c = new Constraint(false);
            c.setName(constraintName);
            constraint2Unique.put(constraintName, unique);
            if (StringUtil.booleanValueWithYN(pk)) {
                c.setType(Constraint.ConstraintType.PRIMARYKEY.getText());
            } else if (StringUtil.booleanValueWithYN(fk)) {
                c.setType(Constraint.ConstraintType.FOREIGNKEY.getText());
            } else {
                if (StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
                    c.setType(Constraint.ConstraintType.UNIQUE.getText());
                } else if (!StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
                    c.setType(Constraint.ConstraintType.REVERSEINDEX.getText());
                } else if (StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
                    c.setType(Constraint.ConstraintType.REVERSEUNIQUE.getText());
                } else if (!StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
                    c.setType(Constraint.ConstraintType.INDEX.getText());
                }
            }
            c.setKeyCount(keyCount);
            schemaInfo.addConstraint(c);
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
    List<Constraint> cList = schemaInfo.getConstraints();
    boolean isSupportPrefixIndexLength = CompatibleUtil.isSupportPrefixIndexLength(databaseInfo);
    sql = "SELECT key_attr_name, asc_desc, key_order";
    if (isSupportPrefixIndexLength) {
        sql += ", key_prefix_length";
    }
    sql += " FROM db_index_key WHERE index_name=? AND class_name=? ORDER BY key_order";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    for (Constraint c : cList) {
        try {
            stmt = connection.prepareStatement(sql);
            final String constraintName = c.getName();
            ((PreparedStatement) stmt).setString(1, constraintName);
            ((PreparedStatement) stmt).setString(2, tableName);
            rs = ((PreparedStatement) stmt).executeQuery();
            while (rs.next()) {
                String attrName = rs.getString("key_attr_name");
                String ascDesc = rs.getString("asc_desc");
                String indexPrefix = "";
                if (isSupportPrefixIndexLength) {
                    int indexLength = rs.getInt("key_prefix_length");
                    if (indexLength > 0) {
                        indexPrefix = "(" + indexLength + ")";
                    }
                }
                c.addAttribute(attrName);
                if (Constraint.ConstraintType.FOREIGNKEY.getText().equals(c.getType())) {
                    Map<String, String> fkInfo = foreignKeys.get(attrName);
                    if (null != fkInfo) {
                        String referencedTable = c.getReferencedTable();
                        String pkTable = fkInfo.get("PKTABLE_NAME");
                        if (StringUtil.isEqual(referencedTable, pkTable)) {
                            continue;
                        }
                        c.addRule("REFERENCES " + pkTable);
                        c.addRule("ON DELETE " + fkInfo.get("DELETE_RULE"));
                        c.addRule("ON UPDATE " + fkInfo.get("UPDATE_RULE"));
                    }
                } else {
                    c.addRule(attrName + indexPrefix + " " + ascDesc);
                }
                // set the db attributes' unique property.
                if (StringUtil.booleanValueWithYN(constraint2Unique.get(c.getName()))) {
                    DBAttribute dba = schemaInfo.getDBAttributeByName(attrName, true);
                    if (dba == null) {
                        dba = schemaInfo.getDBAttributeByName(attrName, false);
                    }
                    if (null != dba) {
                        dba.setUnique(true);
                    }
                }
            }
        } finally {
            QueryUtil.freeQuery(stmt, rs);
        }
    }
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) Constraint(com.cubrid.common.core.common.model.Constraint)

Aggregations

Constraint (com.cubrid.common.core.common.model.Constraint)90 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)47 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)37 ArrayList (java.util.ArrayList)27 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)11 TableItem (org.eclipse.swt.widgets.TableItem)10 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)9 List (java.util.List)9 SchemaChangeLog (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)7 HashMap (java.util.HashMap)6 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 Node (org.w3c.dom.Node)4 DBResolution (com.cubrid.common.core.common.model.DBResolution)3 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)3 ERSchema (com.cubrid.common.ui.er.model.ERSchema)3 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 NodeList (org.w3c.dom.NodeList)3