public void TestCursor() { Cache().Put(1, new QueryPerson("Ivanov", 30)); Cache().Put(1, new QueryPerson("Petrov", 40)); Cache().Put(1, new QueryPerson("Sidorov", 50)); SqlQuery qry = new SqlQuery(typeof(QueryPerson), "age >= 20"); // 1. Test GetAll(). using (IQueryCursor <ICacheEntry <int, QueryPerson> > cursor = Cache().Query(qry)) { cursor.GetAll(); Assert.Throws <InvalidOperationException>(() => { cursor.GetAll(); }); Assert.Throws <InvalidOperationException>(() => { cursor.GetEnumerator(); }); } // 2. Test GetEnumerator. using (IQueryCursor <ICacheEntry <int, QueryPerson> > cursor = Cache().Query(qry)) { cursor.GetEnumerator(); Assert.Throws <InvalidOperationException>(() => { cursor.GetAll(); }); Assert.Throws <InvalidOperationException>(() => { cursor.GetEnumerator(); }); } }
static void Main() { IIgnite ignite = Ignition.Start(); ICache <int, Person> cache = ignite.GetOrCreateCache <int, Person>( new CacheConfiguration("persons", typeof(Person))); cache[1] = new Person { Name = "John Doe", Age = 27 }; cache[2] = new Person { Name = "Jane Moe", Age = 43 }; var sqlQuery = new SqlQuery(typeof(Person), "where Age > ?", 30); IQueryCursor <ICacheEntry <int, Person> > queryCursor = cache.Query(sqlQuery); foreach (ICacheEntry <int, Person> cacheEntry in queryCursor) { Console.WriteLine(cacheEntry); } var fieldsQuery = new SqlFieldsQuery("select name from Person where Age > ? ", 30); IQueryCursor <IList> quertCursor = cache.QueryFields(fieldsQuery); foreach (IList fieldsList in quertCursor) { Console.WriteLine(fieldsList[0]); } var fieldsquery = new SqlFieldsQuery("select sum(Age) from Person"); IQueryCursor <IList> queryCursor1 = cache.QueryFields(fieldsquery); Console.WriteLine(queryCursor1.GetAll()[0][0]); }
public void TestSqlFieldsQuery([Values(true, false)] bool loc, [Values(true, false)] bool distrJoin, [Values(true, false)] bool enforceJoinOrder, [Values(true, false)] bool lazy) { int cnt = MaxItemCnt; var cache = Cache(); // 1. Populate cache with data, calculating expected count in parallel. var exp = PopulateCache(cache, loc, cnt, x => x < 50); // 2. Validate results. var qry = new SqlFieldsQuery("SELECT name, age FROM QueryPerson WHERE age < 50") { EnableDistributedJoins = distrJoin, EnforceJoinOrder = enforceJoinOrder, Colocated = !distrJoin, ReplicatedOnly = false, Local = loc, Timeout = TimeSpan.FromSeconds(2), Lazy = lazy }; using (IQueryCursor <IList> cursor = cache.QueryFields(qry)) { HashSet <int> exp0 = new HashSet <int>(exp); foreach (var entry in cursor.GetAll()) { Assert.AreEqual(2, entry.Count); Assert.AreEqual(entry[0].ToString(), entry[1].ToString()); exp0.Remove((int)entry[1]); } Assert.AreEqual(0, exp0.Count); } using (IQueryCursor <IList> cursor = cache.QueryFields(qry)) { HashSet <int> exp0 = new HashSet <int>(exp); foreach (var entry in cursor) { Assert.AreEqual(entry[0].ToString(), entry[1].ToString()); exp0.Remove((int)entry[1]); } Assert.AreEqual(0, exp0.Count); } }
/// <summary> /// Test SQL fields query arguments passing. /// </summary> public void TestSqlFieldsQueryArguments() { Cache().Put(1, new QueryPerson("Ivanov", 30)); Cache().Put(2, new QueryPerson("Petrov", 40)); Cache().Put(3, new QueryPerson("Sidorov", 50)); // 1. Empty result set. using ( IQueryCursor <IList> cursor = Cache().QueryFields( new SqlFieldsQuery("SELECT age FROM QueryPerson WHERE age < ?", 50))) { foreach (IList entry in cursor.GetAll()) { Assert.IsTrue((int)entry[0] < 50); } } }
/// <summary> /// Test SQL query arguments passing. /// </summary> public void TestSqlQueryArguments() { Cache().Put(1, new QueryPerson("Ivanov", 30)); Cache().Put(2, new QueryPerson("Petrov", 40)); Cache().Put(3, new QueryPerson("Sidorov", 50)); // 1. Empty result set. using ( IQueryCursor <ICacheEntry <int, QueryPerson> > cursor = Cache().Query(new SqlQuery(typeof(QueryPerson), "age < ?", 50))) { foreach (ICacheEntry <int, QueryPerson> entry in cursor.GetAll()) { Assert.IsTrue(entry.Key == 1 || entry.Key == 2); } } }
/// <summary> /// Check SQL fields query. /// </summary> /// <param name="cnt">Amount of cache entries to create.</param> /// <param name="loc">Local query flag.</param> private void CheckSqlFieldsQuery(int cnt, bool loc) { var cache = Cache(); // 1. Populate cache with data, calculating expected count in parallel. var exp = PopulateCache(cache, loc, cnt, x => x < 50); // 2. Vlaidate results. SqlFieldsQuery qry = loc ? new SqlFieldsQuery("SELECT name, age FROM QueryPerson WHERE age < 50", true) : new SqlFieldsQuery("SELECT name, age FROM QueryPerson WHERE age < 50"); using (IQueryCursor <IList> cursor = cache.QueryFields(qry)) { HashSet <int> exp0 = new HashSet <int>(exp); foreach (var entry in cursor.GetAll()) { Assert.AreEqual(2, entry.Count); Assert.AreEqual(entry[0].ToString(), entry[1].ToString()); exp0.Remove((int)entry[1]); } Assert.AreEqual(0, exp0.Count); } using (IQueryCursor <IList> cursor = cache.QueryFields(qry)) { HashSet <int> exp0 = new HashSet <int>(exp); foreach (var entry in cursor) { Assert.AreEqual(entry[0].ToString(), entry[1].ToString()); exp0.Remove((int)entry[1]); } Assert.AreEqual(0, exp0.Count); } }