use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class ReferencesResultsContainer method refreshReferenceKeyList.
/**
* Load list of referencing keys
*/
private void refreshReferenceKeyList() {
activeReferenceKey = null;
referenceKeys.clear();
UIUtils.syncExec(() -> {
dataViewer.clearData();
fkCombo.removeAll();
dataViewer.showEmptyPresentation();
});
List<DBDAttributeBinding> visibleAttributes = parentController.getModel().getVisibleAttributes();
if (visibleAttributes.isEmpty()) {
return;
}
parentDataContainer = parentController.getDataContainer();
if (parentDataContainer == null) {
return;
}
Set<DBSEntity> allEntities = new LinkedHashSet<>();
for (DBDAttributeBinding attr : visibleAttributes) {
DBSEntityAttribute entityAttribute = attr.getEntityAttribute();
if (entityAttribute != null) {
allEntities.add(entityAttribute.getParentObject());
}
}
if (allEntities.isEmpty() && parentDataContainer instanceof DBSEntity) {
allEntities.add((DBSEntity) parentDataContainer);
}
List<ReferenceKeyMemo> refKeyMemos = new ArrayList<>();
{
DBVEntity vEntityOwner = DBVUtils.getVirtualEntity(parentDataContainer, false);
if (vEntityOwner != null) {
Object activeAssociations = vEntityOwner.getProperty(V_PROP_ACTIVE_ASSOCIATIONS);
if (activeAssociations instanceof Collection) {
for (Object refKeyMemoMap : (Collection) activeAssociations) {
if (refKeyMemoMap instanceof Map) {
refKeyMemos.add(new ReferenceKeyMemo((Map) refKeyMemoMap));
}
}
}
}
}
if (!allEntities.isEmpty()) {
new AbstractJob("Load reference keys") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
monitor.beginTask("Load references", allEntities.size());
try {
List<ReferenceKey> refs = new ArrayList<>();
for (DBSEntity entity : allEntities) {
monitor.subTask(entity.getName());
if (entity instanceof DBVEntity) {
// Skip virtual entities
continue;
}
// Foreign keys
Collection<? extends DBSEntityAssociation> associations = DBVUtils.getAllAssociations(monitor, entity);
for (DBSEntityAssociation assoc : associations) {
if (assoc instanceof DBSEntityReferrer) {
List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
if (!CommonUtils.isEmpty(attrs)) {
ReferenceKey referenceKey = new ReferenceKey(monitor, false, assoc.getAssociatedEntity(), assoc, attrs);
refs.add(referenceKey);
}
}
}
// References
Collection<? extends DBSEntityAssociation> references = DBVUtils.getAllReferences(monitor, entity);
{
for (DBSEntityAssociation assoc : references) {
if (assoc instanceof DBSEntityReferrer) {
List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
if (!CommonUtils.isEmpty(attrs)) {
ReferenceKey referenceKey = new ReferenceKey(monitor, true, entity, assoc, attrs);
refs.add(referenceKey);
}
}
}
}
monitor.worked(1);
}
synchronized (referenceKeys) {
referenceKeys.clear();
referenceKeys.addAll(refs);
// Detect active ref key from memo
if (!referenceKeys.isEmpty()) {
if (!refKeyMemos.isEmpty()) {
for (ReferenceKey key : referenceKeys) {
for (ReferenceKeyMemo memo : refKeyMemos) {
if (key.matches(memo)) {
activeReferenceKey = key;
break;
}
}
if (activeReferenceKey != null)
break;
}
}
if (activeReferenceKey == null) {
activeReferenceKey = referenceKeys.get(0);
}
}
}
UIUtils.syncExec(() -> fillKeysCombo());
} catch (DBException e) {
log.debug("Error reading references", e);
// Do not show errors. References or FKs may be unsupported by current database
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
}.schedule();
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class StatisticsPresentation method refreshData.
@Override
public void refreshData(boolean refreshMetadata, boolean append, boolean keepState) {
table.removeAll();
if (controller.getModel().getRowCount() > 0) {
ResultSetRow row = controller.getModel().getRow(0);
java.util.List<DBDAttributeBinding> visibleAttributes = controller.getModel().getVisibleAttributes();
for (int i = 0; i < visibleAttributes.size(); i++) {
DBDAttributeBinding attr = visibleAttributes.get(i);
Object value = row.getValues()[i];
String valueString = DBValueFormatting.getDefaultValueDisplayString(value, DBDDisplayFormat.UI);
String[] lines = valueString.split("\n");
for (int k = 0; k < lines.length; k++) {
String line = lines[k];
if (CommonUtils.isEmptyTrimmed(line)) {
continue;
}
line = line.replace("\t", " ");
TableItem item = new TableItem(table, SWT.LEFT);
item.setText(0, k == 0 ? attr.getName() : "");
item.setText(1, line);
item.setData(attr);
}
}
}
UIUtils.packColumns(table);
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class SQLGeneratorSelectFromData method generateSQL.
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) {
for (ResultSetRow firstRow : getSelectedRows()) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("SELECT ");
boolean hasAttr = false;
for (DBSAttributeBase attr : getAllAttributes(monitor, object)) {
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append(getLineSeparator()).append("FROM ").append(getEntityName(getSingleEntity()));
sql.append(getLineSeparator()).append("WHERE ");
hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
appendValueCondition(getController(), sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class SQLGeneratorDeleteFromData method generateSQL.
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) {
for (ResultSetRow firstRow : getSelectedRows()) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("DELETE FROM ").append(getEntityName(getSingleEntity()));
sql.append(getLineSeparator()).append("WHERE ");
boolean hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
appendValueCondition(getController(), sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.
the class SpreadsheetFindReplaceTarget method replaceSelection.
@Override
public void replaceSelection(String text, boolean regExReplace) {
GridPos selection = (GridPos) owner.getSelection().getFirstElement();
if (selection == null) {
return;
}
GridCell cell = owner.getSpreadsheet().posToCell(selection);
if (cell == null) {
return;
}
String oldValue = CommonUtils.toString(owner.getSpreadsheet().getContentProvider().getCellValue(cell.col, cell.row, true, true));
String newValue = text;
if (searchPattern != null) {
newValue = searchPattern.matcher(oldValue).replaceAll(newValue);
}
boolean recordMode = owner.getController().isRecordMode();
final DBDAttributeBinding attr = (DBDAttributeBinding) (recordMode ? cell.row : cell.col);
final ResultSetRow row = (ResultSetRow) (recordMode ? cell.col : cell.row);
owner.getController().getModel().updateCellValue(attr, row, newValue);
owner.getController().updatePanelsContent(false);
}
Aggregations