Search in sources :

Example 11 with SQLDialect

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

the class DBUtils method getQuotedIdentifier.

@NotNull
public static String getQuotedIdentifier(@NotNull DBPDataSource dataSource, @NotNull String str, boolean caseSensitiveNames, boolean quoteAlways) {
    if (isQuotedIdentifier(dataSource, str)) {
        // Already quoted
        return str;
    }
    final SQLDialect sqlDialect = dataSource.getSQLDialect();
    String[][] quoteStrings = sqlDialect.getIdentifierQuoteStrings();
    if (ArrayUtils.isEmpty(quoteStrings)) {
        return str;
    }
    // Check for keyword conflict
    final DBPKeywordType keywordType = sqlDialect.getKeywordType(str);
    boolean hasBadChars = quoteAlways || ((keywordType == DBPKeywordType.KEYWORD || keywordType == DBPKeywordType.TYPE || keywordType == DBPKeywordType.OTHER) && sqlDialect.isQuoteReservedWords());
    if (!hasBadChars && !str.isEmpty()) {
        hasBadChars = !sqlDialect.validIdentifierStart(str.charAt(0));
    }
    if (!hasBadChars && caseSensitiveNames) {
        // unless database use case-insensitive search always (e.g. MySL with lower_case_table_names <> 0)
        if (!sqlDialect.useCaseInsensitiveNameLookup()) {
            // If passed identifier case differs from unquoted then we need to escape it
            switch(sqlDialect.storesUnquotedCase()) {
                case UPPER:
                    hasBadChars = !str.equals(str.toUpperCase());
                    break;
                case LOWER:
                    hasBadChars = !str.equals(str.toLowerCase());
                    break;
            }
        }
    }
    // Check for bad characters
    if (!hasBadChars && !str.isEmpty()) {
        for (int i = 0; i < str.length(); i++) {
            if (!sqlDialect.validIdentifierPart(str.charAt(i), false)) {
                hasBadChars = true;
                break;
            }
        }
    }
    if (!hasBadChars) {
        return str;
    }
    // Escape quote chars
    for (int i = 0; i < quoteStrings.length; i++) {
        String q1 = quoteStrings[i][0], q2 = quoteStrings[i][1];
        if (q1.equals(q2) && (q1.equals("\"") || q1.equals("'"))) {
            if (str.contains(q1)) {
                str = str.replace(q1, q1 + q1);
            }
        }
    }
    // Escape with first (default) quote string
    return quoteStrings[0][0] + str + quoteStrings[0][1];
}
Also used : BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) DBVEntityConstraint(org.jkiss.dbeaver.model.virtual.DBVEntityConstraint) NotNull(org.jkiss.code.NotNull)

Example 12 with SQLDialect

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

the class GroupingResultsDecorator method getEmptyDataDescription.

