Search in sources :

Example 1 with SQLCommandHandlerDescriptor

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

the class SQLCompletionProcessor method makeCommandProposals.

private ICompletionProposal[] makeCommandProposals(SQLCompletionRequest request, String prefix) {
    final String controlCommandPrefix = editor.getSyntaxManager().getControlCommandPrefix();
    if (prefix.startsWith(controlCommandPrefix)) {
        prefix = prefix.substring(controlCommandPrefix.length());
    }
    final List<SQLCommandCompletionProposal> commandProposals = new ArrayList<>();
    for (SQLCommandHandlerDescriptor command : SQLCommandsRegistry.getInstance().getCommandHandlers()) {
        if (command.getId().startsWith(prefix)) {
            commandProposals.add(new SQLCommandCompletionProposal(request, command));
        }
    }
    return commandProposals.toArray(new ICompletionProposal[0]);
}
Also used : ArrayList(java.util.ArrayList) SQLCommandHandlerDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor)

Example 2 with SQLCommandHandlerDescriptor

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

the class SQLRuleManager method loadRules.

public void loadRules(@Nullable DBPDataSource dataSource, boolean minimalRules) {
    SQLDialect dialect = syntaxManager.getDialect();
    TPRuleProvider ruleProvider = GeneralUtils.adapt(dialect, TPRuleProvider.class);
    DBPDataSourceContainer dataSourceContainer = dataSource == null ? null : dataSource.getContainer();
    final TPToken keywordToken = new TPTokenDefault(SQLTokenType.T_KEYWORD);
    final TPToken typeToken = new TPTokenDefault(SQLTokenType.T_TYPE);
    final TPToken stringToken = new TPTokenDefault(SQLTokenType.T_STRING);
    final TPToken quotedToken = new TPTokenDefault(SQLTokenType.T_QUOTED);
    final TPToken numberToken = new TPTokenDefault(SQLTokenType.T_NUMBER);
    final TPToken commentToken = new SQLCommentToken();
    final SQLDelimiterToken delimiterToken = new SQLDelimiterToken();
    final SQLParameterToken parameterToken = new SQLParameterToken();
    final SQLVariableToken variableToken = new SQLVariableToken();
    final TPToken otherToken = new TPTokenDefault(SQLTokenType.T_OTHER);
    final SQLBlockHeaderToken blockHeaderToken = new SQLBlockHeaderToken();
    final SQLBlockBeginToken blockBeginToken = new SQLBlockBeginToken();
    final SQLBlockEndToken blockEndToken = new SQLBlockEndToken();
    List<TPRule> rules = new ArrayList<>();
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.INITIAL);
    }
    // Add rule for single-line comments.
    for (String lineComment : dialect.getSingleLineComments()) {
        if (lineComment.startsWith("^")) {
            rules.add(new LineCommentRule(lineComment, commentToken, (char) 0, false, true));
        } else {
            rules.add(new EndOfLineRule(lineComment, commentToken, (char) 0, false, true));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.CONTROL);
    }
    if (!minimalRules) {
        final SQLControlToken controlToken = new SQLControlToken();
        String commandPrefix = syntaxManager.getControlCommandPrefix();
        // Control rules
        for (SQLCommandHandlerDescriptor controlCommand : SQLCommandsRegistry.getInstance().getCommandHandlers()) {
            // $NON-NLS-1$
            rules.add(new SQLCommandRule(commandPrefix, controlCommand, controlToken));
        }
    }
    {
        if (!minimalRules && syntaxManager.isVariablesEnabled()) {
            // Variable rule
            rules.add(new SQLVariableRule(parameterToken));
        }
    }
    if (!minimalRules) {
        // Add rules for delimited identifiers and string literals.
        char escapeChar = syntaxManager.getEscapeChar();
        String[][] identifierQuoteStrings = syntaxManager.getIdentifierQuoteStrings();
        String[][] stringQuoteStrings = syntaxManager.getStringQuoteStrings();
        boolean hasDoubleQuoteRule = false;
        if (!ArrayUtils.isEmpty(identifierQuoteStrings)) {
            for (String[] quotes : identifierQuoteStrings) {
                rules.add(new SingleLineRule(quotes[0], quotes[1], quotedToken, escapeChar));
                if (quotes[1].equals(SQLConstants.STR_QUOTE_DOUBLE) && quotes[0].equals(quotes[1])) {
                    hasDoubleQuoteRule = true;
                }
            }
        }
        if (!ArrayUtils.isEmpty(stringQuoteStrings)) {
            for (String[] quotes : stringQuoteStrings) {
                rules.add(new MultiLineRule(quotes[0], quotes[1], stringToken, escapeChar));
            }
        }
        if (!hasDoubleQuoteRule) {
            rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_DOUBLE, SQLConstants.STR_QUOTE_DOUBLE, quotedToken, escapeChar));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.QUOTES);
    }
    // Add rules for multi-line comments
    Pair<String, String> multiLineComments = dialect.getMultiLineComments();
    if (multiLineComments != null) {
        rules.add(new MultiLineRule(multiLineComments.getFirst(), multiLineComments.getSecond(), commentToken, (char) 0, true));
    }
    if (!minimalRules) {
        // Add generic whitespace rule.
        rules.add(new WhitespaceRule(TPTokenAbstract.WHITESPACE));
        // Add numeric rule
        rules.add(new NumberRule(numberToken));
    }
    SQLDelimiterRule delimRule = new SQLDelimiterRule(syntaxManager.getStatementDelimiters(), delimiterToken);
    rules.add(delimRule);
    {
        // Delimiter redefine
        String delimRedefine = dialect.getScriptDelimiterRedefiner();
        if (!CommonUtils.isEmpty(delimRedefine)) {
            final SQLSetDelimiterToken setDelimiterToken = new SQLSetDelimiterToken();
            rules.add(new SQLDelimiterSetRule(delimRedefine, setDelimiterToken, delimRule));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.KEYWORDS);
    }
    if (!minimalRules) {
        // Add word rule for keywords, functions, types, and constants.
        SQLWordRule wordRule = new SQLWordRule(delimRule, typeToken, otherToken);
        for (String reservedWord : dialect.getReservedWords()) {
            DBPKeywordType keywordType = dialect.getKeywordType(reservedWord);
            // Functions without parentheses has type 'DBPKeywordType.OTHER' (#8710)
            if (keywordType == DBPKeywordType.FUNCTION || keywordType == DBPKeywordType.OTHER) {
                wordRule.addFunction(reservedWord);
            } else {
                wordRule.addWord(reservedWord, keywordToken);
            }
        }
        if (dataSource != null) {
            for (String type : dialect.getDataTypes(dataSource)) {
                wordRule.addWord(type, typeToken);
            }
            for (String function : dialect.getFunctions(dataSource)) {
                wordRule.addFunction(function);
            }
        }
        final String[] blockHeaderStrings = dialect.getBlockHeaderStrings();
        if (!ArrayUtils.isEmpty(blockHeaderStrings)) {
            for (String bhs : blockHeaderStrings) {
                wordRule.addWord(bhs, blockHeaderToken);
            }
        }
        String[][] blockBounds = dialect.getBlockBoundStrings();
        if (blockBounds != null) {
            for (String[] block : blockBounds) {
                if (block.length != 2) {
                    continue;
                }
                wordRule.addWord(block[0], blockBeginToken);
                wordRule.addWord(block[1], blockEndToken);
            }
        }
        rules.add(wordRule);
        // Parameter rule
        for (String npPrefix : syntaxManager.getNamedParameterPrefixes()) {
            rules.add(new SQLParameterRule(syntaxManager, parameterToken, npPrefix));
        }
    }
    allRules = rules.toArray(new TPRule[0]);
}
Also used : ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) SQLCommandHandlerDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor) DBPKeywordType(org.jkiss.dbeaver.model.DBPKeywordType) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect)

