Search in sources :

Example 1 with ExportDataFailedOneTableEvent

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

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

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

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

the class ExportToTxtHandler method exportByQuerying.

public void exportByQuerying(String tableName) throws IOException, SQLException {
    BufferedWriter fs = null;
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    boolean hasNextPage = true;
    long totalRecord = exportConfig.getTotalCount(tableName);
    long beginIndex = 1;
    int exportedCount = 0;
    String whereCondition = exportConfig.getWhereCondition(tableName);
    boolean isExportedColumnTitles = false;
    List<String> columnTitles = new ArrayList<String>();
    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();
                int colCount = rsmt.getColumnCount();
                // Init title
                if (!isExportedColumnTitles) {
                    for (int j = 1; j < colCount; j++) {
                        String columnName = rsmt.getColumnName(j);
                        String columnType = rsmt.getColumnTypeName(j);
                        int precision = rsmt.getPrecision(j);
                        columnType = columnType == null ? "" : columnType;
                        setIsHasBigValue(columnType, precision);
                        columnTitles.add(columnName);
                    }
                    isExportedColumnTitles = true;
                    if (exportConfig.isFirstRowAsColumnName()) {
                        for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
                            fs.write(surround + rsmt.getColumnName(j) + surround);
                            if (j != rsmt.getColumnCount()) {
                                fs.write(columnSeprator);
                            }
                        }
                        fs.write(rowSeprator);
                        fs.flush();
                    }
                }
                while (rs.next()) {
                    writeNextLine(tableName, fs, rs, rsmt, columnSeprator, rowSeprator, surround);
                    fs.write(rowSeprator);
                    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);
                exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
            } finally {
                QueryUtil.freeQuery(pStmt, rs);
            }
            if (hasNextPage(beginIndex, totalRecord)) {
                hasNextPage = true;
                fs.write(rowSeprator);
            } else {
                hasNextPage = false;
            }
            System.gc();
        }
    } finally {
        QueryUtil.freeQuery(conn);
        Closer.close(fs);
    }
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) 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)

Example 5 with ExportDataFailedOneTableEvent

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

the class ExportToTxtHandler method exportFromCache.

public void exportFromCache(String tableName) throws IOException {
    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();
            for (int j = 0; j < colCount; j++) {
                fs.write(surround + columnInfos.get(j).getName() + surround);
                if (j != colCount - 1) {
                    fs.write(columnSeprator);
                }
            }
            fs.write(rowSeprator);
            fs.flush();
            List<ArrayList<Object>> datas = resultSetDataCache.getDatas();
            for (ArrayList<Object> rowData : datas) {
                writeNextLine(tableName, fs, columnInfos, rowData, columnSeprator, rowSeprator, surround);
                fs.write(rowSeprator);
                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);
            exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
        }
        System.gc();
    } finally {
        Closer.close(fs);
    }
}
Also used : ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) ArrayList(java.util.ArrayList) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) ResultSetDataCache(com.cubrid.common.ui.cubrid.table.export.ResultSetDataCache) SQLException(java.sql.SQLException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

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