Discussion:
[Libpqxx-general] string_traits<T> specializations for
Johannes Lochmann
2012-03-05 15:12:30 UTC
Permalink
Hi list,

in case anybody besides me should find this useful.

HAND

Johannes

#include <string>
#include <sstream>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>

#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

namespace pqxx {
template<> PQXX_LIBEXPORT struct string_traits<boost::uuids::uuid> {
static const char *name() {
return "boost::uuids::uuid";
}
static bool has_null() {
return false;
}
static bool is_null(const boost::uuids::uuid &) {
return false;
}
static boost::uuids::uuid null() {
internal::throw_null_conversion(name());
boost::uuids::nil_generator gen;
boost::uuids::uuid u = gen();
return u;
}
static void from_string(const char Str[], boost::uuids::uuid &Obj) {
std::stringstream ss;
ss << std::string(Str);
ss >> Obj;
}
static PGSTD::string to_string(const boost::uuids::uuid &Obj) {
std::stringstream ss;
ss << Obj;
return ss.str();
}
};

template<> PQXX_LIBEXPORT struct string_traits<boost::posix_time::ptime> {
static const char *name() {
return "boost::posix_time::ptime";
}
static bool has_null() {
return false;
}
static bool is_null(const boost::posix_time::ptime &) {
return false;
}
static boost::posix_time::ptime null() {
return boost::posix_time::ptime();
}
static void from_string(const char Str[], boost::posix_time::ptime
&Obj) {
Obj = boost::posix_time::time_from_string(std::string(Str));
}
static PGSTD::string to_string(const boost::posix_time::ptime &Obj) {
std::stringstream ss;
ss << Obj;
return ss.str();
}
};

}
Jeroen Vermeulen
2012-03-10 07:56:25 UTC
Permalink
Post by Johannes Lochmann
in case anybody besides me should find this useful.
Thanks for posting this! And my apologies for taking so long to respond
-- things have been a bit crazy lately.

It'd be nice to have this code easily available for people who use
libpqxx, although I'd also like to avoid adding a dependency on Boost to
the library itself. It may be useful to create some kind of gallery
with such add-ons. I don't know yet what form that would take, but for
now, would you mind if I put this on the wiki?


Jeroen
Johannes Lochmann
2012-03-10 10:23:01 UTC
Permalink
Post by Jeroen Vermeulen
Post by Johannes Lochmann
in case anybody besides me should find this useful.
Thanks for posting this! And my apologies for taking so long to
respond -- things have been a bit crazy lately.
You're welcome - thanks for maintaining such a useful peace of code :)
Post by Jeroen Vermeulen
It'd be nice to have this code easily available for people who use
libpqxx, although I'd also like to avoid adding a dependency on Boost
to the library itself.
Yes, i thought the same... OTOH the date/time stuff is very usefule.
uuids certainly can be handled as strings in user code. I can not think
of any situation, where user code might be interested in uuid parts.
Post by Jeroen Vermeulen
It may be useful to create some kind of gallery with such add-ons. I
don't know yet what form that would take, but for now, would you mind
if I put this on the wiki?
I would be glad if you did.

Johannes
Daniel Frey
2012-03-10 10:53:03 UTC
Permalink
It'd be nice to have this code easily available for people who use libpqxx, although I'd also like to avoid adding a dependency on Boost to the library itself. It may be useful to create some kind of gallery with such add-ons. I don't know yet what form that would take, but for now, would you mind if I put this on the wiki?
How about putting it into a header (and only a header) which is distributed as part of libpqxx but not included from the anywhere else inside libpqxx? That way the basic library doesn't depend on boost and the user just needs to include it manually before using any boost types with libpqxx.

Regards, Daniel

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.pgfoundry.org/pipermail/libpqxx-general/attachments/20120310/fb75819c/attachment.bin>
Johannes Lochmann
2012-03-10 11:22:36 UTC
Permalink
Post by Daniel Frey
It'd be nice to have this code easily available for people who use libpqxx, although I'd also like to avoid adding a dependency on Boost to the library itself. It may be useful to create some kind of gallery with such add-ons. I don't know yet what form that would take, but for now, would you mind if I put this on the wiki?
How about putting it into a header (and only a header) which is distributed as part of libpqxx but not included from the anywhere else inside libpqxx? That way the basic library doesn't depend on boost and the user just needs to include it manually before using any boost types with libpqxx.
+10

Johannes
Jeroen Vermeulen
2012-03-10 16:11:29 UTC
Permalink
Post by Daniel Frey
It'd be nice to have this code easily available for people who use libpqxx, although I'd also like to avoid adding a dependency on Boost to the library itself. It may be useful to create some kind of gallery with such add-ons. I don't know yet what form that would take, but for now, would you mind if I put this on the wiki?
How about putting it into a header (and only a header) which is distributed as part of libpqxx but not included from the anywhere else inside libpqxx? That way the basic library doesn't depend on boost and the user just needs to include it manually before using any boost types with libpqxx.
Hi Daniel - hope you're well!

Maybe I should just do that, yes... I do want the code as distributed to
be covered by the test suite, but I could detect the presence of boost
in the configure script and run tests for it where possible.

But that does mean work. :) For now, I put the conversions on the wiki
as two separate snippets (for date-time and uuid respectively).


Jeroen
Jeroen Vermeulen
2012-08-09 09:00:56 UTC
Permalink
Post by Johannes Lochmann
Hi list,
in case anybody besides me should find this useful.
[...]
Post by Johannes Lochmann
template<> PQXX_LIBEXPORT struct string_traits<boost::posix_time::ptime> {
I tried to integrate this into the library recently, because it would be
so incredibly useful. But there were massive problems with boost's
date-time class! I don't see any reasonable way to make it usable for
us, short of adding our own parsing and presentation layer. :(

One problem I ran into is that the BOOST class doesn't seem to accept or
generate the ISO-standard date/time format. The default format I got
was a little like ISO format, but what BOOST calls ISO format is worse,
not better. Its choice of default format may be influenced by the
locale, but if so, I didn't see any mention of it in the documentation.

What disturbed me more is that its parser happily accepts a date in ISO
format without a time? but rather than accepting the data and setting
the time to midnight, it interprets the input as a time, ignoring the
obvious fact that it's malformed, and tacks on a seemingly random date!
If the parser can't extract an unambiguous result from its input, it
really ought to fail rather than produce bogus data.

Another worry is that as far as I've been told, the class does not
support leap seconds. I didn't get around to writing the tests for
this, but it'd be horrible to think that there might be perfectly valid
times that you wouldn't be able to work with.

This is still the kind of complication that has made it so hard to
support date/time types in libpqxx in the past. Date/time is a very
difficult subject but I would have hoped for more mature (semi-)standard
library support for it too.


Jeroen

Loading...