Search in sources :

Example 11 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class DB2TableUniqueKeyCache method prepareObjectsStatement.

@NotNull
@Override
protected JDBCStatement prepareObjectsStatement(JDBCSession session, DB2Schema db2Schema, DB2Table forTable) throws SQLException {
    String sql;
    if (forTable != null) {
        sql = SQL_UK_TAB;
    } else {
        sql = SQL_UK_ALL;
    }
    JDBCPreparedStatement dbStat = session.prepareStatement(sql);
    dbStat.setString(1, db2Schema.getName());
    if (forTable != null) {
        dbStat.setString(2, forTable.getName());
    }
    return dbStat;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) NotNull(org.jkiss.code.NotNull)

Example 12 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class DB2ViewCache method prepareLookupStatement.

@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, @Nullable DB2View db2View, @Nullable String db2ViewName) throws SQLException {
    if (db2View != null || db2ViewName != null) {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_VIEW);
        dbStat.setString(1, db2Schema.getName());
        dbStat.setString(2, db2View != null ? db2View.getName() : db2ViewName);
        return dbStat;
    } else {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_VIEW_ALL);
        dbStat.setString(1, db2Schema.getName());
        return dbStat;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) NotNull(org.jkiss.code.NotNull)

Example 13 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class ExecuteBatchImpl method processBatch.

/**
     * Execute batch OR generate batch script.
     * @param session    session
     * @param actions    script actions. If not null then no execution will be done
     * @return execution statistics
     * @throws DBCException
     */
@NotNull
private DBCStatistics processBatch(@NotNull DBCSession session, @Nullable List<DBEPersistAction> actions) throws DBCException {
    DBDValueHandler[] handlers = new DBDValueHandler[attributes.length];
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i] instanceof DBDAttributeBinding) {
            handlers[i] = ((DBDAttributeBinding) attributes[i]).getValueHandler();
        } else {
            handlers[i] = DBUtils.findValueHandler(session, attributes[i]);
        }
    }
    boolean useBatch = session.getDataSource().getInfo().supportsBatchUpdates() && reuseStatement;
    if (values.size() <= 1) {
        useBatch = false;
    }
    DBCStatistics statistics = new DBCStatistics();
    DBCStatement statement = null;
    try {
        // Here we'll try to reuse prepared statement.
        // It makes a great sense in case of data transfer where we need millions of inserts.
        // We must be aware of nulls because actual insert statements may differ depending on null values.
        // So if row nulls aren't the same as in previous row we need to prepare new statement and restart batch.
        // Quite complicated but works.
        boolean[] prevNulls = new boolean[attributes.length];
        boolean[] nulls = new boolean[attributes.length];
        int statementsInBatch = 0;
        for (Object[] rowValues : values) {
            boolean reuse = reuseStatement;
            if (reuse) {
                for (int i = 0; i < rowValues.length; i++) {
                    nulls[i] = DBUtils.isNullValue(rowValues[i]);
                }
                if (!Arrays.equals(prevNulls, nulls) && statementsInBatch > 0) {
                    reuse = false;
                }
                System.arraycopy(nulls, 0, prevNulls, 0, nulls.length);
                if (!reuse && statementsInBatch > 0) {
                    // Flush batch
                    if (actions == null) {
                        flushBatch(statistics, statement);
                    }
                    statement.close();
                    statement = null;
                    statementsInBatch = 0;
                    reuse = true;
                }
            }
            if (statement == null || !reuse) {
                statement = prepareStatement(session, rowValues);
                statistics.setQueryText(statement.getQueryString());
            }
            try {
                bindStatement(handlers, statement, rowValues);
                if (actions == null) {
                    if (useBatch) {
                        statement.addToBatch();
                        statementsInBatch++;
                    } else {
                        // Execute each row separately
                        long startTime = System.currentTimeMillis();
                        executeStatement(statement);
                        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
                        long rowCount = statement.getUpdateRowCount();
                        if (rowCount > 0) {
                            statistics.addRowsUpdated(rowCount);
                        }
                        // Read keys
                        if (keysReceiver != null) {
                            readKeys(statement.getSession(), statement, keysReceiver);
                        }
                    }
                } else {
                    String queryString;
                    if (statement instanceof DBCParameterizedStatement) {
                        queryString = ((DBCParameterizedStatement) statement).getFormattedQuery();
                    } else {
                        queryString = statement.getQueryString();
                    }
                    actions.add(new SQLDatabasePersistAction("Execute statement", queryString));
                }
            } finally {
                if (!reuse) {
                    statement.close();
                }
            }
        }
        values.clear();
        if (statementsInBatch > 0) {
            if (actions == null) {
                flushBatch(statistics, statement);
            }
            statement.close();
            statement = null;
        }
    } finally {
        if (reuseStatement && statement != null) {
            statement.close();
        }
    }
    return statistics;
}
Also used : DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) NotNull(org.jkiss.code.NotNull)

