Search in sources :

Example 11 with SerialInfo

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

the class GetAutoIncrementTask method execute.

/**
	 * 
	 * Send request to Server
	 * 
	 */
public void execute() {
    serialInfoList = new ArrayList<SerialInfo>();
    try {
        if (errorMsg != null && errorMsg.trim().length() > 0) {
            return;
        }
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return;
        }
        //databaseInfo.getServerInfo().compareVersionKey("8.2.2") >= 0;			
        boolean isSupportCache = CompatibleUtil.isSupportCache(databaseInfo);
        String sql = "SELECT owner.name, db_serial.* FROM db_serial WHERE class_name=?";
        // [TOOLS-2425]Support shard broker
        sql = databaseInfo.wrapShardQuery(sql);
        stmt = connection.prepareStatement(sql);
        ((PreparedStatement) stmt).setString(1, tableName);
        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 = "1".equals(cyclic) ? true : false;
            SerialInfo serialInfo = new SerialInfo(name, owner, currentVal, incrementVal, maxVal, minVal, isCycle, startVal, cacheCount, className, attName);
            serialInfoList.add(serialInfo);
        }
    } catch (SQLException e) {
        errorMsg = e.getMessage();
    } finally {
        finish();
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SerialInfo(com.cubrid.common.core.common.model.SerialInfo)

Example 12 with SerialInfo

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

the class SerialDashboardEditorPart method editSerial.

public void editSerial() {
    TableItem[] items = serialsDetailInfoTable.getTable().getSelection();
    if (items.length != 0) {
        TableItem item = items[0];
        SerialInfo serialInfo = (SerialInfo) item.getData();
        openEditSerialDialog(serialInfo);
    } else {
        CommonUITool.openWarningBox(Messages.errSerialNoSelection);
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) SerialInfo(com.cubrid.common.core.common.model.SerialInfo)

Example 13 with SerialInfo

use of com.cubrid.common.core.common.model.SerialInfo 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();
}
Also used : Table(org.eclipse.swt.widgets.Table) Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) List(java.util.List) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 14 with SerialInfo

use of com.cubrid.common.core.common.model.SerialInfo 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;
}
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 15 with SerialInfo

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

the class ExportSchemaThread method exportSerial.

/**
	 * exportSerial
	 *
	 * @param serialFilePath
	 * @throws Exception
	 */
private void exportSerial(String serialFilePath) throws Exception {
    // FIXME move this logic to core module
    BufferedWriter fs = null;
    boolean hasSerial = false;
    File serialFile = null;
    try {
        fs = FileUtil.getBufferedWriter(serialFilePath, exportConfig.getFileCharset());
        if (exportConfig.isExportSerial()) {
            GetSerialInfoListTask task = new GetSerialInfoListTask(dbInfo);
            task.execute();
            for (SerialInfo serial : task.getSerialInfoList()) {
                fs.write(createSerialSQLScript(serial, dbInfo));
                fs.write(StringUtil.NEWLINE);
                hasSerial = true;
            }
            fs.flush();
        }
    } finally {
        try {
            if (fs != null) {
                fs.close();
            }
        } catch (IOException e) {
            LOGGER.error("", e);
        }
        if (!hasSerial) {
            if (serialFile != null) {
                serialFile.delete();
            }
        }
    }
}
Also used : GetSerialInfoListTask(com.cubrid.cubridmanager.core.cubrid.serial.task.GetSerialInfoListTask) IOException(java.io.IOException) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

SerialInfo (com.cubrid.common.core.common.model.SerialInfo)34 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)14 Constraint (com.cubrid.common.core.common.model.Constraint)12 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)9 ArrayList (java.util.ArrayList)8 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 TableItem (org.eclipse.swt.widgets.TableItem)4 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)3 CubridNodeChangedEvent (com.cubrid.common.ui.spi.event.CubridNodeChangedEvent)3 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)3 CreateOrEditSerialDialog (com.cubrid.common.ui.cubrid.serial.dialog.CreateOrEditSerialDialog)2 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)2 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)2 GetSerialInfoListTask (com.cubrid.cubridmanager.core.cubrid.serial.task.GetSerialInfoListTask)2 BufferedWriter (java.io.BufferedWriter)2 Connection (java.sql.Connection)2 List (java.util.List)2 Table (org.eclipse.swt.widgets.Table)2 DBResolution (com.cubrid.common.core.common.model.DBResolution)1