use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class JDBCObjectLookupCache 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(ex, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
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("Not connected to database");
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "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()) {
break;
}
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 object = forObject;
if (object == null) {
object = super.getCachedObject(objectName);
if (object == null) {
log.debug("Object '" + objectName + "' not found");
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<CHILD>());
}
}
this.childrenCached = true;
}
} else if (!objectMap.containsKey(forObject)) {
cacheChildren(forObject, new ArrayList<CHILD>());
}
} finally {
dbResult.close();
}
}
}
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class DB2Utils method checkExplainTables.
public static Boolean checkExplainTables(DBRProgressMonitor monitor, DB2DataSource dataSource, String explainTableSchemaName) throws DBCException {
LOG.debug("Check EXPLAIN tables in '" + explainTableSchemaName + "'");
monitor.beginTask("Check EXPLAIN tables", 1);
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Verify EXPLAIN tables")) {
// First Check with given schema
try (JDBCCallableStatement stmtSP = session.prepareCall(CALL_INST_OBJ)) {
// EXPLAIN
stmtSP.setString(1, "EXPLAIN");
// Verify
stmtSP.setString(2, "V");
// Tablespace
stmtSP.setString(3, "");
// Schema
stmtSP.setString(4, explainTableSchemaName);
stmtSP.execute();
LOG.debug("EXPLAIN tables with schema " + explainTableSchemaName + " found.");
return true;
} catch (SQLException e) {
LOG.debug("RC:" + e.getErrorCode() + " SQLState:" + e.getSQLState() + " " + e.getMessage());
if (e.getErrorCode() == CALL_INST_OBJ_BAD_RC) {
LOG.debug("No valid EXPLAIN tables found in schema '" + explainTableSchemaName + "'.");
return false;
}
throw new DBCException(e, dataSource);
}
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class DB2DataSource method initialize.
// -----------------------
// Initialisation/Structure
// -----------------------
@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
super.initialize(monitor);
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load data source meta info")) {
// First try to get active schema from special register 'CURRENT SCHEMA'
this.activeSchemaName = determineActiveSchema(session);
this.db2CurrentUserPrivileges = new DB2CurrentUserPrivileges(monitor, session, activeSchemaName, this);
} catch (SQLException e) {
LOG.warn("Error reading active schema", e);
}
try {
this.dataTypeCache.getAllObjects(monitor, this);
} catch (DBException e) {
LOG.warn("Error reading types info", e);
this.dataTypeCache.setCache(Collections.<DB2DataType>emptyList());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class DB2IndexColumn method getDependentView.
// -----------------
// Helpers
// -----------------
private DB2View getDependentView(DBRProgressMonitor monitor, DB2DataSource db2DataSource, String indexSchema, String indexName) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, db2DataSource, "Read Index view dependency")) {
try (JDBCPreparedStatement stmtSel = session.prepareStatement(I_DEP)) {
stmtSel.setString(1, indexSchema);
stmtSel.setString(2, indexName);
JDBCResultSet dbResult = stmtSel.executeQuery();
if (dbResult.next()) {
String viewSchema = dbResult.getString("BSCHEMA").trim();
String viewName = dbResult.getString("BNAME");
return DB2Utils.findViewBySchemaNameAndName(monitor, db2DataSource, viewSchema, viewName);
} else {
return null;
}
}
} catch (SQLException e) {
throw new DBException(e, db2DataSource);
}
}
Aggregations