Hi,

I see an old thread from back in November about possible solutions to using
NULLs in prepared statements.  Was this ever implemented in any form?

Offhand the only good solution I can think of is an optional two-argument
calling syntax, so that:

trans.prepared("saveTargetStmt")(someValue);

...would just replace $1 with someValue (current implementation), and:

trans.prepared("saveTargetStmt")(someValue, treatAsNULL);

...would replace $1 with someValue UNLESS someValue == treatAsNULL, and then
it would insert a NULL.  Impetus on the user to implement operator== on the
type being passed in if they want it to work on custom/non-scalar types
(which probable won't work anyway, so moot point).

In my situation, I'm working with foreign keys, so I really need to treat 0 as
a genuine database NULL or I'll get constraint violations.  I don't want to
use a trigger to convert the data type (but that's what I'm doing for now).
It would also do the right thing with NULL char *, which is generally what you
want if you need to differentiate between "" and NULL strings.

As a somewhat wonky use case you could also do a literal:

trans.prepared("saveTargetStmt")(0, 0)

...or whatever type is appropriate for the prepared argument, and force a NULL
if there's no operator== for the type you're using.  This last example would
require conditional code, but would at least allow for both re-use of a single
prepared statement and handling types without operator==.

Not sure how hard it would be to duplicate all the possible prepared methods
to implement this (I haven't looked at the source for handling prepared
statements yet), but the syntax would be clean and backwards-compatible if
both single- and double-argument methods were supported.

I like this idea and would be willing to implement it myself and provide a
patch, if there's a consensus.  If there's another solution out there already,
please let me know.

Thanks,
Jim

_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@gborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to