Example 14 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class WMIDataSourceProvider method openDataSource.

@NotNull
@Override
public DBPDataSource openDataSource(@NotNull DBRProgressMonitor monitor, @NotNull DBPDataSourceContainer container) throws DBException {
    if (!libLoaded) {
        DBPDriver driver = container.getDriver();
        driver.loadDriver(monitor);
        loadNativeLib(driver);
        libLoaded = true;
    }
    return new WMIDataSource(container);
}
Also used : DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) WMIDataSource(org.jkiss.dbeaver.ext.wmi.model.WMIDataSource) NotNull(org.jkiss.code.NotNull)

Example 15 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class JDBCConnectionImpl method prepareStatement.

@NotNull
@Override
public JDBCStatement prepareStatement(@NotNull DBCStatementType type, @NotNull String sqlQuery, boolean scrollable, boolean updatable, boolean returnGeneratedKeys) throws DBCException {
    try {
        if (type == DBCStatementType.EXEC) {
            // Execute as call
            try {
                return prepareCall(sqlQuery, scrollable ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
            } catch (SQLFeatureNotSupportedException | UnsupportedOperationException | IncompatibleClassChangeError e) {
                return prepareCall(sqlQuery);
            }
        } else if (type == DBCStatementType.SCRIPT) {
            // Just simplest statement for scripts
            // Sometimes prepared statements perform additional checks of queries
            // (e.g. in Oracle it parses IN/OUT parameters)
            JDBCStatement statement;
            try {
                statement = createStatement(scrollable ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
            } catch (Throwable e) {
                try {
                    statement = createStatement();
                } catch (Throwable e1) {
                    try {
                        statement = prepareStatement(sqlQuery, scrollable ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
                    } catch (Throwable e2) {
                        log.debug(e);
                        statement = prepareStatement(sqlQuery);
                    }
                }
            }
            if (statement instanceof JDBCStatementImpl) {
                ((JDBCStatementImpl) statement).setQueryString(sqlQuery);
            }
            return statement;
        } else if (returnGeneratedKeys) {
            // Return keys
            try {
                return prepareStatement(sqlQuery, Statement.RETURN_GENERATED_KEYS);
            } catch (SQLFeatureNotSupportedException | UnsupportedOperationException | IncompatibleClassChangeError e) {
                return prepareStatement(sqlQuery);
            }
        } else {
            try {
                // Generic prepared statement
                return prepareStatement(sqlQuery, scrollable ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
            } catch (SQLFeatureNotSupportedException | UnsupportedOperationException | IncompatibleClassChangeError e) {
                return prepareStatement(sqlQuery);
            }
        }
    } catch (SQLException e) {
        throw new JDBCException(e, getDataSource());
    }
}
Also used : JDBCException(org.jkiss.dbeaver.model.impl.jdbc.JDBCException) NotNull(org.jkiss.code.NotNull)

Aggregations

NotNull (org.jkiss.code.NotNull)58 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)11 SQLException (java.sql.SQLException)9 DBCException (org.jkiss.dbeaver.model.exec.DBCException)9 DBException (org.jkiss.dbeaver.DBException)7 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)6 ArrayList (java.util.ArrayList)5 Tree (org.eclipse.swt.widgets.Tree)5 TreeColumn (org.eclipse.swt.widgets.TreeColumn)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 Nullable (org.jkiss.code.Nullable)5 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)5 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)5 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)5 SQLScriptStatusDialog (org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog)4 DBDValueHandler (org.jkiss.dbeaver.model.data.DBDValueHandler)3 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)3 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)3