Search in sources :

Example 1 with ExportDataSuccessEvent

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

the class ExprotToOBSHandler method exportLoad.

/**
	 * Export data as CUBRID load format
	 *
	 * @param stmt Statement
	 * @param tableName String
	 * @param monitor IProgressMonitor
	 * @param fs String
	 * @param fileName BufferedWriter
	 * @throws SQLException The exception
	 * @throws IOException The exception
	 */
private void exportLoad(Connection conn, String tableName, BufferedWriter fs, String fileName, String sql) throws SQLException, IOException {
    // FIXME move this logic to core module
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    boolean hasNextPage = true;
    long beginIndex = 1;
    int exportedCount = 0;
    long totalRecord = exportConfig.getTotalCount(tableName);
    String whereCondition = exportConfig.getWhereCondition(tableName);
    isPaginating = isPagination(tableName, sql, whereCondition);
    boolean isNeedWriteHeader = true;
    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 (isNeedWriteHeader) {
                StringBuffer header = new StringBuffer("%class ");
                header.append(QuerySyntax.escapeKeyword(tableName));
                header.append(" (");
                for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
                    if (i > 1) {
                        header.append(" ");
                    }
                    header.append(QuerySyntax.escapeKeyword(rsmt.getColumnName(i)));
                }
                header.append(")\n");
                fs.write(header.toString());
                isNeedWriteHeader = false;
            }
            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 (Exception e) {
            LOGGER.error("export date write load db error : ", e);
        } finally {
            fs.flush();
            QueryUtil.freeQuery(pStmt, rs);
        }
        if (hasNextPage(beginIndex, totalRecord)) {
            hasNextPage = true;
        } else {
            hasNextPage = false;
        }
        System.gc();
    }
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) CUBRIDResultSetMetaDataProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 2 with ExportDataSuccessEvent

use of com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent 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 3 with ExportDataSuccessEvent

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

the class ExprotToSqlHandler method exportByQuerying.

public void exportByQuerying(String tableName) throws IOException, SQLException {
    if (StringUtil.isEmpty(tableName)) {
        return;
    }
    long totalRecord = exportConfig.getTotalCount(tableName);
    if (totalRecord == 0) {
        return;
    }
    BufferedWriter fs = null;
    String whereCondition = exportConfig.getWhereCondition(tableName);
    boolean hasNextPage = true;
    long beginIndex = 1;
    int exportedCount = 0;
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    try {
        conn = getConnection();
        fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(tableName), exportConfig.getFileCharset());
        String sql = QueryUtil.getSelectSQL(conn, tableName);
        isPaginating = isPagination(tableName, sql, whereCondition);
        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();
                StringBuffer insert = new StringBuffer("INSERT INTO ");
                insert.append(QuerySyntax.escapeKeyword(tableName));
                insert.append(" (");
                for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
                    if (i > 1) {
                        insert.append(", ");
                    }
                    insert.append(QuerySyntax.escapeKeyword(rsmt.getColumnName(i)));
                }
                insert.append(") ");
                while (rs.next()) {
                    StringBuffer values = new StringBuffer("VALUES (");
                    for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
                        if (j > 1) {
                            values.append(", ");
                        }
                        String columnType = rsmt.getColumnTypeName(j);
                        int precision = rsmt.getPrecision(j);
                        columnType = FieldHandlerUtils.amendDataTypeByResult(rs, j, columnType);
                        setIsHasBigValue(columnType, precision);
                        values.append(FieldHandlerUtils.getRsValueForExportSQL(columnType, rs, j).toString());
                    }
                    values.append(");\n");
                    fs.write(insert.toString());
                    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 (Exception e) {
                LOGGER.error(e.getMessage(), e);
                exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
            } finally {
                QueryUtil.freeQuery(pStmt, rs);
            }
            if (hasNextPage(beginIndex, totalRecord)) {
                hasNextPage = true;
            } else {
                hasNextPage = false;
            }
            System.gc();
        }
    } finally {
        QueryUtil.freeQuery(conn);
        FileUtil.close(fs);
    }
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) Connection(java.sql.Connection) IOException(java.io.IOException) SQLException(java.sql.SQLException) 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)

