Search in sources :

Example 11 with SQLQuery

use of org.jkiss.dbeaver.model.sql.SQLQuery in project dbeaver by serge-rider.

the class SQLQueryParameterBindDialog method updateQueryPreview.

private void updateQueryPreview() {
    SQLQuery queryCopy = new SQLQuery(query.getDataSource(), query.getText(), query);
    List<SQLQueryParameter> setParams = new ArrayList<>(this.parameters);
    setParams.removeIf(parameter -> !parameter.isVariableSet());
    SQLUtils.fillQueryParameters(queryCopy, setParams);
    {
        UIUtils.asyncExec(() -> {
            DBWorkbench.getService(UIServiceSQL.class).setSQLPanelText(queryPreviewPanel, queryCopy.getText());
        });
    }
}
Also used : SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter)

Example 12 with SQLQuery

use of org.jkiss.dbeaver.model.sql.SQLQuery in project dbeaver by serge-rider.

the class SessionManagerViewer method updatePreview.

private void updatePreview() {
    if (previewFolder.getSelectionIndex() == 0) {
        // Show SQL
        detailsItem.setText(SessionEditorMessages.viewer_details_item_session_details);
        updateSQL();
        if (curSession == null) {
            sessionProps.clearProperties();
        } else {
            PropertyCollector propCollector = new PropertyCollector(curSession, true);
            propCollector.collectProperties();
            sessionProps.loadProperties(propCollector);
        }
    } else if (planViewer != null) {
        // Show execution plan
        String sqlText = curSession == null ? "" : CommonUtils.notEmpty(curSession.getActiveQuery());
        if (!CommonUtils.isEmpty(sqlText)) {
            planViewer.explainQueryPlan(new SQLQuery(sessionManager.getDataSource(), sqlText), curSession.getActiveQueryId());
        }
    }
    if (detailsFolder.getSelectionIndex() > 0) {
        CTabItem detailsItem = detailsFolder.getItem(detailsFolder.getSelectionIndex());
        Object data = detailsItem.getData();
        if (data instanceof DBAServerSessionDetails) {
            if (detailsItem.getControl() instanceof StyledText) {
                StyledText styledText = (StyledText) detailsItem.getControl();
                loadPlainTextDetails((DBAServerSessionDetails) data, styledText);
            } else {
                DetailsListControl detailsListControl = (DetailsListControl) detailsItem.getControl();
                detailsListControl.loadData();
            }
        }
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) DBAServerSessionDetails(org.jkiss.dbeaver.model.admin.sessions.DBAServerSessionDetails) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) DBPObject(org.jkiss.dbeaver.model.DBPObject) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) CTabItem(org.eclipse.swt.custom.CTabItem)

Example 13 with SQLQuery

use of org.jkiss.dbeaver.model.sql.SQLQuery 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();
    }
}
Also used : DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) DBVEntityConstraint(org.jkiss.dbeaver.model.virtual.DBVEntityConstraint) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 14 with SQLQuery

use of org.jkiss.dbeaver.model.sql.SQLQuery in project dbeaver by serge-rider.

the class SQLQueryJob method showExecutionResult.

private void showExecutionResult(DBCSession session) {
    if (statistics.getStatementsCount() > 1 || resultSetNumber == 0) {
        SQLQuery query = new SQLQuery(session.getDataSource(), "", -1, -1);
        if (queries.size() == 1) {
            query.setQuery(queries.get(0).getQuery());
        }
        // It will set tab name to "Stats"
        query.setData(STATS_RESULTS);
        DBDDataReceiver dataReceiver = resultsConsumer.getDataReceiver(query, resultSetNumber);
        if (dataReceiver != null) {
            try {
                fetchExecutionResult(session, dataReceiver, query);
            } catch (DBCException e) {
                log.error("Error generating execution result stats", e);
            }
        }
    }
}
Also used : DBDDataReceiver(org.jkiss.dbeaver.model.data.DBDDataReceiver) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery)

Example 15 with SQLQuery

use of org.jkiss.dbeaver.model.sql.SQLQuery in project dbeaver by serge-rider.

the class SQLEditor method explainQueryPlan.

public void explainQueryPlan() {
    final SQLQuery sqlQuery = extractActiveQuery();
    if (sqlQuery == null) {
        setStatus(CoreMessages.editors_sql_status_empty_query_string, DBPMessageType.ERROR);
        return;
    }
    explainQueryPlan(sqlQuery);
}
Also used : SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery)

Aggregations

SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)17 DBException (org.jkiss.dbeaver.DBException)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)4 Point (org.eclipse.swt.graphics.Point)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)3 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)2 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)2 DBVEntityConstraint (org.jkiss.dbeaver.model.virtual.DBVEntityConstraint)2 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 Expression (net.sf.jsqlparser.expression.Expression)1 Function (net.sf.jsqlparser.expression.Function)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 Column (net.sf.jsqlparser.schema.Column)1 Statement (net.sf.jsqlparser.statement.Statement)1