Search in sources :

Example 46 with DBDAttributeBinding

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

the class ExecuteBatchImpl method processBatch.

/**
 * Execute batch OR generate batch script.
 * @param session    session
 * @param actions    script actions. If not null then no execution will be done
 * @param options
 * @return execution statistics
 * @throws DBCException
 */
@NotNull
private DBCStatistics processBatch(@NotNull DBCSession session, @Nullable List<DBEPersistAction> actions, Map<String, Object> options) throws DBCException {
    // session.getProgressMonitor().subTask("Save batch (" + values.size() + ")");
    DBDValueHandler[] handlers = new DBDValueHandler[attributes.length];
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i] instanceof DBDAttributeBinding) {
            handlers[i] = ((DBDAttributeBinding) attributes[i]).getValueHandler();
        } else {
            handlers[i] = DBUtils.findValueHandler(session, attributes[i]);
        }
    }
    boolean useBatch = session.getDataSource().getInfo().supportsBatchUpdates() && reuseStatement && Boolean.FALSE.equals(options.get(DBSDataManipulator.OPTION_DISABLE_BATCHES));
    if (values.size() <= 1) {
        useBatch = false;
    }
    DBCStatistics statistics = new DBCStatistics();
    DBCStatement statement = null;
    try {
        // Here we'll try to reuse prepared statement.
        // It makes a great sense in case of data transfer where we need millions of inserts.
        // We must be aware of nulls because actual insert statements may differ depending on null values.
        // So if row nulls aren't the same as in previous row we need to prepare new statement and restart batch.
        // Quite complicated but works.
        boolean[] prevNulls = new boolean[attributes.length];
        boolean[] nulls = new boolean[attributes.length];
        int statementsInBatch = 0;
        for (int rowIndex = 0; rowIndex < values.size(); rowIndex++) {
            Object[] rowValues = values.get(rowIndex);
            if (session.getProgressMonitor().isCanceled()) {
                break;
            }
            boolean reuse = reuseStatement;
            if (reuse) {
                for (int i = 0; i < rowValues.length; i++) {
                    nulls[i] = DBUtils.isNullValue(rowValues[i]);
                }
                if (!Arrays.equals(prevNulls, nulls) && statementsInBatch > 0) {
                    reuse = false;
                }
                System.arraycopy(nulls, 0, prevNulls, 0, nulls.length);
                if (!reuse && statementsInBatch > 0) {
                    // Flush batch
                    if (actions == null) {
                        flushBatch(statistics, statement);
                    }
                    statement.close();
                    statement = null;
                    statementsInBatch = 0;
                    reuse = true;
                }
            }
            if (statement == null || !reuse) {
                statement = prepareStatement(session, handlers, rowValues, options);
                statistics.setQueryText(statement.getQueryString());
                statistics.addStatementsCount();
            }
            try {
                bindStatement(handlers, statement, rowValues);
                if (actions == null) {
                    if (useBatch) {
                        statement.addToBatch();
                        statementsInBatch++;
                    } else {
                        // Execute each row separately
                        long startTime = System.currentTimeMillis();
                        executeStatement(statistics, statement);
                        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
                        long rowCount = statement.getUpdateRowCount();
                        if (rowCount > 0) {
                            statistics.addRowsUpdated(rowCount);
                        }
                        // Read keys
                        if (keysReceiver != null) {
                            try {
                                readKeys(statement.getSession(), statement, keysReceiver);
                            } catch (Exception e) {
                                log.warn("Error reading auto-generated keys", e);
                            }
                        }
                    }
                } else {
                    String queryString = formatQueryParameters(session, statement.getQueryString(), handlers, rowValues);
                    actions.add(new SQLDatabasePersistAction("Execute statement", queryString));
                }
            } finally {
                if (!reuse) {
                    statement.close();
                }
                if (rowIndex > 0 && rowIndex % 100 == 0) {
                    session.getProgressMonitor().subTask("Save batch (" + rowIndex + " of " + values.size() + ")");
                }
            }
        }
        values.clear();
        if (statementsInBatch > 0) {
            if (actions == null) {
                flushBatch(statistics, statement);
            }
            statement.close();
            statement = null;
        }
    } finally {
        if (reuseStatement && statement != null) {
            statement.close();
        }
        if (!useBatch && !values.isEmpty()) {
            values.clear();
        }
    }
    return statistics;
}
Also used : DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) NotNull(org.jkiss.code.NotNull)

