Search in sources :

Example 6 with CUBRIDResultSetProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.

the class ExportToXlsHandler method exportByQuerying.

public void exportByQuerying(String tableName) throws IOException, SQLException {
    if (StringUtil.isEmpty(tableName)) {
        // FIXME move this logic to core module
        return;
    }
    long totalRecord = exportConfig.getTotalCount(tableName);
    if (totalRecord == 0) {
        return;
    }
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    WritableWorkbook workbook = null;
    int workbookNum = 0;
    String whereCondition = exportConfig.getWhereCondition(tableName);
    boolean hasNextPage = true;
    long beginIndex = 1;
    int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
    // 65536: limit xls row number.
    int rowLimit = ImportFileConstants.XLS_ROW_LIMIT;
    boolean isInitedColumnTitles = false;
    List<String> columnTitles = new ArrayList<String>();
    try {
        conn = getConnection();
        int sheetNum = 0;
        int xlsRecordNum = 0;
        workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
        WritableSheet sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
        sheetNum++;
        int exportedCount = 0;
        String sql = QueryUtil.getSelectSQL(conn, tableName);
        isPaginating = isPagination(whereCondition, 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();
                if (!isInitedColumnTitles) {
                    isInitedColumnTitles = true;
                    columnTitles = getColumnTitleList(rsmt);
                    if (isExit) {
                        return;
                    }
                    // first line add column name
                    if (exportConfig.isFirstRowAsColumnName()) {
                        writeHeader(sheet, columnTitles);
                        xlsRecordNum++;
                    }
                }
                while (rs.next()) {
                    //Check memory
                    if (!CommonUITool.isAvailableMemory(REMAINING_MEMORY_SIZE)) {
                        closeWorkbook(workbook);
                        workbook = null;
                        System.gc();
                        workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
                        sheetNum = 0;
                        sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
                        sheetNum++;
                        xlsRecordNum = 0;
                        // first line add column name
                        if (exportConfig.isFirstRowAsColumnName()) {
                            writeHeader(sheet, columnTitles);
                            xlsRecordNum++;
                        }
                    }
                    for (int j = 1; j <= colCount; j++) {
                        String colType = rsmt.getColumnTypeName(j);
                        colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, colType);
                        int precision = rsmt.getPrecision(j);
                        setIsHasBigValue(colType, precision);
                        Object cellValue = FieldHandlerUtils.getRsValueForExport(colType, rs, j, exportConfig.getNULLValueTranslation());
                        // We need judge the CLOB/BLOD data by column type
                        if (DataType.DATATYPE_BLOB.equals(colType) || DataType.DATATYPE_CLOB.equals(colType)) {
                            if (DataType.DATATYPE_BLOB.equals(colType)) {
                                String fileName = exportBlobData(tableName, rs, j);
                                String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                                if (StringUtil.isNotEmpty(fileName)) {
                                    dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + BLOB_FOLDER_POSTFIX + File.separator + fileName;
                                }
                                sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                            } else {
                                String fileName = exportClobData(tableName, rs, j);
                                String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                                if (StringUtil.isNotEmpty(fileName)) {
                                    dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + CLOB_FOLDER_POSTFIX + File.separator + fileName;
                                }
                                sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                            }
                        } else if (cellValue instanceof Long) {
                            sheet.addCell(new Number(j - 1, xlsRecordNum, (Long) cellValue));
                        } else if (cellValue instanceof Double) {
                            sheet.addCell(new Number(j - 1, xlsRecordNum, (Double) cellValue));
                        } else if (cellValue instanceof Timestamp) {
                            String dataCellValue = FieldHandlerUtils.formatDateTime((Timestamp) cellValue);
                            sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                        } else if (cellValue instanceof java.sql.Time) {
                            String dataCellValue = DateUtil.getDatetimeString(((java.sql.Time) cellValue).getTime(), FieldHandlerUtils.FORMAT_TIME);
                            sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                        } else if (cellValue instanceof java.sql.Date) {
                            String dataCellValue = DateUtil.getDatetimeString(((java.sql.Date) cellValue).getTime(), FieldHandlerUtils.FORMAT_DATE);
                            sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
                        } else {
                            sheet.addCell(new Label(j - 1, xlsRecordNum, cellValue.toString().length() > cellCharacterLimit ? cellValue.toString().substring(0, cellCharacterLimit) : cellValue.toString()));
                        }
                    }
                    xlsRecordNum++;
                    if ((xlsRecordNum + 1) % rowLimit == 0) {
                        xlsRecordNum = 0;
                        sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
                        sheetNum++;
                        // first line add column name
                        if (exportConfig.isFirstRowAsColumnName()) {
                            writeHeader(sheet, columnTitles);
                            xlsRecordNum++;
                        }
                    }
                    exportedCount++;
                    if (exportedCount >= COMMIT_LINES) {
                        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;
            } else {
                hasNextPage = false;
            }
            System.gc();
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn);
        closeWorkbook(workbook);
    }
}
Also used : CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) ArrayList(java.util.ArrayList) Label(jxl.write.Label) Timestamp(java.sql.Timestamp) CUBRIDResultSetMetaDataProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy) ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) Number(jxl.write.Number) CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) Connection(java.sql.Connection) WritableSheet(jxl.write.WritableSheet) WriteException(jxl.write.WriteException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RowsExceededException(jxl.write.biff.RowsExceededException) WritableWorkbook(jxl.write.WritableWorkbook) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)

