Search in sources :

Example 1 with ExportDataFinishOneTableEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent in project cubrid-manager by CUBRID.

the class ExprotToOBSHandler method handle.

public void handle(String tableName) throws IOException, SQLException {
    // FIXME move this logic to core module
    String schemaFile = exportConfig.getSchemaFilePath();
    String indexFile = exportConfig.getIndexFilePath();
    Set<String> tableSet = new HashSet<String>();
    tableSet.addAll(exportConfig.getTableNameList());
    try {
        try {
            if (exportConfig.isExportSchema()) {
                exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(schemaFile));
            }
            if (exportConfig.isExportIndex()) {
                exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(indexFile));
            }
            exportSchemaToOBSFile(dbInfo, exportDataEventHandler, tableSet, schemaFile, indexFile, exportConfig.getFileCharset(), exportConfig.isExportSerialStartValue());
            if (exportConfig.isExportSchema()) {
                exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(schemaFile));
                exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(schemaFile));
            }
            if (exportConfig.isExportIndex()) {
                exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(indexFile));
                exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(indexFile));
            }
            if (exportConfig.isExportData()) {
                //data
                long totalRecord = exportConfig.getTotalCount(tableName);
                if (totalRecord == 0) {
                    return;
                }
                String path = exportConfig.getDataFilePath(tableName);
                BufferedWriter fs = null;
                Connection conn = null;
                try {
                    fs = FileUtil.getBufferedWriter(path, exportConfig.getFileCharset());
                    conn = getConnection();
                    if (exportConfig.isExportData()) {
                        exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(path));
                        String sql = QueryUtil.getSelectSQL(conn, tableName);
                        // [TOOLS-2425]Support shard broker
                        sql = DatabaseInfo.wrapShardQuery(dbInfo, sql);
                        exportLoad(conn, tableName, fs, path, sql);
                        exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(path));
                    }
                } finally {
                    QueryUtil.freeQuery(conn);
                    try {
                        if (fs != null) {
                            fs.close();
                        }
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("create schema index error : ", e);
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) ExportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent) Connection(java.sql.Connection) IOException(java.io.IOException) ExportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent) IOException(java.io.IOException) SQLException(java.sql.SQLException) HashSet(java.util.HashSet) BufferedWriter(java.io.BufferedWriter)

Example 2 with ExportDataFinishOneTableEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent in project cubrid-manager by CUBRID.

the class ExportDataThread method doRun.

protected void doRun() {
    exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(tableName));
    try {
        AbsExportDataHandler handler = ExportHandlerFactory.getExportHandler(dbInfo, exportConfig, exportDataEventHandler);
        handler.handle(tableName);
    } catch (Exception e) {
        isSuccess = false;
        LOGGER.error("", e);
    } catch (OutOfMemoryError error) {
        isSuccess = false;
        error.printStackTrace();
    } finally {
        if (isSuccess) {
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(tableName));
        } else {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
        }
    }
}
Also used : ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) AbsExportDataHandler(com.cubrid.common.ui.cubrid.table.export.handler.AbsExportDataHandler) ExportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent) ExportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent)

Example 3 with ExportDataFinishOneTableEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent in project cubrid-manager by CUBRID.

the class ExportLoadDBHandler method handle.

