Skip to content

Commit 8e4661a

Browse files
malone-at-workdbentley-vsql
authored andcommitted
sdk: Samples should use idomatic C++ (#93)
1 parent 3547391 commit 8e4661a

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

villagesql/sdk/include/villagesql/extension.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
// Create a .cc file for your extension:
3030
//
3131
// #include <villagesql/extension.h>
32+
// using namespace villagesql;
3233
//
3334
// // Implement your function
34-
// void add_impl(vef_context_t* ctx,
35-
// vef_invalue_t* a, vef_invalue_t* b,
36-
// vef_vdf_result_t* result) {
37-
// result->int_value = a->int_value + b->int_value;
38-
// result->type = VEF_RESULT_VALUE;
35+
// void add_impl(IntArg a, IntArg b, IntResult out) {
36+
// if (a.is_null() || b.is_null()) { out.set_null(); return; }
37+
// out.set(a.value() + b.value());
3938
// }
4039
//
4140
// // Register the extension with inline function definitions
@@ -79,7 +78,7 @@
7978
// out.set_length(src.size());
8079
// }
8180
//
82-
// DEFINING FUNCTIONS (deprecated, will be removed)
81+
// DEFINING FUNCTIONS
8382
// ------------------
8483
//
8584
// Functions are defined using make_func<&impl>("name") and chained builder
@@ -173,6 +172,7 @@
173172
//
174173
// #include <villagesql/extension.h>
175174
// #include <cstring>
175+
// using namespace villagesql;
176176
//
177177
// static const size_t kBytearrayLen = 8;
178178
// constexpr const char* BYTEARRAY = "bytearray";
@@ -206,17 +206,17 @@
206206
// }
207207
//
208208
// // ROT13: apply ROT13 cipher to ASCII letters in a bytearray
209-
// void rot13_impl(vef_context_t* ctx,
210-
// vef_invalue_t* input, vef_vdf_result_t* result) {
211-
// if (input->is_null) { result->type = VEF_RESULT_NULL; return; }
209+
// void rot13_impl(BinaryArg input, BinaryResult out) {
210+
// if (input.is_null()) { out.set_null(); return; }
211+
// auto src = input.value(); // villagesql::Span<const unsigned char>
212+
// auto dst = out.buffer(); // villagesql::Span<unsigned char>
212213
// for (size_t i = 0; i < kBytearrayLen; i++) {
213-
// unsigned char c = input->bin_value[i];
214+
// unsigned char c = src[i];
214215
// if (c >= 'A' && c <= 'Z') c = 'A' + ((c - 'A' + 13) % 26);
215216
// else if (c >= 'a' && c <= 'z') c = 'a' + ((c - 'a' + 13) % 26);
216-
// result->bin_buf[i] = c;
217+
// dst[i] = c;
217218
// }
218-
// result->type = VEF_RESULT_VALUE;
219-
// result->actual_len = kBytearrayLen;
219+
// out.set_length(kBytearrayLen);
220220
// }
221221
//
222222
// // Register everything inline

villagesql/sdk/include/villagesql/func_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
// StringResult / BinaryResult: write into buffer(), then call set_length().
3232
//
3333
// Example - integer add:
34-
// void add(vef_context_t*, IntArg a, IntArg b, IntResult out) {
34+
// void add(IntArg a, IntArg b, IntResult out) {
3535
// if (a.is_null() || b.is_null()) { out.set_null(); return; }
3636
// out.set(a.value() + b.value());
3737
// }
3838
//
3939
// Example - binary transform (ROT13 on a fixed-size BYTEARRAY):
40-
// void rot13(vef_context_t*, BinaryArg in, BinaryResult out) {
40+
// void rot13(BinaryArg in, BinaryResult out) {
4141
// if (in.is_null()) { out.set_null(); return; }
4242
// auto src = in.value(); // villagesql::Span<const unsigned char>
4343
// auto dst = out.buffer(); // villagesql::Span<unsigned char>

villagesql/sdk/template/src/extension.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020
// See the extension.h header for full documentation.
2121

2222
#include <villagesql/extension.h>
23+
using namespace villagesql;
2324

2425
// Example function: adds two integers
25-
// INT parameters are passed natively in the int_value field.
26-
void add_impl(vef_context_t *ctx, vef_invalue_t *a, vef_invalue_t *b,
27-
vef_vdf_result_t *result) {
28-
if (a->is_null || b->is_null) {
29-
result->type = VEF_RESULT_NULL;
26+
void add_impl(IntArg a, IntArg b, IntResult out) {
27+
if (a.is_null() || b.is_null()) {
28+
out.set_null();
3029
return;
3130
}
32-
33-
result->int_value = a->int_value + b->int_value;
34-
result->type = VEF_RESULT_VALUE;
31+
out.set(a.value() + b.value());
3532
}
3633

3734
// Register the extension

0 commit comments

Comments
 (0)