Search in sources :

Example 1 with SchemaChangeManager

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager 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);
        }
    }
}
Also used : SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) Connection(java.sql.Connection) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) BufferedWriter(java.io.BufferedWriter) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 2 with SchemaChangeManager

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager in project cubrid-manager by CUBRID.

the class TableSchemaCompareComposite method getTableAlterScript.

/**
	 * Returns table schema update/alter script
	 *
	 * @param sourceDatabase
	 * @param targetDatabase
	 * @param tableCompare
	 */
public String getTableAlterScript(CubridDatabase sourceDatabase, CubridDatabase targetDatabase, String tableCompare, SchemaInfo sourceSchemaInfo, SchemaInfo targetSchemaInfo) {
    // FIXME logic code move to core module
    String alterDDL = null;
    try {
        DatabaseInfo source_dbInfo = sourceDatabase.getDatabaseInfo();
        DatabaseInfo target_dbInfo = targetDatabase.getDatabaseInfo();
        if (sourceSchemaInfo == null || !"normal".equals(sourceSchemaInfo.getVirtual()))
            sourceSchemaInfo = null;
        if (targetSchemaInfo == null || !"normal".equals(targetSchemaInfo.getVirtual()))
            targetSchemaInfo = null;
        SchemaChangeManager sourceChangeManger = new SchemaChangeManager(source_dbInfo, false);
        SchemaDDL sourceSchemaDDL = new SchemaDDL(sourceChangeManger, sourceDatabase.getDatabaseInfo());
        SchemaChangeManager targetChangeManger = new SchemaChangeManager(target_dbInfo, false);
        SchemaDDL targetSchemaDDL = null;
        if (targetDatabase.isVirtual()) {
            WrappedDatabaseInfo info = ERXmlDatabaseInfoMapper.getWrappedDatabaseInfo(target_dbInfo);
            targetSchemaDDL = new SchemaDDL(targetChangeManger, info);
        } else {
            targetSchemaDDL = new SchemaDDL(targetChangeManger, targetDatabase.getDatabaseInfo());
        }
        TableSchemaCompareUpdateDDL tableCompareDDL = new TableSchemaCompareUpdateDDL(sourceChangeManger, sourceSchemaDDL, targetSchemaDDL, sourceSchemaInfo, targetSchemaInfo);
        alterDDL = tableCompareDDL.getTableSchemaAlterDDL();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return alterDDL;
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) TableSchemaCompareUpdateDDL(com.cubrid.common.ui.compare.schema.model.TableSchemaCompareUpdateDDL)

Example 3 with SchemaChangeManager

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager in project cubrid-manager by CUBRID.

the class TableEditorPart method init.

public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    TableEditorInput tableInfoEditorInput = (TableEditorInput) input;
    this.database = tableInfoEditorInput.getDatabase();
    this.editedTableNode = tableInfoEditorInput.getEditedTableNode();
    this.isNewTableFlag = tableInfoEditorInput.isNewTableFlag();
    this.dbUserList = tableInfoEditorInput.getDbUserList();
    this.showDefaultType = tableInfoEditorInput.getType();
    this.collationList = tableInfoEditorInput.getCollationList();
    if (collationList != null) {
        Collation emptyCollation = new Collation();
        emptyCollation.setCharset("");
        emptyCollation.setName("");
        collationList.add(0, emptyCollation);
    }
    this.oldSchemaInfo = tableInfoEditorInput.getSchemaInfo();
    this.supportCharset = CompatibleUtil.isSupportCreateDBByCharset(database.getDatabaseInfo());
    if (isNewTableFlag) {
        newSchemaInfo = new SchemaInfo();
        //$NON-NLS-1$
        newSchemaInfo.setClassname("");
        newSchemaInfo.setOwner(database.getUserName());
        newSchemaInfo.setDbname(database.getName());
        newSchemaInfo.setType(Messages.userSchema);
        newSchemaInfo.setVirtual(Messages.schemaTypeClass);
        if (database.getDatabaseInfo() != null) {
            newSchemaInfo.setCollation(database.getDatabaseInfo().getCollation());
        }
    } else {
        newSchemaInfo = null;
        if (tableInfoEditorInput.getSchemaInfo() != null) {
            newSchemaInfo = tableInfoEditorInput.getSchemaInfo().clone();
            originalConstraints.addAll(newSchemaInfo.getConstraints());
        }
    }
    if (supportCharset) {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_COLLATION, IAttributeColumn.COL_MEMO };
    } else {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_MEMO };
    }
    Connection conn = null;
    try {
        conn = JDBCConnectionManager.getConnection(database.getDatabaseInfo(), false);
        IDatabaseSpec dbSpec = database.getDatabaseInfo();
        isSupportTableComment = SchemaCommentHandler.isInstalledMetaTable(dbSpec, conn);
        database.getDatabaseInfo().setSupportTableComment(isSupportTableComment);
        if (isSupportTableComment && !isNewTableFlag && newSchemaInfo != null) {
            Map<String, SchemaComment> map = SchemaCommentHandler.loadDescription(dbSpec, conn, newSchemaInfo.getClassname());
            for (DBAttribute attr : newSchemaInfo.getAttributes()) {
                SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), attr.getName());
                if (schemaComment != null) {
                    attr.setDescription(schemaComment.getDescription());
                }
            }
            // get description for index
            for (Constraint cons : newSchemaInfo.getConstraints()) {
                if (CompatibleUtil.isCommentSupports(dbSpec)) {
                    String indexName = cons.getName();
                    SchemaComment indexComment = SchemaCommentHandler.loadObjectDescription(dbSpec, conn, indexName, CommentType.INDEX);
                    if (indexComment != null) {
                        cons.setDescription(indexComment.getDescription());
                    }
                }
            }
            SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), null);
            if (schemaComment != null) {
                newSchemaInfo.setDescription(schemaComment.getDescription());
            }
        }
    } catch (SQLException e) {
        LOGGER.error("", e);
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn);
    }
    schemaChangeMgr = new SchemaChangeManager(database.getDatabaseInfo(), isNewTableFlag);
    schemaDDL = new SchemaDDL(schemaChangeMgr, database.getDatabaseInfo());
    if (database != null) {
        isSupportChange = CompatibleUtil.isSupportChangeColumn(database.getDatabaseInfo());
    }
    setSite(site);
    setInput(input);
    setPartName(input.getName());
    setTitleToolTip(input.getName());
    setTitleImage(CommonUIPlugin.getImage("icons/navigator/schema_table.png"));
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SQLException(java.sql.SQLException) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) Connection(java.sql.Connection) Collation(com.cubrid.cubridmanager.core.cubrid.database.model.Collation) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) IDatabaseSpec(com.cubrid.common.core.common.model.IDatabaseSpec) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 4 with SchemaChangeManager

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager in project cubrid-manager by CUBRID.

