Search in sources :

Example 86 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.

the class DataExporterXLSX method printHeader.

private void printHeader(Worksheet wsh) {
    boolean hasDescription = false;
    if (showDescription) {
        for (DBDAttributeBinding column : columns) {
            if (!CommonUtils.isEmpty(column.getDescription())) {
                hasDescription = true;
                break;
            }
        }
    }
    SXSSFSheet sh = (SXSSFSheet) wsh.getSh();
    Row row = sh.createRow(wsh.getCurrentRow());
    int startCol = rowNumber ? 1 : 0;
    for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
        sh.trackColumnForAutoSizing(i);
        DBDAttributeBinding column = columns.get(i);
        String colName = column.getLabel();
        if (CommonUtils.isEmpty(colName)) {
            colName = column.getName();
        }
        Cell cell = row.createCell(i + startCol, CellType.STRING);
        cell.setCellValue(colName);
        cell.setCellStyle(styleHeader);
    }
    if (hasDescription) {
        wsh.incRow();
        Row descRow = sh.createRow(wsh.getCurrentRow());
        for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
            Cell descCell = descRow.createCell(i + startCol, CellType.STRING);
            String description = columns.get(i).getDescription();
            if (CommonUtils.isEmpty(description)) {
                description = "";
            }
            descCell.setCellValue(description);
            descCell.setCellStyle(styleHeader);
        }
    }
    for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
        sh.autoSizeColumn(i);
    }
    wsh.incRow();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SXSSFSheet(org.apache.poi.xssf.streaming.SXSSFSheet)

Example 87 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.

the class GenerateSQLContributor method makeResultSetContributions.

