use of org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel in project dbeaver by serge-rider.
the class SpreadsheetFindReplaceTarget method findAndSelect.
@Override
public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) {
searchPattern = null;
ResultSetModel model = owner.getController().getModel();
if (model.isEmpty()) {
return -1;
}
Spreadsheet spreadsheet = owner.getSpreadsheet();
int rowCount = spreadsheet.getItemCount();
int columnCount = spreadsheet.getColumnCount();
Collection<GridPos> selection = spreadsheet.getSelection();
GridPos startPosition = selection.isEmpty() ? null : selection.iterator().next();
if (startPosition == null) {
// From the beginning
startPosition = new GridPos(0, 0);
}
Pattern findPattern;
if (regExSearch) {
try {
findPattern = Pattern.compile(findString, caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
log.warn("Bad regex pattern: " + findString);
return -1;
}
} else {
findPattern = Pattern.compile(Pattern.quote(findString), caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
}
int minColumnNum = owner.getController().isRecordMode() ? -1 : 0;
for (GridPos curPosition = new GridPos(startPosition); ; ) {
//Object element = contentProvider.getElement(curPosition);
if (searchForward) {
curPosition.col++;
if (curPosition.col >= columnCount) {
curPosition.col = minColumnNum;
curPosition.row++;
}
} else {
curPosition.col--;
if (curPosition.col < minColumnNum) {
curPosition.col = columnCount - 1;
curPosition.row--;
}
}
if (curPosition.row < 0 || curPosition.row >= rowCount) {
if (offset == -1) {
// Wrap search - redo search one more time
offset = 0;
if (searchForward) {
curPosition = new GridPos(0, 0);
} else {
curPosition = new GridPos(columnCount - 1, rowCount - 1);
}
} else {
// Not found
return -1;
}
}
String cellText;
if (owner.getController().isRecordMode() && curPosition.col == minColumnNum) {
// Header
cellText = spreadsheet.getLabelProvider().getText(spreadsheet.getRowElement(curPosition.row));
} else {
GridCell cell = spreadsheet.posToCell(curPosition);
if (cell != null) {
cellText = spreadsheet.getContentProvider().getCellText(cell.col, cell.row);
} else {
continue;
}
}
Matcher matcher = findPattern.matcher(cellText);
if (wholeWord ? matcher.matches() : matcher.find()) {
if (curPosition.col == minColumnNum) {
curPosition.col = 0;
}
spreadsheet.setCellSelection(curPosition);
spreadsheet.showSelection();
searchPattern = findPattern;
return curPosition.row;
}
}
}
use of org.jkiss.dbeaver.ui.controls.resultset.ResultSetModel in project dbeaver by serge-rider.
the class GenerateSQLContributor method makeResultSetContributions.
private void makeResultSetContributions(List<IContributionItem> menu, IResultSetSelection rss) {
final IResultSetController rsv = rss.getController();
DBSDataContainer dataContainer = rsv.getDataContainer();
final List<DBDAttributeBinding> visibleAttributes = rsv.getModel().getVisibleAttributes();
final DBSEntity entity = rsv.getModel().getSingleSource();
if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null) {
final Collection<ResultSetRow> selectedRows = rss.getSelectedRows();
if (!CommonUtils.isEmpty(selectedRows)) {
menu.add(makeAction("SELECT by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("SELECT ");
boolean hasAttr = false;
for (DBSAttributeBase attr : getValueAttributes(monitor, object, keyAttributes)) {
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append("\nFROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nWHERE ");
hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
menu.add(makeAction("INSERT", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<? extends DBSAttributeBase> allAttributes = getAllAttributes(monitor, object);
sql.append("INSERT INTO ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\n(");
boolean hasAttr = false;
for (DBSAttributeBase attr : allAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append(")\nVALUES(");
hasAttr = false;
for (DBSAttributeBase attr : allAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
if (binding == null) {
appendDefaultValue(sql, attr);
} else {
appendAttributeValue(rsv, sql, binding, firstRow);
}
hasAttr = true;
}
sql.append(");\n");
}
}
}));
menu.add(makeAction("UPDATE", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
Collection<? extends DBSAttributeBase> valueAttributes = getValueAttributes(monitor, object, keyAttributes);
sql.append("UPDATE ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nSET ");
boolean hasAttr = false;
for (DBSAttributeBase attr : valueAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
if (binding == null) {
appendDefaultValue(sql, attr);
} else {
appendAttributeValue(rsv, sql, binding, firstRow);
}
hasAttr = true;
}
sql.append("\nWHERE ");
hasAttr = false;
for (DBDAttributeBinding attr : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, attr, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
menu.add(makeAction("DELETE by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("DELETE FROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nWHERE ");
boolean hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
}
}
}
Aggregations