Search in sources :

Example 1 with ExecuteBatchImpl

use of org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl in project dbeaver by serge-rider.

the class JDBCTable method insertData.

/**
     * Inserts data row.
     * Note: if column value is NULL then it will be skipped (to let default value to be applied)
     * If ALL columns are null then explicit NULL values will be used for all of them (to let INSERT to execute - it won't work with empty column list)
     */
@NotNull
@Override
public ExecuteBatch insertData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] attributes, @Nullable DBDDataReceiver keysReceiver, @NotNull final DBCExecutionSource source) throws DBCException {
    readRequiredMeta(session.getProgressMonitor());
    return new ExecuteBatchImpl(attributes, keysReceiver, true) {

        private boolean allNulls;

        @NotNull
        @Override
        protected DBCStatement prepareStatement(@NotNull DBCSession session, Object[] attributeValues) throws DBCException {
            // Make query
            StringBuilder query = new StringBuilder(200);
            query.append(useUpsert(session) ? "UPSERT" : "INSERT").append(" INTO ").append(getFullyQualifiedName(DBPEvaluationContext.DML)).append(//$NON-NLS-1$ //$NON-NLS-2$
            " (");
            allNulls = true;
            for (int i = 0; i < attributes.length; i++) {
                if (!DBUtils.isNullValue(attributeValues[i])) {
                    allNulls = false;
                    break;
                }
            }
            boolean hasKey = false;
            for (int i = 0; i < attributes.length; i++) {
                DBSAttributeBase attribute = attributes[i];
                if (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[i]))) {
                    continue;
                }
                //$NON-NLS-1$
                if (hasKey)
                    query.append(",");
                hasKey = true;
                query.append(getAttributeName(attribute));
            }
            //$NON-NLS-1$
            query.append(")\nVALUES (");
            hasKey = false;
            for (int i = 0; i < attributes.length; i++) {
                DBSAttributeBase attribute = attributes[i];
                if (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[i]))) {
                    continue;
                }
                //$NON-NLS-1$
                if (hasKey)
                    query.append(",");
                hasKey = true;
                //$NON-NLS-1$
                query.append("?");
            }
            //$NON-NLS-1$
            query.append(")");
            // 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 (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[k]))) {
                    continue;
                }
                handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
            }
        }
    };
}
Also used : ExecuteBatchImpl(org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl) NotNull(org.jkiss.code.NotNull) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Example 2 with ExecuteBatchImpl

use of org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl 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 3 with ExecuteBatchImpl

use of org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl 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

NotNull (org.jkiss.code.NotNull)3 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)3 ExecuteBatchImpl (org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl)3 JDBCSQLDialect (org.jkiss.dbeaver.model.impl.jdbc.JDBCSQLDialect)2 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)2 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)2