private void makeResultSetContributions(List<IContributionItem> menu, IResultSetSelection rss) {
    final IResultSetController rsv = rss.getController();
    DBSDataContainer dataContainer = rsv.getDataContainer();
    final List<DBDAttributeBinding> visibleAttributes = rsv.getModel().getVisibleAttributes();
    final DBSEntity entity = rsv.getModel().getSingleSource();
    if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null) {
        final List<ResultSetRow> selectedRows = new ArrayList<>(rss.getSelectedRows());
        if (!CommonUtils.isEmpty(selectedRows)) {
            menu.add(makeAction("SELECT .. WHERE .. =", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        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(entity));
                        sql.append(getLineSeparator()).append("WHERE ");
                        hasAttr = false;
                        for (DBDAttributeBinding binding : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            appendValueCondition(rsv, sql, binding, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
            if (selectedRows.size() > 1) {
                menu.add(makeAction("SELECT .. WHERE .. IN", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                    @Override
                    public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                        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(entity));
                        sql.append(getLineSeparator()).append("WHERE ");
                        boolean multiKey = keyAttributes.size() > 1;
                        if (multiKey)
                            sql.append("(");
                        hasAttr = false;
                        for (DBDAttributeBinding binding : keyAttributes) {
                            if (hasAttr)
                                sql.append(",");
                            sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML));
                            hasAttr = true;
                        }
                        if (multiKey)
                            sql.append(")");
                        sql.append(" IN (");
                        if (multiKey)
                            sql.append("\n");
                        for (int i = 0; i < selectedRows.size(); i++) {
                            ResultSetRow firstRow = selectedRows.get(i);
                            if (multiKey)
                                sql.append("(");
                            hasAttr = false;
                            for (DBDAttributeBinding binding : keyAttributes) {
                                if (hasAttr)
                                    sql.append(",");
                                appendAttributeValue(rsv, sql, binding, firstRow);
                                hasAttr = true;
                            }
                            if (multiKey)
                                sql.append(")");
                            if (i < selectedRows.size() - 1)
                                sql.append(",");
                            if (multiKey)
                                sql.append("\n");
                        }
                        sql.append(");\n");
                    }
                }));
            }
            menu.add(makeAction("INSERT", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<? extends DBSAttributeBase> allAttributes = getAllAttributes(monitor, object);
                        sql.append("INSERT INTO ").append(getEntityName(entity));
                        sql.append(getLineSeparator()).append("(");
                        boolean hasAttr = false;
                        for (DBSAttributeBase attr : allAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
                            hasAttr = true;
                        }
                        sql.append(")").append(getLineSeparator()).append("VALUES(");
                        hasAttr = false;
                        for (DBSAttributeBase attr : allAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
                            if (binding == null) {
                                appendDefaultValue(sql, attr);
                            } else {
                                appendAttributeValue(rsv, sql, binding, firstRow);
                            }
                            hasAttr = true;
                        }
                        sql.append(");\n");
                    }
                }
            }));
            menu.add(makeAction("UPDATE", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
                        Collection<? extends DBSAttributeBase> valueAttributes = getValueAttributes(monitor, object, keyAttributes);
                        sql.append("UPDATE ").append(getEntityName(entity));
                        sql.append(getLineSeparator()).append("SET ");
                        boolean hasAttr = false;
                        for (DBSAttributeBase attr : valueAttributes) {
                            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                                continue;
                            }
                            if (hasAttr)
                                sql.append(", ");
                            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
                            DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
                            if (binding == null) {
                                appendDefaultValue(sql, attr);
                            } else {
                                appendAttributeValue(rsv, sql, binding, firstRow);
                            }
                            hasAttr = true;
                        }
                        sql.append(getLineSeparator()).append("WHERE ");
                        hasAttr = false;
                        for (DBDAttributeBinding attr : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            appendValueCondition(rsv, sql, attr, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
            menu.add(makeAction("DELETE by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {

                @Override
                public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
                    for (ResultSetRow firstRow : selectedRows) {
                        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
                        sql.append("DELETE FROM ").append(getEntityName(entity));
                        sql.append(getLineSeparator()).append("WHERE ");
                        boolean hasAttr = false;
                        for (DBDAttributeBinding binding : keyAttributes) {
                            if (hasAttr)
                                sql.append(" AND ");
                            appendValueCondition(rsv, sql, binding, firstRow);
                            hasAttr = true;
                        }
                        sql.append(";\n");
                    }
                }
            }));
        }
    } else {
        // if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null)
        String message = dataContainer == null ? "no data container" : (visibleAttributes.isEmpty() ? "empty attribute list" : "can't resolve table");
        Action disabledAction = new Action("Not available - " + message) {
        };
        disabledAction.setEnabled(false);
        menu.add(new ActionContributionItem(disabledAction));
    }
}
Also used : ResultSetModel(org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 88 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.

the class GenerateUUIDHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart == null) {
        return null;
    }
    IResultSetController rsc = activePart.getAdapter(IResultSetController.class);
    if (rsc != null && UIUtils.hasFocus(rsc.getControl())) {
        IResultSetSelection selection = rsc.getSelection();
        if (selection != null && !selection.isEmpty()) {
            for (Object cell : selection.toArray()) {
                DBDAttributeBinding attr = selection.getElementAttribute(cell);
                ResultSetRow row = selection.getElementRow(cell);
                if (row != null && attr != null) {
                    ResultSetValueController valueController = new ResultSetValueController(rsc, attr, row, IValueController.EditType.NONE, null);
                    DBDValueHandler valueHandler = valueController.getValueHandler();
                    String uuid = generateUUID();
                    valueController.updateValue(uuid, false);
                }
            }
            rsc.redrawData(false, false);
            rsc.updateEditControls();
        }
    } else {
        ITextViewer textViewer = activePart.getAdapter(ITextViewer.class);
        if (textViewer != null) {
            ISelection selection = textViewer.getSelectionProvider().getSelection();
            if (selection instanceof TextSelection) {
                try {
                    int offset = ((TextSelection) selection).getOffset();
                    int length = ((TextSelection) selection).getLength();
                    String uuid = generateUUID();
                    textViewer.getDocument().replace(offset, length, uuid);
                    textViewer.getSelectionProvider().setSelection(new TextSelection(offset + uuid.length(), 0));
                } catch (BadLocationException e) {
                    DBWorkbench.getPlatformUI().showError("Insert UUID", "Error inserting UUID in text editor", e);
                }
            }
        }
    }
    return null;
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) IResultSetSelection(org.jkiss.dbeaver.ui.controls.resultset.IResultSetSelection) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) ITextViewer(org.eclipse.jface.text.ITextViewer) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) ResultSetValueController(org.jkiss.dbeaver.ui.controls.resultset.ResultSetValueController) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 89 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.

