Skip to content

Commit 61c39c9

Browse files
authored
DOC-8272-C4-C --N1QL Code Snippets (#533)
* DOC-8272-C4-C --N1QL Code Snippets https://issues.couchbase.com/browse/DOC-8272 * DOC-8272-C4-C --N1QL Code Snippets Feedback https://issues.couchbase.com/browse/DOC-8272
1 parent 2d836f0 commit 61c39c9

File tree

1 file changed

+46
-9
lines changed
  • modules/c/examples/code_snippets

1 file changed

+46
-9
lines changed

modules/c/examples/code_snippets/main.c

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void getting_started() {
8989
CBLDocument_Release(docAgain);
9090
FLSliceResult_Release(id);
9191

92-
// tag::query-syntax-n1ql-params[]
92+
// tag::query-syntax-n1ql[]
9393
// Create a query to fetch documents of type SDK
9494
int errorPos;
9595
CBLQuery* query = CBLDatabase_CreateQuery(database, kCBLN1QLLanguage, FLSTR("SELECT * FROM _ WHERE type = \"SDK\""), &errorPos, &err);
@@ -105,7 +105,7 @@ static void getting_started() {
105105
// Failed to run query, do error handling as above
106106
return;
107107
}
108-
// end::query-syntax-n1ql-params[]
108+
// end::query-syntax-n1ql[]
109109

110110
// TODO: Result set count?
111111
CBLResultSet_Release(result);
@@ -827,7 +827,7 @@ static void order_by() {
827827
// NOTE: No error handling, for brevity (see getting started)
828828

829829
CBLError err;
830-
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
830+
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
831831
FLSTR("SELECT meta().id, title FROM _ WHERE type = \"hotel\" ORDER BY title ASC LIMIT 10"),
832832
NULL, &err);
833833

@@ -850,7 +850,7 @@ static void test_explain_statement() {
850850
// NOTE: No error handling, for brevity (see getting started)
851851

852852
CBLError err;
853-
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
853+
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
854854
FLSTR("SELECT * FROM _ WHERE type = \"hotel\" GROUP BY country ORDER BY title ASC LIMIT 10"),
855855
NULL, &err);
856856

@@ -865,15 +865,15 @@ static void test_explain_statement() {
865865

866866
static void query_result_json() {
867867
CBLDatabase* db = kDatabase;
868-
868+
869869
CBLError err;
870870
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
871871
FLSTR("SELECT meta().id as id, name, city, type FROM _ LIMIT 10"),
872872
NULL, &err);
873873

874874
// tag::query-access-json[]
875875
// NOTE: No error handling, for brevity (see getting started)
876-
876+
877877
CBLResultSet* results = CBLQuery_Execute(query, &err);
878878
while(CBLResultSet_Next(results)) {
879879
FLDict result = CBLResultSet_ResultDict(results);
@@ -882,9 +882,9 @@ static void query_result_json() {
882882
FLSliceResult_Release(json);
883883
}
884884
CBLResultSet_Release(results);
885-
885+
886886
// end::query-access-json[]
887-
887+
888888
CBLQuery_Release(query);
889889
}
890890

@@ -931,7 +931,7 @@ static void full_text_search() {
931931
// NOTE: No error handling, for brevity (see getting started)
932932

933933
CBLError err;
934-
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
934+
CBLQuery* query = CBLDatabase_CreateQuery(db, kCBLN1QLLanguage,
935935
FLSTR("SELECT meta().id FROM _ WHERE MATCH(nameFTSIndex, \"'buy'\")"),
936936
NULL, &err);
937937

@@ -1046,6 +1046,43 @@ static void enable_basic_auth() {
10461046
// end::basic-authentication[]
10471047
}
10481048

1049+
static void docsonly_N1QL_Params() {
1050+
CBLDatabase* database = kDatabase;
1051+
1052+
// tag::query-syntax-n1ql-params[]
1053+
int errorPos;
1054+
1055+
CBLError err;
1056+
1057+
FLString n1qlstr = FLSTR("SELECT * FROM _ WHERE type = $type");
1058+
1059+
FLMutableDict n1qlparams = FLMutableDict_New();
1060+
FLMutableDict_SetString(n1qlparams, FLSTR("type"), FLSTR("hotel"));
1061+
1062+
CBLQuery* query = CBLDatabase_CreateQuery(database,
1063+
kCBLN1QLLanguage,
1064+
n1qlstr,
1065+
&errorPos,
1066+
&err);
1067+
1068+
CBLQuery_SetParameters(query, n1qlparams);
1069+
1070+
if(!query) {
1071+
/* Do appropriate error handling ...
1072+
Note that (where applicable) errorPos contains the position
1073+
in the N1QL string that the parse failed
1074+
*/
1075+
return;
1076+
}
1077+
1078+
CBLResultSet* result = CBLQuery_Execute(query, &err);
1079+
if(!result) {
1080+
// Failed to run query, do error handling ...
1081+
return;
1082+
}
1083+
// end::query-syntax-n1ql-params[]
1084+
}
1085+
10491086
int main(int argc, char** argv) {
10501087
create_new_database();
10511088
create_document();

0 commit comments

Comments
 (0)