Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null values #53

Open
llaine opened this issue Jul 19, 2016 · 8 comments
Open

Null values #53

llaine opened this issue Jul 19, 2016 · 8 comments

Comments

@llaine
Copy link

llaine commented Jul 19, 2016

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?

@pszturmaj
Copy link
Owner

pszturmaj commented Jul 19, 2016

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

@llaine
Copy link
Author

llaine commented Jul 19, 2016

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!

@jacob-carlborg
Copy link
Collaborator

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?

@pszturmaj
Copy link
Owner

pszturmaj commented Jul 19, 2016

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.

@jacob-carlborg
Copy link
Collaborator

jacob-carlborg commented Jul 20, 2016

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 faec7e4 😃. 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.

@llaine
Copy link
Author

llaine commented Jul 21, 2016

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

@bubnenkoff
Copy link

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"

@jacob-carlborg
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants