Java Code Examples for com.datastax.driver.core.querybuilder.Select#Where

The following examples show how to use com.datastax.driver.core.querybuilder.Select#Where . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseRelationDao.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<List<EntityRelation>> findRelations(EntityId from, String relationType, RelationTypeGroup typeGroup, ThingType childType, TimePageLink pageLink) {
    Select.Where query = CassandraAbstractSearchTimeDao.buildQuery(ModelConstants.RELATION_BY_TYPE_AND_CHILD_TYPE_VIEW_NAME,
            Arrays.asList(eq(ModelConstants.RELATION_FROM_ID_PROPERTY, from.getId()),
                    eq(ModelConstants.RELATION_FROM_TYPE_PROPERTY, from.getEntityType().name()),
                    eq(ModelConstants.RELATION_TYPE_GROUP_PROPERTY, typeGroup.name()),
                    eq(ModelConstants.RELATION_TYPE_PROPERTY, relationType),
                    eq(ModelConstants.RELATION_TO_TYPE_PROPERTY, childType.name())),
            Arrays.asList(
                    pageLink.isAscOrder() ? QueryBuilder.desc(ModelConstants.RELATION_TYPE_GROUP_PROPERTY) :
                            QueryBuilder.asc(ModelConstants.RELATION_TYPE_GROUP_PROPERTY),
                    pageLink.isAscOrder() ? QueryBuilder.desc(ModelConstants.RELATION_TYPE_PROPERTY) :
                            QueryBuilder.asc(ModelConstants.RELATION_TYPE_PROPERTY),
                    pageLink.isAscOrder() ? QueryBuilder.desc(ModelConstants.RELATION_TO_TYPE_PROPERTY) :
                            QueryBuilder.asc(ModelConstants.RELATION_TO_TYPE_PROPERTY)
            ),
            pageLink, ModelConstants.RELATION_TO_ID_PROPERTY);
    return getFuture(executeAsyncRead(query), this::getEntityRelations);
}
 
Example 2
Source File: SelectStatementHandler.java    From scalardb with Apache License 2.0 6 votes vote down vote up
private void setStart(Select.Where statement, Scan scan) {
  if (!scan.getStartClusteringKey().isPresent()) {
    return;
  }

  scan.getStartClusteringKey()
      .ifPresent(
          k -> {
            List<Value> start = k.get();
            IntStream.range(0, start.size())
                .forEach(
                    i -> {
                      if (i == (start.size() - 1)) {
                        if (scan.getStartInclusive()) {
                          statement.and(gte(start.get(i).getName(), bindMarker()));
                        } else {
                          statement.and(gt(start.get(i).getName(), bindMarker()));
                        }
                      } else {
                        statement.and(eq(start.get(i).getName(), bindMarker()));
                      }
                    });
          });
}
 
Example 3
Source File: DLocatorIO.java    From blueflood with Apache License 2.0 6 votes vote down vote up
/**
 * Create all prepared statements use in this class for metrics_locator
 */
private void createPreparedStatements()  {

    // create a generic select statement for retrieving from metrics_locator
    Select.Where select = QueryBuilder
            .select()
            .all()
            .from( CassandraModel.CF_METRICS_LOCATOR_NAME )
            .where( eq ( KEY, bindMarker() ));
    getValue = DatastaxIO.getSession().prepare( select );

    // create a generic insert statement for inserting into metrics_locator
    Insert insert = QueryBuilder.insertInto( CassandraModel.CF_METRICS_LOCATOR_NAME)
            .using(ttl(TenantTtlProvider.LOCATOR_TTL))
            .value(KEY, bindMarker())
            .value(COLUMN1, bindMarker())
            .value(VALUE, bindMarker());
    putValue = DatastaxIO.getSession()
            .prepare(insert)
            .setConsistencyLevel( ConsistencyLevel.LOCAL_ONE );
}
 
