Java Code Examples for org.apache.ignite.cache.query.SqlQuery#setArgs()

The following examples show how to use org.apache.ignite.cache.query.SqlQuery#setArgs() . 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: IgniteAlertsStore.java    From spring-boot-ignite with Apache License 2.0 5 votes vote down vote up
@Override
public List<AlertEntry> getAlertForServiceId(String serviceId) {
    final String sql = "serviceId = ?";
    SqlQuery<String, AlertEntry> query = new SqlQuery<>(AlertEntry.class, sql);
    query.setArgs(serviceId);
    return Optional.ofNullable(getAlertsCache().query(query).getAll()
            .stream()
            .map(Cache.Entry::getValue)
            .collect(Collectors.toList()))
            .orElseThrow(() -> new ResourceNotFoundException(String.format("Alert for %s not found", serviceId)));
}
 
Example 2
Source File: IgniteRepositoryQuery.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param prmtrs Prmtrs.
 * @return prepared query for execution
 */
@SuppressWarnings("deprecation")
@NotNull private Query prepareQuery(Object[] prmtrs) {
    Object[] parameters = prmtrs;
    String sql = qry.sql();

    switch (qry.options()) {
        case SORTING:
            sql = addSorting(new StringBuilder(sql), (Sort)parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);

            break;

        case PAGINATION:
            sql = addPaging(new StringBuilder(sql), (Pageable)parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);

            break;

        case NONE:
            // No-op.
    }

    if (qry.isFieldQuery()) {
        SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(sql);
        sqlFieldsQry.setArgs(parameters);

        return sqlFieldsQry;
    }

    SqlQuery sqlQry = new SqlQuery(type, sql);
    sqlQry.setArgs(parameters);

    return sqlQry;
}
 
Example 3
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Test simple query by key.
 *
 * @param cacheName Cache name.
 * @param key1 Key 1.
 * @param key2 Key 2.
 * @param <T> Key type.
 */
private <T> void testKeyQuery(final String cacheName, final T key1, final T key2) {
    final IgniteCache<T, Person> cache = ignite(0).cache(cacheName);

    final Person p1 = new Person("p1");
    final Person p2 = new Person("p2");

    cache.put(key1, p1);
    cache.put(key2, p2);

    final SqlQuery<T, Person> qry = new SqlQuery<>(Person.class, "where _key=?");

    final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from Person where _key=?");

    qry.setLocal(isLocal());
    fieldsQry.setLocal(isLocal());

    qry.setArgs(key1);
    fieldsQry.setArgs(key1);

    final List<Cache.Entry<T, Person>> res = cache.query(qry).getAll();
    final List<List<?>> fieldsRes = cache.query(fieldsQry).getAll();

    assertEquals(1, res.size());
    assertEquals(1, fieldsRes.size());

    assertEquals(p1, res.get(0).getValue());
    assertEquals(key1, res.get(0).getKey());

    assertTrue(fieldsRes.get(0).size() >= 2);
    assertEquals(key1, fieldsRes.get(0).get(0));
    assertEquals(p1, fieldsRes.get(0).get(1));
}
 
Example 4
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Test simple query by value.
 *
 * @param cacheName Cache name.
 * @param val1 Value 1.
 * @param val2 Value 2.
 * @param <T> Value type.
 */
private <T> void testValQuery(final String cacheName, final T val1, final T val2) {
    final IgniteCache<Person, T> cache = ignite(0).cache(cacheName);

    final Class<?> valType = val1.getClass();

    final Person p1 = new Person("p1");
    final Person p2 = new Person("p2");

    cache.put(p1, val1);
    cache.put(p2, val2);

    final SqlQuery<Person, T> qry = new SqlQuery<>(valType, "where _val=?");

    final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from " + valType.getSimpleName() + " where _val=?");

    qry.setLocal(isLocal());
    fieldsQry.setLocal(isLocal());

    qry.setArgs(val1);
    fieldsQry.setArgs(val1);

    final List<Cache.Entry<Person, T>> res = cache.query(qry).getAll();
    final List<List<?>> fieldsRes = cache.query(fieldsQry).getAll();

    assertEquals(1, res.size());
    assertEquals(1, fieldsRes.size());

    assertEquals(p1, res.get(0).getKey());
    assertEquals(val1, res.get(0).getValue());

    assertTrue(fieldsRes.get(0).size() >= 2);
    assertEquals(p1, fieldsRes.get(0).get(0));
    assertEquals(val1, fieldsRes.get(0).get(1));
}
 