the class ConsoleTextPresentation method printRecord.

private void printRecord() {
    text.append("print record ...\n");
    DBPPreferenceStore prefs = getController().getPreferenceStore();
    boolean delimLeading = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_LEADING);
    boolean delimTrailing = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_TRAILING);
    DBDDisplayFormat displayFormat = DBDDisplayFormat.safeValueOf(prefs.getString(ResultSetPreferences.RESULT_TEXT_VALUE_FORMAT));
    StringBuilder grid = new StringBuilder(512);
    ResultSetModel model = controller.getModel();
    List<DBDAttributeBinding> attrs = model.getVisibleAttributes();
    String[] values = new String[attrs.size()];
    ResultSetRow currentRow = controller.getCurrentRow();
    // Calculate column widths
    int nameWidth = 4, valueWidth = 5;
    for (int i = 0; i < attrs.size(); i++) {
        DBDAttributeBinding attr = attrs.get(i);
        nameWidth = Math.max(nameWidth, getAttributeName(attr).length());
        if (currentRow != null) {
            String displayString = getCellString(model, attr, currentRow, displayFormat);
            values[i] = displayString;
            valueWidth = Math.max(valueWidth, values[i].length());
        }
    }
    // Header
    if (delimLeading)
        grid.append("|");
    grid.append("Name");
    for (int j = nameWidth - 4; j > 0; j--) {
        grid.append(" ");
    }
    grid.append("|Value");
    for (int j = valueWidth - 5; j > 0; j--) {
        grid.append(" ");
    }
    if (delimTrailing)
        grid.append("|");
    grid.append("\n");
    if (delimLeading)
        grid.append("|");
    for (int j = 0; j < nameWidth; j++) grid.append("-");
    grid.append("|");
    for (int j = 0; j < valueWidth; j++) grid.append("-");
    if (delimTrailing)
        grid.append("|");
    grid.append("\n");
    if (currentRow != null) {
        // Values
        for (int i = 0; i < attrs.size(); i++) {
            DBDAttributeBinding attr = attrs.get(i);
            String name = getAttributeName(attr);
            if (delimLeading)
                grid.append("|");
            grid.append(name);
            for (int j = nameWidth - name.length(); j > 0; j--) {
                grid.append(" ");
            }
            grid.append("|");
            grid.append(values[i]);
            for (int j = valueWidth - values[i].length(); j > 0; j--) {
                grid.append(" ");
            }
            if (delimTrailing)
                grid.append("|");
            grid.append("\n");
        }
    }
    // cut last line feed
    grid.setLength(grid.length() - 1);
    text.append(grid.toString());
// text.setText(grid.toString());
}
Also used : DBDDisplayFormat(org.jkiss.dbeaver.model.data.DBDDisplayFormat) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

Example 90 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by dbeaver.

the class GeometryDataUtils method setGeometryProperties.

public static void setGeometryProperties(IResultSetController controller, GeomAttrs geomAttrs, DBGeometry geometry, ResultSetRow row) {
    // Now extract all geom values from data
    ResultSetModel model = controller.getModel();
    if (row != null) {
        // Now get description
        if (!geomAttrs.descAttrs.isEmpty()) {
            Map<String, Object> properties = new LinkedHashMap<>();
            for (DBDAttributeBinding da : geomAttrs.descAttrs) {
                Object descValue = model.getCellValue(da, row);
                if (!DBUtils.isNullValue(descValue)) {
                    properties.put(da.getName(), descValue);
                }
            }
            geometry.setProperties(properties);
        }
    }
}
Also used : ResultSetModel(org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)147 ArrayList (java.util.ArrayList)32 DBException (org.jkiss.dbeaver.DBException)29 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)24 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)23 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)23 List (java.util.List)18 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)16 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)14 Reader (java.io.Reader)13 PrintWriter (java.io.PrintWriter)12 GridData (org.eclipse.swt.layout.GridData)12 Nullable (org.jkiss.code.Nullable)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 Date (java.util.Date)10 SWT (org.eclipse.swt.SWT)10 NotNull (org.jkiss.code.NotNull)10 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)10 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)10 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)10