Skip to content

Commit 8f327a1

Browse files
committedDec 19, 2017
IGNITE-7229 .NET: Rename ICache.QueryFields to Query, add FieldNames
This closes apache#3247
1 parent a2b195c commit 8f327a1

28 files changed

+224
-82
lines changed
 

‎modules/core/src/main/java/org/apache/ignite/internal/processors/platform/cache/query/PlatformAbstractQueryCursor.java

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public abstract class PlatformAbstractQueryCursor<T> extends PlatformAbstractTar
175175
*/
176176
protected abstract void write(BinaryRawWriterEx writer, T val);
177177

178+
/**
179+
* Gets the cursor.
180+
*
181+
* @return Cursor.
182+
*/
183+
public QueryCursorEx<T> cursor() {
184+
return cursor;
185+
}
186+
178187
/**
179188
* Query cursor consumer.
180189
*/

‎modules/core/src/main/java/org/apache/ignite/internal/processors/platform/cache/query/PlatformFieldsQueryCursor.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717

1818
package org.apache.ignite.internal.processors.platform.cache.query;
1919

20-
import java.util.List;
20+
import org.apache.ignite.IgniteCheckedException;
21+
import org.apache.ignite.cache.query.FieldsQueryCursor;
2122
import org.apache.ignite.internal.binary.BinaryRawWriterEx;
2223
import org.apache.ignite.internal.processors.cache.query.QueryCursorEx;
2324
import org.apache.ignite.internal.processors.platform.PlatformContext;
2425

26+
import java.util.List;
27+
2528
/**
2629
* Interop cursor for fields query.
2730
*/
2831
public class PlatformFieldsQueryCursor extends PlatformAbstractQueryCursor<List<?>> {
32+
/** Gets field names. */
33+
private static final int OP_GET_FIELD_NAMES = 7;
34+
2935
/**
3036
* Constructor.
3137
*
@@ -52,4 +58,20 @@ public PlatformFieldsQueryCursor(PlatformContext platformCtx, QueryCursorEx<List
5258

5359
writer.writeInt(rowSizePos, rowEndPos - rowSizePos);
5460
}
61+
62+
/** {@inheritDoc} */
63+
@Override public void processOutStream(int type, final BinaryRawWriterEx writer) throws IgniteCheckedException {
64+
if (type == OP_GET_FIELD_NAMES) {
65+
FieldsQueryCursor fq = (FieldsQueryCursor) cursor();
66+
67+
int cnt = fq.getColumnsCount();
68+
writer.writeInt(cnt);
69+
70+
for (int i = 0; i < cnt; i++) {
71+
writer.writeString(fq.getFieldName(i));
72+
}
73+
} else {
74+
super.processOutStream(type, writer);
75+
}
76+
}
5577
}

‎modules/platforms/dotnet/Apache.Ignite.AspNet.Tests/ExpiryCacheHolderTest.cs

+5
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ public IQueryCursor<ICacheEntry<int, int>> Query(QueryBase qry)
419419
throw new NotImplementedException();
420420
}
421421

422+
public IFieldsQueryCursor Query(SqlFieldsQuery qry)
423+
{
424+
throw new NotImplementedException();
425+
}
426+
422427
public IQueryCursor<IList> QueryFields(SqlFieldsQuery qry)
423428
{
424429
throw new NotImplementedException();

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryFooterTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private static void TestSql(OffsetTest dt, Marshaller marsh)
146146
Assert.AreEqual(dt.Arr, cache[1].Arr);
147147

148148
// SQL: read field on Java side to ensure correct offset handling.
149-
var res = cache.QueryFields(new SqlFieldsQuery("select int from OffsetTest")).GetAll()[0][0];
149+
var res = cache.Query(new SqlFieldsQuery("select int from OffsetTest")).GetAll()[0][0];
150150
Assert.AreEqual(dt.Int, (int) res);
151151
}
152152

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/SqlDmlTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void TestSimpleSerializable()
107107

