use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class ExasolStructureAssistant method findTableObjectByName.
private void findTableObjectByName(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references, String type) throws SQLException, DBException {
DBRProgressMonitor monitor = session.getProgressMonitor();
// don't use parameter marks because of performance
String sql = "";
if (schema == null) {
sql = String.format(SQL_TABLES_ALL, ExasolUtils.quoteString(objectNameMask), type);
} else {
sql = String.format(SQL_TABLES_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(objectNameMask), type);
}
try (JDBCStatement dbstat = session.createStatement()) {
try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
int num = maxResults;
while (dbResult.next() && num-- > 0) {
if (monitor.isCanceled()) {
break;
}
final String schemaName = JDBCUtils.safeGetString(dbResult, "TABLE_SCHEM");
final String tableName = JDBCUtils.safeGetString(dbResult, "COLUMN_TABLE");
references.add(new AbstractObjectReference(tableName, dataSource.getSchema(monitor, schemaName), null, ExasolTableBase.class, RelationalObjectType.TYPE_TABLE_COLUMN) {
@Override
public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
if (tableSchema == null) {
throw new DBException("Table schema '" + schemaName + "' not found");
}
if (type == "VIEW") {
ExasolView view = tableSchema.getViewCache().getObject(monitor, tableSchema, tableName);
if (view == null)
throw new DBException("View '" + tableName + "' not found in schema '" + schemaName + "'");
return view;
} else if (type == "TABLE") {
ExasolTable table = tableSchema.getTableCache().getObject(monitor, tableSchema, tableName);
if (table == null)
throw new DBException("Table '" + tableName + "' not found in schema '" + schemaName + "'");
return table;
} else {
throw new DBException("Object type " + type + " unknown");
}
}
});
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class ExasolStructureAssistant method findProceduresByMask.
private void findProceduresByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references) throws SQLException, DBException {
DBRProgressMonitor monitor = session.getProgressMonitor();
// don't use parameter marks because of performance
String sql = "";
if (schema == null) {
sql = String.format(sqlProceduresAll, ExasolUtils.quoteString(objectNameMask));
} else {
sql = String.format(sqlProcedureSchema, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(objectNameMask));
}
try (JDBCStatement dbstat = session.createStatement()) {
try (JDBCResultSet dbResult = dbstat.executeQuery(sql)) {
int num = maxResults;
while (dbResult.next() && num-- > 0) {
if (monitor.isCanceled()) {
break;
}
final String schemaName = JDBCUtils.safeGetString(dbResult, "SCRIPT_SCHEMA");
final String scriptName = JDBCUtils.safeGetString(dbResult, "SCRIPT_NAME");
references.add(new AbstractObjectReference(scriptName, dataSource.getSchema(monitor, schemaName), null, ExasolScript.class, RelationalObjectType.TYPE_PROCEDURE) {
@Override
public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
if (tableSchema == null) {
throw new DBException("Table schema '" + schemaName + "' not found");
}
ExasolScript script = tableSchema.scriptCache.getObject(monitor, tableSchema, scriptName);
if (script == null) {
throw new DBException("Script '" + script + "' not found in schema '" + schemaName + "'");
}
return script;
}
});
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class JDBCTable method readData.
// //////////////////////////////////////////////////////////////////
// Select
@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags, int fetchSize) 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.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);
}
SQLUtils.appendQueryConditions(dataSource, query, tableAlias, dataFilter);
SQLUtils.appendQueryOrder(dataSource, 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 && (fetchSize > 0 || maxRows > 0)) {
DBExecUtils.setStatementFetchSize(dbStat, firstRow, maxRows, fetchSize);
}
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);
DBFetchProgress fetchProgress = new DBFetchProgress(session.getProgressMonitor());
while (dbResult.nextRow()) {
if (fetchProgress.isCanceled() || (hasLimits && fetchProgress.isMaxRowsFetched(maxRows))) {
// Fetch not more than max rows
break;
}
dataReceiver.fetchRow(session, dbResult);
fetchProgress.monitorRowFetch();
}
fetchProgress.dumpStatistics(statistics);
} 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 JDBCStructCache method loadChildren.
/**
* Reads children objects from database
*
* @param monitor
* monitor
* @param forObject
* object for which to read children. If null then reads children for all objects in this container.
* @throws org.jkiss.dbeaver.DBException
* on error
*/
public synchronized void loadChildren(DBRProgressMonitor monitor, OWNER owner, @Nullable final OBJECT forObject) throws DBException {
if ((forObject == null && this.childrenCached) || (forObject != null && (!forObject.isPersisted() || isChildrenCached(forObject))) || monitor.isCanceled()) {
return;
}
if (forObject == null) {
// If we have some child objects read before that - do not clear them.
// We have to reuse them because there could be some references in cached model
// clearChildrenCache(null);
super.loadObjects(monitor, owner);
}
DBPDataSource dataSource = owner.getDataSource();
if (dataSource == null) {
throw new DBException(ModelMessages.error_not_connected_to_database);
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, "Load child objects")) {
Map<OBJECT, List<CHILD>> objectMap = new HashMap<>();
// Load columns
try (JDBCStatement dbStat = prepareChildrenStatement(session, owner, forObject)) {
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null) {
try {
while (dbResult.next()) {
if (monitor.isCanceled()) {
return;
}
OBJECT object = forObject;
if (object == null) {
String objectName;
if (objectNameColumn instanceof Number) {
objectName = JDBCUtils.safeGetString(dbResult, ((Number) objectNameColumn).intValue());
} else {
objectName = JDBCUtils.safeGetStringTrimmed(dbResult, objectNameColumn.toString());
}
if (objectName == null) {
log.debug("NULL object name in " + this);
continue;
}
object = super.getCachedObject(objectName);
if (object == null) {
log.debug("Object '" + objectName + "' not found in struct cache (" + getClass().getSimpleName() + ")");
continue;
}
}
if (isChildrenCached(object)) {
// Already read
continue;
}
CHILD child = fetchChild(session, owner, object, dbResult);
if (child == null) {
continue;
}
// Add to map
List<CHILD> children = objectMap.get(object);
if (children == null) {
children = new ArrayList<>();
objectMap.put(object, children);
}
children.add(child);
}
if (monitor.isCanceled()) {
return;
}
// All children are read. Now assign them to parents
for (Map.Entry<OBJECT, List<CHILD>> colEntry : objectMap.entrySet()) {
if (!isChildrenCached(colEntry.getKey())) {
// isChildrenCached may return true if the same cache was read in other thread
// just skip
cacheChildren(colEntry.getKey(), colEntry.getValue());
}
}
if (forObject == null) {
if (objectMap.isEmpty()) {
// Nothing was read. May be it means empty list of children
// but possibly this feature is not supported [JDBC: SQLite]
} else {
// Now set empty column list for other tables
for (OBJECT tmpObject : getAllObjects(monitor, owner)) {
if (!isChildrenCached(tmpObject) && !objectMap.containsKey(tmpObject)) {
cacheChildren(tmpObject, new ArrayList<>());
}
}
this.childrenCached = true;
}
} else if (!objectMap.containsKey(forObject)) {
cacheChildren(forObject, new ArrayList<>());
}
} finally {
dbResult.close();
}
}
}
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.
the class JDBCCompositeCache method loadObjects.
protected void loadObjects(DBRProgressMonitor monitor, OWNER owner, PARENT forParent) throws DBException {
synchronized (objectCache) {
if ((forParent == null && isFullyCached()) || (forParent != null && (!forParent.isPersisted() || objectCache.containsKey(forParent)))) {
return;
}
}
// Load tables and columns first
if (forParent == null) {
parentCache.loadObjects(monitor, owner);
parentCache.loadChildren(monitor, owner, null);
}
Map<PARENT, Map<String, ObjectInfo>> parentObjectMap = new LinkedHashMap<>();
// Load index columns
DBPDataSource dataSource = owner.getDataSource();
assert (dataSource != null);
monitor.beginTask("Load composite cache", 1);
try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, "Load composite objects")) {
JDBCStatement dbStat = prepareObjectsStatement(session, owner, forParent);
dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
try {
dbStat.executeStatement();
JDBCResultSet dbResult = dbStat.getResultSet();
if (dbResult != null)
try {
while (dbResult.next()) {
if (monitor.isCanceled()) {
return;
}
String parentName = forParent != null ? forParent.getName() : (parentColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) parentColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, parentColumnName.toString()));
String objectName = objectColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) objectColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, objectColumnName.toString());
if (CommonUtils.isEmpty(objectName)) {
// Use default name
objectName = getDefaultObjectName(dbResult, parentName);
}
if (forParent == null && CommonUtils.isEmpty(parentName)) {
// No parent - can't evaluate it
log.debug("Empty parent name in " + this);
continue;
}
PARENT parent = forParent;
if (parent == null) {
parent = parentCache.getObject(monitor, owner, parentName, parentType);
if (parent == null) {
log.debug("Object '" + objectName + "' owner '" + parentName + "' not found");
continue;
}
}
synchronized (objectCache) {
if (objectCache.containsKey(parent)) {
// Already cached
continue;
}
}
// Add to map
Map<String, ObjectInfo> objectMap = parentObjectMap.get(parent);
if (objectMap == null) {
objectMap = new TreeMap<>();
parentObjectMap.put(parent, objectMap);
}
ObjectInfo objectInfo = objectMap.get(objectName);
if (objectInfo == null) {
OBJECT object = fetchObject(session, owner, parent, objectName, dbResult);
if (object == null || !isValidObject(monitor, owner, object)) {
// Can't fetch object
continue;
}
objectName = object.getName();
objectInfo = new ObjectInfo(object);
objectMap.put(objectName, objectInfo);
}
ROW_REF[] rowRef = fetchObjectRow(session, parent, objectInfo.object, dbResult);
if (rowRef == null || rowRef.length == 0) {
// At least one of rows is broken.
// So entire object is broken, let's just skip it.
objectInfo.broken = true;
// log.debug("Object '" + objectName + "' metadata corrupted - NULL child returned");
continue;
}
for (ROW_REF row : rowRef) {
if (row != null) {
objectInfo.rows.add(row);
}
}
}
} finally {
dbResult.close();
}
} finally {
dbStat.close();
}
} catch (SQLException ex) {
if (ex instanceof SQLFeatureNotSupportedException) {
log.debug("Error reading cache: feature not supported", ex);
} else {
throw new DBException(ex, dataSource);
}
} finally {
monitor.done();
}
if (monitor.isCanceled()) {
return;
}
// Fill global cache
synchronized (this) {
synchronized (objectCache) {
if (forParent != null || !parentObjectMap.isEmpty()) {
if (forParent == null) {
// Cache global object list
List<OBJECT> globalCache = new ArrayList<>();
for (Map<String, ObjectInfo> objMap : parentObjectMap.values()) {
if (objMap != null) {
for (ObjectInfo info : objMap.values()) {
if (!info.broken) {
globalCache.add(info.object);
}
}
}
}
// Save precached objects in global cache
for (List<OBJECT> objects : objectCache.values()) {
globalCache.addAll(objects);
}
// Add precached objects to global cache too
super.setCache(globalCache);
this.invalidateObjects(monitor, owner, new CacheIterator());
}
}
// All objects are read. Now assign them to parents
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
if (colEntry.getValue() == null || objectCache.containsKey(colEntry.getKey())) {
// Do not overwrite this object's cache
continue;
}
Collection<ObjectInfo> objectInfos = colEntry.getValue().values();
ArrayList<OBJECT> objects = new ArrayList<>(objectInfos.size());
for (ObjectInfo objectInfo : objectInfos) {
objectInfo.needsCaching = true;
objects.add(objectInfo.object);
}
objectCache.put(colEntry.getKey(), objects);
}
// Now set empty object list for other parents
if (forParent == null) {
for (PARENT tmpParent : parentCache.getTypedObjects(monitor, owner, parentType)) {
if (!parentObjectMap.containsKey(tmpParent) && !objectCache.containsKey(tmpParent)) {
objectCache.put(tmpParent, new ArrayList<OBJECT>());
}
}
} else if (!parentObjectMap.containsKey(forParent) && !objectCache.containsKey(forParent)) {
objectCache.put(forParent, new ArrayList<OBJECT>());
}
}
// Cache children lists (we do it in the end because children caching may operate with other model objects)
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
for (ObjectInfo objectInfo : colEntry.getValue().values()) {
if (objectInfo.needsCaching) {
cacheChildren(monitor, objectInfo.object, objectInfo.rows);
}
}
}
for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
for (ObjectInfo objectInfo : colEntry.getValue().values()) {
if (objectInfo.needsCaching) {
cacheChildren2(monitor, objectInfo.object, objectInfo.rows);
}
}
}
}
}
Aggregations