use of org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector in project dbeaver by serge-rider.
the class SQLCompletionProposal method validate.
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
if (event == null) {
return false;
}
SQLSyntaxManager syntaxManager = getContext().getSyntaxManager();
DBPDataSource dataSource = getContext().getDataSource();
final SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, offset);
String wordPart = wordDetector.getWordPart();
int divPos = wordPart.lastIndexOf(syntaxManager.getStructSeparator());
if (divPos != -1) {
if (divPos == wordPart.length() - 1) {
// It is valid only if full word matches (it should be the only proposal)
if (replacementString.equals(wordPart.substring(0, divPos))) {
{
// Call completion popup again
UIUtils.asyncExec(() -> {
IEditorPart activeEditor = UIUtils.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (activeEditor != null) {
ITextViewer textViewer = activeEditor.getAdapter(ITextViewer.class);
if (textViewer != null) {
textViewer.getTextOperationTarget().doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
}
}
});
}
}
return false;
}
wordPart = wordPart.substring(divPos + 1);
}
String wordLower = wordPart.toLowerCase(Locale.ENGLISH);
if (!CommonUtils.isEmpty(wordPart)) {
boolean matchContains = dataSource != null && dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.PROPOSALS_MATCH_CONTAINS);
boolean matched;
if (getObject() == null || !matchContains) {
// For keywords use strict matching
matched = (matchContains ? replacementFull.contains(wordLower) : replacementFull.startsWith(wordLower)) && (CommonUtils.isEmpty(event.getText()) || replacementFull.contains(event.getText().toLowerCase(Locale.ENGLISH))) || (this.replacementLast != null && this.replacementLast.startsWith(wordLower));
} else {
// For objects use fuzzy matching
int score = TextUtils.fuzzyScore(replacementFull, wordLower);
matched = (score > 0 && (CommonUtils.isEmpty(event.getText()) || TextUtils.fuzzyScore(replacementFull, event.getText()) > 0)) || (this.replacementLast != null && TextUtils.fuzzyScore(this.replacementLast, wordLower) > 0);
if (matched) {
setProposalScore(score);
}
}
if (matched) {
setPosition(wordDetector);
return true;
}
} else if (divPos != -1) {
// Most likely it is a column name after an alias - all columns are valid
if (getObject() != null) {
return true;
}
}
return false;
}
use of org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector in project dbeaver by serge-rider.
the class SQLContextInformer method searchInformation.
public void searchInformation(IRegion region) {
ITextViewer textViewer = editor.getTextViewer();
final DBCExecutionContext executionContext = editor.getExecutionContext();
if (region == null || textViewer == null || executionContext == null) {
return;
}
IDocument document = textViewer.getDocument();
if (document == null) {
return;
}
SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, region.getOffset());
wordRegion = wordDetector.detectIdentifier(document, region);
if (wordRegion.word.length() == 0) {
return;
}
String fullName = wordRegion.identifier;
String tableName = wordRegion.word;
boolean caseSensitive = false;
if (wordDetector.isQuoted(tableName)) {
tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
caseSensitive = true;
}
String[] containerNames = null;
if (!CommonUtils.equalObjects(fullName, tableName)) {
int divPos = fullName.indexOf(syntaxManager.getStructSeparator());
if (divPos != -1) {
String[] parts = wordDetector.splitIdentifier(fullName);
tableName = parts[parts.length - 1];
containerNames = ArrayUtils.remove(String.class, parts, parts.length - 1);
for (int i = 0; i < containerNames.length; i++) {
if (wordDetector.isQuoted(containerNames[i])) {
containerNames[i] = DBUtils.getUnQuotedIdentifier(containerNames[i], syntaxManager.getIdentifierQuoteStrings());
}
containerNames[i] = DBObjectNameCaseTransformer.transformName(editor.getDataSource(), containerNames[i]);
}
if (wordDetector.isQuoted(tableName)) {
tableName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
}
} else {
// Full name could be quoted
if (wordDetector.isQuoted(fullName)) {
String unquotedName = DBUtils.getUnQuotedIdentifier(tableName, syntaxManager.getIdentifierQuoteStrings());
if (unquotedName.equals(tableName)) {
caseSensitive = true;
}
}
}
}
final SQLDialect dialect = syntaxManager.getDialect();
keywordType = dialect.getKeywordType(fullName);
if (keywordType == DBPKeywordType.KEYWORD && region.getLength() > 1) {
// It is a keyword = let's use whole selection
try {
fullName = document.get(region.getOffset(), region.getLength());
} catch (BadLocationException e) {
log.warn(e);
}
}
keywords = new String[] { fullName };
if (keywordType == DBPKeywordType.KEYWORD || keywordType == DBPKeywordType.FUNCTION) {
// Skip keywords
return;
}
final Map<String, ObjectLookupCache> contextCache = getLinksCache();
if (contextCache == null) {
return;
}
ObjectLookupCache tlc = contextCache.get(fullName);
if (tlc == null) {
// Start new word finder job
tlc = new ObjectLookupCache();
contextCache.put(fullName, tlc);
DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, editor.getDataSource());
TablesFinderJob job = new TablesFinderJob(executionContext, structureAssistant, containerNames, tableName, caseSensitive, tlc);
job.schedule();
}
if (tlc.loading) {
// Wait for 1000ms maximum
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// interrupted - just go further
break;
}
if (!tlc.loading) {
break;
}
Display.getCurrent().readAndDispatch();
}
}
if (!tlc.loading) {
synchronized (this) {
objectReferences = tlc.references;
}
}
}
use of org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector in project dbeaver by serge-rider.
the class ResultSetFilterPanel method getProposals.
@Override
public IContentProposal[] getProposals(String contents, int position) {
if (!viewer.getPreferenceStore().getBoolean(ResultSetPreferences.RESULT_SET_FILTER_AUTO_COMPLETE_PROPOSIAL)) {
return null;
}
SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
DBPDataSource dataSource = viewer.getDataSource();
if (dataSource != null) {
syntaxManager.init(dataSource);
}
SQLWordPartDetector wordDetector = new SQLWordPartDetector(new Document(contents), syntaxManager, position);
String word = wordDetector.getFullWord();
final List<IContentProposal> proposals = new ArrayList<>();
if (CommonUtils.isEmptyTrimmed(word))
word = contents;
word = word.toLowerCase(Locale.ENGLISH);
String attrName = word;
final DBRRunnableWithProgress reader = monitor -> {
DBDAttributeBinding[] attributes = viewer.getModel().getAttributes();
for (DBDAttributeBinding attribute : attributes) {
if (attribute.isCustom()) {
continue;
}
final String name = DBUtils.getUnQuotedIdentifier(attribute.getDataSource(), attribute.getName());
if (CommonUtils.isEmpty(attrName) || name.toLowerCase(Locale.ENGLISH).startsWith(attrName)) {
final String content = DBUtils.getQuotedIdentifier(attribute) + " ";
proposals.add(new ContentProposalExt(content, attribute.getName(), DBInfoUtils.makeObjectDescription(monitor, attribute.getAttribute(), false), content.length(), DBValueFormatting.getObjectImage(attribute)));
}
}
};
SystemJob searchJob = new SystemJob("Extract attribute proposals", reader);
searchJob.schedule();
UIUtils.waitJobCompletion(searchJob);
String[] filterKeywords = { SQLConstants.KEYWORD_AND, SQLConstants.KEYWORD_OR, SQLConstants.KEYWORD_IS, SQLConstants.KEYWORD_NOT, SQLConstants.KEYWORD_NULL };
for (String kw : filterKeywords) {
if (attrName.isEmpty() || kw.startsWith(attrName.toUpperCase())) {
if (dataSource != null) {
kw = dataSource.getSQLDialect().storesUnquotedCase().transform(kw);
}
proposals.add(new ContentProposal(kw + " ", kw + ": SQL expression keyword"));
}
}
return proposals.toArray(new IContentProposal[0]);
}
use of org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector in project dbeaver by serge-rider.
the class SQLCompletionAnalyzer method makeJoinColumnProposals.
private boolean makeJoinColumnProposals(DBSObjectContainer sc, DBSEntity leftTable) {
SQLWordPartDetector joinTableDetector = new SQLWordPartDetector(request.getDocument(), request.getContext().getSyntaxManager(), request.getWordDetector().getStartOffset(), 2);
List<String> prevWords = joinTableDetector.getPrevWords();
if (!CommonUtils.isEmpty(prevWords)) {
DBPDataSource dataSource = request.getContext().getDataSource();
SQLDialect sqlDialect = dataSource.getSQLDialect();
String rightTableName = prevWords.get(0);
String[] allNames = SQLUtils.splitFullIdentifier(rightTableName, sqlDialect.getCatalogSeparator(), sqlDialect.getIdentifierQuoteStrings(), false);
DBSObject rightTable = SQLSearchUtils.findObjectByFQN(monitor, sc, request.getContext().getExecutionContext(), Arrays.asList(allNames), !request.isSimpleMode(), request.getWordDetector());
if (rightTable instanceof DBSEntity) {
try {
String joinCriteria = SQLUtils.generateTableJoin(monitor, leftTable, DBUtils.getQuotedIdentifier(leftTable), (DBSEntity) rightTable, DBUtils.getQuotedIdentifier(rightTable));
proposals.add(createCompletionProposal(request, joinCriteria, joinCriteria, DBPKeywordType.OTHER, "Join condition"));
return true;
} catch (DBException e) {
log.error("Error generating join condition", e);
}
}
}
return false;
}
use of org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector in project dbeaver by dbeaver.
the class SQLCompletionProposal method validate.
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
if (event == null) {
return false;
}
SQLSyntaxManager syntaxManager = getContext().getSyntaxManager();
DBPDataSource dataSource = getContext().getDataSource();
final SQLWordPartDetector wordDetector = new SQLWordPartDetector(document, syntaxManager, offset);
String wordPart = wordDetector.getWordPart();
int divPos = wordPart.lastIndexOf(syntaxManager.getStructSeparator());
if (divPos != -1) {
if (divPos == wordPart.length() - 1) {
// It is valid only if full word matches (it should be the only proposal)
if (replacementString.equals(wordPart.substring(0, divPos))) {
{
// Call completion popup again
UIUtils.asyncExec(() -> {
IEditorPart activeEditor = UIUtils.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (activeEditor != null) {
ITextViewer textViewer = activeEditor.getAdapter(ITextViewer.class);
if (textViewer != null) {
textViewer.getTextOperationTarget().doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
}
}
});
}
}
return false;
}
wordPart = wordPart.substring(divPos + 1);
}
String wordLower = wordPart.toLowerCase(Locale.ENGLISH);
if (!CommonUtils.isEmpty(wordPart)) {
boolean matchContains = dataSource != null && dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.PROPOSALS_MATCH_CONTAINS);
boolean matched;
if (getObject() == null || !matchContains) {
// For keywords use strict matching
matched = (matchContains ? replacementFull.contains(wordLower) : replacementFull.startsWith(wordLower)) && (CommonUtils.isEmpty(event.getText()) || replacementFull.contains(event.getText().toLowerCase(Locale.ENGLISH))) || (this.replacementLast != null && this.replacementLast.startsWith(wordLower));
} else {
// For objects use fuzzy matching
int score = TextUtils.fuzzyScore(replacementFull, wordLower);
matched = (score > 0 && (CommonUtils.isEmpty(event.getText()) || TextUtils.fuzzyScore(replacementFull, event.getText()) > 0)) || (this.replacementLast != null && TextUtils.fuzzyScore(this.replacementLast, wordLower) > 0);
if (matched) {
setProposalScore(score);
}
}
if (matched) {
setPosition(wordDetector);
return true;
}
} else if (divPos != -1) {
// Most likely it is a column name after an alias - all columns are valid
if (getObject() != null) {
return true;
}
}
return false;
}
Aggregations