Search in sources :

Example 1 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method openQuery.

public ResultSet openQuery(PreparedStatement ps, IRowMeta params, Object[] data) throws HopDatabaseException {
    ResultSet res;
    // Create a Statement
    try {
        log.snap(Metrics.METRIC_DATABASE_OPEN_QUERY_START, databaseMeta.getName());
        log.snap(Metrics.METRIC_DATABASE_SQL_VALUES_START, databaseMeta.getName());
        // set the parameters!
        setValues(params, data, ps);
        log.snap(Metrics.METRIC_DATABASE_SQL_VALUES_STOP, databaseMeta.getName());
        if (canWeSetFetchSize(ps)) {
            int maxRows = ps.getMaxRows();
            int fs = Const.FETCH_SIZE <= maxRows ? maxRows : Const.FETCH_SIZE;
            // mysql have some restriction on fetch size assignment
            if (databaseMeta.isMySqlVariant()) {
                setMysqlFetchSize(ps, fs, maxRows);
            } else {
                // other databases seems not.
                ps.setFetchSize(fs);
            }
            ps.setFetchDirection(ResultSet.FETCH_FORWARD);
        }
        if (rowlimit > 0 && databaseMeta.supportsSetMaxRows()) {
            ps.setMaxRows(rowlimit);
        }
        log.snap(Metrics.METRIC_DATABASE_EXECUTE_SQL_START, databaseMeta.getName());
        res = ps.executeQuery();
        log.snap(Metrics.METRIC_DATABASE_EXECUTE_SQL_STOP, databaseMeta.getName());
        // MySQL Hack only. It seems too much for the cursor type of operation on
        // MySQL, to have another cursor opened
        // to get the length of a String field. So, on MySQL, we ignore the length
        // of Strings in result rows.
        // 
        log.snap(Metrics.METRIC_DATABASE_GET_ROW_META_START, databaseMeta.getName());
        rowMeta = getRowInfo(res.getMetaData(), databaseMeta.isMySqlVariant(), false);
        log.snap(Metrics.METRIC_DATABASE_GET_ROW_META_STOP, databaseMeta.getName());
    } catch (SQLException ex) {
        throw new HopDatabaseException("ERROR executing query", ex);
    } catch (Exception e) {
        throw new HopDatabaseException("ERROR executing query", e);
    } finally {
        log.snap(Metrics.METRIC_DATABASE_OPEN_QUERY_STOP, databaseMeta.getName());
    }
    return res;
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopExtensionPoint(org.apache.hop.core.extension.HopExtensionPoint) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 2 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method emptyAndCommit.

/**
 * Close the prepared statement of the insert statement.
 *
 * @param ps The prepared statement to empty and close.
 * @param batch true if you are using batch processing
 * @param batchCounter The number of rows on the batch queue
 * @param closeStatement Set to true if we want to close the statement
 * @throws HopDatabaseException
 */
public void emptyAndCommit(PreparedStatement ps, boolean batch, int batchCounter, boolean closeStatement) throws HopDatabaseException {
    boolean isBatchUpdate = false;
    try {
        if (ps != null) {
            if (!isAutoCommit()) {
                // Execute the batch or just perform a commit.
                if (batch && getDatabaseMetaData().supportsBatchUpdates() && batchCounter > 0) {
                    // The problem with the batch counters is that you can't just
                    // execute the current batch.
                    // Certain databases have a problem if you execute the batch and if
                    // there are no statements in it.
                    // You can't just catch the exception either because you would have
                    // to roll back on certain databases before you can then continue to
                    // do anything.
                    // That leaves the task of keeping track of the number of rows up to
                    // our responsibility.
                    isBatchUpdate = true;
                    ps.executeBatch();
                    commit();
                    ps.clearBatch();
                } else {
                    commit();
                }
            }
            // 
            if (closeStatement)
                ps.close();
        }
    } catch (BatchUpdateException ex) {
        throw createHopDatabaseBatchException("Error updating batch", ex);
    } catch (SQLException ex) {
        if (isBatchUpdate) {
            throw createHopDatabaseBatchException("Error updating batch", ex);
        } else {
            throw new HopDatabaseException("Unable to empty ps and commit connection.", ex);
        }
    }
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException)

Example 3 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method execStatementsFromFile.

/**
 * Execute an SQL statement inside a file on the database connection (has to be open)
 *
 * @param filename the file containing the SQL to execute
 * @param sendSinglestatement set to true if you want to send the whole file as a single
 *     statement. If false separate statements will be isolated and executed.
 * @return a Result object indicating the number of lines read, deleted, inserted, updated, ...
 * @throws HopDatabaseException in case anything goes wrong.
 * @sendSinglestatement send one statement
 */
public Result execStatementsFromFile(String filename, boolean sendSinglestatement) throws HopException {
    FileObject sqlFile = null;
    InputStream is = null;
    InputStreamReader bis = null;
    try {
        if (Utils.isEmpty(filename)) {
            throw new HopException("Filename is missing!");
        }
        sqlFile = HopVfs.getFileObject(filename);
        if (!sqlFile.exists()) {
            throw new HopException("We can not find file [" + filename + "]!");
        }
        is = HopVfs.getInputStream(sqlFile);
        bis = new InputStreamReader(new BufferedInputStream(is, 500));
        StringBuilder lineStringBuilder = new StringBuilder(256);
        lineStringBuilder.setLength(0);
        BufferedReader buff = new BufferedReader(bis);
        String sLine = null;
        String sql = Const.CR;
        while ((sLine = buff.readLine()) != null) {
            if (Utils.isEmpty(sLine)) {
                sql = sql + Const.CR;
            } else {
                sql = sql + Const.CR + sLine;
            }
        }
        if (sendSinglestatement) {
            return execStatement(sql);
        } else {
            return execStatements(sql);
        }
    } catch (Exception e) {
        throw new HopException(e);
    } finally {
        try {
            if (sqlFile != null) {
                sqlFile.close();
            }
            if (is != null) {
                is.close();
            }
            if (bis != null) {
                bis.close();
            }
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HopException(org.apache.hop.core.exception.HopException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileObject(org.apache.commons.vfs2.FileObject) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 4 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getLookup.

public Object[] getLookup(PreparedStatement ps, boolean failOnMultipleResults, boolean lazyConversion) throws HopDatabaseException {
    ResultSet res = null;
    try {
        log.snap(Metrics.METRIC_DATABASE_GET_LOOKUP_START, databaseMeta.getName());
        res = ps.executeQuery();
        Object[] ret = getRow(res, lazyConversion);
        if (failOnMultipleResults && ret != null && res.next()) {
            // JDBC driver).
            throw new HopDatabaseException("Only 1 row was expected as a result of a lookup, and at least 2 were found!");
        }
        return ret;
    } catch (SQLException ex) {
        throw new HopDatabaseException("Error looking up row in database", ex);
    } finally {
        try {
            if (res != null) {
                // close resultset!
                res.close();
            }
        } catch (SQLException e) {
            throw new HopDatabaseException("Unable to close resultset after looking up data", e);
        } finally {
            log.snap(Metrics.METRIC_DATABASE_GET_LOOKUP_STOP, databaseMeta.getName());
        }
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException)

Example 5 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method closeConnectionOnly.

/**
 * Only for unique connections usage, typically you use disconnect() to disconnect() from the
 * database.
 *
 * @throws HopDatabaseException in case there is an error during connection close.
 */
public synchronized void closeConnectionOnly() throws HopDatabaseException {
    try {
        if (connection != null) {
            connection.close();
            connection = null;
        }
        if (log.isDetailed()) {
            log.logDetailed("Connection to database closed!");
        }
    } catch (SQLException e) {
        throw new HopDatabaseException("Error disconnecting from database '" + toString() + "'", e);
    }
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException)

Aggregations

HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)106 HopException (org.apache.hop.core.exception.HopException)42 IRowMeta (org.apache.hop.core.row.IRowMeta)30 HopDatabaseBatchException (org.apache.hop.core.exception.HopDatabaseBatchException)29 HopValueException (org.apache.hop.core.exception.HopValueException)29 Database (org.apache.hop.core.database.Database)25 DatabaseMeta (org.apache.hop.core.database.DatabaseMeta)23 IValueMeta (org.apache.hop.core.row.IValueMeta)20 SQLException (java.sql.SQLException)16 FileObject (org.apache.commons.vfs2.FileObject)15 HopExtensionPoint (org.apache.hop.core.extension.HopExtensionPoint)15 RowMeta (org.apache.hop.core.row.RowMeta)14 Result (org.apache.hop.core.Result)9 HopTransformException (org.apache.hop.core.exception.HopTransformException)9 ICheckResult (org.apache.hop.core.ICheckResult)8 HopXmlException (org.apache.hop.core.exception.HopXmlException)6 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)6 PreparedStatement (java.sql.PreparedStatement)5 EnterSelectionDialog (org.apache.hop.ui.core.dialog.EnterSelectionDialog)5 ResultSet (java.sql.ResultSet)4