use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by serge-rider.
the class SQLQueryJob method executeStatement.
private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, long startTime, SQLQueryResult curResult) throws DBCException {
AbstractExecutionSource source = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), partSite.getPart(), sqlQuery);
source.setScriptContext(scriptContext);
final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, rsOffset, rsMaxRows);
DBExecUtils.setStatementFetchSize(dbcStatement, rsOffset, rsMaxRows, fetchSize);
curStatement = dbcStatement;
int statementTimeout = getDataSourceContainer().getPreferenceStore().getInt(SQLPreferenceConstants.STATEMENT_TIMEOUT);
if (statementTimeout > 0) {
try {
dbcStatement.setStatementTimeout(statementTimeout);
} catch (Throwable e) {
log.debug("Can't set statement timeout:" + e.getMessage());
}
}
// Execute statement
try {
DBRProgressMonitor monitor = session.getProgressMonitor();
monitor.subTask("Execute query");
boolean hasResultSet = dbcStatement.executeStatement();
statistics.addExecuteTime(System.currentTimeMillis() - startTime);
statistics.addStatementsCount();
curResult.setHasResultSet(hasResultSet);
long updateCount = -1;
while (true) {
// Fetch data only if we have to fetch all results or if it is rs requested
if (fetchResultSetNumber < 0 || fetchResultSetNumber == resultSetNumber) {
if (hasResultSet && fetchResultSets) {
DBCResultSet resultSet;
try {
resultSet = dbcStatement.openResultSet();
} catch (DBCException e) {
DBPErrorAssistant.ErrorType errorType = DBExecUtils.discoverErrorType(session.getDataSource(), e);
if (errorType == DBPErrorAssistant.ErrorType.RESULT_SET_MISSING) {
// We need to ignore this error and try to get next results
if (dbcStatement.nextResults()) {
continue;
}
}
throw e;
}
if (resultSet == null) {
// Kind of bug in the driver. It says it has resultset but returns null
break;
} else {
DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
if (dataReceiver != null) {
hasResultSet = fetchQueryData(session, resultSet, curResult, curResult.addExecuteResult(true), dataReceiver, true);
}
}
}
}
if (!hasResultSet) {
try {
updateCount = dbcStatement.getUpdateRowCount();
SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(false);
if (updateCount >= 0) {
executeResult.setUpdateCount(updateCount);
statistics.addRowsUpdated(updateCount);
}
} catch (DBCException e) {
// In some cases we can't read update count
// This is bad but we can live with it
// Just print a warning
log.warn("Can't obtain update count", e);
}
}
if (hasResultSet && fetchResultSets) {
resultSetNumber++;
fetchResultSetNumber = resultSetNumber;
}
if (!hasResultSet && updateCount < 0) {
// Nothing else to fetch
break;
}
if (session.getDataSource().getInfo().supportsMultipleResults()) {
try {
hasResultSet = dbcStatement.nextResults();
} catch (DBCException e) {
if (session.getDataSource().getInfo().isMultipleResultsFetchBroken()) {
log.error(e);
// #2792: Check this twice. Some drivers (e.g. Sybase jConnect)
// throw error on n'th result fetch - but it still can keep fetching next results
hasResultSet = dbcStatement.nextResults();
} else {
throw e;
}
}
updateCount = hasResultSet ? -1 : 0;
} else {
break;
}
}
;
} finally {
try {
curResult.addWarnings(dbcStatement.getStatementWarnings());
} catch (Throwable e) {
log.warn("Can't read execution warnings", e);
}
// monitor.subTask("Close query");
if (!keepStatementOpen()) {
closeStatement();
}
}
}
use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.
the class ResultSetViewer method readRowCount.
/**
* Reads row count and sets value in status label
*/
private long readRowCount(DBRProgressMonitor monitor) throws DBException {
final DBCExecutionContext executionContext = getExecutionContext();
DBSDataContainer dataContainer = getDataContainer();
if (executionContext == null || dataContainer == null) {
throw new DBException(ModelMessages.error_not_connected_to_database);
}
long[] result = new long[1];
DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Read total row count")) {
long rowCount = dataContainer.countData(new AbstractExecutionSource(dataContainer, executionContext, this), session, model.getDataFilter(), DBSDataContainer.FLAG_NONE);
model.setTotalRowCount(rowCount);
result[0] = rowCount;
} catch (DBCException e) {
throw new InvocationTargetException(e);
}
});
return result[0];
}
use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.
the class SQLQueryJob method executeStatement.
private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, long startTime, SQLQueryResult curResult) throws DBCException {
AbstractExecutionSource source = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), partSite.getPart(), sqlQuery);
source.setScriptContext(scriptContext);
final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, rsOffset, rsMaxRows);
DBExecUtils.setStatementFetchSize(dbcStatement, rsOffset, rsMaxRows, fetchSize);
curStatement = dbcStatement;
int statementTimeout = getDataSourceContainer().getPreferenceStore().getInt(SQLPreferenceConstants.STATEMENT_TIMEOUT);
if (statementTimeout > 0) {
try {
dbcStatement.setStatementTimeout(statementTimeout);
} catch (Throwable e) {
log.debug("Can't set statement timeout:" + e.getMessage());
}
}
// Execute statement
try {
DBRProgressMonitor monitor = session.getProgressMonitor();
monitor.subTask("Execute query");
boolean hasResultSet = dbcStatement.executeStatement();
statistics.addExecuteTime(System.currentTimeMillis() - startTime);
statistics.addStatementsCount();
curResult.setHasResultSet(hasResultSet);
long updateCount = -1;
while (true) {
// Fetch data only if we have to fetch all results or if it is rs requested
if (fetchResultSetNumber < 0 || fetchResultSetNumber == resultSetNumber) {
if (hasResultSet && fetchResultSets) {
DBCResultSet resultSet;
try {
resultSet = dbcStatement.openResultSet();
} catch (DBCException e) {
DBPErrorAssistant.ErrorType errorType = DBExecUtils.discoverErrorType(session.getDataSource(), e);
if (errorType == DBPErrorAssistant.ErrorType.RESULT_SET_MISSING) {
// We need to ignore this error and try to get next results
if (dbcStatement.nextResults()) {
continue;
}
}
throw e;
}
if (resultSet == null) {
// Kind of bug in the driver. It says it has resultset but returns null
break;
} else {
DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(sqlQuery, resultSetNumber);
if (dataReceiver != null) {
hasResultSet = fetchQueryData(session, resultSet, curResult, curResult.addExecuteResult(true), dataReceiver, true);
}
}
}
}
if (!hasResultSet) {
try {
updateCount = dbcStatement.getUpdateRowCount();
SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(false);
if (updateCount >= 0) {
executeResult.setUpdateCount(updateCount);
statistics.addRowsUpdated(updateCount);
}
} catch (DBCException e) {
// In some cases we can't read update count
// This is bad but we can live with it
// Just print a warning
log.warn("Can't obtain update count", e);
}
}
if (hasResultSet && fetchResultSets) {
resultSetNumber++;
fetchResultSetNumber = resultSetNumber;
}
if (!hasResultSet && updateCount < 0) {
// Nothing else to fetch
break;
}
if (session.getDataSource().getInfo().supportsMultipleResults()) {
try {
hasResultSet = dbcStatement.nextResults();
} catch (DBCException e) {
if (session.getDataSource().getInfo().isMultipleResultsFetchBroken()) {
log.error(e);
// #2792: Check this twice. Some drivers (e.g. Sybase jConnect)
// throw error on n'th result fetch - but it still can keep fetching next results
hasResultSet = dbcStatement.nextResults();
} else {
throw e;
}
}
updateCount = hasResultSet ? -1 : 0;
} else {
break;
}
}
;
} finally {
try {
curResult.addWarnings(dbcStatement.getStatementWarnings());
} catch (Throwable e) {
log.warn("Can't read execution warnings", e);
}
// monitor.subTask("Close query");
if (!keepStatementOpen()) {
closeStatement();
}
}
}
use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.
the class SearchDataQuery method findRows.
private DBCStatistics findRows(@NotNull DBCSession session, @NotNull DBSDataContainer dataContainer, @NotNull TestDataReceiver dataReceiver) throws DBCException {
DBSEntity entity;
if (dataContainer instanceof DBSEntity) {
entity = (DBSEntity) dataContainer;
} else {
log.warn("Data container " + dataContainer + " isn't entity");
return null;
}
try {
List<DBDAttributeConstraint> constraints = new ArrayList<>();
for (DBSEntityAttribute attribute : CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()))) {
if (params.fastSearch) {
if (DBUtils.findAttributeIndex(session.getProgressMonitor(), attribute) == null) {
continue;
}
}
if (DBUtils.isPseudoAttribute(attribute) || DBUtils.isHiddenObject(attribute)) {
continue;
}
DBCLogicalOperator[] supportedOperators = DBUtils.getAttributeOperators(attribute);
DBCLogicalOperator operator;
Object value;
switch(attribute.getDataKind()) {
case BOOLEAN:
continue;
case NUMERIC:
if (!params.searchNumbers) {
continue;
}
if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
continue;
}
operator = DBCLogicalOperator.EQUALS;
try {
value = Integer.valueOf(params.searchString);
} catch (NumberFormatException e) {
try {
value = Long.valueOf(params.searchString);
} catch (NumberFormatException e1) {
try {
value = Double.valueOf(params.searchString);
} catch (NumberFormatException e2) {
try {
value = new BigDecimal(params.searchString);
} catch (Exception e3) {
// Not a number
continue;
}
}
}
}
break;
case CONTENT:
case BINARY:
if (!params.searchLOBs) {
continue;
}
case STRING:
if (!params.isCaseSensitive() && ArrayUtils.contains(supportedOperators, DBCLogicalOperator.ILIKE)) {
operator = DBCLogicalOperator.ILIKE;
value = "%" + params.searchString + "%";
} else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.LIKE)) {
operator = DBCLogicalOperator.LIKE;
value = "%" + params.searchString + "%";
} else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
operator = DBCLogicalOperator.EQUALS;
value = params.searchString;
} else {
continue;
}
break;
default:
{
// On success search by exact match
if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
continue;
}
String typeName = attribute.getTypeName();
if (typeName.equals(DBConstants.TYPE_NAME_UUID) || typeName.equals(DBConstants.TYPE_NAME_UUID2)) {
try {
UUID uuid = UUID.fromString(params.searchString);
operator = DBCLogicalOperator.EQUALS;
value = uuid.toString();
} catch (Exception e) {
// No a UUID
continue;
}
} else {
continue;
}
}
}
DBDAttributeConstraint constraint = new DBDAttributeConstraint(attribute, constraints.size());
constraint.setOperator(operator);
constraint.setValue(value);
constraint.setVisible(true);
constraints.add(constraint);
}
if (constraints.isEmpty()) {
return null;
}
dataReceiver.filter = new DBDDataFilter(constraints);
dataReceiver.filter.setAnyConstraint(true);
DBCExecutionSource searchSource = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this);
return dataContainer.readData(searchSource, session, dataReceiver, dataReceiver.filter, -1, -1, 0, 0);
} catch (DBException e) {
throw new DBCException("Error finding rows", e);
}
}
use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.
the class DatabaseMappingContainer method readAttributes.
private void readAttributes(DBRProgressMonitor monitor) throws DBException {
if (source instanceof DBSEntity && !(source instanceof DBSDocumentContainer)) {
for (DBSEntityAttribute attr : CommonUtils.safeCollection(((DBSEntity) source).getAttributes(monitor))) {
if (DBUtils.isHiddenObject(attr)) {
continue;
}
addAttributeMapping(monitor, attr);
}
} else {
// Seems to be a dynamic query. Execute it to get metadata
DBPDataSource dataSource = source.getDataSource();
assert (dataSource != null);
DBCExecutionContext context;
if (source instanceof DBPContextProvider) {
context = ((DBPContextProvider) source).getExecutionContext();
} else {
context = DBUtils.getDefaultContext(source, false);
}
if (context == null) {
throw new DBCException("No execution context");
}
DBExecUtils.tryExecuteRecover(monitor, context.getDataSource(), monitor1 -> {
try (DBCSession session = context.openSession(monitor1, DBCExecutionPurpose.META, "Read query meta data")) {
MetadataReceiver receiver = new MetadataReceiver();
try {
source.readData(new AbstractExecutionSource(source, session.getExecutionContext(), this), session, receiver, null, 0, 1, DBSDataContainer.FLAG_NONE, 1);
for (DBDAttributeBinding attr : receiver.attributes) {
if (DBUtils.isHiddenObject(attr)) {
continue;
}
addAttributeMapping(monitor1, attr);
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
}
}
Aggregations