the class ExportSQLDataTask method execute.

public void execute() {
    DatabaseInfo databaseInfo = erSchema.getCubridDatabase().getDatabaseInfo();
    SchemaDDL schemaDDL = new SchemaDDL(new SchemaChangeManager(databaseInfo, true), databaseInfo);
    Map<String, SchemaInfo> tables = erSchema.getAllSchemaInfo();
    StringBuilder text = new StringBuilder("");
    for (SchemaInfo table : tables.values()) {
        String sql = schemaDDL.getSchemaDDL(table, isContainIndex);
        text.append(sql);
        text.append("\n");
    }
    isSuccess = FileUtil.writeToFile(fileFullName, text.toString(), fileCharset, false);
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

SchemaChangeManager (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager)4 SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)4 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)3 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)2 Connection (java.sql.Connection)2 Constraint (com.cubrid.common.core.common.model.Constraint)1 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1 IDatabaseSpec (com.cubrid.common.core.common.model.IDatabaseSpec)1 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)1 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)1 TableSchemaCompareUpdateDDL (com.cubrid.common.ui.compare.schema.model.TableSchemaCompareUpdateDDL)1 WrappedDatabaseInfo (com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo)1 Collation (com.cubrid.cubridmanager.core.cubrid.database.model.Collation)1 BufferedWriter (java.io.BufferedWriter)1 SQLException (java.sql.SQLException)1 PartInitException (org.eclipse.ui.PartInitException)1