Search in sources :

Example 11 with CUBRIDPreparedStatementProxy

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

the class UIQueryUtil method getTableNameFromQuery.

/**
	 * Get the table for query
	 *
	 * @param connection
	 * @param query
	 * @return
	 */
public static String getTableNameFromQuery(Connection connection, String query) {
    boolean matched = true;
    String tableName = null;
    CUBRIDPreparedStatementProxy pstmt = null;
    ResultSetMetaData rsMetaData = null;
    try {
        pstmt = QueryExecuter.getStatement(connection, query, false, false);
        rsMetaData = pstmt.getMetaData();
        tableName = rsMetaData.getTableName(1);
        for (int i = 2; i <= rsMetaData.getColumnCount(); i++) {
            if (!tableName.equals(rsMetaData.getTableName(i))) {
                matched = false;
                break;
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        matched = false;
    } finally {
        QueryUtil.freeQuery(pstmt);
    }
    if (matched) {
        return tableName;
    }
    return null;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)

Example 12 with CUBRIDPreparedStatementProxy

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

the class TableUtil method isHasResultSet.

/**
	 * Check whether this SQL has result set
	 * 
	 * @param database CubridDatabase
	 * @param sql String
	 * @return boolean
	 */
public static boolean isHasResultSet(CubridDatabase database, String sql) {
    Connection conn = null;
    CUBRIDPreparedStatementProxy pStmt = null;
    try {
        conn = JDBCConnectionManager.getConnection(database.getDatabaseInfo(), false);
        pStmt = (CUBRIDPreparedStatementProxy) conn.prepareStatement(sql);
        return pStmt.hasResultSet();
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        QueryUtil.freeQuery(conn, pStmt);
    }
    return false;
}
Also used : SQLException(java.sql.SQLException) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) Connection(java.sql.Connection)

Example 13 with CUBRIDPreparedStatementProxy

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

use of com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy 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)

Example 15 with CUBRIDPreparedStatementProxy

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

the class AbsExportDataHandler method getStatement.

protected CUBRIDPreparedStatementProxy getStatement(Connection conn, String sql, String tableName) throws SQLException {
    // FIXME move this logic to core module
    CUBRIDPreparedStatementProxy stmt = QueryExecuter.getStatement(conn, sql, false, true);
    List<PstmtParameter> pstmList = exportConfig.getParameterList(tableName);
    if (pstmList != null) {
        String charset = dbInfo.getCharSet();
        for (PstmtParameter pstmtParameter : pstmList) {
            FieldHandlerUtils.setPreparedStatementValue(pstmtParameter, stmt, charset);
        }
    }
    return stmt;
}
Also used : PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)

Aggregations

CUBRIDPreparedStatementProxy (com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)15 SQLException (java.sql.SQLException)13 IOException (java.io.IOException)8 CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)7 Connection (java.sql.Connection)7 ExportDataSuccessEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataSuccessEvent)6 CUBRIDResultSetMetaDataProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy)6 ExportDataFailedOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent)5 BufferedWriter (java.io.BufferedWriter)3 ArrayList (java.util.ArrayList)3 PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)2 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 WriteException (jxl.write.WriteException)2 Point (org.eclipse.swt.graphics.Point)2 XlsxWriterHelper (com.cubrid.common.ui.cubrid.table.control.XlsxWriterHelper)1 ExportDataBeginOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataBeginOneTableEvent)1 ExportDataFinishOneTableEvent (com.cubrid.common.ui.cubrid.table.event.ExportDataFinishOneTableEvent)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 File (java.io.File)1