ddb
ddb copied to clipboard
Null values
Let's imagine that I have this struct
struct V {
int x;
int y;
}
// Query
auto cmd = new PGCommand(conn, "select x, y from vectors");
auto result = cmd.executeQuery!(V)();
V[] r = result.map!(a => cast(V) a).array;
Let's imagine that my dataset returns something like | 1 | 2 | | null | 3 |
I will always have an error saying that it can't handle null values.
How can I fix this?
If you need to handle nulls then you have to make fields nullable, in this case:
import std.typecons;
struct V {
Nullable!int x;
int y;
}
Thanks for the answer. I have this weird error
Error: std.typecons.Nullable(T) at /home/llaine/dlang/dmd-2.071.0/linux/bin64/../../src/phobos/std/typecons.d(1933,1) conflicts with ddb.db.Nullable(T) if (!__traits(compiles, ()
And if I remove the import std.typecons;
I have this one don't know if it's related or not
/home/llaine/dlang/dmd-2.071.0/linux/bin64/../../src/phobos/std/variant.d(580,9): Error: static assert "Cannot store a typeof(null) in a VariantN!(8LU, int, void*). Valid types are (int, void*)"
I'm really newbie to D so it might looks dumb questions!
The way ddb.db.Nullable
is implement doesn't work with Variant
. Use the one in std.typecons
instead. When you get a conflict use the fully qualified name or an alias. @pszturmaj can we remove ddb.db.Nullable
or is there a reason it exists?
This code was written in 2011 and AFAIR there was a problem with storing null in Variant/Algebraic. We need to upgrade this to newer D language version. I can't do it because I'm busy with other things, sorry. We need a volunteer for this.
@jacob-carlborg yes we can remove it, but other code should be adjusted accordingly.
This code was written in 2011 and AFAIR there was a problem with storing null in Variant/Algebraic. We need to upgrade this to newer D language version. I can't do it because I'm busy with other things, sorry. We need a volunteer for this.
I've already been using ddb with the latest DMD, at that point, perhaps one or two versions old now.
yes we can remove it, but other code should be adjusted accordingly.
I've already done that https://github.com/pszturmaj/ddb/commit/faec7e464ecce5c6ea647684ff110854b8834339 😃. I don't think that ddb uses nullable directly. I left the nullable declared in ddb for backwards compatibility, but if it doesn't work anyway there's not much point in keeping it.
Using the full name solves the compilation errors, thanks for replying !
I can't get it work. I need to handle situation if answer is null.
struct Q1
{
Nullable!string place_id;
Nullable!string parent_place_id;
}
Q1 row1;
auto cmd = new PGCommand(connection, s1);
row1 = cmd.executeRow!Q1;
But this code is failing with error. "Result doesn't contain any rows"
@bubnenkoff try executeQuery
instead of executeRow
, does that make any difference?