Search in sources :

Example 26 with DBAttribute

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

the class SchemaChangeManagerTest method testGetDDL.

/**
	 * test getting ddl
	 */
public void testGetDDL() {
    SchemaInfo schema1 = new SchemaInfo();
    schema1.setClassname(tableName);
    schema1.setOwner(owner);
    schema1.setVirtual(virtual);
    schema1.setType(type);
    DBAttribute a = new DBAttribute();
    a.setName(attributeName);
    a.setType(dataType);
    a.setInherit(inherit);
    a.setNotNull(isNotNull);
    a.setShared(false);
    a.setUnique(unique);
    a.setDefault(defaultv);
    schema1.addAttribute(a);
    schema1.addClassAttribute(a);
    Constraint pk = new Constraint(false);
    pk.setName(pkName);
    pk.setType(pkType);
    pk.addAttribute(attributes[0]);
    pk.addAttribute(attributes[1]);
    pk.addAttribute(attributes[2]);
    schema1.addConstraint(pk);
    schemaChangeManager.getChangeList();
    schemaChangeManager.isNewAdded(SchemeInnerType.TYPE_ATTRIBUTE, "value");
    schemaChangeManager.isNewAdded("attrName", true);
    schemaChangeManager.isNewAdded("attrName", false);
    schemaChangeManager.getIndexChangeLogs();
    schemaChangeManager.getFKChangeLogs();
    schemaChangeManager.getAttrChangeLogs();
    schemaChangeManager.getClassAttrChangeLogs();
    schemaChangeManager.getChangeLogs(SchemeInnerType.TYPE_ATTRIBUTE);
    schemaChangeManager.setNewTableFlag(true);
    schemaChangeManager.isNewTableFlag();
    schemaChangeManager.setDatabaseInfo(database);
    schemaChangeManager.getDatabaseInfo();
    String oldValue = "oldValue";
    String newValue = "newValue";
    SchemeInnerType stype = SchemeInnerType.TYPE_ATTRIBUTE;
    SchemaChangeLog schemeChangeLog = new SchemaChangeLog(oldValue, newValue, stype);
    changeList = new ArrayList<SchemaChangeLog>();
    changeList.add(schemeChangeLog);
    schemaChangeManager.setChangeList(changeList);
    schemaChangeManager.getChangeLogs(SchemeInnerType.TYPE_ATTRIBUTE);
    schemaChangeManager.addSchemeChangeLog(schemeChangeLog);
    schemaChangeManager.setNewTableFlag(false);
    schemaChangeManager.addSchemeChangeLog(schemeChangeLog);
    schemaChangeManager.isNewAdded(SchemeInnerType.TYPE_ATTRIBUTE, "value");
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemeInnerType(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog.SchemeInnerType) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 27 with DBAttribute

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

the class SchemaDDLTest method testdata.

/**
	 * test data()
	 * 
	 */
@SuppressWarnings("unused")
public void testdata() throws Exception {
    boolean success = createTestTable();
    assertTrue(success);
    SchemaInfo sup1 = databaseInfo.getSchemaInfo("sup1");
    SchemaInfo sup2 = databaseInfo.getSchemaInfo("sup2");
    SchemaInfo test = databaseInfo.getSchemaInfo("test");
    SchemaInfo testSuperTableName = databaseInfo.getSchemaInfo("testSuperTableName");
    SchemaInfo testTableName = databaseInfo.getSchemaInfo("testTableName");
    SchemaChangeManager changeList = new SchemaChangeManager(databaseInfo, true);
    List<SchemaChangeLog> changeListNoAuto = new ArrayList<SchemaChangeLog>();
    //SchemeChangeLog schemeChangeLog= new SchemeChangeLog();
    changeList.addSchemeChangeLog(new SchemaChangeLog("a", null, SchemeInnerType.TYPE_CLASSATTRIBUTE));
    changeList.addSchemeChangeLog(new SchemaChangeLog("a", null, SchemeInnerType.TYPE_ATTRIBUTE));
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, "fk", SchemeInnerType.TYPE_FK));
    changeList.addSchemeChangeLog(new SchemaChangeLog("fk", null, SchemeInnerType.TYPE_FK));
    Constraint index = testTableName.getConstraintByName("index", "UNIQUE");
    changeList.addSchemeChangeLog(new SchemaChangeLog(index.getDefaultName(testTableName.getClassname()) + "$" + //$NON-NLS-1$
    index.getName(), //$NON-NLS-1$
    null, SchemeInnerType.TYPE_INDEX));
    changeList.addSchemeChangeLog(new SchemaChangeLog("a", "a", SchemeInnerType.TYPE_ATTRIBUTE));
    changeList.addSchemeChangeLog(new SchemaChangeLog("a", "a", SchemeInnerType.TYPE_ATTRIBUTE));
    //changeListNoAuto.add(o)
    changeList.setChangeList(changeListNoAuto);
    SchemaDDL ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaChangeManager changeList2 = new SchemaChangeManager(databaseInfo, false);
    SchemaDDL ddl2 = new SchemaDDL(changeList2, databaseInfo);
    List<String[]> columnConflicts = SuperClassUtil.getColumnConflicts(databaseInfo, testTableName, testTableName.getSuperClasses(), true);
    String[][] classConflicts = columnConflicts.toArray(new String[columnConflicts.size()][]);
    columnConflicts = SuperClassUtil.getColumnConflicts(databaseInfo, testTableName, testTableName.getSuperClasses(), false);
    String[][] conflicts = columnConflicts.toArray(new String[columnConflicts.size()][]);
    ddl.getSchemaDDL(sup1);
    ddl.getSchemaDDL(sup2);
    ddl.getSchemaDDL(sup1, sup2);
    ddl.getAlterDDL(sup1, sup2);
    ddl.getSchemaDDL(testTableName);
    ddl.getSchemaDDL(testTableName, testTableName);
    ddl.getAlterDDL(testTableName, testTableName);
    ddl.getSchemaDDL(testTableName, sup1);
    ddl.getSchemaDDL(sup1, testTableName);
    ddl2.getSchemaDDL(sup1);
    ddl2.getSchemaDDL(sup2);
    ddl2.getSchemaDDL(sup1, sup2);
    ddl2.getAlterDDL(sup1, sup2);
    ddl2.getSchemaDDL(testTableName);
    ddl2.getSchemaDDL(testTableName, testTableName);
    ddl2.getAlterDDL(testTableName, testTableName);
    ddl2.getSchemaDDL(testTableName, sup1);
    ddl2.getSchemaDDL(sup1, testTableName);
    String name = "name";
    String className = "className";
    String alias = "alias";
    boolean isClassResolution = true;
    DBResolution oldResolutions = new DBResolution(name, className, alias);
    oldResolutions.setName(name);
    oldResolutions.setClassName(className);
    oldResolutions.setAlias(alias);
    oldResolutions.setClassResolution(isClassResolution);
    DBResolution newResolutions = new DBResolution(name, className, alias);
    newResolutions.setName(name);
    newResolutions.setClassName(className);
    newResolutions.setAlias(alias);
    newResolutions.setClassResolution(isClassResolution);
    List<DBResolution> oldResolution = new ArrayList<DBResolution>();
    oldResolution.add(oldResolutions);
    List<DBResolution> newResolution = new ArrayList<DBResolution>();
    newResolution.add(newResolutions);
    ddl.getResolutionChanges(oldResolution, newResolution);
    List<String> oldSupers = new ArrayList<String>();
    oldSupers.add("oldstring");
    List<String> newSupers = new ArrayList<String>();
    newSupers.add("newstring");
    ddl.getSuperclassChanges(oldSupers, newSupers);
    ddl.getAddSuperClassDDL("tableName", newSupers, oldResolution, newResolution);
    ddl.getDropSuperClassesDDL("tableName", newSupers);
    ddl.getChangeOwnerDDL("tableName", "newOwner");
    String aname = "name";
    String type = "type";
    // it belongs to which class
    String inherit = "inherit";
    boolean indexed = true;
    boolean notNull = true;
    boolean shared = true;
    boolean unique = true;
    String defaultValue = "defaultValue";
    SerialInfo autoIncrement = null;
    String domainClassName = "domainClassName";
    boolean isClassAttribute = true;
    DBAttribute dbAttribute = new DBAttribute(aname, type, inherit, indexed, notNull, shared, unique, defaultValue, "iso88591_bin");
    ddl.getAddColumnDDL("tableName", dbAttribute, newSupers, sup1);
    ddl.setEndLineChar("endLineChar");
    String aclassName = "className";
    String partitionName = "partitionName";
    String partitionClassName = "partitionClassName";
    PartitionType partitionType = PartitionType.HASH;
    String partitionExpr = "partitionExpr";
    final List<String> partitionValues = new ArrayList<String>();
    partitionValues.add("str");
    partitionValues.add("str1");
    final List<String> partitionValues2 = new ArrayList<String>();
    partitionValues.add("str");
    partitionValues.add(null);
    int rows = -1;
    PartitionInfo partitionInfo4 = new PartitionInfo(aclassName, partitionName, partitionClassName, PartitionType.LIST, partitionExpr, partitionValues, rows);
    PartitionInfo partitionInfo6 = new PartitionInfo(aclassName, partitionName, partitionClassName, partitionType, partitionExpr, partitionValues2, rows);
    PartitionInfo partitionInfo7 = new PartitionInfo(aclassName, partitionName, partitionClassName, PartitionType.RANGE, partitionExpr, partitionValues, rows);
    List<PartitionInfo> partInfoList = new ArrayList<PartitionInfo>();
    partInfoList.add(partitionInfo4);
    ddl.getTransformToPartitionDDL(partInfoList);
    List<PartitionInfo> partInfoListRange = new ArrayList<PartitionInfo>();
    partInfoListRange.add(partitionInfo7);
    ddl.getTransformToPartitionDDL(partInfoListRange);
    ddl.getTransformToGenericDDL("tableName");
    ddl.getAddPartitionDDL(partitionInfo4);
    ddl.getDelPartitionDDL("tableName", "partName");
    PartitionInfo partitionInfo5 = new PartitionInfo(aclassName, partitionName, partitionClassName, partitionType, "partitionExpr1", partitionValues, -1);
    List<PartitionInfo> newPartInfoList = new ArrayList<PartitionInfo>();
    newPartInfoList.add(partitionInfo5);
    newPartInfoList.add(partitionInfo7);
    ddl.getCoalescePartitionDDL(partInfoList, newPartInfoList);
    ddl.getCoalescePartitionDDL(newPartInfoList, partInfoList);
    ddl.getCoalescePartitionDDL(newPartInfoList, partInfoListRange);
    ddl.getSplitPartitionDDL(partInfoList, newPartInfoList);
    partInfoList.clear();
    partInfoList.add(partitionInfo6);
    ddl.getSplitPartitionDDL(partInfoList, newPartInfoList);
    ddl.getAlterAutoIncrementDDL("tableName", "columnName");
    partInfoList.clear();
    partitionInfo4 = new PartitionInfo(aclassName, partitionName, partitionClassName, PartitionType.RANGE, partitionExpr, partitionValues, rows);
    partInfoList.add(partitionInfo4);
    ddl.getSplitPartitionDDL(partInfoList, newPartInfoList);
    partInfoList.clear();
    partitionInfo4 = new PartitionInfo(aclassName, partitionName, partitionClassName, PartitionType.LIST, partitionExpr, partitionValues, rows);
    partInfoList.add(partitionInfo4);
    ddl.getSplitPartitionDDL(partInfoList, newPartInfoList);
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) PartitionType(com.cubrid.common.core.common.model.PartitionType) Constraint(com.cubrid.common.core.common.model.Constraint) DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 28 with DBAttribute

