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;
}
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;
}
}
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;
}
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);
}
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());
}
}
Aggregations