use of com.cubrid.common.core.common.model.SchemaInfo 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));
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class SubAttribute method execute.
/**
* Execute the tasks
*/
public void execute() {
boolean isAutocommit = false;
try {
if (errorMsg != null && errorMsg.trim().length() > 0) {
return;
}
if (connection == null || connection.isClosed()) {
errorMsg = Messages.error_getConnection;
return;
}
/*Check is canceled*/
if (monitor != null && monitor.isCanceled()) {
setCancel(true);
setErrorMsg("Current user canceled.");
return;
}
// [TOOLS-2425]Support shard broker
if (databaseInfo != null && databaseInfo.isShard()) {
isAutocommit = connection.getAutoCommit();
connection.setAutoCommit(true);
}
SchemaInfo schemaInfo = getTableInfo();
// get super class information
getSuperClassInfo(schemaInfo);
// get column information
getColumnInfo(schemaInfo);
/*Check is canceled*/
if (monitor != null && monitor.isCanceled()) {
setCancel(true);
setErrorMsg("Current user canceled.");
return;
}
// get auto increment information from db_serial table, which is a system table accessed by all users
getAutoIncrementInfo(schemaInfo);
//get set(object) type information from db_attr_setdomain_elm view
getTypeInfo(schemaInfo);
//get pk, fk, index(unique,reverse index, reverse unique)
getConstraintInfo(schemaInfo);
/*Check is canceled*/
if (monitor != null && monitor.isCanceled()) {
setCancel(true);
setErrorMsg("Current user canceled.");
return;
}
//get db resolutions
getDBResolutionInfo(schemaInfo);
//get query specs if it is a view.
getQuerySpecs(schemaInfo);
//get the partition information of table.
getPartitionInfo(schemaInfo);
schema = schemaInfo;
} catch (SQLException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} finally {
// [TOOLS-2425]Support shard broker
if (databaseInfo != null && databaseInfo.isShard()) {
try {
connection.setAutoCommit(isAutocommit);
} catch (SQLException ignored) {
}
}
finish();
}
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class GetAllSchemaTask method getAutoIncrementInfo.
/**
* Get auto increment information
*
* @param schemaInfo the SchemaInfo
* @throws SQLException the exception
*/
private void getAutoIncrementInfo() throws SQLException {
List<SerialInfo> serialInfoList = new ArrayList<SerialInfo>();
boolean isSupportCache = CompatibleUtil.isSupportCache(databaseInfo);
String sql = "SELECT owner.name, db_serial.*" + " FROM db_serial" + " WHERE class_name IS NOT NULL";
// [TOOLS-2425]Support shard broker
sql = databaseInfo.wrapShardQuery(sql);
try {
stmt = connection.prepareStatement(sql);
rs = ((PreparedStatement) stmt).executeQuery();
while (rs.next()) {
String name = rs.getString("name");
String owner = rs.getString("owner.name");
String currentVal = rs.getString("current_val");
String incrementVal = rs.getString("increment_val");
String maxVal = rs.getString("max_val");
String minVal = rs.getString("min_val");
String cyclic = rs.getString("cyclic");
String startVal = rs.getString("started");
String className = rs.getString("class_name");
String attName = rs.getString("att_name");
String cacheCount = null;
if (isSupportCache) {
cacheCount = rs.getString("cached_num");
}
boolean isCycle = false;
if ("1".equals(cyclic)) {
isCycle = true;
}
SerialInfo serialInfo = new SerialInfo(name, owner, currentVal, incrementVal, maxVal, minVal, isCycle, startVal, cacheCount, className, attName);
serialInfoList.add(serialInfo);
}
for (SerialInfo autoIncrement : serialInfoList) {
String className = autoIncrement.getClassName();
String attrName = autoIncrement.getAttName();
assert (null != attrName);
SchemaInfo schemaInfo = schemas.get(className);
if (schemaInfo == null) {
LOGGER.error("Can't find a table with auto increment column : " + attrName);
continue;
}
DBAttribute a = schemaInfo.getDBAttributeByName(attrName, false);
if (a != null) {
a.setAutoIncrement(autoIncrement);
}
}
} finally {
QueryUtil.freeQuery(stmt, rs);
}
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class GetAllSchemaTask method getConstraintInfo.
/**
* Get constraint information
*
* @param schemaInfo the SchemaInfo
* @throws SQLException the exception
*/
private void getConstraintInfo() throws SQLException {
Map<String, Map<String, String>> foreignKeys = getForeignKeyInfo();
boolean isSupportPrefixIndexLength = CompatibleUtil.isSupportPrefixIndexLength(databaseInfo);
String extraColumns = null;
if (isSupportPrefixIndexLength) {
extraColumns = ", key_prefix_length";
} else {
extraColumns = "";
}
Map<String, Constraint> constraintMap = new HashMap<String, Constraint>();
String sql = "SELECT i.class_name, i.index_name, i.is_unique, i.is_reverse," + " i.is_primary_key, i.is_foreign_key, i.key_count," + " k.key_attr_name, k.asc_desc, k.key_order" + extraColumns + " FROM db_index i, db_index_key k" + " WHERE i.class_name=k.class_name AND i.index_name=k.index_name" + " ORDER BY i.class_name, i.index_name, k.key_order";
// [TOOLS-2425]Support shard broker
sql = databaseInfo.wrapShardQuery(sql);
try {
stmt = connection.prepareStatement(sql);
rs = ((PreparedStatement) stmt).executeQuery();
Map<String, String> constraint2Unique = new HashMap<String, String>();
while (rs.next()) {
String className = rs.getString("class_name");
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");
SchemaInfo schemaInfo = schemas.get(className);
if (schemaInfo == null) {
LOGGER.error("Table " + className + " not found on the schema info.");
continue;
}
String constraintKey = className + "_" + constraintName;
Constraint constraint = constraintMap.get(constraintKey);
if (constraint == null) {
constraint = new Constraint(false);
constraint.setName(constraintName);
schemaInfo.addConstraint(constraint);
constraintMap.put(constraintKey, constraint);
if (StringUtil.booleanValueWithYN(pk)) {
constraint.setType(Constraint.ConstraintType.PRIMARYKEY.getText());
} else if (StringUtil.booleanValueWithYN(fk)) {
constraint.setType(Constraint.ConstraintType.FOREIGNKEY.getText());
} else {
if (StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
constraint.setType(Constraint.ConstraintType.UNIQUE.getText());
} else if (!StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
constraint.setType(Constraint.ConstraintType.REVERSEINDEX.getText());
} else if (StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
constraint.setType(Constraint.ConstraintType.REVERSEUNIQUE.getText());
} else if (!StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
constraint.setType(Constraint.ConstraintType.INDEX.getText());
}
}
constraint.setKeyCount(keyCount);
constraint2Unique.put(constraintName, unique);
}
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 + ")";
}
}
constraint.addAttribute(attrName);
if (Constraint.ConstraintType.FOREIGNKEY.getText().equals(constraint.getType())) {
String key = className + "." + attrName;
Map<String, String> fkInfo = foreignKeys.get(key);
if (null != fkInfo) {
//.append(" ON CACHE OBJECT {3}");
constraint.addRule("REFERENCES " + fkInfo.get("PKTABLE_NAME"));
constraint.addRule("ON DELETE " + fkInfo.get("DELETE_RULE"));
constraint.addRule("ON UPDATE " + fkInfo.get("UPDATE_RULE"));
}
} else {
constraint.addRule(attrName + indexPrefix + " " + ascDesc);
}
//set the db attributes' unique property.
if (StringUtil.booleanValueWithYN(constraint2Unique.get(constraint.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);
}
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class GetAllSchemaTask method getTypeInfo.
/**
* Get type information
*
* @param schemaInfo the SchemaInfo
* @throws SQLException the exception
*/
private void getTypeInfo() throws SQLException {
String sql = "SELECT a.class_name, a.attr_name, a.attr_type," + " a.data_type, a.prec, a.scale" + " FROM db_attr_setdomain_elm a" + " ORDER BY a.class_name, a.attr_name";
// [TOOLS-2425]Support shard broker
sql = databaseInfo.wrapShardQuery(sql);
try {
stmt = connection.prepareStatement(sql);
rs = ((PreparedStatement) stmt).executeQuery();
Map<String, Map<String, List<SubAttribute>>> schemaColumnMap = new HashMap<String, Map<String, List<SubAttribute>>>();
while (rs.next()) {
String className = rs.getString("class_name");
String attrName = rs.getString("attr_name");
String type = rs.getString("attr_type");
String dateType = rs.getString("data_type");
String prec = rs.getString("prec");
String scale = rs.getString("scale");
String subType = DataType.convertAttrTypeString(dateType, prec, scale);
Map<String, List<SubAttribute>> columnMap = schemaColumnMap.get("className");
if (columnMap == null) {
columnMap = new HashMap<String, List<SubAttribute>>();
schemaColumnMap.put(className, columnMap);
}
List<SubAttribute> subList = columnMap.get(attrName);
if (subList == null) {
subList = new ArrayList<SubAttribute>();
columnMap.put(attrName, subList);
}
subList.add(new SubAttribute(type, subType));
}
for (Entry<String, Map<String, List<SubAttribute>>> entryMap : schemaColumnMap.entrySet()) {
String className = entryMap.getKey();
Map<String, List<SubAttribute>> columnMap = entryMap.getValue();
SchemaInfo schemaInfo = schemas.get(className);
if (schemaInfo == null) {
LOGGER.error("Table " + className + " not found on the schema info.");
continue;
}
for (Entry<String, List<SubAttribute>> entry : columnMap.entrySet()) {
String name = entry.getKey();
List<SubAttribute> subList = entry.getValue();
DBAttribute attr = null;
if ("INSTANCE".equals(subList.get(0).getParentInstanceType())) {
//INSTANCE
attr = schemaInfo.getDBAttributeByName(name, false);
} else if ("CLASS".equals(subList.get(0).getParentInstanceType())) {
attr = schemaInfo.getDBAttributeByName(name, true);
} else {
attr = schemaInfo.getDBAttributeByName(name, false);
}
StringBuilder sb = new StringBuilder();
sb.append(attr.getType()).append("(");
int size = subList.size();
for (int i = 0; i < size; i++) {
SubAttribute subType = subList.get(i);
sb.append(subType.getsubDataType());
if (i + 1 < size) {
sb.append(",");
}
}
sb.append(")");
attr.setType(sb.toString());
}
}
} finally {
QueryUtil.freeQuery(stmt, rs);
}
}
Aggregations