All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.oneandone.troilus.DeleteQueryDataImpl Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
/*
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.oneandone.troilus;



import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;


import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;

import java.util.List;
import java.util.Map.Entry;

import net.oneandone.troilus.interceptor.DeleteQueryData;

import com.datastax.driver.core.querybuilder.Clause;
import com.datastax.driver.core.querybuilder.Delete;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Statement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;


/**
 * Delete query data implementation
 */
class DeleteQueryDataImpl implements DeleteQueryData {
 
    private final ImmutableMap keyNameValuePairs;
    private final ImmutableList whereConditions;
    private final ImmutableList onlyIfConditions;
    private final Boolean ifExists;
     

    /**
     * constructor 
     */
    DeleteQueryDataImpl() {
        this(ImmutableMap.of(), 
             ImmutableList.of(), 
             ImmutableList.of(),
             null);
    }
    
    private DeleteQueryDataImpl(ImmutableMap keyNameValuePairs, 
                            ImmutableList whereConditions, 
                            ImmutableList onlyIfConditions,
                            Boolean ifExists) {   
        this.keyNameValuePairs = keyNameValuePairs;
        this.whereConditions = whereConditions;
        this.onlyIfConditions = onlyIfConditions;
        this.ifExists = ifExists;
    }
    
    @Override
    public DeleteQueryDataImpl key(ImmutableMap keyNameValuePairs) {
        return new DeleteQueryDataImpl(keyNameValuePairs, 
                                       this.whereConditions, 
                                       this.onlyIfConditions,
                                       this.ifExists);  
    }
    
    @Override
    public DeleteQueryDataImpl whereConditions(ImmutableList whereConditions) {
        return new DeleteQueryDataImpl(this.keyNameValuePairs, 
                                       whereConditions, 
                                       this.onlyIfConditions,
                                       this.ifExists);  
    }
    
    @Override
    public DeleteQueryDataImpl onlyIfConditions(ImmutableList onlyIfConditions) {
        return new DeleteQueryDataImpl(this.keyNameValuePairs, 
                                       this.whereConditions, 
                                       onlyIfConditions,
                                       this.ifExists);  
    }
    
    @Override
    public DeleteQueryDataImpl ifExists(Boolean ifExists) {
        return new DeleteQueryDataImpl(this.keyNameValuePairs, 
                                       this.whereConditions, 
                                       this.onlyIfConditions,
                                       ifExists);  
    }
    
    @Override
    public ImmutableMap getKey() {
        return keyNameValuePairs;
    }

    @Override
    public ImmutableList getWhereConditions() {
        return whereConditions;
    }

    @Override
    public ImmutableList getOnlyIfConditions() {
        return onlyIfConditions;
    }
    
    @Override
    public Boolean getIfExists() {
        return ifExists;
    }
    

    /**
     * @param data  the data 
     * @param ctx   the context
     * @return the query data statement
     */
    static ListenableFuture toStatementAsync(DeleteQueryData data, Context ctx) {
        Delete delete = delete().from(ctx.getDbSession().getTablename());

        for (Clause onlyIfCondition : data.getOnlyIfConditions()) {
            delete.onlyIf(onlyIfCondition);
        }
        
        if ((data.getIfExists() != null) && data.getIfExists()) {
            delete.ifExists();
        }
        
        // key-based delete    
        if (data.getWhereConditions().isEmpty()) {
            List values = Lists.newArrayList();
            
            for (Entry entry : data.getKey().entrySet()) {
                Clause keybasedWhereClause = eq(entry.getKey(), bindMarker());
                delete.where(keybasedWhereClause);
                                
                values.add(ctx.toStatementValue(entry.getKey(), entry.getValue()));
            }
            
            ListenableFuture preparedStatementFuture = ctx.getDbSession().prepareAsync(delete);
            return ctx.getDbSession().bindAsync(preparedStatementFuture, values.toArray());
            
        // where condition-based delete    
        } else {
            for (Clause whereCondition : data.getWhereConditions()) {
                delete.where(whereCondition);
            }
           
            return Futures.immediateFuture(delete);
        }        
    }
}