Search in sources :

Example 6 with SQLDialect

use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by dbeaver.

the class GenerateSQLContributor method CALL_GENERATOR.

@NotNull
public static SQLGenerator<DBSProcedure> CALL_GENERATOR(final List<DBSProcedure> entities) {
    return new ProcedureAnalysisRunner(entities) {

        @Override
        protected void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, DBSProcedure proc) throws DBException {
            Collection<? extends DBSProcedureParameter> parameters = proc.getParameters(monitor);
            DBPDataSource dataSource = proc.getDataSource();
            if (dataSource instanceof SQLDataSource) {
                SQLDataSource sqlDataSource = (SQLDataSource) dataSource;
                SQLDialect sqlDialect = sqlDataSource.getSQLDialect();
                sqlDialect.generateStoredProcedureCall(sql, proc, parameters);
            }
        }
    };
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) DBSProcedure(org.jkiss.dbeaver.model.struct.rdb.DBSProcedure) NotNull(org.jkiss.code.NotNull)

Example 7 with SQLDialect

use of org.jkiss.dbeaver.model.sql.SQLDialect in project dbeaver by dbeaver.

the class SQLRuleManager method refreshRules.

public void refreshRules(@Nullable DBPDataSource dataSource, IEditorInput editorInput) {
    SQLDialect dialect = syntaxManager.getDialect();
    SQLRuleProvider ruleProvider = null;
    if (dialect instanceof SQLRuleProvider) {
        ruleProvider = (SQLRuleProvider) dialect;
    }
    boolean minimalRules = false;
    File file = EditorUtils.getLocalFileFromInput(editorInput);
    if (file != null && file.length() > MAX_FILE_LENGTH_FOR_RULES) {
        minimalRules = true;
    }
    /*final Color backgroundColor = null;unassigned || dataSource != null ?
            getColor(SQLConstants.CONFIG_COLOR_BACKGROUND, SWT.COLOR_WHITE) :
            getColor(SQLConstants.CONFIG_COLOR_DISABLED, SWT.COLOR_WIDGET_LIGHT_SHADOW);*/
    final IToken keywordToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final IToken typeToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DATATYPE), null, SWT.BOLD));
    final IToken stringToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_STRING), null, SWT.NORMAL));
    final IToken quotedToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DATATYPE), null, SWT.NORMAL));
    final IToken numberToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_NUMBER), null, SWT.NORMAL));
    final IToken commentToken = new SQLCommentToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_COMMENT), null, SWT.NORMAL));
    final SQLDelimiterToken delimiterToken = new SQLDelimiterToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DELIMITER, SWT.COLOR_RED), null, SWT.NORMAL));
    final SQLParameterToken parameterToken = new SQLParameterToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_PARAMETER, SWT.COLOR_DARK_BLUE), null, SWT.BOLD));
    final SQLVariableToken variableToken = new SQLVariableToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_PARAMETER, SWT.COLOR_DARK_BLUE), null, SWT.BOLD));
    final IToken otherToken = new Token(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_TEXT), null, SWT.NORMAL));
    final SQLBlockHeaderToken blockHeaderToken = new SQLBlockHeaderToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockBeginToken blockBeginToken = new SQLBlockBeginToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockEndToken blockEndToken = new SQLBlockEndToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_KEYWORD), null, SWT.BOLD));
    final SQLBlockToggleToken blockToggleToken = new SQLBlockToggleToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_DELIMITER), null, SWT.BOLD));
    setDefaultReturnToken(otherToken);
    List<IRule> rules = new ArrayList<>();
    if (ruleProvider != null) {
        ruleProvider.extendRules(rules, SQLRuleProvider.RulePosition.INITIAL);
    }
    // Add rule for single-line comments.
    for (String lineComment : dialect.getSingleLineComments()) {
        if (lineComment.startsWith("^")) {
            // $NON-NLS-1$
            rules.add(new LineCommentRule(lineComment, commentToken));
        } else {
            // $NON-NLS-1$
            rules.add(new EndOfLineRule(lineComment, commentToken));
        }
    }
    {
        final SQLControlToken controlToken = new SQLControlToken(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_COMMAND), null, SWT.BOLD));
        if (ruleProvider != null) {
            ruleProvider.extendRules(rules, SQLRuleProvider.RulePosition.CONTROL);
        }
        String commandPrefix = syntaxManager.getControlCommandPrefix();
        // Control rules
        for (SQLCommandHandlerDescriptor controlCommand : SQLCommandsRegistry.getInstance().getCommandHandlers()) {
            // $NON-NLS-1$
            rules.add(new EndOfLineRule(commandPrefix + controlCommand.getId(), controlToken));
        }
    }
    {
        if (!minimalRules && syntaxManager.isVariablesEnabled()) {
            // Variable rule
            rules.add(new SQLVariableRule(parameterToken));
        }
    }
    {
        // Add rules for delimited identifiers and string literals.
        char escapeChar = syntaxManager.getEscapeChar();
        String[][] quoteStrings = syntaxManager.getQuoteStrings();
        boolean hasSingleQuoteRule = false, hasDoubleQuoteRule = false;
        if (!ArrayUtils.isEmpty(quoteStrings)) {
            for (int i = 0; i < quoteStrings.length; i++) {
                rules.add(new SingleLineRule(quoteStrings[i][0], quoteStrings[i][1], quotedToken, escapeChar));
                if (quoteStrings[i][0].equals(SQLConstants.STR_QUOTE_SINGLE) && quoteStrings[i][0].equals(quoteStrings[i][1])) {
                    hasSingleQuoteRule = true;
                } else if (quoteStrings[i][1].equals(SQLConstants.STR_QUOTE_DOUBLE) && quoteStrings[i][0].equals(quoteStrings[i][1])) {
                    hasDoubleQuoteRule = true;
                }
            }
        }
        if (!hasSingleQuoteRule) {
            rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_SINGLE, SQLConstants.STR_QUOTE_SINGLE, stringToken, escapeChar));
        }
        if (!hasDoubleQuoteRule) {
            rules.add(new MultiLineRule(SQLConstants.STR_QUOTE_DOUBLE, SQLConstants.STR_QUOTE_DOUBLE, quotedToken, escapeChar));
        }
    }
    // 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));
    }
    // Add generic whitespace rule.
    rules.add(new WhitespaceRule(new TextWhiteSpaceDetector()));
    if (!minimalRules) {
        // 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(new TextAttribute(getColor(SQLConstants.CONFIG_COLOR_COMMAND), null, SWT.BOLD));
            rules.add(new SQLDelimiterSetRule(delimRedefine, setDelimiterToken, delimRule));
        }
    }
    final String blockToggleString = dialect.getBlockToggleString();
    if (!CommonUtils.isEmpty(blockToggleString)) {
        int divPos = blockToggleString.indexOf(SQLConstants.KEYWORD_PATTERN_CHARS);
        if (divPos != -1) {
            String prefix = blockToggleString.substring(0, divPos);
            String postfix = blockToggleString.substring(divPos + SQLConstants.KEYWORD_PATTERN_CHARS.length());
            WordPatternRule blockToggleRule = new WordPatternRule(new SQLWordDetector(), prefix, postfix, blockToggleToken);
            rules.add(blockToggleRule);
        } else {
            WordRule blockToggleRule = new WordRule(getWordOrSymbolDetector(blockToggleString), Token.UNDEFINED, true);
            blockToggleRule.addWord(blockToggleString, blockToggleToken);
            rules.add(blockToggleRule);
        }
    }
    if (!minimalRules) {
        if (ruleProvider != null) {
            ruleProvider.extendRules(rules, SQLRuleProvider.RulePosition.KEYWORDS);
        }
        // Add word rule for keywords, types, and constants.
        SQLWordRule wordRule = new SQLWordRule(delimRule, otherToken);
        for (String reservedWord : dialect.getReservedWords()) {
            wordRule.addWord(reservedWord, keywordToken);
        }
        if (dataSource != null) {
            for (String function : dialect.getFunctions(dataSource)) {
                wordRule.addWord(function, typeToken);
            }
            for (String type : dialect.getDataTypes(dataSource)) {
                wordRule.addWord(type, typeToken);
            }
        }
        final String blockHeaderString = dialect.getBlockHeaderString();
        if (!CommonUtils.isEmpty(blockHeaderString)) {
            wordRule.addWord(blockHeaderString, 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);
    }
    if (!minimalRules) {
        // Parameter rule
        rules.add(new SQLParameterRule(syntaxManager, parameterToken));
    }
    IRule[] result = new IRule[rules.size()];
    rules.toArray(result);
    setRules(result);
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) SQLRuleProvider(org.jkiss.dbeaver.runtime.sql.SQLRuleProvider) TextWhiteSpaceDetector(org.jkiss.dbeaver.ui.editors.text.TextWhiteSpaceDetector) SQLCommandHandlerDescriptor(org.jkiss.dbeaver.registry.sql.SQLCommandHandlerDescriptor) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) File(java.io.File)