Example 4
Source File: CassandraDACImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
public Response getRecords(
    String keySpace, String table, Map<String, Object> filters, List<String> fields) {
  Response response = new Response();
  Session session = connectionManager.getSession(keySpace);
  try {
    Select select;
    if (CollectionUtils.isNotEmpty(fields)) {
      select = QueryBuilder.select((String[]) fields.toArray()).from(keySpace, table);
    } else {
      select = QueryBuilder.select().all().from(keySpace, table);
    }

    if (MapUtils.isNotEmpty(filters)) {
      Select.Where where = select.where();
      for (Map.Entry<String, Object> filter : filters.entrySet()) {
        Object value = filter.getValue();
        if (value instanceof List) {
          where = where.and(QueryBuilder.in(filter.getKey(), ((List) filter.getValue())));
        } else {
          where = where.and(QueryBuilder.eq(filter.getKey(), filter.getValue()));
        }
      }
    }

    ResultSet results = null;
    results = session.execute(select);
    response = CassandraUtil.createResponse(results);
  } catch (Exception e) {
    ProjectLogger.log(Constants.EXCEPTION_MSG_FETCH + table + " : " + e.getMessage(), e);
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  return response;
}
 
Example 5
Source File: CassandraDeviceTypeDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<DeviceType> findDeviceTypeByTenantIdAndName(UUID tenantId, String deviceName) {
    Select select = select().from(DEVICE_TYPE_BY_TENANT_AND_NAME_VIEW_NAME);
    Select.Where query = select.where();
    query.and(eq(DEVICE_TYPE_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(DEVICE_TYPE_NAME_PROPERTY, deviceName));
    return Optional.ofNullable(DaoUtil.getData(findOneByStatement(query)));
}
 
Example 6
Source File: CassandraAssetDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<List<Asset>> findAssetsByTenantIdAndCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List<UUID> assetIds) {
    log.debug("Try to find assets by tenantId [{}], customerId [{}] and asset Ids [{}]", tenantId, customerId, assetIds);
    Select select = select().from(getColumnFamilyName());
    Select.Where query = select.where();
    query.and(eq(ASSET_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(ASSET_CUSTOMER_ID_PROPERTY, customerId));
    query.and(in(ID_PROPERTY, assetIds));
    return findListByStatementAsync(query);
}
 
Example 7
Source File: CassandraBaseAttributesDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<List<AttributeKvEntry>> findAll(EntityId entityId, String attributeType) {
    Select.Where select = select().from(ATTRIBUTES_KV_CF)
            .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType()))
            .and(eq(ENTITY_ID_COLUMN, entityId.getId()))
            .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType));
    log.trace("Generated query [{}] for entityId {} and attributeType {}", select, entityId, attributeType);
    return Futures.transform(executeAsyncRead(select), (Function<? super ResultSet, ? extends List<AttributeKvEntry>>) input ->
                    convertResultToAttributesKvEntryList(input)
            , readResultsProcessingExecutor);
}
 
Example 8
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<D> findByIdAsync(UUID key) {
    log.debug("Get entity by key {}", key);
    Select.Where query = select().from(getColumnFamilyName()).where(eq(ModelConstants.ID_PROPERTY, key));
    log.trace("Execute query {}", query);
    return findOneByStatementAsync(query);
}
 
Example 9
Source File: CassandraAbstractModelDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public D findById(UUID key) {
    log.debug("Get entity by key {}", key);
    Select.Where query = select().from(getColumnFamilyName()).where(eq(ModelConstants.ID_PROPERTY, key));
    log.trace("Execute query {}", query);
    E entity = findOneByStatement(query);
    return DaoUtil.getData(entity);
}
 