public void handle(String nullValue) throws IOException, SQLException {
    // FIXME move this logic to core module
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    BufferedWriter fs = null;
    String schemaFile = exportConfig.getDataFilePath(ExportConfig.LOADDB_SCHEMAFILEKEY);
    String indexFile = exportConfig.getDataFilePath(ExportConfig.LOADDB_INDEXFILEKEY);
    String dataTablesName = exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY);
    // Get connection
    try {
        conn = getConnection();
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        if (exportConfig.isExportSchema()) {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(schemaFile));
        }
        if (exportConfig.isExportIndex()) {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(indexFile));
        }
        exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(dataTablesName));
        QueryUtil.freeQuery(conn);
        throw e;
    }
    // Export schema
    boolean isExportSchemaSuccess = true;
    try {
        if (exportConfig.isExportSchema()) {
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(schemaFile));
        }
        if (exportConfig.isExportIndex()) {
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(indexFile));
        }
        Set<String> tableSet = new HashSet<String>();
        tableSet.addAll(exportConfig.getTableNameList());
        exportSchemaToOBSFile(dbInfo, exportDataEventHandler, tableSet, schemaFile, indexFile, exportConfig.getFileCharset(), exportConfig.isExportSerialStartValue());
        if (exportConfig.isExportSchema()) {
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(schemaFile));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(schemaFile));
        }
        if (exportConfig.isExportIndex()) {
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(indexFile));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(indexFile));
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        isExportSchemaSuccess = false;
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        isExportSchemaSuccess = false;
    } finally {
        if (!isExportSchemaSuccess) {
            if (exportConfig.isExportSchema()) {
                exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(schemaFile));
            }
            if (exportConfig.isExportIndex()) {
                exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(indexFile));
            }
        }
    }
    // Export data
    try {
        exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(dataTablesName));
        fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY), exportConfig.getFileCharset());
        for (String tableName : exportConfig.getTableNameList()) {
            String whereCondition = exportConfig.getWhereCondition(tableName);
            long totalRecord = exportConfig.getTotalCount(tableName);
            if (totalRecord == 0) {
                continue;
            }
            boolean hasNextPage = true;
            int exportedCount = 0;
            long beginIndex = 1;
            String sql = QueryUtil.getSelectSQL(conn, tableName);
            isPaginating = isPagination(tableName, sql, whereCondition);
            boolean isExportedColumnTitles = false;
            while (hasNextPage) {
                try {
                    String executeSQL = null;
                    if (isPaginating) {
                        long endIndex = beginIndex + RSPAGESIZE;
                        executeSQL = getExecuteSQL(sql, beginIndex, endIndex, whereCondition);
                        executeSQL = dbInfo.wrapShardQuery(executeSQL);
                        beginIndex = endIndex + 1;
                    } else {
                        executeSQL = getExecuteSQL(sql, whereCondition);
                        executeSQL = dbInfo.wrapShardQuery(sql);
                        beginIndex = totalRecord + 1;
                    }
                    pStmt = getStatement(conn, executeSQL, tableName);
                    rs = (CUBRIDResultSetProxy) pStmt.executeQuery();
                    CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
                    if (!isExportedColumnTitles) {
                        StringBuilder header = new StringBuilder("%class \"");
                        header.append(tableName);
                        header.append("\" (");
                        for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
                            if (i > 1) {
                                header.append(" ");
                            }
                            header.append("\"");
                            header.append(rsmt.getColumnName(i));
                            header.append("\"");
                        }
                        header.append(")\n");
                        fs.write(header.toString());
                        isExportedColumnTitles = true;
                    }
                    while (rs.next()) {
                        StringBuffer values = new StringBuffer();
                        for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
                            String columnType = rsmt.getColumnTypeName(j);
                            int precision = rsmt.getPrecision(j);
                            columnType = FieldHandlerUtils.amendDataTypeByResult(rs, j, columnType);
                            setIsHasBigValue(columnType, precision);
                            values.append(FieldHandlerUtils.getRsValueForExportOBS(columnType, rs, j).toString());
                        }
                        values.append("\n");
                        fs.write(values.toString());
                        exportedCount++;
                        if (exportedCount >= COMMIT_LINES) {
                            fs.flush();
                            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
                            exportedCount = 0;
                        }
                        if (stop) {
                            break;
                        }
                    }
                    exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
                    exportedCount = 0;
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                    exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
                } finally {
                    QueryUtil.freeQuery(rs);
                }
                if (hasNextPage(beginIndex, totalRecord)) {
                    hasNextPage = true;
                    fs.write(StringUtil.NEWLINE);
                } else {
                    hasNextPage = false;
                }
                System.gc();
            }
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw e;
    } finally {
        try {
            if (fs != null) {
                fs.flush();
                fs.close();
                fs = null;
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) SQLException(java.sql.SQLException) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) ExportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent) Connection(java.sql.Connection) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) CUBRIDResultSetMetaDataProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy) ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) ExportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent) HashSet(java.util.HashSet)

Example 4 with ExportDataFinishOneTableEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent in project cubrid-manager by CUBRID.

the class ExportLoadDBThread method doRun.

