Search in sources :

Example 1 with Delete

use of com.datastax.driver.core.querybuilder.Delete in project gora by apache.

the class CassandraQueryFactory method processQuery.

private static String processQuery(Query cassandraQuery, BuiltStatement select, CassandraMapping mapping, List<Object> objects) {
    String primaryKey = null;
    BuiltStatement query = null;
    Object startKey = cassandraQuery.getStartKey();
    Object endKey = cassandraQuery.getEndKey();
    Object key = cassandraQuery.getKey();
    boolean isWhereNeeded = true;
    if (key != null) {
        if (mapping.getCassandraKey() != null) {
            ArrayList<String> cassandraKeys = new ArrayList<>();
            ArrayList<Object> cassandraValues = new ArrayList<>();
            AvroCassandraUtils.processKeys(mapping, key, cassandraKeys, cassandraValues);
            String[] columnKeys = getColumnNames(mapping, cassandraKeys);
            for (int i = 0; i < cassandraKeys.size(); i++) {
                if (isWhereNeeded) {
                    if (select instanceof Select) {
                        query = ((Select) select).where(QueryBuilder.eq(columnKeys[i], "?"));
                    } else if (select instanceof Delete) {
                        query = ((Delete) select).where(QueryBuilder.eq(columnKeys[i], "?"));
                    } else {
                        query = ((Update.Assignments) select).where(QueryBuilder.eq(columnKeys[i], "?"));
                    }
                    objects.add(cassandraValues.get(i));
                    isWhereNeeded = false;
                } else {
                    if (select instanceof Select) {
                        query = ((Select.Where) query).and(QueryBuilder.eq(columnKeys[i], "?"));
                    } else if (select instanceof Delete) {
                        query = ((Delete.Where) query).and(QueryBuilder.eq(columnKeys[i], "?"));
                    } else {
                        query = ((Update.Where) query).and(QueryBuilder.eq(columnKeys[i], "?"));
                    }
                    objects.add(cassandraValues.get(i));
                }
            }
        } else {
            primaryKey = getPKey(mapping.getFieldList());
            if (select instanceof Select) {
                query = ((Select) select).where(QueryBuilder.eq(primaryKey, "?"));
            } else if (select instanceof Delete) {
                query = ((Delete) select).where(QueryBuilder.eq(primaryKey, "?"));
            } else {
                query = ((Update.Assignments) select).where(QueryBuilder.eq(primaryKey, "?"));
            }
            objects.add(key);
        }
    } else {
        if (startKey != null) {
            if (mapping.getCassandraKey() != null) {
                ArrayList<String> cassandraKeys = new ArrayList<>();
                ArrayList<Object> cassandraValues = new ArrayList<>();
                AvroCassandraUtils.processKeys(mapping, startKey, cassandraKeys, cassandraValues);
                String[] columnKeys = getColumnNames(mapping, cassandraKeys);
                for (int i = 0; i < cassandraKeys.size(); i++) {
                    if (isWhereNeeded) {
                        if (select instanceof Select) {
                            query = ((Select) select).where(QueryBuilder.gte(columnKeys[i], "?"));
                        } else if (select instanceof Delete) {
                            /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                            throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                        } else {
                            query = ((Update.Assignments) select).where(QueryBuilder.gte(columnKeys[i], "?"));
                        }
                        objects.add(cassandraValues.get(i));
                        isWhereNeeded = false;
                    } else {
                        if (select instanceof Select) {
                            query = ((Select.Where) query).and(QueryBuilder.gte(columnKeys[i], "?"));
                        } else if (select instanceof Delete) {
                            /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                            throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                        } else {
                            query = ((Update.Where) query).and(QueryBuilder.gte(columnKeys[i], "?"));
                        }
                        objects.add(cassandraValues.get(i));
                    }
                }
            } else {
                primaryKey = getPKey(mapping.getFieldList());
                if (select instanceof Select) {
                    query = ((Select) select).where(QueryBuilder.gte(primaryKey, "?"));
                } else if (select instanceof Delete) {
                    /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                    throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                } else {
                    query = ((Update.Assignments) select).where(QueryBuilder.gte(primaryKey, "?"));
                }
                objects.add(startKey);
                isWhereNeeded = false;
            }
        }
        if (endKey != null) {
            if (mapping.getCassandraKey() != null) {
                ArrayList<String> cassandraKeys = new ArrayList<>();
                ArrayList<Object> cassandraValues = new ArrayList<>();
                AvroCassandraUtils.processKeys(mapping, endKey, cassandraKeys, cassandraValues);
                String[] columnKeys = getColumnNames(mapping, cassandraKeys);
                for (int i = 0; i < cassandraKeys.size(); i++) {
                    if (isWhereNeeded) {
                        if (select instanceof Select) {
                            query = ((Select) select).where(QueryBuilder.lte(columnKeys[i], "?"));
                        } else if (select instanceof Delete) {
                            /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                            throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                        } else {
                            query = ((Update.Assignments) select).where(QueryBuilder.lte(columnKeys[i], "?"));
                        }
                        objects.add(cassandraValues.get(i));
                        isWhereNeeded = false;
                    } else {
                        if (select instanceof Select) {
                            query = ((Select.Where) query).and(QueryBuilder.lte(columnKeys[i], "?"));
                        } else if (select instanceof Delete) {
                            /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                            throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                        } else {
                            query = ((Update.Where) query).and(QueryBuilder.lte(columnKeys[i], "?"));
                        }
                        objects.add(cassandraValues.get(i));
                    }
                }
            } else {
                primaryKey = primaryKey != null ? primaryKey : getPKey(mapping.getFieldList());
                if (isWhereNeeded) {
                    if (select instanceof Select) {
                        query = ((Select) select).where(QueryBuilder.lte(primaryKey, "?"));
                    } else if (select instanceof Delete) {
                        /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                 */
                        throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                    } else {
                        query = ((Update.Assignments) select).where(QueryBuilder.lte(primaryKey, "?"));
                    }
                } else {
                    if (select instanceof Select) {
                        query = ((Select.Where) query).and(QueryBuilder.lte(primaryKey, "?"));
                    } else if (select instanceof Delete) {
                        /*
                According to the JIRA https://issues.apache.org/jira/browse/CASSANDRA-7651 this has been fixed, but It seems this not fixed yet.
                */
                        throw new RuntimeException("Delete by Query is not suppoted for Key Ranges.");
                    } else {
                        query = ((Update.Where) query).and(QueryBuilder.lte(primaryKey, "?"));
                    }
                }
                objects.add(endKey);
            }
        }
    }
    if (startKey == null && endKey == null && key == null) {
        return select.getQueryString();
    }
    return query != null ? query.getQueryString() : null;
}
Also used : Delete(com.datastax.driver.core.querybuilder.Delete) ArrayList(java.util.ArrayList) BuiltStatement(com.datastax.driver.core.querybuilder.BuiltStatement) Select(com.datastax.driver.core.querybuilder.Select)