Example 5
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testFieldSearch() throws Exception {
    final IgniteCache<Integer, SearchValue> cache = ignite(0).cache(FIELD_CACHE);

    final Map<Integer, SearchValue> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.put(i,
            new SearchValue(
                UUID.randomUUID(),
                String.valueOf(i),
                new BigDecimal(i * 0.1),
                i,
                new Date(i),
                new Timestamp(i),
                new Person(String.valueOf("name-" + i)),
                i % 2 == 0 ? EnumKey.KEY1 : EnumKey.KEY2)
        );
    }

    cache.putAll(map);

    SqlQuery<Integer, SearchValue> qry = new SqlQuery<>(SearchValue.class,
        "where uuid=? and str=? and decimal=? and integer=? and date=? and ts=? and person=? and enumKey=?");

    final int k = ThreadLocalRandom.current().nextInt(10);

    final SearchValue val = map.get(k);

    qry.setLocal(isLocal());
    qry.setArgs(val.uuid, val.str, val.decimal, val.integer, val.date, val.ts, val.person, val.enumKey);

    final List<Cache.Entry<Integer, SearchValue>> res = cache.query(qry).getAll();

    assertEquals(1, res.size());

    assertEquals(val.integer, res.get(0).getKey());
    assertEquals(val, res.get(0).getValue());
}
 
Example 6
Source File: GridCacheQuerySerializationSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public List<Cache.Entry<Integer, GridCacheQueryTestValue>> call() throws Exception {
    IgniteCache<Integer, GridCacheQueryTestValue> c = ignite.cache(CACHE_NAME);

    String sqlStr = "FROM GridCacheQueryTestValue WHERE fieldname = ?";
    SqlQuery<Integer, GridCacheQueryTestValue> sql = new SqlQuery<>(GridCacheQueryTestValue.class, sqlStr);
    sql.setArgs("C");

    return c.query(sql.setSql(sqlStr)).getAll();
}
 
Example 7
Source File: IgniteCacheDistributedPartitionQueryAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param orig Originator.
 */
protected void doTestRegionQuery(Ignite orig) {
    IgniteCache<ClientKey, Client> cl = orig.cache("cl");

    for (int regionId = 1; regionId <= PARTS_PER_REGION.length; regionId++) {
        SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "regionId=?");
        qry1.setArgs(regionId);

        List<Cache.Entry<ClientKey, Client>> clients1 = cl.query(qry1).getAll();

        int expRegionCnt = regionId == 5 ? 0 : PARTS_PER_REGION[regionId - 1] * CLIENTS_PER_PARTITION;

        assertEquals("Region " + regionId + " count", expRegionCnt, clients1.size());

        validateClients(regionId, clients1);

        // Repeat the same query with partition set condition.
        List<Integer> range = REGION_TO_PART_MAP.get(regionId);

        SqlQuery<ClientKey, Client> qry2 = new SqlQuery<>(Client.class, "1=1");
        qry2.setPartitions(createRange(range.get(0), range.get(1)));

        try {
            List<Cache.Entry<ClientKey, Client>> clients2 = cl.query(qry2).getAll();

            assertEquals("Region " + regionId + " count with partition set", expRegionCnt, clients2.size());

            // Query must produce only results from single region.
            validateClients(regionId, clients2);

            if (regionId == UNMAPPED_REGION)
                fail();
        } catch (CacheException ignored) {
            if (regionId != UNMAPPED_REGION)
                fail();
        }
    }
}
 
Example 8
Source File: IgniteSqlQueryPutBenchmark.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param minSalary Min salary.
 * @param maxSalary Max salary.
 * @return Query result.
 * @throws Exception If failed.
 */
private Collection<Cache.Entry<Integer, Object>> executeQuery(double minSalary, double maxSalary) throws Exception {
    SqlQuery qry = new SqlQuery(Person.class, "salary >= ? and salary <= ?");

    qry.setArgs(minSalary, maxSalary);

    return cache.query(qry).getAll();
}
 
Example 9
Source File: IgniteSqlMergeQueryBenchmark.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param minSalary Min salary.
 * @param maxSalary Max salary.
 * @return Query result.
 * @throws Exception If failed.
 */