Example 47 with DBDAttributeBinding

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

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetHandlerMain.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", "No active results viewer");
        return null;
    }
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    if (rsSelectedRows.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelection.getSelectedAttributes()) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", ModelMessages.error_not_connected_to_database);
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                tempFile.deleteOnExit();
                StreamExporterAbstract exporter = new DataExporterXLSX();
                StreamTransferConsumer consumer = new StreamTransferConsumer();
                StreamConsumerSettings settings = new StreamConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<String, Object> properties = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(true, false), exporter, properties);
                DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producerSettings.setSelectedRowsOnly(true);
                producerSettings.setSelectedColumnsOnly(true);
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamExporterAbstract(org.jkiss.dbeaver.tools.transfer.stream.exporter.StreamExporterAbstract) ArrayList(java.util.ArrayList) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 48 with DBDAttributeBinding

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

the class DataExporterXLSX method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    Worksheet wsh = getWsh(resultSet, row);
    Row rowX = wsh.getSh().createRow(wsh.getCurrentRow());
    int startCol = 0;
    if (rowNumber) {
        Cell cell = rowX.createCell(startCol, CellType.NUMERIC);
        cell.setCellStyle(style);
        cell.setCellValue(String.valueOf(wsh.getCurrentRow()));
        startCol++;
    }
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns[i];
        Cell cell = rowX.createCell(i + startCol, getCellType(column));
        cell.setCellStyle(style);
        if (DBUtils.isNullValue(row[i])) {
            if (!CommonUtils.isEmpty(nullString)) {
                cell.setCellValue(nullString);
            } else {
                cell.setCellValue("");
            }
        } else if (row[i] instanceof DBDContent) {
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs == null) {
                    cell.setCellValue(DBConstants.NULL_VALUE_LABEL);
                } else if (ContentUtils.isTextContent(content)) {
                    writeCellValue(cell, cs.getContentReader());
                } else {
                    cell.setCellValue(BINARY_FIXED);
                }
            } finally {
                content.release();
            }
        } else if (row[i] instanceof Boolean) {
            if (booleRedefined) {
                cell.setCellValue((Boolean) row[i] ? boolTrue : boolFalse);
            } else {
                cell.setCellValue((Boolean) row[i]);
            }
        } else if (row[i] instanceof Number) {
            cell.setCellValue(((Number) row[i]).doubleValue());
        } else if (row[i] instanceof Date) {
            cell.setCellValue((Date) row[i]);
            cell.setCellStyle(styleDate);
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            cell.setCellValue(stringValue);
        }
    }
    wsh.incRow();
    rowCount++;
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) Date(java.util.Date)

Example 49 with DBDAttributeBinding

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

the class GISLeafletViewer method saveAttributeSettings.

private void saveAttributeSettings() {
    if (valueController instanceof IAttributeController) {
        DBDAttributeBinding binding = ((IAttributeController) valueController).getBinding();
        if (binding.getEntityAttribute() != null) {
            DBVEntity vEntity = DBVUtils.getVirtualEntity(binding, true);
            DBVEntityAttribute vAttr = vEntity.getVirtualAttribute(binding, true);
            if (vAttr != null) {
                vAttr.setProperty(PROP_FLIP_COORDINATES, String.valueOf(flipCoordinates));
                vAttr.setProperty(PROP_SRID, String.valueOf(getValueSRID()));
            }
            valueController.getExecutionContext().getDataSource().getContainer().getRegistry().flushConfig();
        }
    }
}
Also used : DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) IAttributeController(org.jkiss.dbeaver.ui.data.IAttributeController) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 50 with DBDAttributeBinding

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

the class ConsoleTextPresentation method printGrid.

