Skip to content

Commit 5ab55bf

Browse files
authored
1 parent 5020a04 commit 5ab55bf

File tree

2 files changed

+93
-32
lines changed
  • modules/android/examples

2 files changed

+93
-32
lines changed

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: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,30 +1892,35 @@ private static void init() {
18921892
public void testQuerySyntaxAll() throws CouchbaseLiteException {
18931893

18941894
// tag::query-syntax-all[]
1895-
Query listQuery = QueryBuilder.select(SelectResult.all())
1896-
.from(DataSource.database(this_Db)); // <.>
1895+
try {
1896+
this_Db = new Database("hotels");
1897+
} catch (CouchbaseLiteException e) {
1898+
e.printStackTrace();
1899+
}
1900+
1901+
Query listQuery = QueryBuilder.select(SelectResult.all())
1902+
.from(DataSource.database(this_Db)); // <.>
18971903

18981904
// end::query-syntax-all[]
18991905

19001906
// tag::query-access-all[]
19011907
try {
19021908
for (Result result : listQuery.execute().allResults()) {
1903-
int x = result.count();
1904-
// get the k-v pairs from the 'hotel' key's value into a dictionary
1905-
thisDocsProps = result.getDictionary(dbName); // <.>
1909+
// get the k-v pairs from the 'hotel' key's value into a dictionary
1910+
thisDocsProps = result.getDictionary(0)); // <.>
19061911
thisDocsId = thisDocsProps.getString("id");
19071912
thisDocsName = thisDocsProps.getString("Name");
19081913
thisDocsType = thisDocsProps.getString("Type");
19091914
thisDocsCity = thisDocsProps.getString("City");
19101915

19111916
// Alternatively, access results value dictionary directly
19121917
final Hotel hotel = new Hotel();
1913-
hotel.Id = result.getDictionary(dbName).getString("id"); // <.>
1914-
hotel.Type = result.getDictionary(dbName).getString("Type");
1915-
hotel.Name = result.getDictionary(dbName).getString("Name");
1916-
hotel.City = result.getDictionary(dbName).getString("City");
1917-
hotel.Country= result.getDictionary(dbName).getString("Country");
1918-
hotel.Description = result.getDictionary(dbName).getString("Description");
1918+
hotel.Id = result.getDictionary(0).getString("id"); // <.>
1919+
hotel.Type = result.getDictionary(0).getString("Type");
1920+
hotel.Name = result.getDictionary(0).getString("Name");
1921+
hotel.City = result.getDictionary(0).getString("City");
1922+
hotel.Country= result.getDictionary(0).getString("Country");
1923+
hotel.Description = result.getDictionary(0).getString("Description");
19191924
hotels.put(hotel.Id, hotel);
19201925

19211926
}
@@ -1929,6 +1934,12 @@ public void testQuerySyntaxAll() throws CouchbaseLiteException {
19291934
public void testQuerySyntaxProps() throws CouchbaseLiteException {
19301935

19311936
// tag::query-syntax-props[]
1937+
try {
1938+
this_Db = new Database("hotels");
1939+
} catch (CouchbaseLiteException e) {
1940+
e.printStackTrace();
1941+
}
1942+
19321943
Query listQuery =
19331944
QueryBuilder.select(SelectResult.expression(Meta.id),
19341945
SelectResult.property("name"),
@@ -1975,13 +1986,18 @@ public void testQuerySyntaxProps() throws CouchbaseLiteException {
19751986

19761987

19771988
public void testQuerySyntaxCount() throws CouchbaseLiteException {
1989+
try {
1990+
this_Db = new Database("hotels");
1991+
} catch (CouchbaseLiteException e) {
1992+
e.printStackTrace();
1993+
}
19781994

1979-
// tag::query-syntax-count-only[]
1980-
Query listQuery = QueryBuilder.select(
1981-
SelectResult.expression(Function.count(Expression.string("*"))).as("mycount")) // <.>
1982-
.from(DataSource.database(this_Db));
1995+
// tag::query-syntax-count-only[]
1996+
Query listQuery = QueryBuilder.select(
1997+
SelectResult.expression(Function.count(Expression.string("*"))).as("mycount")) // <.>
1998+
.from(DataSource.database(this_Db));
19831999

1984-
// end::query-syntax-count-only[]
2000+
// end::query-syntax-count-only[]
19852001

19862002

19872003
// tag::query-access-count-only[]
@@ -1994,7 +2010,7 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
19942010
// Alternatively, use the index
19952011
Integer orDocId = result.getInt(0);
19962012
}
1997-
// Or even
2013+
// Or even miss out the for-loop altogether
19982014
Integer resultCount = listQuery.execute().next().getInt("mycount");
19992015

20002016
} catch (CouchbaseLiteException e) {
@@ -2005,12 +2021,18 @@ public void testQuerySyntaxCount() throws CouchbaseLiteException {
20052021

20062022

20072023
public void testQuerySyntaxId() throws CouchbaseLiteException {
2008-
// tag::query-syntax-id[]
2009-
Query listQuery =
2010-
QueryBuilder.select(SelectResult.expression(Meta.id).as("metaID"))
2011-
.from(DataSource.database(this_Db));
2024+
// tag::query-syntax-id[]
2025+
try {
2026+
this_Db = new Database("hotels");
2027+
} catch (CouchbaseLiteException e) {
2028+
e.printStackTrace();
2029+
}
2030+
2031+
Query listQuery =
2032+
QueryBuilder.select(SelectResult.expression(Meta.id).as("metaID"))
2033+
.from(DataSource.database(this_Db));
20122034

2013-
// end::query-syntax-id[]
2035+
// end::query-syntax-id[]
20142036

20152037

20162038
// tag::query-access-id[]
@@ -2042,14 +2064,21 @@ public void testQueryPagination() throws CouchbaseLiteException {
20422064

20432065

20442066
// tag::query-syntax-pagination[]
2067+
try {
2068+
this_Db = new Database("hotels");
2069+
} catch (CouchbaseLiteException e) {
2070+
e.printStackTrace();
2071+
}
2072+
20452073
int thisOffset = 0;
20462074
int thisLimit = 20;
20472075

20482076
Query listQuery =
20492077
QueryBuilder
20502078
.select(SelectResult.all())
20512079
.from(DataSource.database(this_Db))
2052-
.limit(Expression.intValue(thisLimit), Expression.intValue(thisOffset)); // <.>
2080+
.limit(Expression.intValue(thisLimit),
2081+
Expression.intValue(thisOffset)); // <.>
20532082

20542083
// end::query-syntax-pagination[]
20552084

0 commit comments

Comments
 (0)