[Tarantool-patches] [PATCH v5 49/52] sql: introduce mem_get_str0() and mem_as_str0()
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Wed Apr 14 02:06:09 MSK 2021
Thanks for the discussion!
See 2 comments below.
1. With this getter it is less trivial than with the previous
ones.
I just realized, that we call mem_as_str() on function arguments.
What if a mem is passed to multiple functions, and one of them
changes it affecting the other functions? What if the same function
is called multiple times during a query? - the first execution
affects the argument for the next executions? Does not look right.
Am I missing something?
It seems that could be fixed not so hard.
If the value is already a 0-terminated string, we return it.
If it is a blob or not terminated string, we extended it, add 0,
and return. It does not really change the original value anyway.
Mem.n stays the same, and the first n bytes stay the same.
If it is a number, we can save its string representation to
Mem.z, which is not used for numbers.
Will it work?
> diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
> index e48e8788c..5848ae729 100644
> --- a/src/box/sql/mem.h
> +++ b/src/box/sql/mem.h
> @@ -568,6 +568,28 @@ mem_get_double(const struct Mem *mem, double *d);
> int
> mem_get_bool(const struct Mem *mem, bool *b);
>
> +/**
> + * Return value for MEM of STRING type if MEM contains a NULL-terminated string.
> + * Otherwise convert value of the MEM to NULL-terminated string if possible and
> + * return converted value. Original MEM is not changed.
> + */
> +int
> +mem_get_str0(const struct Mem *mem, const char **s);
> +
> +/**
> + * Return value for MEM of STRING type if MEM contains NULL-terminated string.
> + * Otherwise convert MEM to MEM of string type that contains NULL-terminated
> + * string and return its value. Return NULL if conversion is impossible.
> + */
> +static inline const char *
> +mem_as_str0(struct Mem *mem)
> +{
> + const char *str;
> + if (mem_to_str0(mem) != 0 || mem_get_str0(mem, &str) != 0)
2. If mem_to_str0 succeeded, why can't you return mem->z right away?
> + return NULL;
> + return str;
> +}
More information about the Tarantool-patches
mailing list