Skip to content

Commit 1596f61

Browse files
review comments
1 parent 2de8b9b commit 1596f61

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

villagesql/sdk/include/villagesql/abi/types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ typedef vef_keyring_result_t (*vef_write_keyring_fn)(const char *data_id,
282282
typedef struct {
283283
bool is_null;
284284
const char *str; // null-terminated; valid only during the row callback
285-
size_t str_len;
285+
size_t str_len; // number of bytes in str, not including the null terminator
286+
// (i.e. str_len == strlen(str)); 0 when is_null is true
286287
} vef_col_value_t;
287288

288289
// Called once before the first row with column names and count.
@@ -316,8 +317,8 @@ typedef enum {
316317
// that return no rows (INSERT, SET, etc.).
317318
// ctx: opaque pointer forwarded to meta_cb and row_cb.
318319
// error_msg: on VEF_QUERY_ERROR, a null-terminated description is written
319-
// here; must point to a buffer of at least VEF_MAX_ERROR_LEN
320-
// bytes; may be NULL if the caller does not need the message.
320+
// here; if non-NULL, must point to a buffer of at least
321+
// VEF_MAX_ERROR_LEN bytes.
321322
// Returns VEF_QUERY_OK, VEF_QUERY_ERROR, or VEF_QUERY_ABORTED.
322323
typedef vef_run_query_result_t (*vef_run_query_fn)(const char *sql,
323324
size_t sql_len,

villagesql/sdk/include/villagesql/vsql/run_query.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ using RunQueryMetaCb =
5959
std::function<void(const std::vector<std::string_view> &col_names)>;
6060

6161
// Row callback type: called once per result row with column values as strings.
62-
// SQL NULL is represented as an empty string_view (check is_null via the raw
63-
// ABI if you need to distinguish NULL from empty string).
62+
// SQL NULL is represented as a default-constructed string_view (data() ==
63+
// nullptr). An empty (non-NULL) string has data() != nullptr and size() == 0.
64+
// To distinguish NULL from empty string, check data() == nullptr.
6465
using RunQueryRowCb =
6566
std::function<void(const std::vector<std::string_view> &values)>;
6667

6768
// Execute a SQL statement. Returns VEF_QUERY_OK, VEF_QUERY_ERROR, or
68-
// VEF_QUERY_ABORTED. On error, error_msg (if non-null) receives a description.
69+
// VEF_QUERY_ABORTED. On error, error_msg (if non-null, must point to a buffer
70+
// of at least VEF_MAX_ERROR_LEN bytes) receives a description.
71+
// inline: header-only implementation to avoid ODR violations across TUs.
6972
inline vef_run_query_result_t run_query(std::string_view sql,
7073
RunQueryMetaCb meta_cb,
7174
RunQueryRowCb row_cb,

villagesql/services/run_query.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ static int cb_get_integer(void *ctx, longlong value) {
182182
return cb_get_longlong(ctx, value, 0);
183183
}
184184

185-
// Decimal/date/time types: fall back to treating their value as a string.
186-
// In CS_TEXT_REPRESENTATION these typically come via get_string instead.
185+
// Decimal/date/time types: no-op stubs required by the callback interface.
186+
// With CS_TEXT_REPRESENTATION the server converts these to strings and routes
187+
// them through cb_get_string instead, so these callbacks are never called in
188+
// practice. They are present only to satisfy the st_command_service_cbs struct.
187189
static int cb_get_decimal(void * /*ctx*/, const decimal_t * /*value*/) {
188190
return 0;
189191
}
@@ -279,7 +281,12 @@ vef_run_query_result_t run_query(const char *sql, size_t sql_len,
279281
return VEF_QUERY_ERROR;
280282
}
281283

282-
// Propagate the caller's current database into the admin session.
284+
// Propagate the caller's current database into the admin session so that
285+
// unqualified table references in the SQL resolve correctly. This is
286+
// intentional: extensions that run queries against tables in the caller's
287+
// current database would otherwise need to fully-qualify every name.
288+
// Extensions that want a clean session context should USE a specific database
289+
// explicitly in their SQL (e.g. "USE mydb; SELECT ...").
283290
if (current_thd != nullptr) {
284291
LEX_CSTRING caller_db = current_thd->db();
285292
if (caller_db.str != nullptr && caller_db.length > 0) {

villagesql/test-extensions/vsql-run-query-test/src/extension.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ void run_query_first_col(StringArg sql, StringResult out) {
7878
[&](const std::vector<std::string_view> &vals) {
7979
if (got_row) return; // only keep the first row
8080
got_row = true;
81+
// data() == nullptr means SQL NULL (empty string has data() !=
82+
// nullptr).
8183
if (vals.empty() || vals[0].data() == nullptr) {
8284
first_is_null = true;
8385
} else {
@@ -103,7 +105,7 @@ void run_query_first_col(StringArg sql, StringResult out) {
103105
}
104106

105107
VEF_GENERATE_ENTRY_POINTS(
106-
make_extension("vsql_run_query_test", "0.0.1")
108+
make_extension()
107109
.func(make_func<&run_query_count>("run_query_count")
108110
.returns(INT)
109111
.param(STRING)

0 commit comments

Comments
 (0)