private Collection<Cache.Entry<Integer, Object>> executeQuery(double minSalary, double maxSalary) throws Exception {
    SqlQuery qry = new SqlQuery(Person.class, "salary >= ? and salary <= ?");

    qry.setArgs(minSalary, maxSalary);

    return cache.query(qry).getAll();
}
 
Example 10
Source File: CacheMvccSqlQueriesAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param key Key.
 * @param cache Cache.
 */
private void checkNoValue(Object key, IgniteCache cache) {
    SqlQuery<Integer, MvccTestSqlIndexValue> qry;

    qry = new SqlQuery<>(MvccTestSqlIndexValue.class, "_key = ?");

    qry.setArgs(key);

    List<IgniteCache.Entry<Integer, MvccTestSqlIndexValue>> res = cache.query(qry).getAll();

    assertTrue(res.isEmpty());
}
 
Example 11
Source File: IgniteSqlQueryPutSeparatedBenchmark.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param minSalary Min salary.
 * @param maxSalary Max salary.
 * @return Query result.
 * @throws Exception If failed.
 */
private Collection<Cache.Entry<Integer, Object>> executeQuery(double minSalary, double maxSalary) throws Exception {
    IgniteCache<Integer, Object> cache = cacheForOperation(true);

    SqlQuery qry = new SqlQuery(Person.class, "salary >= ? and salary <= ?");

    qry.setArgs(minSalary, maxSalary);

    return cache.query(qry).getAll();
}
 
Example 12
Source File: IgniteSqlQueryBenchmark.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param minSalary Min salary.
 * @param maxSalary Max salary.
 * @return Query result.
 * @throws Exception If failed.
 */
private Collection<Cache.Entry<Integer, Object>> executeQuery(double minSalary, double maxSalary) throws Exception {
    IgniteCache<Integer, Object> cache = cacheForOperation(true);

    SqlQuery qry = new SqlQuery(Person.class, "salary >= ? and salary <= ?");

    qry.setArgs(minSalary, maxSalary);

    return cache.query(qry).getAll();
}
 
Example 13
Source File: IgniteSqlQueryFullScanBenchmark.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param minSalary Min salary.
 * @param maxSalary Max salary.
 * @return Query result.
 * @throws Exception If failed.
 */
private Collection<Cache.Entry<Integer, Object>> executeQuery(double minSalary, double maxSalary) throws Exception {
    IgniteCache<Integer, Object> cache = cacheForOperation(true);

    SqlQuery qry = new SqlQuery(PersonNoIndex.class, "salary >= ? and salary <= ?");

    qry.setArgs(minSalary, maxSalary);

    return cache.query(qry).getAll();
}
 
Example 14
Source File: JettyRestProcessorAbstractSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Init cache.
 */
protected void initCache() {
    CacheConfiguration typedCache = new CacheConfiguration<>("test_typed_access");
    ignite(0).getOrCreateCache(typedCache);

    CacheConfiguration<Integer, Organization> orgCacheCfg = new CacheConfiguration<>("organization");

    orgCacheCfg.setIndexedTypes(Integer.class, Organization.class);

    IgniteCache<Integer, Organization> orgCache = ignite(0).getOrCreateCache(orgCacheCfg);

    orgCache.clear();

    Organization o1 = new Organization(1, "o1");
    Organization o2 = new Organization(2, "o2");

    orgCache.put(1, o1);
    orgCache.put(2, o2);

    CacheConfiguration<Integer, Complex> complexCacheCfg = new CacheConfiguration<>("complex");

    complexCacheCfg.setIndexedTypes(Integer.class, Complex.class);

    grid(0).getOrCreateCache(complexCacheCfg).clear();

    CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>("person");

    personCacheCfg.setIndexedTypes(Integer.class, Person.class);

    IgniteCache<Integer, Person> personCache = grid(0).getOrCreateCache(personCacheCfg);

    personCache.clear();

    Person p1 = new Person(1, "John", "Doe", 2000);
    Person p2 = new Person(1, "Jane", "Doe", 1000);
    Person p3 = new Person(2, "John", "Smith", 1000);
    Person p4 = new Person(2, "Jane", "Smith", 2000);

    personCache.put(p1.getId(), p1);
    personCache.put(p2.getId(), p2);
    personCache.put(p3.getId(), p3);
    personCache.put(p4.getId(), p4);

    SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "salary > ? and salary <= ?");

    qry.setArgs(1000, 2000);

    assertEquals(2, personCache.query(qry).getAll().size());
}