Example 7 with CUBRIDResultSetProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.

the class ExportSchemaThread method getAllViewsDDL.

private List<String> getAllViewsDDL() {
    // FIXME move this logic to core module
    List<String> resultList = new ArrayList<String>();
    List<String> viewNameList = new ArrayList<String>();
    LinkedHashMap<String, String> viewQuerySpecMap = new LinkedHashMap<String, String>();
    Connection conn = null;
    Statement stmt = null;
    CUBRIDResultSetProxy rs = null;
    String sql = "SELECT c.class_name, c.class_type" + " FROM db_class c, db_attribute a" + " WHERE c.class_name=a.class_name AND c.is_system_class='NO'" + " AND c.class_type='VCLASS'" + " GROUP BY c.class_name, c.class_type" + " ORDER BY c.class_type, c.class_name";
    // [TOOLS-2425]Support shard broker
    sql = dbInfo.wrapShardQuery(sql);
    try {
        conn = JDBCConnectionManager.getConnection(dbInfo, false);
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        rs = (CUBRIDResultSetProxy) stmt.executeQuery(sql);
        while (rs.next()) {
            viewNameList.add(rs.getString(1));
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
    try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        for (String viewName : viewNameList) {
            String querySpecSql = "SELECT vclass_def FROM db_vclass WHERE vclass_name='" + viewName + "'";
            // [TOOLS-2425]Support shard broker
            querySpecSql = dbInfo.wrapShardQuery(querySpecSql);
            try {
                rs = (CUBRIDResultSetProxy) stmt.executeQuery(querySpecSql);
                if (rs.next()) {
                    viewQuerySpecMap.put(viewName, rs.getString(1));
                }
            } finally {
                QueryUtil.freeQuery(rs);
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn, stmt);
    }
    for (Map.Entry<String, String> entry : viewQuerySpecMap.entrySet()) {
        SchemaInfo viewInfo = dbInfo.getSchemaInfo(entry.getKey());
        String ddl = getViewDDL(viewInfo, dbInfo, entry.getValue());
        resultList.add(ddl);
    }
    return resultList;
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 8 with CUBRIDResultSetProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.

the class OIDNavigatorDialog method searchOID.

/**
	 * 
	 * Get tree constructor by the result of execute oid
	 * 
	 * @param strOid the oid string
	 * @param parent the parent treeitem
	 * @return <code>true</code> if found;<code>false</code> otherwise
	 */
private boolean searchOID(String strOid, TreeItem parent) {
    CUBRIDOIDProxy oid = null;
    String tblName = null;
    String[] columnName;
    String[] typeName;
    String[] value;
    String[] oidSet = null;
    boolean[] isOid = null;
    int cntColumn = 0;
    try {
        oid = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) conn, strOid);
        if (oid == null) {
            return false;
        }
        tblName = oid.getTableName();
    } catch (Exception e) {
        CommonUITool.openErrorBox(getShell(), Messages.errOIDValue2);
        return false;
    }
    if (tblName == null) {
        CommonUITool.openErrorBox(getShell(), Messages.errOIDValue2);
        return false;
    }
    parent.setText(strOid);
    TreeItem item = new TreeItem(parent, SWT.NONE);
    item.setText("table name: " + tblName);
    String sql = "SELECT * FROM " + QuerySyntax.escapeKeyword(tblName) + " WHERE ROWNUM = 1";
    stmt = null;
    CUBRIDResultSetProxy rs = null;
    try {
        stmt = conn.createStatement();
        rs = (CUBRIDResultSetProxy) stmt.executeQuery(sql);
        ResultSetMetaData rsmt = rs.getMetaData();
        cntColumn = rsmt.getColumnCount();
        columnName = new String[cntColumn];
        typeName = new String[cntColumn];
        value = new String[cntColumn];
        for (int i = 0; i < cntColumn; i++) {
            columnName[i] = rsmt.getColumnName(i + 1);
            typeName[i] = rsmt.getColumnTypeName(i + 1);
        }
        rs.close();
        rs = (CUBRIDResultSetProxy) oid.getValues(columnName);
        while (rs.next()) {
            for (int i = 0; i < columnName.length; i++) {
                if (rs.getObject(columnName[i]) == null) {
                    value[i] = "NULL";
                } else {
                    if ("SET".equals(typeName[i]) || "MULTISET".equals(typeName[i]) || "SEQUENCE".equals(typeName[i])) {
                        Object[] set = (Object[]) rs.getCollection(columnName[i]);
                        oidSet = new String[set.length];
                        isOid = new boolean[set.length];
                        value[i] = "{";
                        if (set.length > 0) {
                            for (int j = 0; j < set.length; j++) {
                                if (set[j].getClass() == oid.getCUBRIDOIDClass()) {
                                    value[i] += (new CUBRIDOIDProxy(set[j])).getOidString();
                                    oidSet[j] = (new CUBRIDOIDProxy(set[j])).getOidString();
                                    isOid[j] = true;
                                } else {
                                    value[i] += set[j];
                                    oidSet[j] = null;
                                    isOid[j] = false;
                                }
                                if (i < set.length - 1) {
                                    value[i] += ", ";
                                }
                            }
                        }
                        value[i] += "}";
                    } else {
                        value[i] = rs.getString(columnName[i]);
                    }
                }
            }
        }
        rs.close();
        for (int i = 0; i < value.length; i++) {
            if ("CLASS".equals(typeName[i]) && !"NULL".equals(value[i])) {
                item = new TreeItem(parent, SWT.NONE);
                item.setText(columnName[i] + ": " + value[i]);
                TreeItem treeItem = new TreeItem(item, SWT.NONE);
                treeItem.setText(value[i]);
                treeItem.setData(OID_ITEM_FLAG, value[i]);
                TreeItem dumyItem = new TreeItem(treeItem, SWT.NONE);
                dumyItem.setData(DUMY_ITEM_FLAG, DUMY_ITEM_FLAG);
            } else if ("SET".equals(typeName[i]) || "MULTISET".equals(typeName[i]) || "SEQUENCE".equals(typeName[i])) {
                item = new TreeItem(parent, SWT.NONE);
                item.setText(columnName[i] + ": " + value[i]);
                if (isOid != null) {
                    for (int j = 0; j < oidSet.length; j++) {
                        if (isOid[j]) {
                            TreeItem treeItem = new TreeItem(item, SWT.NONE);
                            treeItem.setData(OID_ITEM_FLAG, oidSet[j]);
                            treeItem.setText(oidSet[j]);
                            TreeItem dumyItem = new TreeItem(treeItem, SWT.NONE);
                            dumyItem.setData(DUMY_ITEM_FLAG, DUMY_ITEM_FLAG);
                        }
                    }
                }
            } else {
                (new TreeItem(parent, SWT.NONE)).setText(columnName[i] + ": " + value[i]);
            }
        }
    } catch (SQLException e) {
        CommonUITool.openErrorBox(getShell(), Messages.bind(com.cubrid.common.ui.common.Messages.errCommonTip, e.getErrorCode(), e.getMessage()));
        LOGGER.error("", e);
        return false;
    } finally {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return true;
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) CUBRIDOIDProxy(com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy) TreeItem(org.eclipse.swt.widgets.TreeItem) SQLException(java.sql.SQLException) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData)

Example 9 with CUBRIDResultSetProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy 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 10 with CUBRIDResultSetProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.

the class ExportToXlsxHandler method exportByQuerying.

public void exportByQuerying(String tableName) throws IOException, SQLException {
    if (StringUtil.isEmpty(tableName)) {
        return;
    }
    long totalRecord = exportConfig.getTotalCount(tableName);
    if (totalRecord == 0) {
        return;
    }
    // 1048576: limit xlsx row number.
    int rowLimit = ImportFileConstants.XLSX_ROW_LIMIT;
    // 16384: limit xlsx column number.
    int columnLimit = ImportFileConstants.XLSX_COLUMN_LIMIT;
    int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
    boolean hasNextPage = true;
    long beginIndex = 1;
    String whereCondition = exportConfig.getWhereCondition(tableName);
    XlsxWriterHelper xlsxWriterhelper = new XlsxWriterHelper();
    //create memory workbook
    XSSFWorkbook workbook = new XSSFWorkbook();
    Calendar cal = Calendar.getInstance();
    int datetimeStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("datetime")).getIndex();
    int timestampStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("timestamp")).getIndex();
    int dateStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("date")).getIndex();
    int timeStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("time")).getIndex();
    int sheetNum = 0;
    int xssfRowNum = 0;
    File file = new File(exportConfig.getDataFilePath(tableName));
    Map<String, File> fileMap = new HashMap<String, File>();
    XlsxWriterHelper.SpreadsheetWriter sheetWriter = null;
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    CUBRIDResultSetProxy rs = null;
    boolean isInitedColumnTitle = false;
    List<String> columnTitles = new ArrayList<String>();
    try {
        conn = getConnection();
        String sql = QueryUtil.getSelectSQL(conn, tableName);
        isPaginating = isPagination(whereCondition, sql, whereCondition);
        int exportedCount = 0;
        while (hasNextPage) {
            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();
            if (colCount >= columnLimit && !isConfirmColumnLimit) {
                isConfirmColumnLimit = true;
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        if (!CommonUITool.openConfirmBox(Messages.exportColumnCountOverWarnInfo)) {
                            isExit = true;
                        }
                    }
                });
                if (isExit) {
                    return;
                }
                colCount = columnLimit;
            }
            if (!isInitedColumnTitle) {
                columnTitles = getColumnTitleList(rsmt);
                isInitedColumnTitle = true;
                if (isExit) {
                    return;
                }
                if (sheetWriter != null) {
                    try {
                        XlsxWriterHelper.writeSheetWriter(sheetWriter);
                    } catch (IOException e) {
                        sheetWriter = null;
                        throw e;
                    }
                }
                sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum++, fileMap, columnTitles, xssfRowNum);
                if (exportConfig.isFirstRowAsColumnName()) {
                    xssfRowNum++;
                }
            }
            try {
                while (rs.next()) {
                    sheetWriter.insertRow(xssfRowNum);
                    for (int k = 1; k <= colCount; k++) {
                        String colType = rsmt.getColumnTypeName(k);
                        colType = FieldHandlerUtils.amendDataTypeByResult(rs, k, colType);
                        int precision = rsmt.getPrecision(k);
                        setIsHasBigValue(colType, precision);
                        Object cellValue = FieldHandlerUtils.getRsValueForExport(colType, rs, k, exportConfig.getNULLValueTranslation());
                        // We need judge the CLOB/BLOD data by column type
                        if (DataType.DATATYPE_BLOB.equals(colType) || DataType.DATATYPE_CLOB.equals(colType)) {
                            if (DataType.DATATYPE_BLOB.equals(colType)) {
                                String fileName = exportBlobData(tableName, rs, k);
                                String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                                if (StringUtil.isNotEmpty(fileName)) {
                                    dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + BLOB_FOLDER_POSTFIX + File.separator + fileName;
                                }
                                sheetWriter.createCell(k - 1, dataCellValue);
                            } else {
                                String fileName = exportClobData(tableName, rs, k);
                                String dataCellValue = DataType.NULL_EXPORT_FORMAT;
                                if (StringUtil.isNotEmpty(fileName)) {
                                    dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + CLOB_FOLDER_POSTFIX + File.separator + fileName;
                                }
                                sheetWriter.createCell(k - 1, dataCellValue);
                            }
                        } else if (cellValue instanceof Long) {
                            sheetWriter.createCell(k - 1, ((Long) cellValue).longValue());
                        } else if (cellValue instanceof Double) {
                            sheetWriter.createCell(k - 1, ((Double) cellValue).doubleValue());
                        } else if (cellValue instanceof Date) {
                            cal.setTime((Date) cellValue);
                            if (DataType.DATATYPE_DATETIME.equals(colType)) {
                                sheetWriter.createCell(k - 1, cal, datetimeStyleIndex);
                            } else if (DataType.DATATYPE_DATE.equals(colType)) {
                                sheetWriter.createCell(k - 1, cal, dateStyleIndex);
                            } else if (DataType.DATATYPE_TIME.equals(colType)) {
                                sheetWriter.createCell(k - 1, cal, timeStyleIndex);
                            } else {
                                sheetWriter.createCell(k - 1, cal, timestampStyleIndex);
                            }
                        } else {
                            String cellStr = cellValue.toString().length() > cellCharacterLimit ? cellValue.toString().substring(0, cellCharacterLimit) : cellValue.toString();
                            sheetWriter.createCell(k - 1, Export.covertXMLString(cellStr));
                        }
                    }
                    sheetWriter.endRow();
                    xssfRowNum++;
                    exportedCount++;
                    if ((xssfRowNum + 1) % rowLimit == 0) {
                        xssfRowNum = 0;
                        if (sheetWriter != null) {
                            try {
                                XlsxWriterHelper.writeSheetWriter(sheetWriter);
                            } catch (IOException e) {
                                sheetWriter = null;
                                throw e;
                            }
                        }
                        sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum, fileMap, columnTitles, xssfRowNum);
                        sheetNum++;
                        if (exportConfig.isFirstRowAsColumnName()) {
                            xssfRowNum++;
                        }
                    }
                    if (exportedCount >= COMMIT_LINES) {
                        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;
            } else {
                hasNextPage = false;
            }
            System.gc();
        }
    } finally {
        QueryUtil.freeQuery(conn);
        try {
            if (sheetWriter != null) {
                XlsxWriterHelper.writeSheetWriter(sheetWriter);
            }
        } catch (IOException e) {
            sheetWriter = null;
            throw e;
        } finally {
            XlsxWriterHelper.writeWorkbook(workbook, xlsxWriterhelper, fileMap, file);
        }
    }
}
Also used : HashMap(java.util.HashMap) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) ArrayList(java.util.ArrayList) XlsxWriterHelper(com.cubrid.common.ui.cubrid.table.control.XlsxWriterHelper) CUBRIDResultSetMetaDataProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) ExportDataFailedOneTableEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) Calendar(java.util.Calendar) Connection(java.sql.Connection) IOException(java.io.IOException) Date(java.util.Date) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExportDataSuccessEvent(com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent) File(java.io.File)

Aggregations

CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)13 SQLException (java.sql.SQLException)11 IOException (java.io.IOException)10 CUBRIDPreparedStatementProxy (com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)7 ExportDataSuccessEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)6 CUBRIDResultSetMetaDataProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy)6 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 ExportDataFailedOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent)5 CUBRIDOIDProxy (com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy)3 BufferedWriter (java.io.BufferedWriter)3 Statement (java.sql.Statement)3 HashMap (java.util.HashMap)3 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 DecimalFormat (java.text.DecimalFormat)2 Constraint (com.cubrid.common.core.common.model.Constraint)1 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1