use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.
the class ResultSetUtils method bindAttributes.
public static void bindAttributes(DBCSession session, DBCResultSet resultSet, DBDAttributeBindingMeta[] bindings, List<Object[]> rows) throws DBException {
final DBRProgressMonitor monitor = session.getProgressMonitor();
final DBPDataSource dataSource = session.getDataSource();
boolean readMetaData = dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.RESULT_SET_READ_METADATA);
if (!readMetaData) {
return;
}
boolean readReferences = dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.RESULT_SET_READ_REFERENCES);
final Map<DBCEntityMetaData, DBSEntity> entityBindingMap = new IdentityHashMap<>();
monitor.beginTask("Discover resultset metadata", 3);
try {
SQLQuery sqlQuery = null;
DBSEntity entity = null;
DBCStatement sourceStatement = resultSet.getSourceStatement();
if (sourceStatement != null && sourceStatement.getStatementSource() != null) {
DBCExecutionSource executionSource = sourceStatement.getStatementSource();
monitor.subTask("Discover owner entity");
DBSDataContainer dataContainer = executionSource.getDataContainer();
if (dataContainer instanceof DBSEntity) {
entity = (DBSEntity) dataContainer;
}
DBCEntityMetaData entityMeta = null;
if (entity == null) {
// Discover from entity metadata
Object sourceDescriptor = executionSource.getSourceDescriptor();
if (sourceDescriptor instanceof SQLQuery) {
sqlQuery = (SQLQuery) sourceDescriptor;
entityMeta = sqlQuery.getSingleSource();
}
if (entityMeta != null) {
entity = getEntityFromMetaData(monitor, dataSource, entityMeta);
if (entity != null) {
entityBindingMap.put(entityMeta, entity);
}
}
}
}
final Map<DBSEntity, DBDRowIdentifier> locatorMap = new IdentityHashMap<>();
monitor.subTask("Discover attributes");
for (DBDAttributeBindingMeta binding : bindings) {
monitor.subTask("Discover attribute '" + binding.getName() + "'");
DBCAttributeMetaData attrMeta = binding.getMetaAttribute();
// We got table name and column name
// To be editable we need this resultset contain set of columns from the same table
// which construct any unique key
DBSEntity attrEntity = null;
final DBCEntityMetaData attrEntityMeta = attrMeta.getEntityMetaData();
if (attrEntityMeta != null) {
attrEntity = entityBindingMap.get(attrEntityMeta);
if (attrEntity == null) {
if (entity != null && entity instanceof DBSTable && ((DBSTable) entity).isView()) {
// If this is a view then don't try to detect entity for each attribute
// MySQL returns source table name instead of view name. That's crazy.
attrEntity = entity;
} else {
attrEntity = getEntityFromMetaData(monitor, dataSource, attrEntityMeta);
}
}
if (attrEntity != null) {
entityBindingMap.put(attrEntityMeta, attrEntity);
}
}
if (attrEntity == null) {
attrEntity = entity;
}
if (attrEntity == null) {
if (attrEntityMeta != null) {
log.debug("Table '" + DBUtils.getSimpleQualifiedName(attrEntityMeta.getCatalogName(), attrEntityMeta.getSchemaName(), attrEntityMeta.getEntityName()) + "' not found in metadata catalog");
}
} else {
DBDPseudoAttribute pseudoAttribute = DBUtils.getPseudoAttribute(attrEntity, attrMeta.getName());
if (pseudoAttribute != null) {
binding.setPseudoAttribute(pseudoAttribute);
}
DBSEntityAttribute tableColumn;
if (binding.getPseudoAttribute() != null) {
tableColumn = binding.getPseudoAttribute().createFakeAttribute(attrEntity, attrMeta);
} else {
tableColumn = attrEntity.getAttribute(monitor, attrMeta.getName());
}
if (sqlQuery != null) {
if (tableColumn != null && tableColumn.getTypeID() != attrMeta.getTypeID()) {
// There should be a better solution but for now let's just disable this too smart feature.
continue;
}
/*
final SQLSelectItem selectItem = sqlQuery.getSelectItem(attrMeta.getName());
if (selectItem != null && !selectItem.isPlainColumn()) {
// It is not a column.
// It maybe an expression, function or anything else
continue;
}
*/
}
if (tableColumn != null && binding.setEntityAttribute(tableColumn)) {
// E.g. we fetched strings and found out that we should handle them as LOBs or enums.
try {
int pos = attrMeta.getOrdinalPosition();
for (Object[] row : rows) {
row[pos] = binding.getValueHandler().getValueFromObject(session, tableColumn, row[pos], false);
}
} catch (DBCException e) {
log.warn("Error resolving attribute '" + binding.getName() + "' values", e);
}
}
}
}
monitor.worked(1);
// Init row identifiers
monitor.subTask("Detect unique identifiers");
for (DBDAttributeBindingMeta binding : bindings) {
//monitor.subTask("Find attribute '" + binding.getName() + "' identifier");
DBSEntityAttribute attr = binding.getEntityAttribute();
if (attr == null) {
continue;
}
DBSEntity attrEntity = attr.getParentObject();
if (attrEntity != null) {
DBDRowIdentifier rowIdentifier = locatorMap.get(attrEntity);
if (rowIdentifier == null) {
DBSEntityReferrer entityIdentifier = getBestIdentifier(monitor, attrEntity, bindings);
if (entityIdentifier != null) {
rowIdentifier = new DBDRowIdentifier(attrEntity, entityIdentifier);
locatorMap.put(attrEntity, rowIdentifier);
}
}
binding.setRowIdentifier(rowIdentifier);
}
}
monitor.worked(1);
if (readReferences) {
monitor.subTask("Late bindings");
// Read nested bindings
for (DBDAttributeBinding binding : bindings) {
binding.lateBinding(session, rows);
}
}
monitor.subTask("Complete metadata load");
// Reload attributes in row identifiers
for (DBDRowIdentifier rowIdentifier : locatorMap.values()) {
rowIdentifier.reloadAttributes(monitor, bindings);
}
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.
the class ResultSetUtils method getAttributeValueFromClipboard.
@Nullable
public static Object getAttributeValueFromClipboard(DBDAttributeBinding attribute) throws DBCException {
DBPDataSource dataSource = attribute.getDataSource();
Clipboard clipboard = new Clipboard(Display.getCurrent());
try (DBCSession session = DBUtils.openUtilSession(VoidProgressMonitor.INSTANCE, dataSource, "Copy from clipboard")) {
String strValue = (String) clipboard.getContents(TextTransfer.getInstance());
return attribute.getValueHandler().getValueFromObject(session, attribute.getAttribute(), strValue, true);
} finally {
clipboard.dispose();
}
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.
the class NavigatorHandlerObjectGoto method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
DBCExecutionContext context = null;
DBSObject container = null;
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof DBPContextProvider) {
context = ((DBPContextProvider) activePart).getExecutionContext();
} else if (activePart instanceof INavigatorModelView) {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) element).getObject();
if (object != null) {
container = object;
while (container instanceof DBSFolder) {
container = container.getParentObject();
}
DBPDataSource dataSource = object.getDataSource();
if (dataSource != null) {
context = dataSource.getDefaultContext(true);
}
}
}
}
}
if (context == null) {
DBUserInterface.getInstance().showError("Go to object", "No active datasource");
return null;
}
IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
dialog.open();
Object[] objectsToOpen = dialog.getResult();
if (!ArrayUtils.isEmpty(objectsToOpen)) {
Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
for (DBNDatabaseNode node : nodes) {
NavigatorUtils.openNavigatorNode(node, workbenchWindow);
}
}
return null;
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.
the class ExplainPlanViewer method explainQueryPlan.
public void explainQueryPlan(DBCExecutionContext executionContext, SQLQuery query) throws DBCException {
this.executionContext = executionContext;
this.query = query;
if (this.executionContext != null) {
DBPDataSource dataSource = executionContext.getDataSource();
planner = DBUtils.getAdapter(DBCQueryPlanner.class, dataSource);
} else {
planner = null;
}
planTree.clearListData();
refreshPlanAction.setEnabled(false);
if (planner == null) {
throw new DBCException("This datasource doesn't support execution plans");
}
if (planTree.isLoading()) {
UIUtils.showMessageBox(getControl().getShell(), "Can't explain plan", "Explain plan already running", SWT.ICON_ERROR);
return;
}
sqlText.setText(query.getQuery());
planTree.init(this.executionContext, planner, query.getQuery());
planTree.loadData();
refreshPlanAction.setEnabled(true);
toggleViewAction.setEnabled(true);
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.
the class AbstractDataSourceHandler method getExecutionContext.
protected DBCExecutionContext getExecutionContext(ExecutionEvent event, boolean useEditor) {
if (useEditor) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof DBPContextProvider) {
return ((DBPContextProvider) editor).getExecutionContext();
}
return null;
}
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof DBPContextProvider) {
return ((DBPContextProvider) activePart).getExecutionContext();
}
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
DBSObject selectedObject = NavigatorUtils.getSelectedObject((IStructuredSelection) selection);
if (selectedObject != null) {
DBPDataSource dataSource = selectedObject.getDataSource();
if (dataSource != null) {
return dataSource.getDefaultContext(false);
}
}
}
return null;
}
Aggregations