@Override
public String getEmptyDataDescription() {
    DBPDataSource dataSource = container.getResultSetController().getDataContainer().getDataSource();
    if (dataSource == null) {
        return ResultSetMessages.results_decorator_no_connected_to_db;
    }
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    /*if (dialect == null) {
            return NLS.bind(ResultSetMessages.results_decorator_grouping_is_not_supported, dataSource.getContainer().getDriver().getFullName());
        } else */
    {
        if (container.getGroupAttributes().isEmpty()) {
            return ResultSetMessages.results_decorator_drag_and_drop_results_column;
        } else {
            return ResultSetMessages.results_decorator_grouping_attempt_failed;
        }
    }
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 13 with SQLDialect

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

the class DBStructUtils method mapTargetDataType.

public static String mapTargetDataType(DBSObject objectContainer, DBSTypedObject typedObject, boolean addModifiers) {
    if (typedObject instanceof DBSObject) {
        // If source and target datasources have the same type then just return the same type name
        if (((DBSObject) typedObject).getDataSource().getClass() == objectContainer.getDataSource().getClass() && addModifiers) {
            return typedObject.getFullTypeName();
        }
    }
    String typeName = typedObject.getTypeName();
    String typeNameLower = typeName.toLowerCase(Locale.ENGLISH);
    DBPDataKind dataKind = typedObject.getDataKind();
    if (objectContainer instanceof DBPDataTypeProvider) {
        DBPDataTypeProvider dataTypeProvider = (DBPDataTypeProvider) objectContainer;
        DBSDataType dataType = dataTypeProvider.getLocalDataType(typeName);
        if (dataType == null && typeNameLower.equals("double")) {
            dataType = dataTypeProvider.getLocalDataType("DOUBLE PRECISION");
            if (dataType != null) {
                typeName = dataType.getTypeName();
            }
        }
        if (dataType != null && !DBPDataKind.canConsume(dataKind, dataType.getDataKind())) {
            // Type mismatch
            dataType = null;
        }
        if (dataType == null) {
            // Type not supported by target database
            // Let's try to find something similar
            Map<String, DBSDataType> possibleTypes = new HashMap<>();
            for (DBSDataType type : dataTypeProvider.getLocalDataTypes()) {
                if (DBPDataKind.canConsume(type.getDataKind(), dataKind)) {
                    possibleTypes.put(type.getTypeName().toLowerCase(Locale.ENGLISH), type);
                }
            }
            DBSDataType targetType = null;
            if (!possibleTypes.isEmpty()) {
                // Try to get any partial match
                targetType = possibleTypes.get(typeNameLower);
                if (targetType == null && dataKind == DBPDataKind.NUMERIC) {
                    // Try to find appropriate type with the same scale/precision
                    for (DBSDataType type : possibleTypes.values()) {
                        if (CommonUtils.equalObjects(type.getScale(), typedObject.getScale()) && CommonUtils.equalObjects(type.getPrecision(), typedObject.getPrecision())) {
                            targetType = type;
                            break;
                        }
                    }
                    if (targetType == null) {
                        if (typeNameLower.contains("float")) {
                            for (String psn : possibleTypes.keySet()) {
                                if (psn.contains("float")) {
                                    targetType = possibleTypes.get(psn);
                                    break;
                                }
                            }
                        } else if (typeNameLower.contains("double")) {
                            for (String psn : possibleTypes.keySet()) {
                                if (psn.contains("double")) {
                                    targetType = possibleTypes.get(psn);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (targetType == null) {
                typeName = DBUtils.getDefaultDataTypeName(objectContainer, dataKind);
                typeNameLower = typeName.toLowerCase(Locale.ENGLISH);
                if (!possibleTypes.isEmpty()) {
                    targetType = possibleTypes.get(typeNameLower);
                }
            }
            if (targetType == null && !possibleTypes.isEmpty()) {
                targetType = possibleTypes.values().iterator().next();
            }
            if (targetType != null) {
                typeName = targetType.getTypeName();
            }
        }
        if (dataType != null) {
            dataKind = dataType.getDataKind();
        }
    }
    // Get type modifiers from target datasource
    if (addModifiers && objectContainer instanceof DBPDataSource) {
        SQLDialect dialect = ((DBPDataSource) objectContainer).getSQLDialect();
        String modifiers = dialect.getColumnTypeModifiers((DBPDataSource) objectContainer, typedObject, typeName, dataKind);
        if (modifiers != null) {
            typeName += modifiers;
        }
    }
    return typeName;
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect)

Example 14 with SQLDialect

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

the class BaseSQLDialog method createSQLPanel.

protected Composite createSQLPanel(Composite parent) {
    Composite panel = UIUtils.createPlaceholder(parent, 1);
    panel.setLayoutData(new GridData(GridData.FILL_BOTH));
    if (isLabelVisible()) {
        UIUtils.createControlLabel(panel, SQLEditorMessages.pref_page_sql_format_label_SQLPreview);
    }
    // new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Composite editorPH = new Composite(panel, SWT.BORDER);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.verticalIndent = 3;
    gd.horizontalSpan = 1;
    gd.minimumHeight = 100;
    gd.minimumWidth = 100;
    editorPH.setLayoutData(gd);
    editorPH.setLayout(new FillLayout());
    sqlViewer = new SQLEditorBase() {

        @NotNull
        @Override
        public SQLDialect getSQLDialect() {
            return BaseSQLDialog.this.getSQLDialect();
        }

        @Override
        public DBCExecutionContext getExecutionContext() {
            return BaseSQLDialog.this.getExecutionContext();
        }
    };
    updateSQL();
    sqlViewer.createPartControl(editorPH);
    if (isWordWrap()) {
        Object text = sqlViewer.getAdapter(Control.class);
        if (text instanceof StyledText) {
            ((StyledText) text).setWordWrap(true);
        }
    }
    sqlViewer.reloadSyntaxRules();
    return panel;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) NotNull(org.jkiss.code.NotNull)

Example 15 with SQLDialect

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

the class JDBCUtils method escapeWildCards.

public static String escapeWildCards(JDBCSession session, String string) {
    if (string == null || string.isEmpty() || (string.indexOf('%') == -1 && string.indexOf('_') == -1)) {
        return string;
    }
    try {
        SQLDialect dialect = SQLUtils.getDialectFromDataSource(session.getDataSource());
        String escapeStr = dialect.getSearchStringEscape();
        if (CommonUtils.isEmpty(escapeStr) || escapeStr.equals(" ")) {
            return string;
        }
        return string.replace("%", escapeStr + "%").replace("_", escapeStr + "_");
    } catch (Throwable e) {
        log.debug("Error escaping wildcard string", e);
        return string;
    }
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect)

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