Example 2 with Delete

use of com.datastax.driver.core.querybuilder.Delete in project camel by apache.

the class CassandraIdempotentRepository method initDeleteStatement.

// -------------------------------------------------------------------------
// Remove key from repository
protected void initDeleteStatement() {
    Delete delete = generateDelete(table, pkColumns, true);
    delete = applyConsistencyLevel(delete, writeConsistencyLevel);
    LOGGER.debug("Generated Delete {}", delete);
    deleteStatement = getSession().prepare(delete);
}
Also used : Delete(com.datastax.driver.core.querybuilder.Delete) CassandraUtils.generateDelete(org.apache.camel.utils.cassandra.CassandraUtils.generateDelete)

Example 3 with Delete

use of com.datastax.driver.core.querybuilder.Delete in project camel by apache.

the class CassandraAggregationRepository method initDeleteStatement.

// -------------------------------------------------------------------------
// Remove exchange from repository
private void initDeleteStatement() {
    Delete delete = generateDelete(table, pkColumns, false);
    delete = applyConsistencyLevel(delete, writeConsistencyLevel);
    LOGGER.debug("Generated Delete {}", delete);
    deleteStatement = getSession().prepare(delete);
}
Also used : CassandraUtils.generateDelete(org.apache.camel.utils.cassandra.CassandraUtils.generateDelete) Delete(com.datastax.driver.core.querybuilder.Delete)

Example 4 with Delete

use of com.datastax.driver.core.querybuilder.Delete in project camel by apache.

the class CassandraAggregationRepository method initDeleteIfIdStatement.

// -------------------------------------------------------------------------
// Confirm exchange in repository
private void initDeleteIfIdStatement() {
    Delete delete = generateDelete(table, pkColumns, false);
    Delete.Conditions deleteIf = delete.onlyIf(eq(exchangeIdColumn, bindMarker()));
    deleteIf = applyConsistencyLevel(deleteIf, writeConsistencyLevel);
    LOGGER.debug("Generated Delete If Id {}", deleteIf);
    deleteIfIdStatement = getSession().prepare(deleteIf);
}
Also used : CassandraUtils.generateDelete(org.apache.camel.utils.cassandra.CassandraUtils.generateDelete) Delete(com.datastax.driver.core.querybuilder.Delete)

Example 5 with Delete

use of com.datastax.driver.core.querybuilder.Delete in project gora by apache.

the class CassandraQueryFactory method processKeys.

private static String processKeys(String[] columnNames, BuiltStatement delete) {
    BuiltStatement query = null;
    boolean isWhereNeeded = true;
    for (String columnName : columnNames) {
        if (isWhereNeeded) {
            if (delete instanceof Delete) {
                query = ((Delete) delete).where(QueryBuilder.eq(columnName, "?"));
            } else {
                query = ((Select) delete).where(QueryBuilder.eq(columnName, "?"));
            }
            isWhereNeeded = false;
        } else {
            if (delete instanceof Delete) {
                query = ((Delete.Where) query).and(QueryBuilder.eq(columnName, "?"));
            } else {
                query = ((Select.Where) query).and(QueryBuilder.eq(columnName, "?"));
            }
        }
    }
    return query != null ? query.getQueryString() : null;
}
Also used : Delete(com.datastax.driver.core.querybuilder.Delete) Select(com.datastax.driver.core.querybuilder.Select) BuiltStatement(com.datastax.driver.core.querybuilder.BuiltStatement)

Aggregations

Delete (com.datastax.driver.core.querybuilder.Delete)8 CassandraUtils.generateDelete (org.apache.camel.utils.cassandra.CassandraUtils.generateDelete)3 BuiltStatement (com.datastax.driver.core.querybuilder.BuiltStatement)2 Select (com.datastax.driver.core.querybuilder.Select)2 HugeKeys (com.baidu.hugegraph.type.define.HugeKeys)1 ArrayList (java.util.ArrayList)1