Example 3 with SQLCommandHandlerDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor in project dbeaver by dbeaver.

the class SQLCompletionProcessor method makeCommandProposals.

private ICompletionProposal[] makeCommandProposals(SQLCompletionRequest request, String prefix) {
    final String controlCommandPrefix = editor.getSyntaxManager().getControlCommandPrefix();
    if (prefix.startsWith(controlCommandPrefix)) {
        prefix = prefix.substring(controlCommandPrefix.length());
    }
    final List<SQLCommandCompletionProposal> commandProposals = new ArrayList<>();
    for (SQLCommandHandlerDescriptor command : SQLCommandsRegistry.getInstance().getCommandHandlers()) {
        if (command.getId().startsWith(prefix)) {
            commandProposals.add(new SQLCommandCompletionProposal(request, command));
        }
    }
    return commandProposals.toArray(new ICompletionProposal[0]);
}
Also used : ArrayList(java.util.ArrayList) SQLCommandHandlerDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor)

Example 4 with SQLCommandHandlerDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor in project dbeaver by dbeaver.

the class SQLRuleManager method loadRules.

public void loadRules(@Nullable DBPDataSource dataSource, boolean minimalRules) {
    SQLDialect dialect = syntaxManager.getDialect();
    TPRuleProvider ruleProvider = GeneralUtils.adapt(dialect, TPRuleProvider.class);
    DBPDataSourceContainer dataSourceContainer = dataSource == null ? null : dataSource.getContainer();
    final TPToken keywordToken = new TPTokenDefault(SQLTokenType.T_KEYWORD);
    final TPToken typeToken = new TPTokenDefault(SQLTokenType.T_TYPE);
    final TPToken stringToken = new TPTokenDefault(SQLTokenType.T_STRING);
    final TPToken quotedToken = new TPTokenDefault(SQLTokenType.T_QUOTED);
    final TPToken numberToken = new TPTokenDefault(SQLTokenType.T_NUMBER);
    final TPToken commentToken = new SQLCommentToken();
    final SQLDelimiterToken delimiterToken = new SQLDelimiterToken();
    final SQLParameterToken parameterToken = new SQLParameterToken();
    final SQLVariableToken variableToken = new SQLVariableToken();
    final TPToken otherToken = new TPTokenDefault(SQLTokenType.T_OTHER);
    final SQLBlockHeaderToken blockHeaderToken = new SQLBlockHeaderToken();
    final SQLBlockBeginToken blockBeginToken = new SQLBlockBeginToken();
    final SQLBlockEndToken blockEndToken = new SQLBlockEndToken();
    List<TPRule> rules = new ArrayList<>();
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.INITIAL);
    }
    // Add rule for single-line comments.
    for (String lineComment : dialect.getSingleLineComments()) {
        if (lineComment.startsWith("^")) {
            rules.add(new LineCommentRule(lineComment, commentToken, (char) 0, false, true));
        } else {
            rules.add(new EndOfLineRule(lineComment, commentToken, (char) 0, false, true));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.CONTROL);
    }
    if (!minimalRules) {
        final SQLControlToken controlToken = new SQLControlToken();
        String commandPrefix = syntaxManager.getControlCommandPrefix();
        // Control rules
        for (SQLCommandHandlerDescriptor controlCommand : SQLCommandsRegistry.getInstance().getCommandHandlers()) {
            // $NON-NLS-1$
            rules.add(new SQLCommandRule(commandPrefix, controlCommand, controlToken));
        }
    }
    {
        if (!minimalRules && syntaxManager.isVariablesEnabled()) {
            // Variable rule
            rules.add(new SQLVariableRule(parameterToken));
        }
    }
    if (!minimalRules) {
        // Add rules for delimited identifiers and string literals.
        char escapeChar = syntaxManager.getEscapeChar();
        String[][] identifierQuoteStrings = syntaxManager.getIdentifierQuoteStrings();
        String[][] stringQuoteStrings = syntaxManager.getStringQuoteStrings();
        boolean hasDoubleQuoteRule = false;
        if (!ArrayUtils.isEmpty(identifierQuoteStrings)) {
            for (String[] quotes : identifierQuoteStrings) {
                rules.add(new SingleLineRule(quotes[0], quotes[1], quotedToken, escapeChar));
                if (quotes[1].equals(SQLConstants.STR_QUOTE_DOUBLE) && quotes[0].equals(quotes[1])) {
                    hasDoubleQuoteRule = true;
                }
            }
        }
        if (!ArrayUtils.isEmpty(stringQuoteStrings)) {
            for (String[] quotes : stringQuoteStrings) {
                rules.add(new MultiLineRule(quotes[0], quotes[1], stringToken, escapeChar));
            }
        }
        if (!hasDoubleQuoteRule) {
            rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_DOUBLE, SQLConstants.STR_QUOTE_DOUBLE, quotedToken, escapeChar));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.QUOTES);
    }
    // Add rules for multi-line comments
    Pair<String, String> multiLineComments = dialect.getMultiLineComments();
    if (multiLineComments != null) {
        rules.add(new MultiLineRule(multiLineComments.getFirst(), multiLineComments.getSecond(), commentToken, (char) 0, true));
    }
    if (!minimalRules) {
        // Add generic whitespace rule.
        rules.add(new WhitespaceRule(TPTokenAbstract.WHITESPACE));
        // Add numeric rule
        rules.add(new NumberRule(numberToken));
    }
    SQLDelimiterRule delimRule = new SQLDelimiterRule(syntaxManager.getStatementDelimiters(), delimiterToken);
    rules.add(delimRule);
    {
        // Delimiter redefine
        String delimRedefine = dialect.getScriptDelimiterRedefiner();
        if (!CommonUtils.isEmpty(delimRedefine)) {
            final SQLSetDelimiterToken setDelimiterToken = new SQLSetDelimiterToken();
            rules.add(new SQLDelimiterSetRule(delimRedefine, setDelimiterToken, delimRule));
        }
    }
    if (ruleProvider != null) {
        ruleProvider.extendRules(dataSourceContainer, rules, TPRuleProvider.RulePosition.KEYWORDS);
    }
    if (!minimalRules) {
        // Add word rule for keywords, functions, types, and constants.
        SQLWordRule wordRule = new SQLWordRule(delimRule, typeToken, otherToken);
        for (String reservedWord : dialect.getReservedWords()) {
            DBPKeywordType keywordType = dialect.getKeywordType(reservedWord);
            // Functions without parentheses has type 'DBPKeywordType.OTHER' (#8710)
            if (keywordType == DBPKeywordType.FUNCTION || keywordType == DBPKeywordType.OTHER) {
                wordRule.addFunction(reservedWord);
            } else {
                wordRule.addWord(reservedWord, keywordToken);
            }
        }
        if (dataSource != null) {
            for (String type : dialect.getDataTypes(dataSource)) {
                wordRule.addWord(type, typeToken);
            }
            for (String function : dialect.getFunctions(dataSource)) {
                wordRule.addFunction(function);
            }
        }
        final String[] blockHeaderStrings = dialect.getBlockHeaderStrings();
        if (!ArrayUtils.isEmpty(blockHeaderStrings)) {
            for (String bhs : blockHeaderStrings) {
                wordRule.addWord(bhs, blockHeaderToken);
            }
        }
        String[][] blockBounds = dialect.getBlockBoundStrings();
        if (blockBounds != null) {
            for (String[] block : blockBounds) {
                if (block.length != 2) {
                    continue;
                }
                wordRule.addWord(block[0], blockBeginToken);
                wordRule.addWord(block[1], blockEndToken);
            }
        }
        rules.add(wordRule);
        // Parameter rule
        for (String npPrefix : syntaxManager.getNamedParameterPrefixes()) {
            rules.add(new SQLParameterRule(syntaxManager, parameterToken, npPrefix));
        }
    }
    allRules = rules.toArray(new TPRule[0]);
}
Also used : ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) SQLCommandHandlerDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor) DBPKeywordType(org.jkiss.dbeaver.model.DBPKeywordType) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect)

Aggregations

ArrayList (java.util.ArrayList)4 SQLCommandHandlerDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLCommandHandlerDescriptor)4 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBPKeywordType (org.jkiss.dbeaver.model.DBPKeywordType)2 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)2