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());
});
}
}
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();
}
}
}
}
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();
}
}
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);
}
}
}
}
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);
}
Aggregations