Skip to content

Commit 3b4c159

Browse files
authored
CXXCBC-672: Add add_named_parameter and add_positional_parameter to query/analytics options (#762)
1 parent a86acb9 commit 3b4c159

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

core/meta/features.hxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@
224224
*/
225225
#define COUCHBASE_CXX_CLIENT_HAS_BUCKET_SETTINGS_NUM_VBUCKETS 1
226226

227+
/**
228+
* couchbase::query_options and couchbase::analytics_options provide the methods:
229+
* - add_positional_parameter
230+
* - add_named_parameter
231+
* - clear_positional_parameters
232+
* - clear_named_parameters
233+
*/
234+
#define COUCHBASE_CXX_CLIENT_QUERY_OPTIONS_HAVE_ADD_PARAMETER 1
235+
227236
/**
228237
* couchbase::query_options and couchbase::analytics_options allow setting both positional and named
229238
* parameters.

couchbase/analytics_options.hxx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,73 @@ struct analytics_options : public common_options<analytics_options> {
286286
return self();
287287
}
288288

289+
/**
290+
* Adds a positional parameter to the current list of positional parameters.
291+
*
292+
* @tparam Parameter type for the parameter.
293+
* @param parameter the positional parameter. It will be encoded into JSON.
294+
* @return this options builder for chaining purposes.
295+
*
296+
* @since 1.1.0
297+
* @committed
298+
*/
299+
template<typename Serializer = codec::tao_json_serializer,
300+
typename Parameter,
301+
std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
302+
auto add_positional_parameter(const Parameter& parameter) -> analytics_options&
303+
{
304+
encode_positional_parameters<Serializer>(parameter);
305+
return self();
306+
}
307+
308+
/**
309+
* Adds a named parameter to the current list of named parameters.
310+
*
311+
* @tparam Value type for the named parameter's value.
312+
* @param name the named parameter's name
313+
* @param value the named parameter's value. It will be encoded into JSON.
314+
* @return this options builder for chaining purposes.
315+
*
316+
* @since 1.1.0
317+
* @committed
318+
*/
319+
template<typename Serializer = codec::tao_json_serializer,
320+
typename Value,
321+
std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
322+
auto add_named_parameter(const std::string& name, const Value& value) -> analytics_options&
323+
{
324+
encode_named_parameters<Serializer>(std::make_pair(name, value));
325+
return self();
326+
}
327+
328+
/**
329+
* Clears the list of positional parameters.
330+
*
331+
* @return this options builder for chaining purposes.
332+
*
333+
* @since 1.1.0
334+
* @committed
335+
*/
336+
auto clear_positional_parameters() -> analytics_options&
337+
{
338+
positional_parameters_.clear();
339+
return self();
340+
}
341+
342+
/**
343+
* Clears the list of named parameters.
344+
*
345+
* @return this options builder for chaining purposes.
346+
*
347+
* @since 1.1.0
348+
* @committed
349+
*/
350+
auto clear_named_parameters() -> analytics_options&
351+
{
352+
named_parameters_.clear();
353+
return self();
354+
}
355+
289356
/**
290357
* Set map of raw options for a query.
291358
*

couchbase/query_options.hxx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,73 @@ struct query_options : public common_options<query_options> {
490490
return self();
491491
}
492492

493+
/**
494+
* Adds a positional parameter to the current list of positional parameters.
495+
*
496+
* @tparam Parameter type for the parameter.
497+
* @param parameter the positional parameter. It will be encoded into JSON.
498+
* @return this options builder for chaining purposes.
499+
*
500+
* @since 1.1.0
501+
* @committed
502+
*/
503+
template<typename Serializer = codec::tao_json_serializer,
504+
typename Parameter,
505+
std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
506+
auto add_positional_parameter(const Parameter& parameter) -> query_options&
507+
{
508+
encode_positional_parameters<Serializer>(parameter);
509+
return self();
510+
}
511+
512+
/**
513+
* Adds a named parameter to the current list of named parameters.
514+
*
515+
* @tparam Value type for the named parameter's value.
516+
* @param name the named parameter's name
517+
* @param value the named parameter's value. It will be encoded into JSON.
518+
* @return this options builder for chaining purposes.
519+
*
520+
* @since 1.1.0
521+
* @committed
522+
*/
523+
template<typename Serializer = codec::tao_json_serializer,
524+
typename Value,
525+
std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
526+
auto add_named_parameter(const std::string& name, const Value& value) -> query_options&
527+
{
528+
encode_named_parameters<Serializer>(std::make_pair(name, value));
529+
return self();
530+
}
531+
532+
/**
533+
* Clears the list of positional parameters.
534+
*
535+
* @return this options builder for chaining purposes.
536+
*
537+
* @since 1.1.0
538+
* @committed
539+
*/
540+
auto clear_positional_parameters() -> query_options&
541+
{
542+
positional_parameters_.clear();
543+
return self();
544+
}
545+
546+
/**
547+
* Clears the list of named parameters.
548+
*
549+
* @return this options builder for chaining purposes.
550+
*
551+
* @since 1.1.0
552+
* @committed
553+
*/
554+
auto clear_named_parameters() -> query_options&
555+
{
556+
named_parameters_.clear();
557+
return self();
558+
}
559+
493560
/**
494561
* Set map of raw options for a query.
495562
*

test/test_unit_query.cxx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "core/operations/document_query.hxx"
2323

24+
#include <couchbase/codec/tao_json_serializer.hxx>
25+
2426
#include <tao/json/value.hpp>
2527

2628
couchbase::core::http_context
@@ -75,3 +77,70 @@ TEST_CASE("unit: query with read from replica", "[unit]")
7577
REQUIRE_FALSE(body.get_object().count("use_replica"));
7678
}
7779
}
80+
81+
TEST_CASE("unit: Public API query options - add/clear parameters")
82+
{
83+
SECTION("positional parameters")
84+
{
85+
couchbase::query_options opts;
86+
opts.positional_parameters(10, 20);
87+
REQUIRE(opts.build().positional_parameters ==
88+
std::vector<couchbase::codec::binary>{
89+
couchbase::codec::tao_json_serializer::serialize(10),
90+
couchbase::codec::tao_json_serializer::serialize(20) });
91+
92+
opts.clear_positional_parameters();
93+
REQUIRE(opts.build().positional_parameters.empty());
94+
95+
opts.add_positional_parameter(25);
96+
REQUIRE(opts.build().positional_parameters ==
97+
std::vector<couchbase::codec::binary>{
98+
couchbase::codec::tao_json_serializer::serialize(25) });
99+
100+
opts.add_positional_parameter("foo");
101+
REQUIRE(opts.build().positional_parameters ==
102+
std::vector<couchbase::codec::binary>{
103+
couchbase::codec::tao_json_serializer::serialize(25),
104+
couchbase::codec::tao_json_serializer::serialize("foo") });
105+
106+
opts.positional_parameters(4, 5);
107+
REQUIRE(
108+
opts.build().positional_parameters ==
109+
std::vector<couchbase::codec::binary>{ couchbase::codec::tao_json_serializer::serialize(4),
110+
couchbase::codec::tao_json_serializer::serialize(5) });
111+
}
112+
113+
SECTION("named parameters")
114+
{
115+
couchbase::query_options opts;
116+
opts.named_parameters(std::make_pair("foo", 10), std::make_pair("bar", 20));
117+
REQUIRE(opts.build().named_parameters ==
118+
std::map<std::string, couchbase::codec::binary, std::less<>>{
119+
{ "foo", couchbase::codec::tao_json_serializer::serialize(10) },
120+
{ "bar", couchbase::codec::tao_json_serializer::serialize(20) },
121+
});
122+
123+
opts.clear_named_parameters();
124+
REQUIRE(opts.build().named_parameters.empty());
125+
126+
opts.add_named_parameter("foo", 25);
127+
REQUIRE(opts.build().named_parameters ==
128+
std::map<std::string, couchbase::codec::binary, std::less<>>{
129+
{ "foo", couchbase::codec::tao_json_serializer::serialize(25) },
130+
});
131+
132+
opts.add_named_parameter("bar", "baz");
133+
REQUIRE(opts.build().named_parameters ==
134+
std::map<std::string, couchbase::codec::binary, std::less<>>{
135+
{ "foo", couchbase::codec::tao_json_serializer::serialize(25) },
136+
{ "bar", couchbase::codec::tao_json_serializer::serialize("baz") },
137+
});
138+
139+
opts.named_parameters(std::make_pair("foo", 3), std::make_pair("bar", 4));
140+
REQUIRE(opts.build().named_parameters ==
141+
std::map<std::string, couchbase::codec::binary, std::less<>>{
142+
{ "foo", couchbase::codec::tao_json_serializer::serialize(3) },
143+
{ "bar", couchbase::codec::tao_json_serializer::serialize(4) },
144+
});
145+
}
146+
}

0 commit comments

Comments
 (0)