Search in sources :

Example 6 with SQLDataSource

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

the class DBObjectNameCaseTransformer method transformName.

@Nullable
public static String transformName(@NotNull DBPDataSource dataSource, @Nullable String value) {
    if (value == null) {
        return null;
    }
    final boolean isNameCaseSensitive = dataSource.getContainer().getPreferenceStore().getBoolean(ModelPreferences.META_CASE_SENSITIVE);
    if (isNameCaseSensitive || !(dataSource instanceof SQLDataSource)) {
        return value;
    }
    final SQLDialect dialect = ((SQLDataSource) dataSource).getSQLDialect();
    if (DBUtils.isQuotedIdentifier(dataSource, value)) {
        if (dialect.supportsQuotedMixedCase()) {
            return value;
        }
    }
    if (dialect.supportsUnquotedMixedCase()) {
        return value;
    }
    String xName = dialect.storesUnquotedCase().transform(value);
    if (!DBUtils.getQuotedIdentifier(dataSource, xName).equals(xName)) {
        // Name contains special characters and has to be quoted - leave it as is
        return value;
    }
    return xName;
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) Nullable(org.jkiss.code.Nullable)

Example 7 with SQLDataSource

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

the class GenerateSQLDialog method getSQLText.

@Override
protected String getSQLText() {
    DBPDataSource dataSource = executionContext.getDataSource();
    if (dataSource instanceof SQLDataSource) {
        String lineSeparator = GeneralUtils.getDefaultLineSeparator();
        String scriptDelimiter = ((SQLDataSource) dataSource).getSQLDialect().getScriptDelimiter() + lineSeparator;
        String[] scriptLines = generateSQLScript();
        StringBuilder sql = new StringBuilder(scriptLines.length * 64);
        for (String line : scriptLines) {
            sql.append(line).append(scriptDelimiter);
        }
        // Cut last line separator
        if (sql.length() > lineSeparator.length()) {
            sql.setLength(sql.length() - lineSeparator.length());
        }
        return sql.toString();
    } else {
        return SQLUtils.generateCommentLine(dataSource, "Non-SQL data source");
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource)

Example 8 with SQLDataSource

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

the class JDBCTable method readData.

@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    boolean hasLimits = firstRow >= 0 && maxRows > 0;
    DBPDataSource dataSource = session.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try {
        readRequiredMeta(monitor);
    } catch (DBException e) {
        log.warn(e);
    }
    DBDPseudoAttribute rowIdAttribute = (flags & FLAG_READ_PSEUDO) != 0 ? DBUtils.getRowIdAttribute(this) : null;
    // Always use alias if we have criteria or ROWID.
    // Some criteria doesn't work without alias
    // (e.g. structured attributes in Oracle requires table alias)
    String tableAlias = null;
    if ((dataFilter != null && dataFilter.hasConditions()) || rowIdAttribute != null) {
        if (dataSource instanceof SQLDataSource) {
            if (((SQLDataSource) dataSource).getSQLDialect().supportsAliasInSelect()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
        }
    }
    if (rowIdAttribute != null && tableAlias == null) {
        log.warn("Can't query ROWID - table alias not supported");
        rowIdAttribute = null;
    }
    StringBuilder query = new StringBuilder(100);
    query.append("SELECT ");
    appendSelectSource(monitor, query, tableAlias, rowIdAttribute);
    query.append(" FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
    if (tableAlias != null) {
        //$NON-NLS-1$
        query.append(" ").append(tableAlias);
    }
    appendQueryConditions(query, tableAlias, dataFilter);
    appendQueryOrder(query, tableAlias, dataFilter);
    String sqlQuery = query.toString();
    statistics.setQueryText(sqlQuery);
    monitor.subTask(ModelMessages.model_jdbc_fetch_table_data);
    try (DBCStatement dbStat = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        if (monitor.isCanceled()) {
            return statistics;
        }
        if (dbStat instanceof JDBCStatement && maxRows > 0) {
            boolean useFetchSize = getDataSource().getContainer().getPreferenceStore().getBoolean(ModelPreferences.RESULT_SET_USE_FETCH_SIZE);
            if (useFetchSize) {
                try {
                    ((JDBCStatement) dbStat).setFetchSize(firstRow < 0 || maxRows <= 0 ? DEFAULT_READ_FETCH_SIZE : (int) (firstRow + maxRows));
                } catch (Exception e) {
                    log.warn(e);
                }
            }
        }
        long startTime = System.currentTimeMillis();
        boolean executeResult = dbStat.executeStatement();
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        if (executeResult) {
            DBCResultSet dbResult = dbStat.openResultSet();
            if (dbResult != null && !monitor.isCanceled()) {
                try {
                    dataReceiver.fetchStart(session, dbResult, firstRow, maxRows);
                    startTime = System.currentTimeMillis();
                    long rowCount = 0;
                    while (dbResult.nextRow()) {
                        if (monitor.isCanceled() || (hasLimits && rowCount >= maxRows)) {
                            // Fetch not more than max rows
                            break;
                        }
                        dataReceiver.fetchRow(session, dbResult);
                        rowCount++;
                        if (rowCount % 100 == 0) {
                            monitor.subTask(rowCount + ModelMessages.model_jdbc__rows_fetched);
                            monitor.worked(100);
                        }
                    }
                    statistics.setFetchTime(System.currentTimeMillis() - startTime);
                    statistics.setRowsFetched(rowCount);
                } finally {
                    // First - close cursor
                    try {
                        dbResult.close();
                    } catch (Throwable e) {
                        //$NON-NLS-1$
                        log.error("Error closing result set", e);
                    }
                    // Then - signal that fetch was ended
                    try {
                        dataReceiver.fetchEnd(session, dbResult);
                    } catch (Throwable e) {
                        //$NON-NLS-1$
                        log.error("Error while finishing result set fetch", e);
                    }
                }
            }
        }
        return statistics;
    } finally {
        dataReceiver.close();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBException(org.jkiss.dbeaver.DBException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) NotNull(org.jkiss.code.NotNull)

Example 9 with SQLDataSource

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

the class JDBCTable method deleteData.

@NotNull
@Override
public ExecuteBatch deleteData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] keyAttributes, @NotNull final DBCExecutionSource source) throws DBCException {
    readRequiredMeta(session.getProgressMonitor());
    return new ExecuteBatchImpl(keyAttributes, null, false) {

        @NotNull
        @Override
        protected DBCStatement prepareStatement(@NotNull DBCSession session, Object[] attributeValues) throws DBCException {
            String tableAlias = null;
            SQLDialect dialect = ((SQLDataSource) session.getDataSource()).getSQLDialect();
            if (dialect.supportsAliasInUpdate()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
            // Make query
            StringBuilder query = new StringBuilder();
            query.append("DELETE FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
            if (tableAlias != null) {
                query.append(' ').append(tableAlias);
            }
            //$NON-NLS-1$ //$NON-NLS-2$
            query.append("\nWHERE ");
            boolean hasKey = false;
            for (int i = 0; i < keyAttributes.length; i++) {
                //$NON-NLS-1$
                if (hasKey)
                    query.append(" AND ");
                hasKey = true;
                appendAttributeCriteria(tableAlias, dialect, query, keyAttributes[i], attributeValues[i]);
            }
            // Execute
            DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false);
            dbStat.setStatementSource(source);
            return dbStat;
        }

        @Override
        protected void bindStatement(@NotNull DBDValueHandler[] handlers, @NotNull DBCStatement statement, Object[] attributeValues) throws DBCException {
            int paramIndex = 0;
            for (int k = 0; k < handlers.length; k++) {
                DBSAttributeBase attribute = attributes[k];
                if (DBUtils.isNullValue(attributeValues[k])) {
                    // Skip NULL criteria binding
                    continue;
                }
                handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
            }
        }
    };
}
Also used : ExecuteBatchImpl(org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl) JDBCSQLDialect(org.jkiss.dbeaver.model.impl.jdbc.JDBCSQLDialect) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) NotNull(org.jkiss.code.NotNull) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Example 10 with SQLDataSource

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

the class JDBCTable method updateData.

@NotNull
@Override
public ExecuteBatch updateData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] updateAttributes, @NotNull final DBSAttributeBase[] keyAttributes, @Nullable DBDDataReceiver keysReceiver, @NotNull final DBCExecutionSource source) throws DBCException {
    if (useUpsert(session)) {
        return insertData(session, ArrayUtils.concatArrays(updateAttributes, keyAttributes), keysReceiver, source);
    }
    readRequiredMeta(session.getProgressMonitor());
    DBSAttributeBase[] attributes = ArrayUtils.concatArrays(updateAttributes, keyAttributes);
    return new ExecuteBatchImpl(attributes, keysReceiver, false) {

        @NotNull
        @Override
        protected DBCStatement prepareStatement(@NotNull DBCSession session, Object[] attributeValues) throws DBCException {
            String tableAlias = null;
            SQLDialect dialect = ((SQLDataSource) session.getDataSource()).getSQLDialect();
            if (dialect.supportsAliasInUpdate()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
            // Make query
            StringBuilder query = new StringBuilder();
            query.append("UPDATE ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
            if (tableAlias != null) {
                query.append(' ').append(tableAlias);
            }
            //$NON-NLS-1$ //$NON-NLS-2$
            query.append("\nSET ");
            boolean hasKey = false;
            for (DBSAttributeBase attribute : updateAttributes) {
                //$NON-NLS-1$
                if (hasKey)
                    query.append(",");
                hasKey = true;
                if (tableAlias != null) {
                    query.append(tableAlias).append(dialect.getStructSeparator());
                }
                //$NON-NLS-1$
                query.append(getAttributeName(attribute)).append("=?");
            }
            //$NON-NLS-1$
            query.append("\nWHERE ");
            hasKey = false;
            for (int i = 0; i < keyAttributes.length; i++) {
                DBSAttributeBase attribute = keyAttributes[i];
                //$NON-NLS-1$
                if (hasKey)
                    query.append(" AND ");
                hasKey = true;
                appendAttributeCriteria(tableAlias, dialect, query, attribute, attributeValues[updateAttributes.length + i]);
            }
            // Execute
            DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, keysReceiver != null);
            dbStat.setStatementSource(source);
            return dbStat;
        }

        @Override
        protected void bindStatement(@NotNull DBDValueHandler[] handlers, @NotNull DBCStatement statement, Object[] attributeValues) throws DBCException {
            int paramIndex = 0;
            for (int k = 0; k < handlers.length; k++) {
                DBSAttributeBase attribute = attributes[k];
                if (k >= updateAttributes.length && DBUtils.isNullValue(attributeValues[k])) {
                    // Skip NULL criteria binding
                    continue;
                }
                handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
            }
        }
    };
}
Also used : ExecuteBatchImpl(org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl) JDBCSQLDialect(org.jkiss.dbeaver.model.impl.jdbc.JDBCSQLDialect) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) NotNull(org.jkiss.code.NotNull) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Aggregations

SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)10 DBException (org.jkiss.dbeaver.DBException)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)4 NotNull (org.jkiss.code.NotNull)3 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)3 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)3 ExecuteBatchImpl (org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl)2 JDBCSQLDialect (org.jkiss.dbeaver.model.impl.jdbc.JDBCSQLDialect)2 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)2 Matcher (java.util.regex.Matcher)1 Point (org.eclipse.swt.graphics.Point)1 Nullable (org.jkiss.code.Nullable)1 DBDBinaryFormatter (org.jkiss.dbeaver.model.data.DBDBinaryFormatter)1 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)1 SQLQueryResult (org.jkiss.dbeaver.model.sql.SQLQueryResult)1 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)1 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)1