use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class TableDashboardComposite method copyTablesDetailToClipboard.
public void copyTablesDetailToClipboard() {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
final Clipboard cb = new Clipboard(shell.getDisplay());
StringBuilder sb = new StringBuilder();
List<Integer> selectIndex = new ArrayList<Integer>();
SchemaInfo schema = (SchemaInfo) columnTableView.getInput();
List<DBAttribute> list = schema.getAttributes();
for (int i = 0; i < columnTableView.getTable().getSelectionIndices().length; i++) {
selectIndex.add(columnTableView.getTable().getSelectionIndices()[i]);
}
for (int i = 0; i < selectIndex.size(); i++) {
if (i != 0) {
sb.append(StringUtil.NEWLINE);
}
DBAttribute attr = list.get(i);
sb.append(attr.getName());
}
TextTransfer textTransfer = TextTransfer.getInstance();
Transfer[] transfers = new Transfer[] { textTransfer };
Object[] data = new Object[] { sb.toString() };
cb.setContents(data, transfers);
cb.dispose();
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class AttributeCellModifier method modify.
public void modify(Object element, String property, Object value) {
// FIXME move this logic to core module
final TableItem item = (TableItem) element;
if (item == null) {
return;
}
DBAttribute attr = (DBAttribute) item.getData();
String attrName = attr.getName();
DBAttribute oldAttribute = null;
if (editor.getOldSchemaInfo() != null) {
oldAttribute = editor.getOldSchemaInfo().getDBAttributeByName(attrName, false);
}
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
return;
}
boolean on = ((Boolean) value).booleanValue();
if (on) {
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
constraint = new Constraint("pk", Constraint.ConstraintType.PRIMARYKEY.getText());
schemaInfo.addConstraint(constraint);
}
constraint.addAttribute(attr.getName());
} else {
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
return;
}
List<String> columns = constraint.getAttributes();
if (columns == null || columns.size() == 0) {
return;
}
boolean isContain = columns.remove(attr.getName());
/*For bug TOOLS-3972 The collumn's setting in Edit Table Inconsistent with the setting in Set Primary Key*/
if (isContain && columns.size() == 0) {
schemaInfo.removeConstraintByName(constraint.getName(), constraint.getType());
}
/*For bug TOOLS-3046 : deal with edit column*/
if (oldAttribute != null && isContain) {
attr.setNotNull(false);
editor.changeForEditElement(attrName, attr, oldAttribute);
}
}
editor.makeChangeLogForIndex(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
String newName = (String) value;
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
// TODO Improve error message
CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
return;
}
if (!StringUtil.isEmpty(newName) && !ValidateUtil.isValidIdentifier(newName)) {
CommonUITool.openErrorBox(Messages.errColumnName);
return;
}
List<DBAttribute> lastAttrs = schemaInfo.getAttributes();
if (StringUtil.isEmpty(newName) && lastAttrs.contains(attr)) {
CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
return;
}
for (DBAttribute lastAttr : lastAttrs) {
if (StringUtil.isEqualIgnoreCase(lastAttr.getName(), newName) && attr != lastAttr) {
CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
return;
}
}
if (!StringUtil.isEqualIgnoreCase(attr.getName(), newName)) {
replaceNewConstraintAttributeName(schemaInfo, attr.getName(), newName);
DBAttribute newAttribute = attr.clone();
if (newAttribute != null) {
newAttribute.setName(newName);
}
if (attr != null) {
attr.setName(newName);
}
if (!hasAddedToSchemaInfo(attr)) {
attr.setName(newName);
if (!StringUtil.isEmpty(newName)) {
if (!lastAttrs.contains(newAttribute)) {
newAttribute.setInherit(editor.getNewSchemaInfo().getClassname());
schemaInfo.addAttribute(newAttribute);
editor.removeElementByName(newName);
}
editor.addNewAttrLog(newName, newAttribute.isClassAttribute());
if (!StringUtil.isEmpty(newAttribute.getName())) {
editor.makeChangeLogForIndex(attrName, newAttribute, oldAttribute);
}
}
} else {
editor.changeForEditElement(attrName, newAttribute, oldAttribute);
}
}
DBAttribute lastDBAttribute = null;
if (lastAttrs.size() > 0) {
Table columnsTable = editor.getColumnsTable();
lastDBAttribute = (DBAttribute) columnsTable.getItem(columnsTable.getItemCount() - 1).getData();
}
if (!StringUtil.isEmpty(newName) && (lastDBAttribute == null || !StringUtil.isEmpty(lastDBAttribute.getName()))) {
editor.addNewColumn();
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
String dataTypeRaw = (String) value;
if (dataTypeRaw != null && dataTypeRaw.trim().toLowerCase().startsWith("enum")) {
int sp = dataTypeRaw.indexOf("(");
if (sp != -1) {
String dataType = dataTypeRaw.substring(0, sp).toLowerCase().trim();
attr.setType(dataType);
String enumeration = dataTypeRaw.substring(sp).trim();
attr.setEnumeration(enumeration);
}
} else {
attr.setType(dataTypeRaw);
}
if (!DataType.isIntegerType(attr.getType())) {
attr.setAutoIncrement(null);
}
if (!DataType.canUseCollation(attr.getType())) {
attr.setCollation("");
}
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
String defaultVal = (String) value;
boolean isStringType = DataType.isStringType(attr.getType());
boolean isEmpty = StringUtil.isEmpty(defaultVal);
boolean isNull = false;
if (defaultVal == null || DataType.NULL_EXPORT_FORMAT.equals(defaultVal) || DataType.VALUE_NULL.equals(defaultVal) || (isEmpty && !isStringType)) {
isNull = true;
}
if (isNull) {
attr.setDefault(null);
} else {
if (attr.getAutoIncrement() != null) {
attr.setDefault(null);
CommonUITool.openErrorBox(Messages.errCanNotSetDefaultOnAI);
return;
}
boolean isConfirmReset = "".equals(defaultVal) && oldAttribute != null && !"".equals(oldAttribute.getDefault());
if (isConfirmReset) {
String confirmResetDef = Messages.confirmResetDef;
if (CommonUITool.openConfirmBox(confirmResetDef)) {
attr.setDefault(null);
} else {
attr.setDefault(defaultVal);
}
} else {
attr.setDefault(defaultVal);
}
}
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
if (aiAttr != null && aiAttr != attr) {
attr.setAutoIncrement(null);
return;
}
String param = (String) value;
if (StringUtil.isNotEmpty(param)) {
if (!param.matches("\\s*[0-9]+\\s*,\\s*[0-9]+\\s*")) {
CommonUITool.openErrorBox(Messages.errInvalidAutoIncrForm);
return;
}
String defaultValue = attr.getDefault();
if (StringUtil.isNotEmpty(defaultValue)) {
CommonUITool.openErrorBox(Messages.errCanNotSetAIOnDefault);
return;
}
String[] params = param.split(",");
String startVal = params[0].trim();
String incrVal = params[1].trim();
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
SerialInfo serial = new SerialInfo();
serial.setOwner(schemaInfo.getOwner());
serial.setClassName(schemaInfo.getClassname());
serial.setAttName(attrName);
serial.setCacheCount("1");
serial.setCurrentValue(startVal);
serial.setCyclic(false);
serial.setIncrementValue(incrVal);
serial.setMaxValue(String.valueOf(Integer.MAX_VALUE));
serial.setMinValue(startVal);
serial.setStartedValue(startVal);
if (attr.getAutoIncrement() != null && schemaInfo != null && schemaInfo.getAutoIncrementColumn() != null && schemaInfo.getAutoIncrementColumn().getAutoIncrement() != null) {
String oldAI = attr.getAutoIncrement().getTableAutoIncrementString();
String newAI = serial.getTableAutoIncrementString();
if (StringUtil.isEqual(oldAI, newAI)) {
return;
}
}
attr.setAutoIncrement(serial);
} else {
attr.setAutoIncrement(null);
}
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
boolean on = ((Boolean) value).booleanValue();
attr.setNotNull(on);
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
boolean on = ((Boolean) value).booleanValue();
if (on && attr.isShared()) {
CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
return;
}
attr.setUnique(on);
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
boolean on = ((Boolean) value).booleanValue();
String defaultValue = attr.getDefault();
if (on && StringUtil.isEmpty(defaultValue)) {
CommonUITool.openErrorBox(Messages.msgInputSharedValue);
return;
}
if (on && attr.isUnique()) {
CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
return;
}
attr.setShared(on);
editor.changeForEditElement(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
String orignCollation = attr.getCollation();
Integer selection = StringUtil.intValue(value.toString(), 0);
if (selection > -1) {
String newCollation = editor.getCollationArray()[selection];
if (!StringUtil.isEqualNotIgnoreNull(orignCollation, newCollation)) {
attr.setCollation(newCollation);
}
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
attr.setDescription((String) value);
}
editor.loadColumnData();
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class AttributeCellModifier method getValue.
public Object getValue(Object element, String property) {
// FIXME move this logic to core module
DBAttribute attr = (DBAttribute) element;
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
return false;
}
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
return false;
}
List<String> columns = constraint.getAttributes();
if (columns == null || columns.size() == 0) {
return false;
}
return columns.contains(attr.getName());
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
return attr.getName();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
String dataType = attr.getType();
if (dataType == null) {
return "";
}
String physicalType = getShownPhysicalType(attr);
return physicalType;
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
String defaultValue = attr.getDefault();
if (defaultValue == null || attr.getAutoIncrement() != null || (StringUtil.isEmpty(defaultValue) && !DataType.isStringType(attr.getType()))) {
return DataType.NULL_EXPORT_FORMAT;
} else {
return defaultValue;
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
if (aiAttr != null && aiAttr != attr) {
CommonUITool.openErrorBox(Messages.errCanNotAddAutoincrementAlreadyExists);
return "";
}
SerialInfo serial = attr.getAutoIncrement();
if (serial == null) {
return "";
}
String defaultValue = attr.getDefault();
if (StringUtil.isNotEmpty(defaultValue)) {
return "";
}
return serial.getTableAutoIncrementString();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
return attr.isNotNull();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
return attr.isUnique();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
return attr.isShared();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
if (DataType.canUseCollation(attr.getType())) {
String collation = attr.getCollation();
return editor.getCollationIndex(collation);
}
return "";
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
return attr.getDescription();
}
return null;
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class ExportSchemaThread method getAllViewsDDL.
private List<String> getAllViewsDDL() {
// FIXME move this logic to core module
List<String> resultList = new ArrayList<String>();
List<String> viewNameList = new ArrayList<String>();
LinkedHashMap<String, String> viewQuerySpecMap = new LinkedHashMap<String, String>();
Connection conn = null;
Statement stmt = null;
CUBRIDResultSetProxy rs = null;
String sql = "SELECT c.class_name, c.class_type" + " FROM db_class c, db_attribute a" + " WHERE c.class_name=a.class_name AND c.is_system_class='NO'" + " AND c.class_type='VCLASS'" + " GROUP BY c.class_name, c.class_type" + " ORDER BY c.class_type, c.class_name";
// [TOOLS-2425]Support shard broker
sql = dbInfo.wrapShardQuery(sql);
try {
conn = JDBCConnectionManager.getConnection(dbInfo, false);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
rs = (CUBRIDResultSetProxy) stmt.executeQuery(sql);
while (rs.next()) {
viewNameList.add(rs.getString(1));
}
} catch (Exception e) {
LOGGER.error("", e);
} finally {
QueryUtil.freeQuery(stmt, rs);
}
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
for (String viewName : viewNameList) {
String querySpecSql = "SELECT vclass_def FROM db_vclass WHERE vclass_name='" + viewName + "'";
// [TOOLS-2425]Support shard broker
querySpecSql = dbInfo.wrapShardQuery(querySpecSql);
try {
rs = (CUBRIDResultSetProxy) stmt.executeQuery(querySpecSql);
if (rs.next()) {
viewQuerySpecMap.put(viewName, rs.getString(1));
}
} finally {
QueryUtil.freeQuery(rs);
}
}
} catch (Exception e) {
LOGGER.error("", e);
} finally {
QueryUtil.freeQuery(conn, stmt);
}
for (Map.Entry<String, String> entry : viewQuerySpecMap.entrySet()) {
SchemaInfo viewInfo = dbInfo.getSchemaInfo(entry.getKey());
String ddl = getViewDDL(viewInfo, dbInfo, entry.getValue());
resultList.add(ddl);
}
return resultList;
}
use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.
the class AbsExportDataHandler method exportSchemaToOBSFile.
/**
* Export the schema and index DDL to file
*
* @param databaseInfo DatabaseInfo
* @param exportDataEventHandler IExportDataEventHandler
* @param tableNameList Set<String>
* @param schemaFile String
* @param indexFile String
* @param fileCharset String
* @param exportStartValue whether keep start value
* @throws SQLException The SQL exception
* @throws IOException The IO exception
*/
public static void exportSchemaToOBSFile(DatabaseInfo databaseInfo, IExportDataEventHandler exportDataEventHandler, Set<String> tableNameList, String schemaFile, String indexFile, String fileCharset, boolean exportStartValue) throws SQLException, IOException {
// FIXME move this logic to core module
if (schemaFile == null && indexFile == null) {
return;
}
Connection conn = null;
BufferedWriter schemaWriter = null;
BufferedWriter indexWriter = null;
LinkedList<SchemaInfo> schemaInfoList = null;
boolean hasDataInIndexFile = false;
try {
if (schemaFile != null) {
schemaWriter = getBufferedWriter(schemaFile, fileCharset);
}
if (indexFile != null) {
indexWriter = getBufferedWriter(indexFile, fileCharset);
schemaInfoList = new LinkedList<SchemaInfo>();
}
SchemaDDL schemaDDL = new SchemaDDL(new SchemaChangeManager(databaseInfo, true), databaseInfo);
conn = JDBCConnectionManager.getConnection(databaseInfo, false);
for (String tableName : tableNameList) {
SchemaInfo schemaInfo = databaseInfo.getSchemaInfo(conn, tableName);
if (schemaInfo == null) {
continue;
}
//write the create DDL
if (schemaWriter == null) {
continue;
}
String ddl = schemaDDL.getSchemaDDL(schemaInfo, false);
if (ddl != null && ddl.trim().length() > 0) {
schemaWriter.write(ddl);
schemaWriter.write(StringUtil.NEWLINE);
}
schemaWriter.flush();
if (exportStartValue) {
List<SerialInfo> autoIncrementList = schemaDDL.getAutoIncrementList(schemaInfo);
if (autoIncrementList.size() > 0) {
for (SerialInfo serialInfo : autoIncrementList) {
String serialName = getSerailName(tableName, serialInfo.getAttName());
String currentValue = getCurrentValueFromSystemTable(serialName, databaseInfo, conn);
String alterStartValueDDL = schemaDDL.getAlterSerialStartValueDDL(serialName, currentValue);
schemaWriter.write(alterStartValueDDL);
schemaWriter.write(StringUtil.NEWLINE);
}
}
}
if (indexWriter == null) {
continue;
}
schemaInfoList.add(schemaInfo);
}
// write PKs, indexes to a file
if (schemaInfoList != null) {
String ddl = null;
// write pk
for (SchemaInfo schemaInfo : schemaInfoList) {
ddl = schemaDDL.getPKsDDL(schemaInfo);
if (ddl != null && ddl.trim().length() > 0) {
indexWriter.write(ddl.trim());
indexWriter.newLine();
hasDataInIndexFile = true;
}
}
if (hasDataInIndexFile) {
indexWriter.newLine();
}
// write index
for (SchemaInfo schemaInfo : schemaInfoList) {
ddl = schemaDDL.getIndexsDDL(schemaInfo);
if (ddl != null && ddl.trim().length() > 0) {
indexWriter.write(ddl.trim());
indexWriter.newLine();
hasDataInIndexFile = true;
}
}
if (hasDataInIndexFile) {
indexWriter.newLine();
}
//write fk
for (SchemaInfo schemaInfo : schemaInfoList) {
ddl = schemaDDL.getFKsDDL(schemaInfo);
if (ddl != null && ddl.trim().length() > 0) {
indexWriter.write(ddl.trim());
indexWriter.newLine();
hasDataInIndexFile = true;
}
}
if (hasDataInIndexFile) {
indexWriter.flush();
}
}
} finally {
QueryUtil.freeQuery(conn);
FileUtil.close(schemaWriter);
FileUtil.close(indexWriter);
if (!hasDataInIndexFile) {
FileUtil.delete(indexFile);
}
}
}
Aggregations