Example 8 with SQLDialect

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

the class QueryTransformerLimit method transformQueryString.

@Override
public String transformQueryString(SQLQuery query) throws DBCException {
    String newQuery;
    String testQuery = query.getText().toUpperCase().trim();
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(query.getDataSource());
    boolean plainSelect = query.isPlainSelect();
    if (!plainSelect && query.getType() == SQLQueryType.UNKNOWN) {
        // Not parsed. Try to check with simple matcher
        plainSelect = "SELECT".equals(SQLUtils.getFirstKeyword(dialect, testQuery));
    }
    if (plainSelect) {
        plainSelect = !NON_LIMIT_QUERY_PATTERN.matcher(testQuery).find();
    }
    if (!plainSelect) {
        // Do not use limit if it is not a select or it already has LIMIT or it is SELECT INTO statement
        limitSet = false;
        newQuery = query.getText();
    } else {
        if (supportsExtendedLimit) {
            newQuery = query.getText() + "\n" + KEYWORD_LIMIT + " " + offset + ", " + length;
        } else if (supportsOffsetKeyword) {
            // LIMIT + OFFSET
            newQuery = query.getText() + "\n" + KEYWORD_LIMIT + " " + length.longValue();
            if (offset.longValue() > 0) {
                newQuery += " " + KEYWORD_OFFSET + " " + offset.longValue();
            }
        } else {
            // We can limit only total row number
            newQuery = query.getText() + "\n" + KEYWORD_LIMIT + " " + (offset.longValue() + length.longValue());
        }
        limitSet = supportsExtendedLimit || supportsOffsetKeyword;
    }
    return newQuery;
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect)