private void printGrid() {
    DBPPreferenceStore prefs = getController().getPreferenceStore();
    int maxColumnSize = prefs.getInt(ResultSetPreferences.RESULT_TEXT_MAX_COLUMN_SIZE);
    boolean delimLeading = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_LEADING);
    boolean delimTrailing = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_DELIMITER_TRAILING);
    boolean extraSpaces = prefs.getBoolean(ResultSetPreferences.RESULT_TEXT_EXTRA_SPACES);
    this.showNulls = getController().getPreferenceStore().getBoolean(ResultSetPreferences.RESULT_TEXT_SHOW_NULLS);
    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();
    grid.append("> ").append(controller.getDataContainer().getName()).append("\n");
    List<ResultSetRow> allRows = model.getAllRows();
    int extraSpacesNum = extraSpaces ? 2 : 0;
    if (colWidths == null) {
        // Calculate column widths
        colWidths = new int[attrs.size()];
        for (int i = 0; i < attrs.size(); i++) {
            DBDAttributeBinding attr = attrs.get(i);
            colWidths[i] = getAttributeName(attr).length() + extraSpacesNum;
            if (showNulls && !attr.isRequired()) {
                colWidths[i] = Math.max(colWidths[i], DBConstants.NULL_VALUE_LABEL.length());
            }
            for (ResultSetRow row : allRows) {
                String displayString = getCellString(model, attr, row, displayFormat);
                colWidths[i] = Math.max(colWidths[i], getStringWidth(displayString) + extraSpacesNum);
            }
        }
        for (int i = 0; i < colWidths.length; i++) {
            if (colWidths[i] > maxColumnSize) {
                colWidths[i] = maxColumnSize;
            }
        }
    }
    // Print header
    if (delimLeading)
        grid.append("|");
    for (int i = 0; i < attrs.size(); i++) {
        if (i > 0)
            grid.append("|");
        if (extraSpaces)
            grid.append(" ");
        DBDAttributeBinding attr = attrs.get(i);
        String attrName = getAttributeName(attr);
        grid.append(attrName);
        for (int k = colWidths[i] - attrName.length() - extraSpacesNum; k > 0; k--) {
            grid.append(" ");
        }
        if (extraSpaces)
            grid.append(" ");
    }
    if (delimTrailing)
        grid.append("|");
    grid.append("\n");
    // Print header
    if (delimLeading)
        grid.append("|");
    for (int i = 0; i < attrs.size(); i++) {
        if (i > 0)
            grid.append("|");
        for (int k = colWidths[i]; k > 0; k--) {
            grid.append("-");
        }
    }
    if (delimTrailing)
        grid.append("|");
    grid.append("\n");
    // Print rows
    int firstRow = totalRows;
    boolean newLines = false;
    for (int i = firstRow; i < allRows.size(); i++) {
        newLines = true;
        ResultSetRow row = allRows.get(i);
        if (delimLeading)
            grid.append("|");
        for (int k = 0; k < attrs.size(); k++) {
            if (k > 0)
                grid.append("|");
            DBDAttributeBinding attr = attrs.get(k);
            String displayString = getCellString(model, attr, row, displayFormat);
            if (displayString.length() >= colWidths[k]) {
                displayString = CommonUtils.truncateString(displayString, colWidths[k]);
            }
            int stringWidth = getStringWidth(displayString);
            if (extraSpaces)
                grid.append(" ");
            DBPDataKind dataKind = attr.getDataKind();
            if ((dataKind == DBPDataKind.NUMERIC && rightJustifyNumbers) || (dataKind == DBPDataKind.DATETIME && rightJustifyDateTime)) {
                // Right justify value
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
                grid.append(displayString);
            } else {
                grid.append(displayString);
                for (int j = colWidths[k] - stringWidth - extraSpacesNum; j > 0; j--) {
                    grid.append(" ");
                }
            }
            if (extraSpaces)
                grid.append(" ");
        }
        if (delimTrailing)
            grid.append("|");
        grid.append("\n");
    }
    // cut last line feed
    grid.setLength(grid.length() - 1);
    totalRows = allRows.size();
    grid.append("\n");
    // Print header
    if (delimLeading)
        grid.append("|");
    for (int i = 0; i < attrs.size(); i++) {
        if (i > 0)
            grid.append("|");
        for (int k = colWidths[i]; k > 0; k--) {
            grid.append("-");
        }
    }
    if (delimTrailing)
        grid.append("|");
    grid.append("\n\n");
    if (newLines) {
        text.append(grid.toString());
        text.append(totalRows + " row(s) fetched.\n\n");
    }
}
Also used : DBDDisplayFormat(org.jkiss.dbeaver.model.data.DBDDisplayFormat) DBPDataKind(org.jkiss.dbeaver.model.DBPDataKind) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)

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