use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by serge-rider.
the class ExasolStructureAssistant method searchTables.
// --------------
// Helper Classes
// --------------
private void searchTables(JDBCSession session, ExasolSchema schema, String searchObjectNameMask, List<ExasolObjectType> exasolObjectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
String sql;
if (schema != null) {
sql = String.format(SQL_TABLES_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
} else {
sql = String.format(SQL_TABLES_ALL, ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
}
try (JDBCStatement dbStat = session.createStatement()) {
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
String schemaName;
String objectName;
ExasolSchema exasolSchema;
ExasolObjectType objectType;
try (JDBCResultSet dbResult = dbStat.executeQuery(sql)) {
while (dbResult.next()) {
if (session.getProgressMonitor().isCanceled()) {
break;
}
if (nbResults++ >= maxResults) {
break;
}
schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABLE_SCHEM");
objectName = JDBCUtils.safeGetString(dbResult, "TABLE_NAME");
exasolSchema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
if (exasolSchema == null) {
LOG.debug("Schema '" + schemaName + "' not found. Probably was filtered");
continue;
}
objectType = ExasolObjectType.TABLE;
objects.add(new ExasolObjectReference(objectName, exasolSchema, objectType));
}
}
}
}
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();
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class ExasolStructureAssistant method searchColumns.
private void searchColumns(JDBCSession session, ExasolSchema schema, String searchObjectNameMask, List<ExasolObjectType> objectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
String sql;
if (schema != null) {
sql = String.format(SQL_COLS_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(searchObjectNameMask));
} else {
// search for columns is to slow in exasol
return;
}
try (JDBCStatement dbStat = session.createStatement()) {
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
String tableSchemaName;
String tableOrViewName;
String columnName;
ExasolSchema exasolSchema;
ExasolTable exasolTable;
try (JDBCResultSet dbResult = dbStat.executeQuery(sql)) {
while (dbResult.next()) {
if (session.getProgressMonitor().isCanceled()) {
break;
}
if (nbResults++ >= maxResults) {
return;
}
tableSchemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABLE_SCHEM");
tableOrViewName = JDBCUtils.safeGetString(dbResult, "TABLE_NAME");
columnName = JDBCUtils.safeGetString(dbResult, "COLUMN_NAME");
exasolSchema = dataSource.getSchema(session.getProgressMonitor(), tableSchemaName);
if (exasolSchema == null) {
LOG.debug("Schema '" + tableSchemaName + "' not found. Probably was filtered");
continue;
}
// Try with table, then view
exasolTable = exasolSchema.getTable(session.getProgressMonitor(), tableOrViewName);
if (exasolTable != null) {
objects.add(new ExasolObjectReference(columnName, exasolTable, ExasolObjectType.COLUMN));
}
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class DB2ToolWithStatus method getExecuteStatistics.
@Override
public List<ToolStatus> getExecuteStatistics(OBJECT_TYPE object, SETTINGS settings, DBEPersistAction action, DBCSession session, DBCStatement dbStat) throws DBCException {
List<ToolStatus> statusList = new ArrayList<>();
try {
int warnNum = 0;
SQLWarning warning = ((JDBCStatement) dbStat).getWarnings();
while (warning != null) {
statusList.add(new ToolStatus(object, warning.getMessage()));
warnNum++;
warning = warning.getNextWarning();
}
if (warnNum == 0) {
// $NON-NLS-1$
statusList.add(new ToolStatus(object, "Done"));
}
} catch (SQLException e) {
// ignore
}
return statusList;
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class ExasolTableCache method prepareChildrenStatement.
@SuppressWarnings("rawtypes")
@Override
protected JDBCStatement prepareChildrenStatement(@NotNull JDBCSession session, @NotNull ExasolSchema exasolSchema, @Nullable ExasolTable exasolTable) throws SQLException {
String tablePrefix = exasolSchema.getDataSource().getTablePrefix(ExasolSysTablePrefix.ALL);
String sql;
if (exasolTable != null)
sql = String.format(SQL_COLS_TAB, tablePrefix, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(exasolTable.getName()));
else
sql = String.format(SQL_COLS_ALL, tablePrefix, ExasolUtils.quoteString(exasolSchema.getName()));
JDBCStatement dbstat = session.createStatement();
((JDBCStatementImpl) dbstat).setQueryString(sql);
return dbstat;
}
Aggregations