Search in sources :

Example 1 with CUBRIDPreparedStatementProxy

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

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

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

the class QueryExecuter method reloadQuery.

public void reloadQuery() {
    CUBRIDPreparedStatementProxy pstmt = null;
    CUBRIDResultSetProxy prs = null;
    try {
        pstmt = QueryExecuter.getStatement(connection.checkAndConnectQuietly(), query, false, false);
        if (parameterList != null) {
            for (PstmtParameter pstmtParameter : parameterList) {
                FieldHandlerUtils.setPreparedStatementValue(pstmtParameter, pstmt, charset);
            }
        }
        if (pstmt.hasResultSet()) {
            pstmt.setQueryInfo(false);
            pstmt.setOnlyQueryPlan(false);
            pstmt.executeQuery();
            prs = (CUBRIDResultSetProxy) pstmt.getResultSet();
            if (allColumnList != null) {
                allColumnList.clear();
            }
            if (allDataList != null) {
                allDataList.clear();
            }
            int page = queryInfo.getCurrentPage();
            makeResult(prs);
            queryInfo.setCurrentPage(page);
            makeItem();
            updateActions();
        }
    } catch (final Exception ee) {
        LOGGER.error("execute SQL failed sql  at query editor : " + query + " error message: " + ee);
        ee.printStackTrace();
    } finally {
        QueryUtil.freeQuery(pstmt, prs);
        pstmt = null;
        prs = null;
    }
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy) Point(org.eclipse.swt.graphics.Point) ParamSetException(com.cubrid.common.ui.spi.util.paramSetter.ParamSetException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 4 with CUBRIDPreparedStatementProxy

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

the class GetInfoDataTask method initDemoDataTable.

/**
	 * Initial demo data table
	 *
	 * @throws SQLException
	 */
private void initDemoDataTable() throws SQLException {
    String sql = getStmtSQL();
    stmt = null;
    rs = null;
    try {
        stmt = getStatement(connection, sql, doesGetOidInfo, false);
        CUBRIDPreparedStatementProxy cubridStmt = (CUBRIDPreparedStatementProxy) stmt;
        cubridStmt.setQueryInfo(false);
        cubridStmt.setOnlyQueryPlan(false);
        cubridStmt.executeQuery();
        rs = (CUBRIDResultSetProxy) stmt.getResultSet();
        makeTableData((CUBRIDResultSetProxy) rs);
    } catch (SQLException event) {
        errorMsg += Messages.runError + event.getErrorCode() + StringUtil.NEWLINE + Messages.errorHead + event.getMessage() + StringUtil.NEWLINE;
        throw event;
    } finally {
        QueryUtil.freeQuery(connection, stmt, rs);
        rs = null;
        stmt = null;
        connection = null;
    }
    Display.getDefault().syncExec(new Runnable() {

        public void run() {
            diaplayDemoDataTable();
        }
    });
}
Also used : SQLException(java.sql.SQLException) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)

Example 5 with CUBRIDPreparedStatementProxy

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

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