/** * @return cql query statement to retrieve a task from the "workflows" table */ public String getSelectTaskStatement() { return QueryBuilder.select(PAYLOAD_KEY) .from(keyspace, TABLE_WORKFLOWS) .where(eq(WORKFLOW_ID_KEY, bindMarker())) .and(eq(SHARD_ID_KEY, bindMarker())) .and(eq(ENTITY_KEY, ENTITY_TYPE_TASK)) .and(eq(TASK_ID_KEY, bindMarker())) .getQueryString(); }
Statement queryStatement; if ("0".equals(pageLink.getOffset())) { queryStatement = select().from(getColumnFamilyName()) .where(eq(EP_BY_APP_ID_APPLICATION_ID_PROPERTY, appId)) .limit(Integer.valueOf(limit) + 1); LOG.debug("Try to find first page of endpoint key hash by application " + "id {} with limit {}", appId, limit); } else { queryStatement = select().from(getColumnFamilyName()) .where(eq(EP_BY_APP_ID_APPLICATION_ID_PROPERTY, appId)) .and(gte(EP_BY_APP_ID_ENDPOINT_KEY_HASH_PROPERTY, endpointKey)) .limit(Integer.valueOf(limit) + 1); LOG.debug("Try to find endpoint key hash list by endpoint group id {} " + "with limit {} start from keyHash {}",
Factory(Session session, TimestampCodec timestampCodec, Set<Integer> buckets) { this.session = session; this.timestampCodec = timestampCodec; this.preparedStatement = session.prepare( QueryBuilder.select("ts", "trace_id") .from(Tables.SERVICE_NAME_INDEX) .where(QueryBuilder.eq("service_name", QueryBuilder.bindMarker("service_name"))) .and(QueryBuilder.in("bucket", buckets)) .and(QueryBuilder.gte("ts", QueryBuilder.bindMarker("start_ts"))) .and(QueryBuilder.lte("ts", QueryBuilder.bindMarker("end_ts"))) .limit(QueryBuilder.bindMarker("limit_")) .orderBy(QueryBuilder.desc("ts"))); }
@Override public CassandraEndpointUser findByExternalIdAndTenantId(String externalId, String tenantId) { LOG.debug("Try to find endpoint user by external id {} and tenant id {}", externalId, tenantId); Where where = select().from(getColumnFamilyName()) .where(eq(EP_USER_EXTERNAL_ID_PROPERTY, externalId)) .and(eq(EP_USER_TENANT_ID_PROPERTY, tenantId)); LOG.trace("Try to find endpoint user by cql select {}", where); CassandraEndpointUser endpointUser = findOneByStatement(where); LOG.trace("Found {} endpoint user", endpointUser); return endpointUser; }
@Test(groups = "short") public void dateHandlingTest() throws Exception { Date d = new Date(); session().execute(insertInto("dateTest").value("t", d)); String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString(); List<Row> rows = session().execute(query).all(); assertEquals(1, rows.size()); Row r1 = rows.get(0); assertEquals(d, r1.getTimestamp("t")); }
row(4, 1, 1)); assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(6))) .hasSize(6); assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(5))) .contains(row(0, 0, 0), row(0, 1, 1), row(1, 0, 0), row(1, 1, 1), row(2, 0, 0)); .execute(select().all().from("test_ppl").where(in("a", 2, 3)).perPartitionLimit(2))) .contains(row(2, 0, 0), row(2, 1, 1), row(3, 0, 0), row(3, 1, 1)); assertThat( .perPartitionLimit(bindMarker()) .perPartitionLimit(bindMarker()) .perPartitionLimit(bindMarker()) .orderBy(desc("b")) .and(gt("b", bindMarker())) .perPartitionLimit(bindMarker()) .and(gt("b", bindMarker())) .orderBy(desc("b"))
.execute( select("a", "b", "e", count("b"), max("e")) .from(table) .where(eq("b", 2)) .groupBy("a", "b") .allowFiltering())) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6)); .groupBy("a", "b", "c"))) .containsExactly(row(1, 2, 6, 1L, 6), row(1, 2, 12, 1L, 12), row(1, 4, 12, 2L, 24)); select("a", count("a")).distinct().from(table).where(eq("a", 1)).groupBy("a"))) .containsExactly(row(1, 1L)); .groupBy("a", "b", "c") .groupBy("a", "b", "c") .groupBy("a", "b", "c") .groupBy("a", "b", "c") .groupBy("a", "b", "c") .and(eq("b", 2)) .groupBy("a", "b", "c"))) .containsExactly(row(1, 2, 6, 1L, 6), row(1, 2, 12, 1L, 12), row(2, 2, 6, 1L, 6)); .groupBy("a")))
select = select().all().from("foo").where(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); assertEquals(select.toString(), query); select().all().from("foo").where().and(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); assertEquals(select.toString(), query); select = select("a", "b", quote("C")) .from("foo") .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3"))) .and(eq(quote("C"), "foo")) .orderBy(asc("a"), desc("b")) .limit(42); assertEquals(select.toString(), query); .from("foo") .where(in("k")) .limit(bindMarker("limit")); assertEquals(select.toString(), query); .from("foo") .where() .and(eq("bar", bindMarker("barmark"))) .and(eq("baz", bindMarker("bazmark"))) .limit(bindMarker("limit")); assertEquals(select.toString(), query); select = select().all().from("foo").where(gt("k", 42)).limit(42); assertEquals(select.toString(), query);
.from("foo") .where(in("a", 2, 4)) .perPartitionLimit(2) .limit(3) .toString()) .from("foo") .where(eq("a", bindMarker())) .perPartitionLimit(bindMarker()) .limit(3) .toString()) .from("foo") .where(eq("a", bindMarker())) .orderBy(desc("b")) .perPartitionLimit(2) .limit(3) .from("foo") .where(eq("a", bindMarker())) .and(gt("b", bindMarker())) .orderBy(desc("b")) .perPartitionLimit(bindMarker()) .limit(3)
select() .all() .from("foo") .where(eq("k", 4)) .and(eq(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); assertEquals(select.toString(), query); .all() .from("foo") .where(eq("k", 4)) .and(gt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); assertEquals(select.toString(), query); .all() .from("foo") .where(eq("k", 4)) .and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))) .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0))); assertEquals(select.toString(), query); .and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); assertEquals(select.toString(), query); ImmutableList.<List<?>>of( ImmutableList.of(1, "foo"), ImmutableList.of(2, "bar"), ImmutableList.of(3, "qix")); select = select().all().from("foo").where(eq("k", 4)).and(in(names, values)); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", 4)).and(in(names, values));
select = select().all().from("foo WHERE k=4"); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4 AND c=5")); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4' AND c='5")); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4' OR '1'='1")); assertEquals(select.toString(), query); select = select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); assertEquals(select.toString(), query);
Factory(Session session, boolean strictTraceId, int maxTraceCols) { this.session = session; this.accumulateSpans = new DecodeAndConvertSpans(); this.preparedStatement = session.prepare( QueryBuilder.select("trace_id", "span") .from("traces") .where(QueryBuilder.in("trace_id", QueryBuilder.bindMarker("trace_id"))) .limit(QueryBuilder.bindMarker("limit_"))); this.maxTraceCols = maxTraceCols; this.strictTraceId = strictTraceId; this.groupByTraceId = GroupByTraceId.create(strictTraceId); }
private T updateLocked(T entity) { long version = (entity.getVersion() == null) ? 0L : entity.getVersion(); Assignments assigns = update(getColumnFamilyName()) .onlyIf(eq(OPT_LOCK, version)) .with(set(OPT_LOCK, version + 1)); CassandraEntityMapper<T> entityMapper = CassandraEntityMapper.getEntityMapperForClass( getColumnFamilyClass(), cassandraClient); + version + ". Entity already changed!"); } else { Select.Where where = select().from(getColumnFamilyName()).where(whereClauses[0]); if (whereClauses.length > 1) { for (int i = 1; i < whereClauses.length; i++) { where = where.and(whereClauses[i]);
private T insertLocked(T entity) { Insert insert = insertInto(getColumnFamilyName()).ifNotExists(); CassandraEntityMapper<T> entityMapper = CassandraEntityMapper.getEntityMapperForClass( getColumnFamilyClass(), cassandraClient); for (String name : entityMapper.getKeyColumnNames()) { insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient)); } for (String name : entityMapper.getNonKeyColumnNames()) { insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient)); } insert.setConsistencyLevel(getWriteConsistencyLevel()); ResultSet res = execute(insert); if (!res.wasApplied()) { LOG.error("[{}] Can't insert entity. Entity already exists!", getColumnFamilyClass()); throw new KaaOptimisticLockingFailureException("Can't insert entity. Entity already exists!"); } else { Clause[] whereClauses = buildKeyClauses(entityMapper, entity); Select.Where where = select().from(getColumnFamilyName()).where(whereClauses[0]); if (whereClauses.length > 1) { for (int i = 1; i < whereClauses.length; i++) { where = where.and(whereClauses[i]); } } return findOneByStatement(where); } }
@Override protected ResultSet queryRowGroupRowsAfter(Row row) { Statement statement = selectDeltaFrom(_placement.getBlockedDeltaTableDDL()) .where(eq(_placement.getBlockedDeltaTableDDL().getRowKeyColumnName(), getKey(row))) .and(gt(ImmutableList.of(_placement.getBlockedDeltaTableDDL().getChangeIdColumnName(), _placement.getBlockedDeltaTableDDL().getBlockColumnName()), ImmutableList.of(getChangeId(row), getBlock(row)))) .orderBy(asc(_placement.getBlockedDeltaTableDDL().getChangeIdColumnName())) .setConsistencyLevel(_consistency); return AdaptiveResultSet.executeAdaptiveQuery(_placement.getKeyspace().getCqlSession(), statement, _driverConfig.getSingleRowFetchSize()); } }
private static void addWhereClause(Where where, List<CassandraColumnHandle> partitionKeyColumns, List<Object> filterPrefix) { for (int i = 0; i < filterPrefix.size(); i++) { CassandraColumnHandle column = partitionKeyColumns.get(i); Object value = column.getCassandraType().getJavaValue(filterPrefix.get(i)); Clause clause = QueryBuilder.eq(CassandraCqlUtils.validColumnName(column.getName()), value); where.and(clause); } }
private static void addWhereInClauses(Where where, List<CassandraColumnHandle> partitionKeyColumns, List<Set<Object>> filterPrefixes) { for (int i = 0; i < filterPrefixes.size(); i++) { CassandraColumnHandle column = partitionKeyColumns.get(i); List<Object> values = filterPrefixes.get(i) .stream() .map(value -> column.getCassandraType().getJavaValue(value)) .collect(toList()); Clause clause = QueryBuilder.in(CassandraCqlUtils.validColumnName(column.getName()), values); where.and(clause); } }