Search in sources :

Example 41 with DBDAttributeBinding

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

the class DatabaseMappingContainer method readAttributes.

private void readAttributes(DBRProgressMonitor monitor) throws DBException {
    if (source instanceof DBSEntity && !(source instanceof DBSDocumentContainer)) {
        for (DBSEntityAttribute attr : CommonUtils.safeCollection(((DBSEntity) source).getAttributes(monitor))) {
            if (DBUtils.isHiddenObject(attr)) {
                continue;
            }
            addAttributeMapping(monitor, attr);
        }
    } else {
        // Seems to be a dynamic query. Execute it to get metadata
        DBPDataSource dataSource = source.getDataSource();
        assert (dataSource != null);
        DBCExecutionContext context;
        if (source instanceof DBPContextProvider) {
            context = ((DBPContextProvider) source).getExecutionContext();
        } else {
            context = DBUtils.getDefaultContext(source, false);
        }
        if (context == null) {
            throw new DBCException("No execution context");
        }
        DBExecUtils.tryExecuteRecover(monitor, context.getDataSource(), monitor1 -> {
            try (DBCSession session = context.openSession(monitor1, DBCExecutionPurpose.META, "Read query meta data")) {
                MetadataReceiver receiver = new MetadataReceiver();
                try {
                    source.readData(new AbstractExecutionSource(source, session.getExecutionContext(), this), session, receiver, null, 0, 1, DBSDataContainer.FLAG_NONE, 1);
                    for (DBDAttributeBinding attr : receiver.attributes) {
                        if (DBUtils.isHiddenObject(attr)) {
                            continue;
                        }
                        addAttributeMapping(monitor1, attr);
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    }
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource)

Example 42 with DBDAttributeBinding

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

the class DataExporterHTML method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    out.write("<tr" + (rowCount++ % 2 == 0 ? " class=\"odd\"" : "") + ">");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns[i];
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null, false);
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                out.write("<td>");
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        writeCellValue(cs.getContentReader());
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
                out.write("</td>");
            } finally {
                content.release();
            }
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            boolean isImage = row[i] instanceof File && stringValue != null && stringValue.endsWith(".jpg");
            if (isImage) {
                writeImageCell((File) row[i]);
            } else {
                writeTextCell(stringValue, false);
            }
        }
    }
    out.write("</tr>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) PrintWriter(java.io.PrintWriter)

Example 43 with DBDAttributeBinding

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

the class DataExporterMarkdownTable method printHeader.

private void printHeader(boolean separator) {
    if (confluenceFormat)
        writeDelimiter();
    writeDelimiter();
    for (int i = 0, columnsSize = columns.length; i < columnsSize; i++) {
        DBDAttributeBinding column = columns[i];
        String colName = column.getLabel();
        if (CommonUtils.isEmpty(colName)) {
            colName = column.getName();
        }
        if (!separator) {
            writeCellValue(colName);
        } else {
            for (int k = 0; k < colName.length(); k++) {
                getWriter().write('-');
            }
        }
        writeDelimiter();
        if (confluenceFormat)
            writeDelimiter();
    }
    writeRowLimit();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 44 with DBDAttributeBinding

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

the class DataExporterSourceCode method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    if (rowNum > 0) {
        out.write("," + rowDelimiter);
    }
    rowNum++;
    if (language == ProgramLanguages.PHP_VERSION_LESS_5_and_4) {
        out.write("\tarray(" + rowDelimiter);
    } else {
        out.write("\t[" + rowDelimiter);
    }
    for (int i = 0; i < columns.length; i++) {
        DBDAttributeBinding column = columns[i];
        String columnName = column.getLabel();
        if (CommonUtils.isEmpty(columnName)) {
            columnName = column.getName();
        }
        out.write("\t\t" + quoteChar + JSONUtils.escapeJsonString(columnName) + quoteChar + " => ");
        Object cellValue = row[column.getOrdinalPosition()];
        if (DBUtils.isNullValue(cellValue)) {
            writeTextCell(null);
        } else if (cellValue instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) cellValue;
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader in = cs.getContentReader()) {
                            out.write(quoteChar);
                            writeCellValue(in);
                            out.write(quoteChar);
                        }
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
            } finally {
                content.release();
            }
        } else {
            if (cellValue instanceof Number || cellValue instanceof Boolean) {
                out.write(cellValue.toString());
            } else if (cellValue instanceof Date && formatDateISO) {
                writeTextCell(JSONUtils.formatDate((Date) cellValue));
            } else {
                writeTextCell(super.getValueDisplayString(column, cellValue));
            }
        }
        if (i < columns.length - 1) {
            out.write(",");
        }
        out.write(rowDelimiter);
    }
    if (language == ProgramLanguages.PHP_VERSION_LESS_5_and_4) {
        out.write("\t)");
    } else {
        out.write("\t]");
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) Reader(java.io.Reader) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 45 with DBDAttributeBinding

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

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)

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