Skip to content

Commit 87dd071

Browse files
authored
Doc 8514 p3x0 (#477)
* DOC-8514 -- Tweak Java examples (#472) https://issues.couchbase.com/browse/DOC-8514 (cherry picked from commit 5ab55bf) * DOC-8514-P3x0 -- Include Java changes from QF210602-1 (2.8)
1 parent 92c3fa5 commit 87dd071

File tree

4 files changed

+267
-179
lines changed

4 files changed

+267
-179
lines changed

modules/ROOT/pages/_partials/commons/common-querybuilder.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ The `Select` statement for this type of query, which returns all document proper
476476
include::{snippet}[tags=query-syntax-all]
477477
478478
----
479-
<.> Assumes `dbname` has been previously defined as the name of your database.
480479
481480
====
482481

modules/android/examples/docsnippets/app/src/main/java/com/example/docsnippet/TestQueries.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class TestQueries {
3232
init();
3333
}
3434

35+
private Hotel hotel;
36+
3537
private static void init() {
3638
}
3739

@@ -40,6 +42,12 @@ private static void init() {
4042
public void testQuerySyntaxAll() throws CouchbaseLiteException {
4143

4244
// tag::query-syntax-all[]
45+
try {
46+
this_Db = new Database(dbName);
47+
} catch (CouchbaseLiteException e) {
48+
e.printStackTrace();
49+
}
50+
4351
Query listQuery = QueryBuilder.select(SelectResult.all())
4452
.from(DataSource.database(this_Db)); // <.>
4553

@@ -50,22 +58,21 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
5058
for (Result result : listQuery.execute().allResults()) {
5159
int x = result.count();
5260
// get the k-v pairs from the 'hotel' key's value into a dictionary
53-
thisDocsProps = result.getDictionary(dbName); // <.>
61+
thisDocsProps = result.getDictionary(0); // <.>
5462
thisDocsId = thisDocsProps.getString("id");
5563
thisDocsName = thisDocsProps.getString("Name");
5664
thisDocsType = thisDocsProps.getString("Type");
5765
thisDocsCity = thisDocsProps.getString("City");
5866

5967
// Alternatively, access results value dictionary directly
6068
final Hotel hotel = new Hotel();
61-
hotel.Id = result.getDictionary(dbName).getString("id"); // <.>
62-
hotel.Type = result.getDictionary(dbName).getString("Type");
63-
hotel.Name = result.getDictionary(dbName).getString("Name");
64-
hotel.City = result.getDictionary(dbName).getString("City");
65-
hotel.Country= result.getDictionary(dbName).getString("Country");
66-
hotel.Description = result.getDictionary(dbName).getString("Description");
69+
hotel.Id = result.getDictionary(0).getString("id"); // <.>
70+
hotel.Type = result.getDictionary(0).getString("Type");
71+
hotel.Name = result.getDictionary(0).getString("Name");
72+
hotel.City = result.getDictionary(0).getString("City");
73+
hotel.Country= result.getDictionary(0).getString("Country");
74+
hotel.Description = result.getDictionary(0).getString("Description");
6775
hotels.put(hotel.Id, hotel);
68-
6976
}
7077
} catch (CouchbaseLiteException e) {
7178
e.printStackTrace();
@@ -77,6 +84,12 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
7784
public void testQuerySyntaxProps() throws CouchbaseLiteException {
7885

7986
// tag::query-syntax-props[]
87+
try {
88+
this_Db = new Database("hotels");
89+
} catch (CouchbaseLiteException e) {
90+
e.printStackTrace();
91+
}
92+
8093
Query listQuery =
8194
QueryBuilder.select(SelectResult.expression(Meta.id),
8295
SelectResult.property("name"),
@@ -125,6 +138,12 @@ public void testQuerySyntaxProps() throws CouchbaseLiteException {
125138
public void testQuerySyntaxCount() throws CouchbaseLiteException {
126139

127140
// tag::query-syntax-count-only[]
141+
try {
142+
this_Db = new Database("hotels");
143+
} catch (CouchbaseLiteException e) {
144+
e.printStackTrace();
145+
}
146+
128147
Query listQuery = QueryBuilder.select(
129148
SelectResult.expression(Function.count(Expression.string("*"))).as("mycount")) // <.>
130149
.from(DataSource.database(this_Db));
@@ -142,7 +161,7 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
142161
// Alternatively, use the index
143162
Integer orDocId = result.getInt(0);
144163
}
145-
// Or even
164+
// Or even omit the for-loop altogether
146165
Integer resultCount = listQuery.execute().next().getInt("mycount");
147166

148167
} catch (CouchbaseLiteException e) {
@@ -154,6 +173,13 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
154173

155174
public void testQuerySyntaxId() throws CouchbaseLiteException {
156175
// tag::query-syntax-id[]
176+
try {
177+
this_Db = new Database("hotels");
178+
} catch (CouchbaseLiteException e) {
179+
e.printStackTrace();
180+
}
181+
182+
157183
Query listQuery =
158184
QueryBuilder.select(SelectResult.expression(Meta.id).as("metaID"))
159185
.from(DataSource.database(this_Db));
@@ -193,6 +219,12 @@ public void testQueryPagination() throws CouchbaseLiteException {
193219
int thisOffset = 0;
194220
int thisLimit = 20;
195221

222+
try {
223+
this_Db = new Database("hotels");
224+
} catch (CouchbaseLiteException e) {
225+
e.printStackTrace();
226+
}
227+
196228
Query listQuery =
197229
QueryBuilder
198230
.select(SelectResult.all())

modules/android/examples/snippets/app/src/main/java/com/couchbase/code_snippets/Examples.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,30 +2601,34 @@ private static void init() {
26012601
public void testQuerySyntaxAll() throws CouchbaseLiteException {
26022602

26032603
// tag::query-syntax-all[]
2604-
Query listQuery = QueryBuilder.select(SelectResult.all())
2605-
.from(DataSource.database(this_Db)); // <.>
2604+
try {
2605+
this_Db = new Database("hotels");
2606+
} catch (CouchbaseLiteException e) {
2607+
e.printStackTrace();
2608+
}
26062609

2610+
Query listQuery = QueryBuilder.select(SelectResult.all())
2611+
.from(DataSource.database(this_Db));
26072612
// end::query-syntax-all[]
26082613

26092614
// tag::query-access-all[]
26102615
try {
26112616
for (Result result : listQuery.execute().allResults()) {
2612-
int x = result.count();
2613-
// get the k-v pairs from the 'hotel' key's value into a dictionary
2614-
thisDocsProps = result.getDictionary(dbName); // <.>
2617+
// get the k-v pairs from the 'hotel' key's value into a dictionary
2618+
thisDocsProps = result.getDictionary(0)); // <.>
26152619
thisDocsId = thisDocsProps.getString("id");
26162620
thisDocsName = thisDocsProps.getString("Name");
26172621
thisDocsType = thisDocsProps.getString("Type");
26182622
thisDocsCity = thisDocsProps.getString("City");
26192623

26202624
// Alternatively, access results value dictionary directly
26212625
final Hotel hotel = new Hotel();
2622-
hotel.Id = result.getDictionary(dbName).getString("id"); // <.>
2623-
hotel.Type = result.getDictionary(dbName).getString("Type");
2624-
hotel.Name = result.getDictionary(dbName).getString("Name");
2625-
hotel.City = result.getDictionary(dbName).getString("City");
2626-
hotel.Country= result.getDictionary(dbName).getString("Country");
2627-
hotel.Description = result.getDictionary(dbName).getString("Description");
2626+
hotel.Id = result.getDictionary(0).getString("id"); // <.>
2627+
hotel.Type = result.getDictionary(0).getString("Type");
2628+
hotel.Name = result.getDictionary(0).getString("Name");
2629+
hotel.City = result.getDictionary(0).getString("City");
2630+
hotel.Country= result.getDictionary(0).getString("Country");
2631+
hotel.Description = result.getDictionary(0).getString("Description");
26282632
hotels.put(hotel.Id, hotel);
26292633

26302634
}
@@ -2638,6 +2642,12 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
26382642
public void testQuerySyntaxProps() throws CouchbaseLiteException {
26392643

26402644
// tag::query-syntax-props[]
2645+
try {
2646+
this_Db = new Database("hotels");
2647+
} catch (CouchbaseLiteException e) {
2648+
e.printStackTrace();
2649+
}
2650+
26412651
Query listQuery =
26422652
QueryBuilder.select(SelectResult.expression(Meta.id),
26432653
SelectResult.property("name"),
@@ -2684,13 +2694,18 @@ public void testQuerySyntaxProps() throws CouchbaseLiteException {
26842694

26852695

26862696
public void testQuerySyntaxCount() throws CouchbaseLiteException {
2697+
try {
2698+
this_Db = new Database("hotels");
2699+
} catch (CouchbaseLiteException e) {
2700+
e.printStackTrace();
2701+
}
26872702

2688-
// tag::query-syntax-count-only[]
2689-
Query listQuery = QueryBuilder.select(
2690-
SelectResult.expression(Function.count(Expression.string("*"))).as("mycount")) // <.>
2691-
.from(DataSource.database(this_Db));
2703+
// tag::query-syntax-count-only[]
2704+
Query listQuery = QueryBuilder.select(
2705+
SelectResult.expression(Function.count(Expression.string("*"))).as("mycount")) // <.>
2706+
.from(DataSource.database(this_Db));
26922707

2693-
// end::query-syntax-count-only[]
2708+
// end::query-syntax-count-only[]
26942709

26952710

26962711
// tag::query-access-count-only[]
@@ -2703,7 +2718,7 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
27032718
// Alternatively, use the index
27042719
Integer orDocId = result.getInt(0);
27052720
}
2706-
// Or even
2721+
// Or even miss out the for-loop altogether
27072722
Integer resultCount = listQuery.execute().next().getInt("mycount");
27082723

27092724
} catch (CouchbaseLiteException e) {
@@ -2714,12 +2729,18 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
27142729

27152730

27162731
public void testQuerySyntaxId() throws CouchbaseLiteException {
2717-
// tag::query-syntax-id[]
2718-
Query listQuery =
2719-
QueryBuilder.select(SelectResult.expression(Meta.id).as("metaID"))
2720-
.from(DataSource.database(this_Db));
2732+
// tag::query-syntax-id[]
2733+
try {
2734+
this_Db = new Database("hotels");
2735+
} catch (CouchbaseLiteException e) {
2736+
e.printStackTrace();
2737+
}
2738+
2739+
Query listQuery =
2740+
QueryBuilder.select(SelectResult.expression(Meta.id).as("metaID"))
2741+
.from(DataSource.database(this_Db));
27212742

2722-
// end::query-syntax-id[]
2743+
// end::query-syntax-id[]
27232744

27242745

27252746
// tag::query-access-id[]
@@ -2751,14 +2772,21 @@ public void testQueryPagination() throws CouchbaseLiteException {
27512772

27522773

27532774
// tag::query-syntax-pagination[]
2775+
try {
2776+
this_Db = new Database("hotels");
2777+
} catch (CouchbaseLiteException e) {
2778+
e.printStackTrace();
2779+
}
2780+
27542781
int thisOffset = 0;
27552782
int thisLimit = 20;
27562783

27572784
Query listQuery =
27582785
QueryBuilder
27592786
.select(SelectResult.all())
27602787
.from(DataSource.database(this_Db))
2761-
.limit(Expression.intValue(thisLimit), Expression.intValue(thisOffset)); // <.>
2788+
.limit(Expression.intValue(thisLimit),
2789+
Expression.intValue(thisOffset)); // <.>
27622790

27632791
// end::query-syntax-pagination[]
27642792

0 commit comments

Comments
 (0)