use of com.cubrid.common.core.common.model.DBAttribute 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 29 with DBAttribute

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

the class SchemaAlterDDLTest method columnTest.

private void columnTest() {
    changeList = new SchemaChangeManager(databaseInfo, false);
    ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaInfo newSchema = testedSchemaInfo.clone();
    String alterDDL = null;
    boolean isClassAttribute = false;
    DBAttribute attr = null;
    String expected = null;
    //"smallint" smallint AUTO_INCREMENT PRIMARY KEY is inherited from "sup1",
    // now, add attribute "smallint smallint"
    String testColumn = "smallint";
    isClassAttribute = false;
    attr = new DBAttribute();
    attr.setName(testColumn);
    attr.setType("smallint");
    attr.setUnique(false);
    attr.setInherit(newSchema.getClassname());
    addColumn(newSchema, isClassAttribute, attr);
    DBAttribute findAttr = newSchema.getDBAttributeByName(testColumn, isClassAttribute);
    assertNotNull(findAttr);
    assertFalse(newSchema.isAttributeUnique(findAttr, superList));
    assertNull(findAttr.getAutoIncrement());
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " ADD COLUMN [smallint] smallint AFTER [shared];";
    assertEquals(expected, alterDDL.trim());
    //edit the column again, rename the column
    DBAttribute oldAttribute = findAttr;
    DBAttribute editAttribute = oldAttribute.clone();
    String newColumnName = "smallintlocal";
    editAttribute.setName(newColumnName);
    boolean isEditAll = true;
    editColumn(newSchema, editAttribute, oldAttribute, isEditAll);
    //expected inherited column exist
    findAttr = newSchema.getDBAttributeByName(testColumn, isClassAttribute);
    assertNotNull(findAttr);
    assertTrue(newSchema.isAttributeUnique(findAttr, superList));
    assertNotNull(findAttr.getAutoIncrement());
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " ADD COLUMN smallintlocal smallint AFTER [shared];";
    assertEquals(expected, alterDDL.trim());
    //drop the renamed column, expected no change
    String attrName = newColumnName;
    isClassAttribute = false;
    dropColumn(newSchema, isClassAttribute, attrName);
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "";
    assertEquals(expected, alterDDL.trim());
    //edit the column again, rename the column
    testColumn = "timestamp";
    oldAttribute = newSchema.getDBAttributeByName(testColumn, isClassAttribute);
    editAttribute = oldAttribute.clone();
    newColumnName = "newtimestamp";
    editAttribute.setName(newColumnName);
    editAttribute.setDefault("TIMESTAMP'12/01/2000 03:07:51'");
    isEditAll = false;
    editColumn(newSchema, editAttribute, oldAttribute, isEditAll);
    //expected inherited column exist
    findAttr = newSchema.getDBAttributeByName(newColumnName, isClassAttribute);
    assertNotNull(findAttr);
    assertEquals("TIMESTAMP'12/01/2000 03:07:51'", editAttribute.getDefault());
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " RENAME [timestamp] AS newtimestamp;" + StringUtil.NEWLINE;
    expected += "ALTER TABLE " + testTableName.toLowerCase() + " ALTER newtimestamp SET DEFAULT TIMESTAMP'2000-12-01 03:07:51';";
    assertEquals(expected, alterDDL.trim());
    //drop this column, expected drop this column		
    attrName = newColumnName;
    isClassAttribute = false;
    dropColumn(newSchema, isClassAttribute, attrName);
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " DROP  COLUMN [timestamp];";
    assertEquals(expected, alterDDL.trim());
    //add class column ff2
    testColumn = "ff2";
    isClassAttribute = true;
    attr = new DBAttribute();
    attr.setName(testColumn);
    attr.setType("smallint");
    attr.setInherit(newSchema.getClassname());
    addColumn(newSchema, isClassAttribute, attr);
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " ADD CLASS ATTRIBUTE ff2 smallint;" + StringUtil.NEWLINE + expected;
    assertEquals(expected.trim(), alterDDL.trim());
    //edit class column ff, rename the column to ff3
    testColumn = "ff";
    oldAttribute = newSchema.getDBAttributeByName(testColumn, isClassAttribute);
    editAttribute = oldAttribute.clone();
    newColumnName = "ff3";
    editAttribute.setName(newColumnName);
    editAttribute.setDefault("4");
    isEditAll = false;
    editColumn(newSchema, editAttribute, oldAttribute, isEditAll);
    findAttr = newSchema.getDBAttributeByName(newColumnName, isClassAttribute);
    assertNotNull(findAttr);
    assertEquals("4", editAttribute.getDefault());
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " RENAME CLASS ff AS ff3;";
    assertNotSame(-1, alterDDL.indexOf(expected));
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " ALTER CLASS ff3 SET DEFAULT 4;";
    assertNotSame(expected, alterDDL);
    //drop this column ff3, expected drop this column		
    attrName = newColumnName;
    isClassAttribute = true;
    dropColumn(newSchema, isClassAttribute, attrName);
    alterDDL = ddl.getAlterDDL(testedSchemaInfo, newSchema);
    expected = "ALTER TABLE " + testTableName.toLowerCase() + " DROP ATTRIBUTE CLASS ff;";
    assertNotSame(-1, alterDDL.indexOf(expected));
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 30 with DBAttribute

use of com.cubrid.common.core.common.model.DBAttribute 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)

Aggregations

DBAttribute (com.cubrid.common.core.common.model.DBAttribute)130 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)57 Constraint (com.cubrid.common.core.common.model.Constraint)53 ArrayList (java.util.ArrayList)46 HashMap (java.util.HashMap)16 List (java.util.List)15 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)14 TableItem (org.eclipse.swt.widgets.TableItem)13 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)11 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)11 GetAllAttrTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)10 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)8 Map (java.util.Map)8 Point (org.eclipse.swt.graphics.Point)8 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)7 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)6 DBResolution (com.cubrid.common.core.common.model.DBResolution)5 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 SqlFormattingStrategy (com.cubrid.common.ui.query.format.SqlFormattingStrategy)5