protected void doRun() {
    exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY)));
    try {
        AbsExportDataHandler handler = ExportHandlerFactory.getExportHandler(dbInfo, exportConfig, exportDataEventHandler);
        handler.handle(null);
    } catch (Exception e) {
        isSuccess = false;
        LOGGER.error("", e);
    } catch (OutOfMemoryError error) {
        isSuccess = false;
        error.printStackTrace();
    } finally {
        if (isSuccess) {
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY)));
        } else {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY)));
        }
    }
}
Also used : ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) AbsExportDataHandler(com.cubrid.common.ui.cubrid.table.export.handler.AbsExportDataHandler) ExportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent) ExportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent)

Example 5 with ExportDataFinishOneTableEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent in project cubrid-manager by CUBRID.

the class ExportSchemaThread method doRun.

protected void doRun() {
    // FIXME move this logic to core module
    File dirFile = null;
    try {
        dirFile = new File(exportConfig.getDataFileFolder() + File.separator + "ddl");
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
    } catch (Exception e) {
        LOGGER.error("create schema dir error : ", e);
        return;
    }
    try {
        String schemaFile = null;
        String indexFile = null;
        if (exportConfig.isExportSchema()) {
            schemaFile = dirFile + File.separator + "schema.sql";
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(ExportConfig.TASK_NAME_SCHEMA));
        }
        if (exportConfig.isExportIndex()) {
            indexFile = dirFile + File.separator + "index.sql";
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(ExportConfig.TASK_NAME_INDEX));
        }
        Set<String> tableSet = new HashSet<String>();
        tableSet.addAll(exportConfig.getTableNameList());
        ExprotToOBSHandler.exportSchemaToOBSFile(dbInfo, exportDataEventHandler, tableSet, schemaFile, indexFile, exportConfig.getFileCharset(), exportConfig.isExportSerialStartValue());
        if (exportConfig.isExportSchema()) {
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(ExportConfig.TASK_NAME_SCHEMA));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(ExportConfig.TASK_NAME_SCHEMA));
        }
        if (exportConfig.isExportIndex()) {
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(ExportConfig.TASK_NAME_INDEX));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(ExportConfig.TASK_NAME_INDEX));
        }
    } catch (Exception e) {
        if (exportConfig.isExportSchema()) {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(ExportConfig.TASK_NAME_SCHEMA));
        }
        if (exportConfig.isExportIndex()) {
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(ExportConfig.TASK_NAME_INDEX));
        }
        LOGGER.error("create schema index error : ", e);
    }
    try {
        if (exportConfig.isExportSerial()) {
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(ExportConfig.TASK_NAME_SERIAL));
            String serialFile = dirFile + File.separator + "serial.sql";
            exportSerial(serialFile);
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(ExportConfig.TASK_NAME_SERIAL));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(ExportConfig.TASK_NAME_SERIAL));
        }
    } catch (Exception e) {
        exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(ExportConfig.TASK_NAME_SERIAL));
        LOGGER.error("create serial.sql error : ", e);
    }
    try {
        if (exportConfig.isExportView()) {
            exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(ExportConfig.TASK_NAME_VIEW));
            String viewFile = dirFile + File.separator + "view.sql";
            exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(ExportConfig.TASK_NAME_VIEW));
            exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(ExportConfig.TASK_NAME_VIEW));
            exportView(viewFile);
        }
    } catch (Exception e) {
        exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(ExportConfig.TASK_NAME_VIEW));
        LOGGER.error("create view.sql error : ", e);
    }
}
Also used : ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) ExportDataFinishOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent) File(java.io.File) IOException(java.io.IOException) ExportDataBeginOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent) HashSet(java.util.HashSet)

Aggregations

ExportDataBeginOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent)5 ExportDataFailedOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent)5 ExportDataFinishOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent)5 ExportDataSuccessEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 AbsExportDataHandler (com.cubrid.common.ui.cubrid.table.export.handler.AbsExportDataHandler)2 BufferedWriter (java.io.BufferedWriter)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 CUBRIDPreparedStatementProxy (com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)1 CUBRIDResultSetMetaDataProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy)1 CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)1 File (java.io.File)1