/** * Creates result from cursor. * * @param cursor cursor to use to read column names and data. * @return Result that contains data and metadata, fetched from cursor. */ public static Result fromCursor(FieldsQueryCursor<List<?>> cursor) { List<String> cols = readColNames(cursor); List<List<?>> vals = cursor.getAll(); return new Result(cols, vals); }
/** */ @Test public void testReplicated() { sql("CREATE TABLE repl_tbl (id LONG PRIMARY KEY, val LONG) WITH \"template=replicated\"").getAll(); try { for (int i = 0; i < 10; i++) sql("insert into repl_tbl(id,val) VALUES(" + i + "," + i + ")").getAll(); Iterator<List<?>> it0 = sql(new SqlFieldsQuery("SELECT * FROM repl_tbl where id > ?").setArgs(1)).iterator(); it0.next(); sql(new SqlFieldsQuery("SELECT * FROM repl_tbl where id > ?").setArgs(1)).getAll(); it0.next(); } finally { sql("DROP TABLE repl_tbl").getAll(); } }
Iterator<List<?>> it = cur.iterator(); String[] colNames = new String[cur.getColumnsCount()]; for (int i = 0; i < colNames.length; ++i) { String colName = cur.getFieldName(i); if (F.isEmpty(fields)) { colNames[i] = colName.toLowerCase();
/** * Read colon names from cursor. * * @param cursor source of metadata. * @return List containing colon names. */ private static List<String> readColNames(FieldsQueryCursor<?> cursor) { ArrayList<String> colNames = new ArrayList<>(); for (int i = 0; i < cursor.getColumnsCount(); i++) colNames.add(cursor.getFieldName(i)); return Collections.unmodifiableList(colNames); }
/** * */ private void executeQuery() { IgniteCache<?, ?> cache = grid(1).cache("person"); SqlFieldsQuery qry = new SqlFieldsQuery("select name as \"Full Name\", age from person where age > 10"); FieldsQueryCursor<List<?>> qryCursor = cache.query(qry); assertEquals(2, qryCursor.getAll().size()); assertEquals(2, qryCursor.getColumnsCount()); // Row contains "name" and "age" fields. assertEquals("Full Name", qryCursor.getFieldName(0)); assertEquals("AGE", qryCursor.getFieldName(1)); }
@Override public void run() { IgniteCache<Object, Object> cache = grid(ThreadLocalRandom.current().nextInt(4)) .cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache.query( new SqlFieldsQuery("INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) values (4,4),(5,5),(6,6)"))) { assertEquals(3L, cur.iterator().next().get(0)); } } }).get(TX_TIMEOUT);
/** * Lazy query release partitions on cursor close test. * * @throws Exception If failed. */ @Test public void testLazyQueryPartitionsReleaseOnClose() throws Exception { Ignite node1 = startGrid(0); IgniteCache<Integer, Person> cache = node1.cache(PERSON_CACHE); cache.clear(); Affinity<Integer> aff = node1.affinity(PERSON_CACHE); int partsFilled = fillAllPartitions(cache, aff); SqlFieldsQuery qry = new SqlFieldsQuery("select name, age from person") .setLazy(true) .setPageSize(1); FieldsQueryCursor<List<?>> qryCursor = cache.query(qry); Iterator<List<?>> it = qryCursor.iterator(); if (it.hasNext()) it.next(); else fail("No query results."); startGrid(1); // Close cursor. Partitions should be released now. qryCursor.close(); for (Ignite ig : G.allGrids()) ig.cache(PERSON_CACHE).rebalance().get(); assertEquals("Wrong result set size", partsFilled, cache.query(qry).getAll().size()); }
/** * Close all cursors. */ public void closeAll() { for (FieldsQueryCursor<List<?>> cursor : cursors) cursor.close(); }
/** * Ctor. * * @param cursor Cursor. * @param pageSize Page size. * @param ctx Context. */ ClientCacheFieldsQueryCursor(FieldsQueryCursor<List> cursor, int pageSize, ClientConnectionContext ctx) { super(cursor, pageSize, ctx); columnCount = cursor.getColumnsCount(); }
assertEquals((long)BATCH_SIZE, cur.iterator().next().get(0));
/** {@inheritDoc} */ @Override public void processOutStream(int type, final BinaryRawWriterEx writer) throws IgniteCheckedException { if (type == OP_GET_FIELD_NAMES) { FieldsQueryCursor fq = (FieldsQueryCursor) cursor(); int cnt = fq.getColumnsCount(); writer.writeInt(cnt); for (int i = 0; i < cnt; i++) { writer.writeString(fq.getFieldName(i)); } } else { super.processOutStream(type, writer); } } }
fldQryCursor.close();
/** * Ctor. * * @param cursor Cursor. * @param pageSize Page size. * @param ctx Context. */ ClientCacheFieldsQueryCursor(FieldsQueryCursor<List> cursor, int pageSize, ClientConnectionContext ctx) { super(cursor, pageSize, ctx); columnCount = cursor.getColumnsCount(); }
/** * @return result of explain plan */ @NotNull private String doExplainPlan(IgniteCache<Object, Object> cache, String sql) { return cache.query(new SqlFieldsQuery(sql)).getAll().get(0).get(0).toString().toLowerCase(); }
/** */ @Test public void testLocalQuery() { sql("CREATE TABLE tbl (id LONG PRIMARY KEY, val LONG)").getAll(); try { for (int i = 0; i < 10; i++) sql("insert into tbl(id,val) VALUES(" + i + "," + i + ")").getAll(); Iterator<List<?>> it0 = sql( new SqlFieldsQuery("SELECT * FROM tbl where id > ?") .setArgs(1) .setLocal(true)) .iterator(); it0.next(); sql(new SqlFieldsQuery("SELECT * FROM tbl where id > ?").setArgs(1).setLocal(true)).getAll(); it0.next(); } finally { sql("DROP TABLE tbl").getAll(); } }
/** * Internal check routine. * * @param cls Entity class. * @throws Exception If failed. */ @SuppressWarnings("unchecked") private void check(Class cls) throws Exception { cache.put(1, createInstance(cls, 10)); cache.put(2, createInstance(cls, 20)); cache.put(3, createInstance(cls, 30)); Iterator iter = cache.query(new SqlQuery(cls, "val=20")).iterator(); assert iter.hasNext(); Cache.Entry res = (Cache.Entry)iter.next(); assertEquals(2, res.getKey()); assertEquals(Integer.valueOf(20), U.field(res.getValue(), "val")); assert !iter.hasNext(); iter = cache.query( new SqlFieldsQuery("SELECT p.val FROM " + cls.getSimpleName() + " p WHERE p.val=20")).iterator(); assert iter.hasNext(); List<Object> fieldsRes = (List<Object>)iter.next(); assertEquals(20, fieldsRes.get(0)); assert !iter.hasNext(); }
/** {@inheritDoc} */ @Override public void encode(BinaryRawWriterEx writer) { super.encode(writer); writer.writeLong(cursor.id()); int cnt = fieldsCursor.getColumnsCount(); writer.writeInt(cnt); if (includeFieldNames) { for (int i = 0; i < cnt; i++) { writer.writeString(fieldsCursor.getFieldName(i)); } } cursor.writePage(writer); } }
/** * Check cluster wide query id generation. * * @throws Exception Exception in case of failure. */ @Test public void testClusterWideQueryIdGeneration() throws Exception { newBarrier(1); IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME); for (int i = 0; i < 100; i++) { FieldsQueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("SELECT * FROM Integer WHERE 1 = 1")); Collection<GridRunningQueryInfo> runningQueries = ignite.context().query().runningQueries(-1); assertEquals(1, runningQueries.size()); GridRunningQueryInfo r = runningQueries.iterator().next(); assertEquals(ignite.context().localNodeId() + "_" + r.id(), r.globalQueryId()); cursor.close(); } }
@Override public Void call() throws Exception { clientCache.query(new SqlFieldsQuery("SELECT * FROM INTEGER ORDER BY _val")).getAll(); return null; } }, CacheServerNotFoundException.class, "Failed to find data nodes for cache");