use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.
the class JDBCObjectCache method loadObjects.
protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner) throws DBException {
if (isFullyCached() || monitor.isCanceled()) {
return;
}
List<OBJECT> tmpObjectList = new ArrayList<>();
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException("Not connected to database");
}
try {
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load objects from " + owner.getName())) {
try (JDBCStatement dbStat = prepareObjectsStatement(session, owner)) {
monitor.subTask("Execute query");
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null) {
try {
while (dbResult.next()) {
if (monitor.isCanceled()) {
break;
}
OBJECT object = fetchObject(session, owner, dbResult);
if (object == null) {
continue;
}
tmpObjectList.add(object);
//monitor.subTask(object.getName());
if (tmpObjectList.size() == maximumCacheSize) {
log.warn("Maximum cache size exceeded (" + maximumCacheSize + ") in " + this);
break;
}
}
} finally {
dbResult.close();
}
}
}
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
} catch (DBException e) {
if (!handleCacheReadError(e)) {
throw e;
}
}
Comparator<OBJECT> comparator = getListOrderComparator();
if (comparator != null) {
Collections.sort(tmpObjectList, comparator);
}
detectCaseSensitivity(owner);
mergeCache(tmpObjectList);
this.invalidateObjects(monitor, owner, new CacheIterator());
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.
the class JDBCStructLookupCache method reloadObject.
protected OBJECT reloadObject(@NotNull DBRProgressMonitor monitor, @NotNull OWNER owner, @Nullable OBJECT object, @Nullable String objectName) throws DBException {
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException("Not connected to database");
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, object == null ? "Load object '" + objectName + "' from " + owner.getName() : "Reload object '" + object + "' from " + owner.getName())) {
try (JDBCStatement dbStat = prepareLookupStatement(session, owner, object, objectName)) {
dbStat.setFetchSize(1);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null) {
try {
if (dbResult.next()) {
return fetchObject(session, owner, dbResult);
}
} finally {
dbResult.close();
}
}
return null;
}
} catch (SQLException ex) {
throw new DBException("Error loading object metadata from database", ex, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.
the class JDBCTable method readData.
@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags) throws DBCException {
DBCStatistics statistics = new DBCStatistics();
boolean hasLimits = firstRow >= 0 && maxRows > 0;
DBPDataSource dataSource = session.getDataSource();
DBRProgressMonitor monitor = session.getProgressMonitor();
try {
readRequiredMeta(monitor);
} catch (DBException e) {
log.warn(e);
}
DBDPseudoAttribute rowIdAttribute = (flags & FLAG_READ_PSEUDO) != 0 ? DBUtils.getRowIdAttribute(this) : null;
// Always use alias if we have criteria or ROWID.
// Some criteria doesn't work without alias
// (e.g. structured attributes in Oracle requires table alias)
String tableAlias = null;
if ((dataFilter != null && dataFilter.hasConditions()) || rowIdAttribute != null) {
if (dataSource instanceof SQLDataSource) {
if (((SQLDataSource) dataSource).getSQLDialect().supportsAliasInSelect()) {
tableAlias = DEFAULT_TABLE_ALIAS;
}
}
}
if (rowIdAttribute != null && tableAlias == null) {
log.warn("Can't query ROWID - table alias not supported");
rowIdAttribute = null;
}
StringBuilder query = new StringBuilder(100);
query.append("SELECT ");
appendSelectSource(monitor, query, tableAlias, rowIdAttribute);
query.append(" FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
if (tableAlias != null) {
//$NON-NLS-1$
query.append(" ").append(tableAlias);
}
appendQueryConditions(query, tableAlias, dataFilter);
appendQueryOrder(query, tableAlias, dataFilter);
String sqlQuery = query.toString();
statistics.setQueryText(sqlQuery);
monitor.subTask(ModelMessages.model_jdbc_fetch_table_data);
try (DBCStatement dbStat = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
if (monitor.isCanceled()) {
return statistics;
}
if (dbStat instanceof JDBCStatement && maxRows > 0) {
boolean useFetchSize = getDataSource().getContainer().getPreferenceStore().getBoolean(ModelPreferences.RESULT_SET_USE_FETCH_SIZE);
if (useFetchSize) {
try {
((JDBCStatement) dbStat).setFetchSize(firstRow < 0 || maxRows <= 0 ? DEFAULT_READ_FETCH_SIZE : (int) (firstRow + maxRows));
} catch (Exception e) {
log.warn(e);
}
}
}
long startTime = System.currentTimeMillis();
boolean executeResult = dbStat.executeStatement();
statistics.setExecuteTime(System.currentTimeMillis() - startTime);
if (executeResult) {
DBCResultSet dbResult = dbStat.openResultSet();
if (dbResult != null && !monitor.isCanceled()) {
try {
dataReceiver.fetchStart(session, dbResult, firstRow, maxRows);
startTime = System.currentTimeMillis();
long rowCount = 0;
while (dbResult.nextRow()) {
if (monitor.isCanceled() || (hasLimits && rowCount >= maxRows)) {
// Fetch not more than max rows
break;
}
dataReceiver.fetchRow(session, dbResult);
rowCount++;
if (rowCount % 100 == 0) {
monitor.subTask(rowCount + ModelMessages.model_jdbc__rows_fetched);
monitor.worked(100);
}
}
statistics.setFetchTime(System.currentTimeMillis() - startTime);
statistics.setRowsFetched(rowCount);
} finally {
// First - close cursor
try {
dbResult.close();
} catch (Throwable e) {
//$NON-NLS-1$
log.error("Error closing result set", e);
}
// Then - signal that fetch was ended
try {
dataReceiver.fetchEnd(session, dbResult);
} catch (Throwable e) {
//$NON-NLS-1$
log.error("Error while finishing result set fetch", e);
}
}
}
}
return statistics;
} finally {
dataReceiver.close();
}
}
Aggregations