Example 10
Source File: CassandraAssetDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Asset> findAssetsByTenantIdAndName(UUID tenantId, String assetName) {
    Select select = select().from(ASSET_BY_TENANT_AND_NAME_VIEW_NAME);
    Select.Where query = select.where();
    query.and(eq(ASSET_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(ASSET_NAME_PROPERTY, assetName));
    AssetEntity assetEntity = (AssetEntity) findOneByStatement(query);
    return Optional.ofNullable(DaoUtil.getData(assetEntity));
}
 
Example 11
Source File: CassandraBaseAttributesDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Optional<AttributeKvEntry>> find(EntityId entityId, String attributeType, String attributeKey) {
    Select.Where select = select().from(ATTRIBUTES_KV_CF)
            .where(eq(ENTITY_TYPE_COLUMN, entityId.getEntityType()))
            .and(eq(ENTITY_ID_COLUMN, entityId.getId()))
            .and(eq(ATTRIBUTE_TYPE_COLUMN, attributeType))
            .and(eq(ATTRIBUTE_KEY_COLUMN, attributeKey));
    log.trace("Generated query [{}] for entityId {} and key {}", select, entityId, attributeKey);
    return Futures.transform(executeAsyncRead(select), (Function<? super ResultSet, ? extends Optional<AttributeKvEntry>>) input ->
                    Optional.ofNullable(convertResultToAttributesKvEntry(attributeKey, input.one()))
            , readResultsProcessingExecutor);
}
 
Example 12
Source File: CassandraAlarmDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) {
    Select select = select().from(ALARM_COLUMN_FAMILY_NAME);
    Select.Where query = select.where();
    query.and(eq(ALARM_TENANT_ID_PROPERTY, tenantId.getId()));
    query.and(eq(ALARM_ORIGINATOR_ID_PROPERTY, originator.getId()));
    query.and(eq(ALARM_ORIGINATOR_TYPE_PROPERTY, originator.getEntityType()));
    query.and(eq(ALARM_TYPE_PROPERTY, type));
    query.limit(1);
    query.orderBy(QueryBuilder.asc(ModelConstants.ALARM_TYPE_PROPERTY), QueryBuilder.desc(ModelConstants.ID_PROPERTY));
    return findOneByStatementAsync(query);
}
 
Example 13
Source File: CassandraDeviceDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<List<Device>> findDevicesByTenantIdAndIdsAsync(UUID tenantId, List<UUID> deviceIds) {
    log.debug("Try to find devices by tenantId [{}] and device Ids [{}]", tenantId, deviceIds);
    Select select = select().from(getColumnFamilyName());
    Select.Where query = select.where();
    query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));
    query.and(in(ID_PROPERTY, deviceIds));
    return findListByStatementAsync(query);
}
 
Example 14
Source File: CassandraPerUserMaxQuotaDao.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Select.Where getMaxStorageStatement() {
    return select(CassandraMaxQuota.STORAGE)
        .from(CassandraMaxQuota.TABLE_NAME)
        .where(eq(CassandraMaxQuota.QUOTA_ROOT, bindMarker()));
}
 
Example 15
Source File: CassandraStore.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public List<U> getAll(T key, String... columns) throws IOException {
  Select.Where statement = QueryBuilder.select(columns).from(TABLE_NAME).where(QueryBuilder.eq(primaryKey, getCassandraKey(key, k)));
  ResultSet results = runQuery(statement);
  return getResults(results);
}
 
Example 16
Source File: CassandraStore.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
@Override public U get(T key, String... columns) throws IOException {
  Select.Where statement = QueryBuilder.select(columns).from(TABLE_NAME).where(QueryBuilder.eq(primaryKey, getCassandraKey(key, k)));
  ResultSet results = runQuery(statement);
  return getResult(results);
}
 
Example 17
Source File: CassandraAnnotationMapper.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Select.Where getStoredAnnotationsQueryByKey(CassandraId mailboxId, String key) {
    return getStoredAnnotationsQuery(mailboxId)
        .and(eq(CassandraAnnotationTable.KEY, key));
}
 
Example 18
Source File: CassandraPerUserMaxQuotaDao.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Select.Where getMaxMessageStatement() {
    return select(CassandraMaxQuota.MESSAGE_COUNT)
        .from(CassandraMaxQuota.TABLE_NAME)
        .where(eq(CassandraMaxQuota.QUOTA_ROOT, bindMarker()));
}
 
Example 19
Source File: CassandraAnnotationMapper.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Select.Where getStoredAnnotationsQuery(CassandraId mailboxId) {
    return select(CassandraAnnotationTable.SELECT_FIELDS)
        .from(CassandraAnnotationTable.TABLE_NAME)
        .where(eq(CassandraAnnotationTable.MAILBOX_ID, mailboxId.asUuid()));
}
 
Example 20
Source File: CassandraSieveDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Select.Where getScriptsQuery() {
    return select(SCRIPT_CONTENT, IS_ACTIVE, SCRIPT_NAME, SIZE)
        .from(TABLE_NAME)
        .where(eq(USER_NAME, bindMarker(USER_NAME)));
}