Search in sources :

Example 1 with DBPKeywordType

use of org.jkiss.dbeaver.model.DBPKeywordType in project dbeaver by serge-rider.

the class BasicSQLDialect method addKeywords.

/**
     * Add keywords.
     * @param set     keywords. Must be in upper case.
     * @param type    keyword type
     */
public void addKeywords(Collection<String> set, DBPKeywordType type) {
    if (set != null) {
        for (String keyword : set) {
            keyword = keyword.toUpperCase(Locale.ENGLISH);
            reservedWords.add(keyword);
            DBPKeywordType oldType = allKeywords.get(keyword);
            if (oldType != DBPKeywordType.KEYWORD) {
                // We can't mark keywords as functions or types because keywords are reserved and
                // if some identifier conflicts with keyword it must be quoted.
                allKeywords.put(keyword, type);
            }
        }
    }
}
Also used : DBPKeywordType(org.jkiss.dbeaver.model.DBPKeywordType)

Example 2 with DBPKeywordType

use of org.jkiss.dbeaver.model.DBPKeywordType 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)

Aggregations

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