Example 4 with ExportDataSuccessEvent

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

the class ExprotToSqlHandler method exportFromCache.

public void exportFromCache(String tableName) throws IOException {
    if (StringUtil.isEmpty(tableName)) {
        return;
    }
    BufferedWriter fs = null;
    int exportedCount = 0;
    ResultSetDataCache resultSetDataCache = exportConfig.getResultSetDataCache();
    try {
        fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(tableName), exportConfig.getFileCharset());
        try {
            List<ColumnInfo> columnInfos = resultSetDataCache.getColumnInfos();
            int colCount = columnInfos.size();
            StringBuffer insert = new StringBuffer("INSERT INTO ");
            insert.append(QuerySyntax.escapeKeyword(tableName));
            insert.append(" (");
            for (int i = 0; i < colCount; i++) {
                if (i > 0) {
                    insert.append(", ");
                }
                insert.append(QuerySyntax.escapeKeyword(columnInfos.get(i).getName()));
            }
            insert.append(") ");
            List<ArrayList<Object>> datas = resultSetDataCache.getDatas();
            for (ArrayList<Object> rowData : datas) {
                StringBuffer values = new StringBuffer("VALUES (");
                for (int j = 0; j < colCount; j++) {
                    if (j > 0) {
                        values.append(", ");
                    }
                    int precision = columnInfos.get(j).getPrecision();
                    String columnType = columnInfos.get(j).getType();
                    setIsHasBigValue(columnType, precision);
                    Object value = rowData.get(j);
                    if (DataType.DATATYPE_BLOB.equals(columnType) || DataType.DATATYPE_CLOB.equals(columnType)) {
                        value = DataType.VALUE_NULL;
                    }
                    values.append(value.toString());
                }
                values.append(");\n");
                fs.write(insert.toString());
                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 (Exception e) {
            LOGGER.error(e.getMessage(), e);
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
        }
        System.gc();
    } finally {
        FileUtil.close(fs);
    }
}
Also used : ArrayList(java.util.ArrayList) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) IOException(java.io.IOException) SQLException(java.sql.SQLException) BufferedWriter(java.io.BufferedWriter) ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) ResultSetDataCache(com.cubrid.common.ui.cubrid.table.export.ResultSetDataCache)

Example 5 with ExportDataSuccessEvent

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

the class TableContentProvider method updateTableData.

private void updateTableData(ExportDataEvent evt) {
    if (stop) {
        return;
    }
    String tableName = "";
    int successCount = 1;
    if (evt instanceof ExportDataSuccessEvent) {
        ExportDataSuccessEvent event = (ExportDataSuccessEvent) evt;
        tableName = event.getTableName();
        successCount = event.getSuccessCount();
    }
    for (ExportMonitor po : tableList) {
        if (po.getTableName().equals(tableName)) {
            long oldParseCount = po.getParseCount();
            po.setElapsedTime(evt.getEventTime() - po.getBeginTime());
            po.setParseCount(oldParseCount + successCount);
            po.setStatus(ExportMonitor.STATUS_RUNNING);
            tvProgress.refresh(tableList);
            break;
        }
    }
}
Also used : ExportMonitor(com.cubrid.common.ui.cubrid.table.dialog.exp.ExportMonitor) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)

Aggregations

ExportDataSuccessEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)14 IOException (java.io.IOException)12 ExportDataFailedOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 CUBRIDPreparedStatementProxy (com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)6 CUBRIDResultSetMetaDataProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy)6 CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)6 BufferedWriter (java.io.BufferedWriter)6 Connection (java.sql.Connection)6 ResultSetDataCache (com.cubrid.common.ui.cubrid.table.export.ResultSetDataCache)4 ColumnInfo (com.cubrid.common.ui.query.control.ColumnInfo)4 ExportDataBeginOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent)3 ExportDataFinishOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent)3 File (java.io.File)3 HashSet (java.util.HashSet)3 XlsxWriterHelper (com.cubrid.common.ui.cubrid.table.control.XlsxWriterHelper)2 ExportMonitor (com.cubrid.common.ui.cubrid.table.dialog.exp.ExportMonitor)2 Timestamp (java.sql.Timestamp)2 Calendar (java.util.Calendar)2