Example 9 with SQLDialect

use of org.jkiss.dbeaver.model.sql.SQLDialect 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;
        }
    }
}
Also used : SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) ITextViewer(org.eclipse.jface.text.ITextViewer) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with SQLDialect

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

the class DBUtils method getFullyQualifiedName.

@NotNull
public static String getFullyQualifiedName(@NotNull DBPDataSource dataSource, @NotNull String... names) {
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    StringBuilder name = new StringBuilder(names.length * 16);
    for (String namePart : names) {
        if (namePart == null) {
            continue;
        }
        if (name.length() > 0)
            name.append(dialect.getStructSeparator());
        name.append(DBUtils.getQuotedIdentifier(dataSource, namePart));
    }
    return name.toString();
}
Also used : BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) NotNull(org.jkiss.code.NotNull)

Aggregations

SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)27 NotNull (org.jkiss.code.NotNull)12 BasicSQLDialect (org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect)6 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)5 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)4 ExecuteBatchImpl (org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl)4 JDBCSQLDialect (org.jkiss.dbeaver.model.impl.jdbc.JDBCSQLDialect)4 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)4 BadLocationException (org.eclipse.jface.text.BadLocationException)3 IDocument (org.eclipse.jface.text.IDocument)3 StyledText (org.eclipse.swt.custom.StyledText)3 FillLayout (org.eclipse.swt.layout.FillLayout)3 GridData (org.eclipse.swt.layout.GridData)3 Composite (org.eclipse.swt.widgets.Composite)3 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)3 SQLEditorBase (org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase)3 File (java.io.File)2 Map (java.util.Map)2 ITextViewer (org.eclipse.jface.text.ITextViewer)2 TextAttribute (org.eclipse.jface.text.TextAttribute)2