ddb icon indicating copy to clipboard operation
ddb copied to clipboard

Null values

Open llaine opened this issue 8 years ago • 8 comments

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?

llaine avatar Jul 19 '16 09:07 llaine

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;
}

pszturmaj avatar Jul 19 '16 10:07 pszturmaj

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!

llaine avatar Jul 19 '16 13:07 llaine

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?

jacob-carlborg avatar Jul 19 '16 18:07 jacob-carlborg

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.

pszturmaj avatar Jul 19 '16 20:07 pszturmaj

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.

jacob-carlborg avatar Jul 20 '16 19:07 jacob-carlborg

Using the full name solves the compilation errors, thanks for replying !

llaine avatar Jul 21 '16 07:07 llaine

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 avatar Apr 07 '17 09:04 bubnenkoff

@bubnenkoff try executeQuery instead of executeRow, does that make any difference?

jacob-carlborg avatar Apr 07 '17 15:04 jacob-carlborg