108108
// Test DML.
109109
var guid = Guid.NewGuid();
110-
var insertRes = cache.QueryFields(new SqlFieldsQuery(
110+
var insertRes = cache.Query(new SqlFieldsQuery(
111111
"insert into SimpleSerializable(_key, Byte, Bool, Short, Int, Long, Float, Double, " +
112112
"Decimal, Guid, String) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
113113
3, 45, true, 43, 33, 99, 4.5f, 6.7, 9.04m, guid, "bar33")).GetAll();
@@ -141,7 +141,7 @@ public void TestDotNetSpecificSerializable()
141141
Assert.AreEqual(uint.MaxValue, cache[1].Uint);
142142

143143
// Test SQL.
144-
var sqlRes = cache.QueryFields(new SqlFieldsQuery(
144+
var sqlRes = cache.Query(new SqlFieldsQuery(
145145
"select uint from DotNetSpecificSerializable where uint <> 0")).GetAll();
146146

147147
Assert.AreEqual(1, sqlRes.Count);
@@ -152,7 +152,7 @@ public void TestDotNetSpecificSerializable()
152152
Assert.AreEqual(uint.MaxValue, linqRes);
153153

154154
// Test DML.
155-
var dmlRes = cache.QueryFields(new SqlFieldsQuery(
155+
var dmlRes = cache.Query(new SqlFieldsQuery(
156156
"insert into DotNetSpecificSerializable(_key, uint) values (?, ?), (?, ?)",
157157
2, uint.MaxValue, 3, 88)).GetAll();
158158
Assert.AreEqual(1, dmlRes.Count);

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/CacheTestAsyncWrapper.cs

+7
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ public IQueryCursor<ICacheEntry<TK, TV>> Query(QueryBase qry)
445445
}
446446

447447
/** <inheritDoc /> */
448+
public IFieldsQueryCursor Query(SqlFieldsQuery qry)
449+
{
450+
return _cache.Query(qry);
451+
}
452+
453+
/** <inheritDoc /> */
454+
[Obsolete]
448455
public IQueryCursor<IList> QueryFields(SqlFieldsQuery qry)
449456
{
450457
return _cache.QueryFields(qry);

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void TestPrimitiveKey()
7676
var cache = Ignition.GetIgnite().CreateCache<int, Foo>(cfg);
7777

7878
// Test insert.
79-
var res = cache.QueryFields(new SqlFieldsQuery("insert into foo(_key, id, name) " +
79+
var res = cache.Query(new SqlFieldsQuery("insert into foo(_key, id, name) " +
8080
"values (?, ?, ?), (?, ?, ?)",
8181
1, 2, "John", 3, 4, "Mary")).GetAll();
8282

@@ -118,7 +118,7 @@ public void TestNotNull()
118118

119119
var cache = Ignition.GetIgnite().CreateCache<int, Foo>(cfg);
120120

121-
var ex = Assert.Throws<IgniteException>(() => cache.QueryFields(new SqlFieldsQuery(
121+
var ex = Assert.Throws<IgniteException>(() => cache.Query(new SqlFieldsQuery(
122122
"insert into foo(_key, name) values (?, ?)", 1, "bar")).GetAll());
123123

124124
Assert.AreEqual("Null value is not allowed for column 'ID'", ex.Message);
@@ -133,7 +133,7 @@ public void TestNotNullAttribute()
133133
var cfg = new CacheConfiguration("not_null_attr", new QueryEntity(typeof(int), typeof(Foo)));
134134
var cache = Ignition.GetIgnite().CreateCache<int, Foo>(cfg);
135135

136-
var ex = Assert.Throws<IgniteException>(() => cache.QueryFields(new SqlFieldsQuery(
136+
var ex = Assert.Throws<IgniteException>(() => cache.Query(new SqlFieldsQuery(
137137
"insert into foo(_key, id) values (?, ?)", 1, 2)).GetAll());
138138

139139
Assert.AreEqual("Null value is not allowed for column 'NAME'", ex.Message);
@@ -169,7 +169,7 @@ private static void TestKey<T>(params T[] vals)
169169

170170
foreach (var val in vals)
171171
{
172-
var res = cache.QueryFields(new SqlFieldsQuery(
172+
var res = cache.Query(new SqlFieldsQuery(
173173
"insert into string(_key, _val) values (?, ?)", val, val.ToString())).GetAll();
174174

175175
Assert.AreEqual(1, res.Count);
@@ -190,7 +190,7 @@ public void TestCompositeKey()
190190
var cache = Ignition.GetIgnite().CreateCache<Key, Foo>(cfg);
191191

192192
// Test insert.
193-
var res = cache.QueryFields(new SqlFieldsQuery("insert into foo(hi, lo, id, name) " +
193+
var res = cache.Query(new SqlFieldsQuery("insert into foo(hi, lo, id, name) " +
194194
"values (-1, 65500, 3, 'John'), (255, 128, 6, 'Mary')")).GetAll();
195195

196196
Assert.AreEqual(1, res.Count);
@@ -240,7 +240,7 @@ public void TestCompositeKey2()
240240
var cache = Ignition.GetIgnite().CreateCache<Key2, Foo>(cfg);
241241

242242
// Test insert.
243-
var res = cache.QueryFields(new SqlFieldsQuery("insert into foo(hi, lo, str, id, name) " +
243+
var res = cache.Query(new SqlFieldsQuery("insert into foo(hi, lo, str, id, name) " +
244244
"values (1, 2, 'Фу', 3, 'John'), (4, 5, 'Бар', 6, 'Mary')")).GetAll();
245245

246246
Assert.AreEqual(1, res.Count);
@@ -293,7 +293,7 @@ public void TestInvalidCompositeKey()
293293
var cache = Ignition.GetIgnite().CreateCache<Key, Foo>(cfg);
294294

295295
var ex = Assert.Throws<IgniteException>(
296-
() => cache.QueryFields(new SqlFieldsQuery("insert into foo(lo, hi, id, name) " +
296+
() => cache.Query(new SqlFieldsQuery("insert into foo(lo, hi, id, name) " +
297297
"values (1, 2, 3, 'John'), (4, 5, 6, 'Mary')")));
298298

299299
Assert.AreEqual("Ownership flag not set for binary property. Have you set 'keyFields' " +
@@ -322,7 +322,7 @@ public void TestBinaryMode()
322322
var cache = Ignition.GetIgnite().CreateCache<object, object>(cfg)
323323
.WithKeepBinary<IBinaryObject, IBinaryObject>();
324324

325-
var res = cache.QueryFields(new SqlFieldsQuery("insert into car(VIN, Id, Make, Year) " +
325+
var res = cache.Query(new SqlFieldsQuery("insert into car(VIN, Id, Make, Year) " +
326326
"values ('DLRDMC', 88, 'DeLorean', 1982)")).GetAll();
327327

328328
Assert.AreEqual(1, res.Count);
@@ -362,7 +362,7 @@ public void TestCompositeKeyAllDataTypes()
362362
};
363363

364364
// Test insert.
365-
var res = cache.QueryFields(new SqlFieldsQuery(
365+
var res = cache.Query(new SqlFieldsQuery(
366366
"insert into string(byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, " +
367367
"guid, string, key, _val) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
368368
key.Byte, key.SByte, key.Short, key.UShort, key.Int, key.UInt, key.Long, key.ULong, key.Float,

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void TestQueryEntityConfiguration()
9191
Assert.AreEqual(2, cursor.GetAll().Single().Key);
9292
}
9393

94-
using (var cursor = cache.QueryFields(new SqlFieldsQuery(
94+
using (var cursor = cache.Query(new SqlFieldsQuery(
9595
"select _key from CustomTableName where age > ? and birthday < ?", 10, DateTime.UtcNow)))
9696
{
9797
Assert.AreEqual(2, cursor.GetAll().Single()[0]);

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs

+53-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
namespace Apache.Ignite.Core.Tests.Cache.Query
2121
{
2222
using System;
23-
using System.Collections;
2423
using System.Collections.Generic;
2524
using System.Diagnostics.CodeAnalysis;
2625
using System.Linq;
@@ -156,7 +155,7 @@ public void TestValidationSql()
156155
public void TestValidationSqlFields()
157156
{
158157
// 1. No sql.
159-
Assert.Throws<ArgumentException>(() => { Cache().QueryFields(new SqlFieldsQuery(null)); });
158+
Assert.Throws<ArgumentException>(() => { Cache().Query(new SqlFieldsQuery(null)); });
160159
}
161160

162161
/// <summary>
@@ -285,11 +284,9 @@ public void TestSqlFieldsQueryArguments()
285284
Cache().Put(3, new QueryPerson("Sidorov", 50));
286285

287286
// 1. Empty result set.
288-
using (
289-
IQueryCursor<IList> cursor = Cache().QueryFields(
290-
new SqlFieldsQuery("SELECT age FROM QueryPerson WHERE age < ?", 50)))
287+
using (var cursor = Cache().Query(new SqlFieldsQuery("SELECT age FROM QueryPerson WHERE age < ?", 50)))
291288
{
292-
foreach (IList entry in cursor.GetAll())
289+
foreach (var entry in cursor.GetAll())
293290
Assert.IsTrue((int) entry[0] < 50);
294291
}
295292
}
@@ -390,7 +387,7 @@ public void TestSqlFieldsQuery([Values(true, false)] bool loc, [Values(true, fal
390387
Lazy = lazy
391388
};
392389

393-
using (IQueryCursor<IList> cursor = cache.QueryFields(qry))
390+
using (var cursor = cache.Query(qry))
394391
{
395392
HashSet<int> exp0 = new HashSet<int>(exp);
396393

@@ -403,9 +400,12 @@ public void TestSqlFieldsQuery([Values(true, false)] bool loc, [Values(true, fal
403400
}
404401

405402
Assert.AreEqual(0, exp0.Count);
403+
Assert.AreEqual(new[] {"NAME", "AGE"}, cursor.FieldNames);
406404
}
407405

408-
using (IQueryCursor<IList> cursor = cache.QueryFields(qry))
406+
// Test old API as well.
407+
#pragma warning disable 618
408+
using (var cursor = cache.QueryFields(qry))
409409
{
410410
HashSet<int> exp0 = new HashSet<int>(exp);
411411

@@ -418,6 +418,7 @@ public void TestSqlFieldsQuery([Values(true, false)] bool loc, [Values(true, fal
418418

419419
Assert.AreEqual(0, exp0.Count);
420420
}
421+
#pragma warning restore 618
421422
}
422423

423424
/// <summary>
@@ -622,15 +623,15 @@ public void TestCustomSchema()
622623
strings[1] = "foo";
623624

624625
// Default schema.
625-
var res = doubles.QueryFields(new SqlFieldsQuery(
626+
var res = doubles.Query(new SqlFieldsQuery(
626627
"select S._val from double as D join \"strings\".string as S on S._key = D._key"))
627628
.Select(x => (string) x[0])
628629
.Single();
629630

630631
Assert.AreEqual("foo", res);
631632

632633
// Custom schema.
633-
res = doubles.QueryFields(new SqlFieldsQuery(
634+
res = doubles.Query(new SqlFieldsQuery(
634635
"select S._val from \"doubles\".double as D join string as S on S._key = D._key")
635636
{
636637
Schema = strings.Name
@@ -667,13 +668,13 @@ public void TestDistributedJoins()
667668
var sql = "select T0.Age from QueryPerson as T0 " +
668669
"inner join QueryPerson as T1 on ((? - T1.Age - 1) = T0._key)";
669670

670-
var res = cache.QueryFields(new SqlFieldsQuery(sql, count)).GetAll().Distinct().Count();
671+
var res = cache.Query(new SqlFieldsQuery(sql, count)).GetAll().Distinct().Count();
671672

672673
Assert.Greater(res, 0);
673674
Assert.Less(res, count);
674675

675676
// Test distributed join: returns complete results
676-
res = cache.QueryFields(new SqlFieldsQuery(sql, count) {EnableDistributedJoins = true})
677+
res = cache.Query(new SqlFieldsQuery(sql, count) {EnableDistributedJoins = true})
677678
.GetAll().Distinct().Count();
678679

679680
Assert.AreEqual(count, res);
@@ -709,7 +710,7 @@ public void TestCustomKeyValueFieldNames()
709710

710711
cache[1] = new QueryPerson("Joe", 48);
711712

712-
var row = cache.QueryFields(new SqlFieldsQuery("select * from QueryPerson")).GetAll()[0];
713+
var row = cache.Query(new SqlFieldsQuery("select * from QueryPerson")).GetAll()[0];
713714
Assert.AreEqual(2, row.Count);
714715
Assert.AreEqual(48, row[0]);
715716
Assert.AreEqual("Joe", row[1]);
@@ -736,7 +737,7 @@ public void TestCustomKeyValueFieldNames()
736737

737738
cache[1] = new QueryPerson("John", 33);
738739

739-
row = cache.QueryFields(new SqlFieldsQuery("select * from QueryPerson")).GetAll()[0];
740+
row = cache.Query(new SqlFieldsQuery("select * from QueryPerson")).GetAll()[0];
740741

741742
Assert.AreEqual(3, row.Count);
742743
Assert.AreEqual(33, row[0]);
@@ -746,7 +747,7 @@ public void TestCustomKeyValueFieldNames()
746747
Assert.AreEqual("John", person.Name);
747748

748749
// Check explicit select.
749-
row = cache.QueryFields(new SqlFieldsQuery("select FullKey from QueryPerson")).GetAll()[0];
750+
row = cache.Query(new SqlFieldsQuery("select FullKey from QueryPerson")).GetAll()[0];
750751
Assert.AreEqual(1, row[0]);
751752
}
752753

@@ -786,10 +787,46 @@ public void TestSqlFieldsQueryTimeout()
786787
};
787788

788789
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
789-
var ex = Assert.Throws<CacheException>(() => cache.QueryFields(fieldsQry).ToArray());
790+
var ex = Assert.Throws<CacheException>(() => cache.Query(fieldsQry).ToArray());
790791
Assert.IsTrue(ex.ToString().Contains("QueryCancelledException: The query was cancelled while executing."));
791792
}
792793

794+
/// <summary>
795+
/// Tests the FieldNames property.
796+
/// </summary>
797+
[Test]
798+
public void TestFieldNames()
799+
{
800+
var cache = Cache();
801+
PopulateCache(cache, false, 5, x => true);
802+
803+
// Get before iteration.
804+
var qry = new SqlFieldsQuery("SELECT * FROM QueryPerson");
805+
var cur = cache.Query(qry);
806+
var names = cur.FieldNames;
807+
808+
Assert.AreEqual(new[] {"AGE", "NAME" }, names);
809+
810+
cur.Dispose();
811+
Assert.AreSame(names, cur.FieldNames);
812+
813+
Assert.Throws<NotSupportedException>(() => cur.FieldNames.Add("x"));
814+
815+
// Custom order, key-val, get after iteration.
816+
qry.Sql = "SELECT NAME, _key, AGE, _val FROM QueryPerson";
817+
cur = cache.Query(qry);
818+
cur.GetAll();
819+
820+
Assert.AreEqual(new[] { "NAME", "_KEY", "AGE", "_VAL" }, cur.FieldNames);
821+
822+
// Get after disposal.
823+
qry.Sql = "SELECT 1, AGE FROM QueryPerson";
824+
cur = cache.Query(qry);
825+
cur.Dispose();
826+
827+
Assert.AreEqual(new[] { "1", "AGE" }, cur.FieldNames);
828+
}
829+
793830
/// <summary>
794831
/// Validates the query results.
795832
/// </summary>

‎modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/Linq/CacheLinqTest.Introspection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void TestIntrospection()
7272

7373
Assert.AreEqual(new[] { 10 }, fq.Arguments);
7474
Assert.IsTrue(fq.Local);
75-
Assert.AreEqual(PersonCount - 11, cache.QueryFields(fq).GetAll().Count);
75+
Assert.AreEqual(PersonCount - 11, cache.Query(fq).GetAll().Count);
7676
Assert.AreEqual(999, fq.PageSize);
7777
Assert.IsFalse(fq.EnableDistributedJoins);
7878
Assert.IsTrue(fq.EnforceJoinOrder);

0 commit comments

Comments
 (0)
Please sign in to comment.