use of org.jkiss.dbeaver.model.data.DBDValueHandler in project dbeaver by serge-rider.
the class JDBCTableColumn method getValueEnumeration.
@NotNull
@Override
public List<DBDLabelValuePair> getValueEnumeration(@NotNull DBCSession session, @Nullable Object valuePattern, int maxResults, boolean formatValues) throws DBException {
DBDValueHandler valueHandler = DBUtils.findValueHandler(session, this);
StringBuilder query = new StringBuilder();
query.append("SELECT ").append(DBUtils.getQuotedIdentifier(this)).append(", count(*)");
// Do not use description columns because they duplicate distinct value
// String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), this);
// if (descColumns != null) {
// query.append(", ").append(descColumns);
// }
query.append("\nFROM ").append(DBUtils.getObjectFullName(getTable(), DBPEvaluationContext.DML));
if (valuePattern instanceof String) {
query.append("\nWHERE ").append(DBUtils.getQuotedIdentifier(this));
if (getDataKind() == DBPDataKind.STRING) {
query.append(" LIKE ?");
} else {
query.append(" = ?");
}
}
query.append("\nGROUP BY ").append(DBUtils.getQuotedIdentifier(this));
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
if (valuePattern instanceof String) {
if (getDataKind() == DBPDataKind.STRING) {
valueHandler.bindValueObject(session, dbStat, this, 0, "%" + valuePattern + "%");
} else {
valueHandler.bindValueObject(session, dbStat, this, 0, valuePattern);
}
}
dbStat.setLimit(0, maxResults);
if (dbStat.executeStatement()) {
try (DBCResultSet dbResult = dbStat.openResultSet()) {
return DBVUtils.readDictionaryRows(session, this, valueHandler, dbResult, formatValues);
}
} else {
return Collections.emptyList();
}
}
}
use of org.jkiss.dbeaver.model.data.DBDValueHandler in project dbeaver by serge-rider.
the class ResultSetHandlerMain method execute.
@Nullable
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart == null) {
return null;
}
final ResultSetViewer rsv = (ResultSetViewer) getActiveResultSet(activePart);
if (rsv == null) {
return null;
}
Shell activeShell = HandlerUtil.getActiveShell(event);
String actionId = event.getCommand().getId();
IResultSetPresentation presentation = rsv.getActivePresentation();
DBPDataSource dataSource = rsv.getDataSource();
switch(actionId) {
case IWorkbenchCommandConstants.FILE_REFRESH:
rsv.refreshData(null);
break;
case CMD_TOGGLE_MODE:
rsv.toggleMode();
break;
case CMD_TOGGLE_PANELS:
rsv.showPanels(!rsv.isPanelsVisible(), true, true);
break;
case CMD_ACTIVATE_PANELS:
rsv.togglePanelsFocus();
break;
case CMD_TOGGLE_MAXIMIZE:
rsv.togglePanelsMaximize();
break;
case CMD_TOGGLE_LAYOUT:
rsv.toggleVerticalLayout();
break;
case CMD_FOCUS_FILTER:
rsv.switchFilterFocus();
break;
case CMD_SWITCH_PRESENTATION:
rsv.switchPresentation();
break;
case CMD_ROW_PREVIOUS:
case ITextEditorActionDefinitionIds.WORD_PREVIOUS:
presentation.scrollToRow(IResultSetPresentation.RowPosition.PREVIOUS);
break;
case CMD_ROW_NEXT:
case ITextEditorActionDefinitionIds.WORD_NEXT:
presentation.scrollToRow(IResultSetPresentation.RowPosition.NEXT);
break;
case CMD_ROW_FIRST:
case ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS:
presentation.scrollToRow(IResultSetPresentation.RowPosition.FIRST);
break;
case CMD_ROW_LAST:
case ITextEditorActionDefinitionIds.SELECT_WORD_NEXT:
presentation.scrollToRow(IResultSetPresentation.RowPosition.LAST);
break;
case CMD_FETCH_PAGE:
rsv.readNextSegment();
break;
case CMD_FETCH_ALL:
rsv.readAllData();
break;
case CMD_ROW_EDIT:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(false);
}
break;
case CMD_ROW_EDIT_INLINE:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(true);
}
break;
case CMD_ROW_ADD:
case CMD_ROW_COPY:
{
boolean copy = actionId.equals(CMD_ROW_COPY);
boolean shiftPressed = event.getTrigger() instanceof Event && ((((Event) event.getTrigger()).stateMask & SWT.SHIFT) == SWT.SHIFT);
boolean insertAfter = rsv.getPreferenceStore().getBoolean(ResultSetPreferences.RS_EDIT_NEW_ROWS_AFTER);
if (shiftPressed)
insertAfter = !insertAfter;
rsv.addNewRow(copy, insertAfter, true);
if (insertAfter) {
presentation.scrollToRow(IResultSetPresentation.RowPosition.NEXT);
}
break;
}
case CMD_ROW_DELETE:
case IWorkbenchCommandConstants.EDIT_DELETE:
rsv.deleteSelectedRows();
break;
case CMD_CELL_SET_NULL:
case CMD_CELL_SET_DEFAULT:
case CMD_CELL_RESET:
{
IResultSetSelection selection = rsv.getSelection();
for (Object cell : selection.toArray()) {
DBDAttributeBinding attr = selection.getElementAttribute(cell);
ResultSetRow row = selection.getElementRow(cell);
if (row != null && attr != null) {
ResultSetValueController valueController = new ResultSetValueController(rsv, attr, row, IValueController.EditType.NONE, null);
if (actionId.equals(CMD_CELL_SET_NULL)) {
valueController.updateValue(BaseValueManager.makeNullValue(valueController), false);
} else if (actionId.equals(CMD_CELL_SET_DEFAULT)) {
DBDValueHandler valueHandler = valueController.getValueHandler();
if (valueHandler instanceof DBDValueDefaultGenerator) {
try (DBCSession session = rsv.getExecutionContext().openSession(new VoidProgressMonitor(), DBCExecutionPurpose.UTIL, "Generate default value")) {
Object defValue = ((DBDValueDefaultGenerator) valueHandler).generateDefaultValue(session, valueController.getValueType());
valueController.updateValue(defValue, false);
}
}
} else {
rsv.getModel().resetCellValue(attr, row);
}
}
}
rsv.redrawData(false, false);
rsv.updatePanelsContent(false);
break;
}
case CMD_APPLY_CHANGES:
{
if (dataSource == null) {
return null;
}
ResultSetSaveSettings saveSettings = new ResultSetSaveSettings();
rsv.applyChanges(null, saveSettings);
break;
}
case CMD_REJECT_CHANGES:
rsv.rejectChanges();
break;
case CMD_GENERATE_SCRIPT:
{
ResultSetSaveReport saveReport = rsv.generateChangesReport();
if (saveReport == null) {
return null;
}
ResultSetSaveSettings saveSettings = showPreviewScript(rsv, saveReport);
if (saveSettings != null) {
rsv.applyChanges(null, saveSettings);
}
break;
}
case CMD_COPY_COLUMN_NAMES:
{
StringBuilder buffer = new StringBuilder();
String columnNames = event.getParameter("columns");
IResultSetSelection selection = rsv.getSelection();
List<DBDAttributeBinding> attrs = selection.isEmpty() ? rsv.getModel().getVisibleAttributes() : selection.getSelectedAttributes();
if (!CommonUtils.isEmpty(columnNames) && attrs.size() == 1) {
attrs = new ArrayList<>();
for (String colName : columnNames.split(",")) {
for (DBDAttributeBinding attr : rsv.getModel().getVisibleAttributes()) {
if (colName.equals(attr.getName())) {
attrs.add(attr);
}
}
}
}
ResultSetCopySettings settings = new ResultSetCopySettings();
if (attrs.size() > 1) {
ResultSetHandlerCopySpecial.CopyConfigDialog configDialog = new ResultSetHandlerCopySpecial.CopyConfigDialog(activeShell, "CopyGridNamesOptionsDialog");
if (configDialog.open() != IDialogConstants.OK_ID) {
return null;
}
settings = configDialog.copySettings;
}
for (DBDAttributeBinding attr : attrs) {
if (buffer.length() > 0) {
buffer.append(settings.getColumnDelimiter());
}
String colName = attr.getLabel();
if (CommonUtils.isEmpty(colName)) {
colName = attr.getName();
}
if (dataSource == null) {
buffer.append(colName);
} else {
buffer.append(DBUtils.getQuotedIdentifier(dataSource, colName));
}
}
ResultSetUtils.copyToClipboard(buffer.toString());
break;
}
case CMD_COPY_ROW_NAMES:
{
StringBuilder buffer = new StringBuilder();
List<ResultSetRow> selectedRows = rsv.getSelection().getSelectedRows();
ResultSetCopySettings settings = new ResultSetCopySettings();
if (selectedRows.size() > 1) {
ResultSetHandlerCopySpecial.CopyConfigDialog configDialog = new ResultSetHandlerCopySpecial.CopyConfigDialog(activeShell, "CopyGridNamesOptionsDialog");
if (configDialog.open() != IDialogConstants.OK_ID) {
return null;
}
settings = configDialog.copySettings;
}
for (ResultSetRow row : selectedRows) {
if (buffer.length() > 0) {
buffer.append(settings.getRowDelimiter());
}
buffer.append(row.getVisualNumber() + 1);
}
ResultSetUtils.copyToClipboard(buffer.toString());
break;
}
case IWorkbenchCommandConstants.EDIT_COPY:
ResultSetUtils.copyToClipboard(presentation.copySelection(new ResultSetCopySettings(false, false, false, true, false, null, null, null, DBDDisplayFormat.EDIT)));
break;
case IWorkbenchCommandConstants.EDIT_PASTE:
case IActionConstants.CMD_PASTE_SPECIAL:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).pasteFromClipboard(actionId.equals(IActionConstants.CMD_PASTE_SPECIAL));
}
break;
case IWorkbenchCommandConstants.EDIT_CUT:
ResultSetUtils.copyToClipboard(presentation.copySelection(new ResultSetCopySettings(false, false, true, true, false, null, null, null, DBDDisplayFormat.EDIT)));
break;
case IWorkbenchCommandConstants.FILE_PRINT:
presentation.printResultSet();
break;
case ITextEditorActionDefinitionIds.SMART_ENTER:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(false);
}
break;
case IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE:
IAction action = TextEditorUtils.createFindReplaceAction(activeShell, rsv.getAdapter(IFindReplaceTarget.class));
action.run();
break;
case CMD_NAVIGATE_LINK:
{
final DBDAttributeBinding attr = rsv.getActivePresentation().getCurrentAttribute();
if (attr != null) {
new AbstractJob("Navigate association") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try {
rsv.navigateAssociation(monitor, rsv.getModel(), DBExecUtils.getAssociationByAttribute(attr), rsv.getSelection().getSelectedRows(), false);
} catch (DBException e) {
return GeneralUtils.makeExceptionStatus(e);
}
return Status.OK_STATUS;
}
}.schedule();
}
break;
}
case CMD_COUNT:
rsv.updateRowCount();
break;
case IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY:
{
final int hp = rsv.getHistoryPosition();
if (hp > 0) {
rsv.navigateHistory(hp - 1);
}
break;
}
case IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY:
{
final int hp = rsv.getHistoryPosition();
if (hp < rsv.getHistorySize() - 1) {
rsv.navigateHistory(hp + 1);
}
break;
}
case ITextEditorActionDefinitionIds.LINE_GOTO:
{
ResultSetRow currentRow = rsv.getCurrentRow();
final int rowCount = rsv.getModel().getRowCount();
if (rowCount <= 0) {
break;
}
GotoLineDialog d = new GotoLineDialog(activeShell, "Go to Row", "Enter row number (1.." + rowCount + ")", String.valueOf(currentRow == null ? 1 : currentRow.getVisualNumber() + 1), input -> {
try {
int i = Integer.parseInt(input);
if (i <= 0 || rowCount < i) {
return "Row number is out of range";
}
} catch (NumberFormatException x) {
return "Not a number";
}
return null;
});
if (d.open() == Window.OK) {
int line = Integer.parseInt(d.getValue());
rsv.setCurrentRow(rsv.getModel().getRow(line - 1));
rsv.getActivePresentation().scrollToRow(IResultSetPresentation.RowPosition.CURRENT);
}
break;
}
case CMD_FILTER_MENU:
{
rsv.showFiltersMenu();
break;
}
case CMD_FILTER_MENU_DISTINCT:
{
DBDAttributeBinding curAttribute = rsv.getActivePresentation().getCurrentAttribute();
if (curAttribute != null) {
rsv.showFiltersDistinctMenu(curAttribute, true);
}
break;
}
case CMD_FILTER_EDIT_SETTINGS:
{
rsv.showFilterSettingsDialog();
break;
}
case CMD_FILTER_SAVE_SETTING:
{
rsv.saveDataFilter();
break;
}
case CMD_FILTER_CLEAR_SETTING:
{
rsv.clearDataFilter(true);
}
case CMD_REFERENCES_MENU:
{
boolean shiftPressed = event.getTrigger() instanceof Event && ((((Event) event.getTrigger()).stateMask & SWT.SHIFT) == SWT.SHIFT);
rsv.showReferencesMenu(shiftPressed);
break;
}
case CMD_EXPORT:
{
List<Long> selectedRows = new ArrayList<>();
for (ResultSetRow selectedRow : rsv.getSelection().getSelectedRows()) {
selectedRows.add(Long.valueOf(selectedRow.getRowNumber()));
}
List<String> selectedAttributes = new ArrayList<>();
for (DBDAttributeBinding attributeBinding : rsv.getSelection().getSelectedAttributes()) {
selectedAttributes.add(attributeBinding.getName());
}
ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
options.setSelectedRows(selectedRows);
options.setSelectedColumns(selectedAttributes);
ResultSetDataContainer dataContainer = new ResultSetDataContainer(rsv, options);
DataTransferWizard.openWizard(HandlerUtil.getActiveWorkbenchWindow(event), Collections.singletonList(new DatabaseTransferProducer(dataContainer, rsv.getModel().getDataFilter())), null, rsv.getSelection());
break;
}
case CMD_ZOOM_IN:
case CMD_ZOOM_OUT:
{
FontRegistry fontRegistry = rsv.getSite().getWorkbenchWindow().getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry();
Font font = fontRegistry.get(ThemeConstants.FONT_SQL_RESULT_SET);
if (font != null) {
FontData[] fondData = font.getFontData();
if (fondData != null) {
int zoomFactor = actionId.equals(CMD_ZOOM_IN) ? 1 : -1;
FontDescriptor fd = createFontDescriptor(fondData, zoomFactor);
fontRegistry.put(ThemeConstants.FONT_SQL_RESULT_SET, fd.getFontData());
}
}
break;
}
case CMD_TOGGLE_ORDER:
{
final DBDAttributeBinding attr = rsv.getActivePresentation().getCurrentAttribute();
if (attr != null) {
rsv.toggleSortOrder(attr, false, false);
}
break;
}
}
return null;
}
use of org.jkiss.dbeaver.model.data.DBDValueHandler in project dbeaver by serge-rider.
the class PostgreArrayValueHandler method convertStringToCollection.
private JDBCCollection convertStringToCollection(@NotNull DBCSession session, @NotNull DBSTypedObject arrayType, @NotNull PostgreDataType itemType, @NotNull String value) throws DBCException {
String delimiter;
PostgreDataType arrayDataType = PostgreUtils.findDataType(session, (PostgreDataSource) session.getDataSource(), arrayType);
if (arrayDataType != null) {
delimiter = CommonUtils.toString(arrayDataType.getArrayDelimiter(), PostgreConstants.DEFAULT_ARRAY_DELIMITER);
} else {
delimiter = PostgreConstants.DEFAULT_ARRAY_DELIMITER;
}
if (itemType.getDataKind() == DBPDataKind.STRUCT) {
// Items are structures. Parse them as CSV
List<Object> itemStrings = PostgreValueParser.parseArrayString(value, delimiter);
Object[] itemValues = new Object[itemStrings.size()];
DBDValueHandler itemValueHandler = DBUtils.findValueHandler(session, itemType);
for (int i = 0; i < itemStrings.size(); i++) {
Object itemString = itemStrings.get(i);
Object itemValue = itemValueHandler.getValueFromObject(session, itemType, itemString, false, false);
itemValues[i] = itemValue;
}
return new JDBCCollection(itemType, itemValueHandler, itemValues);
} else {
List<Object> strings = PostgreValueParser.parseArrayString(value, delimiter);
Object[] contents = new Object[strings.size()];
for (int i = 0; i < strings.size(); i++) {
contents[i] = PostgreValueParser.convertStringToValue(session, itemType, String.valueOf(strings.get(i)));
}
return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), contents);
}
}
use of org.jkiss.dbeaver.model.data.DBDValueHandler in project dbeaver by dbeaver.
the class JDBCTableColumn method getValueEnumeration.
@NotNull
@Override
public List<DBDLabelValuePair> getValueEnumeration(@NotNull DBCSession session, @Nullable Object valuePattern, int maxResults, boolean formatValues) throws DBException {
DBDValueHandler valueHandler = DBUtils.findValueHandler(session, this);
StringBuilder query = new StringBuilder();
query.append("SELECT ").append(DBUtils.getQuotedIdentifier(this)).append(", count(*)");
// Do not use description columns because they duplicate distinct value
// String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), this);
// if (descColumns != null) {
// query.append(", ").append(descColumns);
// }
query.append("\nFROM ").append(DBUtils.getObjectFullName(getTable(), DBPEvaluationContext.DML));
if (valuePattern instanceof String) {
query.append("\nWHERE ").append(DBUtils.getQuotedIdentifier(this));
if (getDataKind() == DBPDataKind.STRING) {
query.append(" LIKE ?");
} else {
query.append(" = ?");
}
}
query.append("\nGROUP BY ").append(DBUtils.getQuotedIdentifier(this));
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
if (valuePattern instanceof String) {
if (getDataKind() == DBPDataKind.STRING) {
valueHandler.bindValueObject(session, dbStat, this, 0, "%" + valuePattern + "%");
} else {
valueHandler.bindValueObject(session, dbStat, this, 0, valuePattern);
}
}
dbStat.setLimit(0, maxResults);
if (dbStat.executeStatement()) {
try (DBCResultSet dbResult = dbStat.openResultSet()) {
return DBVUtils.readDictionaryRows(session, this, valueHandler, dbResult, formatValues);
}
} else {
return Collections.emptyList();
}
}
}
use of org.jkiss.dbeaver.model.data.DBDValueHandler in project dbeaver by dbeaver.
the class PostgreArrayValueHandler method convertStringToCollection.
private JDBCCollection convertStringToCollection(@NotNull DBCSession session, @NotNull DBSTypedObject arrayType, @NotNull PostgreDataType itemType, @NotNull String value) throws DBCException {
String delimiter;
PostgreDataType arrayDataType = PostgreUtils.findDataType(session, (PostgreDataSource) session.getDataSource(), arrayType);
if (arrayDataType != null) {
delimiter = CommonUtils.toString(arrayDataType.getArrayDelimiter(), PostgreConstants.DEFAULT_ARRAY_DELIMITER);
} else {
delimiter = PostgreConstants.DEFAULT_ARRAY_DELIMITER;
}
if (itemType.getDataKind() == DBPDataKind.STRUCT) {
// Items are structures. Parse them as CSV
List<Object> itemStrings = PostgreValueParser.parseArrayString(value, delimiter);
Object[] itemValues = new Object[itemStrings.size()];
DBDValueHandler itemValueHandler = DBUtils.findValueHandler(session, itemType);
for (int i = 0; i < itemStrings.size(); i++) {
Object itemString = itemStrings.get(i);
Object itemValue = itemValueHandler.getValueFromObject(session, itemType, itemString, false, false);
itemValues[i] = itemValue;
}
return new JDBCCollection(itemType, itemValueHandler, itemValues);
} else {
List<Object> strings = PostgreValueParser.parseArrayString(value, delimiter);
Object[] contents = new Object[strings.size()];
for (int i = 0; i < strings.size(); i++) {
contents[i] = PostgreValueParser.convertStringToValue(session, itemType, String.valueOf(strings.get(i)));
}
return new JDBCCollection(itemType, DBUtils.findValueHandler(session, itemType), contents);
}
}
Aggregations