Search in sources :

Example 1 with DeleteException

use of io.jans.orm.exception.operation.DeleteException in project jans by JanssenProject.

the class SqlOperationServiceImpl method deleteImpl.

private long deleteImpl(TableMapping tableMapping, ConvertedExpression expression, int count) throws DeleteException {
    try {
        RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
        SQLDeleteClause sqlDeleteQuery = this.sqlQueryFactory.delete(tableRelationalPath);
        Predicate exp = (Predicate) expression.expression();
        sqlDeleteQuery.where(exp);
        if (count > 0) {
            sqlDeleteQuery = sqlDeleteQuery.limit(count);
        }
        long rowDeleted = sqlDeleteQuery.execute();
        return rowDeleted;
    } catch (QueryException ex) {
        throw new DeleteException(String.format("Failed to delete entries. Expression: '%s'", expression.expression()), ex);
    }
}
Also used : QueryException(com.querydsl.core.QueryException) SQLDeleteClause(com.querydsl.sql.dml.SQLDeleteClause) DeleteException(io.jans.orm.exception.operation.DeleteException) Predicate(com.querydsl.core.types.Predicate)

Example 2 with DeleteException

use of io.jans.orm.exception.operation.DeleteException in project jans by JanssenProject.

the class CouchbaseOperationServiceImpl method deleteImpl.

private int deleteImpl(BucketMapping bucketMapping, String key, ScanConsistency scanConsistency, Expression expression, int count) throws DeleteException {
    Bucket bucket = bucketMapping.getBucket();
    Expression finalExpression = expression;
    if (enableScopeSupport) {
        Expression scopeExpression = Expression.path("META().id").like(Expression.s(key + "%"));
        finalExpression = scopeExpression.and(expression);
    }
    MutateLimitPath deleteQuery = Delete.deleteFrom(Expression.i(bucketMapping.getBucketName())).where(finalExpression);
    ReturningPath query = deleteQuery.limit(count);
    LOG.debug("Execution query: '" + query + "'");
    N1qlQueryResult result = bucket.query(N1qlQuery.simple(query, N1qlParams.build().consistency(scanConsistency)));
    if (!result.finalSuccess()) {
        throw new DeleteException(String.format("Failed to delete entries. Query: '%s'. Error: '%s', Error count: '%d'", query, result.errors(), result.info().errorCount()), result.errors().get(0).getInt("code"));
    }
    return result.info().mutationCount();
}
Also used : MutateLimitPath(com.couchbase.client.java.query.dsl.path.MutateLimitPath) Bucket(com.couchbase.client.java.Bucket) Expression(com.couchbase.client.java.query.dsl.Expression) DeleteException(io.jans.orm.exception.operation.DeleteException) ReturningPath(com.couchbase.client.java.query.dsl.path.ReturningPath) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 3 with DeleteException

use of io.jans.orm.exception.operation.DeleteException in project jans by JanssenProject.

the class SpannerOperationServiceImpl method deleteImpl.

private long deleteImpl(TableMapping tableMapping, ConvertedExpression expression, int count) throws DeleteException {
    try {
        Table table = buildTable(tableMapping);
        // select
        PlainSelect sqlSelectQuery = new PlainSelect();
        sqlSelectQuery.setFromItem(table);
        // doc_id
        Column selectDocIdColumn = new Column(tableAlias, DOC_ID);
        SelectExpressionItem selectDocIdItem = new SelectExpressionItem(selectDocIdColumn);
        sqlSelectQuery.addSelectItems(selectDocIdItem);
        applyWhereExpression(sqlSelectQuery, expression);
        long useCount = connectionProvider.getMaximumResultDeleteSize();
        if (count > 0) {
            useCount = Math.min(count, useCount);
        }
        Limit limit = new Limit();
        limit.setRowCount(new LongValue(useCount));
        sqlSelectQuery.setLimit(limit);
        SubSelect subSelect = new SubSelect();
        subSelect.setSelectBody(sqlSelectQuery);
        subSelect.withUseBrackets(true);
        Expression inExpression = new InExpression(selectDocIdColumn, subSelect);
        Delete sqlDeleteQuery = new Delete();
        sqlDeleteQuery.setTable(table);
        sqlDeleteQuery.setWhere(inExpression);
        Statement.Builder statementBuilder = Statement.newBuilder(sqlDeleteQuery.toString());
        applyParametersBinding(statementBuilder, expression);
        Statement statement = statementBuilder.build();
        LOG.debug("Executing delete query: '{}'", statement);
        Long rowDeleted = databaseClient.readWriteTransaction().run(new TransactionCallable<Long>() {

            @Override
            public Long run(TransactionContext transaction) throws Exception {
                long rowCount = transaction.executeUpdate(statement);
                return rowCount;
            }
        });
        return rowDeleted;
    } catch (SpannerException | IncompatibleTypeException ex) {
        throw new DeleteException(String.format("Failed to delete entries. Expression: '%s'", expression.expression()), ex);
    }
}
Also used : Delete(net.sf.jsqlparser.statement.delete.Delete) Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) DeleteException(io.jans.orm.exception.operation.DeleteException) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) DeleteException(io.jans.orm.exception.operation.DeleteException) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) PersistenceException(io.jans.orm.exception.operation.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) SpannerException(com.google.cloud.spanner.SpannerException) SearchException(io.jans.orm.exception.operation.SearchException) EntryNotFoundException(io.jans.orm.exception.operation.EntryNotFoundException) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression) TransactionContext(com.google.cloud.spanner.TransactionContext) LongValue(net.sf.jsqlparser.expression.LongValue) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Builder(com.google.cloud.spanner.Statement.Builder)

Aggregations

DeleteException (io.jans.orm.exception.operation.DeleteException)3 Bucket (com.couchbase.client.java.Bucket)1 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)1 Expression (com.couchbase.client.java.query.dsl.Expression)1 MutateLimitPath (com.couchbase.client.java.query.dsl.path.MutateLimitPath)1 ReturningPath (com.couchbase.client.java.query.dsl.path.ReturningPath)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Statement (com.google.cloud.spanner.Statement)1 Builder (com.google.cloud.spanner.Statement.Builder)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 QueryException (com.querydsl.core.QueryException)1 Predicate (com.querydsl.core.types.Predicate)1 SQLDeleteClause (com.querydsl.sql.dml.SQLDeleteClause)1 ConvertedExpression (io.jans.orm.cloud.spanner.model.ConvertedExpression)1 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)1 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)1 EntryNotFoundException (io.jans.orm.exception.operation.EntryNotFoundException)1 IncompatibleTypeException (io.jans.orm.exception.operation.IncompatibleTypeException)1 PersistenceException (io.jans.orm.exception.operation.PersistenceException)1 SearchException (io.jans.orm.exception.operation.SearchException)1