From 13d32335e020257da465f376337a37cbc762252d Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 23 Feb 2008 01:25:52 +0000 Subject: [PATCH] Check for null values in the PostGIS results and don't add attributes with null values to the feature rather than adding a value based on decoding a buffer full of undefined data. --- plugins/input/postgis/postgisfs.cpp | 90 +++++++++++++++-------------- plugins/input/postgis/resultset.hpp | 5 ++ 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/plugins/input/postgis/postgisfs.cpp b/plugins/input/postgis/postgisfs.cpp index d6a88db7e..f7eaff40c 100644 --- a/plugins/input/postgis/postgisfs.cpp +++ b/plugins/input/postgis/postgisfs.cpp @@ -105,57 +105,61 @@ feature_ptr postgis_featureset::next() for (unsigned pos=1;posgetFieldName(pos); - const char* buf=rs_->getValue(pos); - int oid = rs_->getTypeOID(pos); + + if (!rs_->isNull(pos)) + { + const char* buf=rs_->getValue(pos); + int oid = rs_->getTypeOID(pos); - if (oid==23) //int4 - { - int val = int4net(buf); - boost::put(*feature,name,val); - } - else if (oid==21) //int2 - { - int val = int2net(buf); - boost::put(*feature,name,val); - } - else if (oid == 700) // float4 - { - float val; - float4net(val,buf); - boost::put(*feature,name,val); - } - else if (oid == 701) // float8 - { - double val; - float8net(val,buf); - boost::put(*feature,name,val); - } - else if (oid==25 || oid==1042 || oid==1043) // text or bpchar or varchar - { - //std::string str(buf); - //trim(str); - //std::wstring wstr = tr_->transcode(str); - UnicodeString ustr = tr_->transcode(buf); - boost::put(*feature,name,ustr); - } - else if (oid == 1700) // numeric - { - std::string str = numeric2string(buf); - try + if (oid==23) //int4 { - double val = boost::lexical_cast(str); + int val = int4net(buf); boost::put(*feature,name,val); } - catch (boost::bad_lexical_cast & ex) + else if (oid==21) //int2 { - std::clog << ex.what() << "\n"; + int val = int2net(buf); + boost::put(*feature,name,val); } - } - else - { + else if (oid == 700) // float4 + { + float val; + float4net(val,buf); + boost::put(*feature,name,val); + } + else if (oid == 701) // float8 + { + double val; + float8net(val,buf); + boost::put(*feature,name,val); + } + else if (oid==25 || oid==1042 || oid==1043) // text or bpchar or varchar + { + //std::string str(buf); + //trim(str); + //std::wstring wstr = tr_->transcode(str); + UnicodeString ustr = tr_->transcode(buf); + boost::put(*feature,name,ustr); + } + else if (oid == 1700) // numeric + { + std::string str = numeric2string(buf); + try + { + double val = boost::lexical_cast(str); + boost::put(*feature,name,val); + } + catch (boost::bad_lexical_cast & ex) + { + std::clog << ex.what() << "\n"; + } + } + else + { #ifdef MAPNIK_DEBUG - std::clog << "uknown OID = " << oid << " FIXME \n"; + std::clog << "uknown OID = " << oid << " FIXME \n"; #endif + } } } ++count_; diff --git a/plugins/input/postgis/resultset.hpp b/plugins/input/postgis/resultset.hpp index e16dd959a..9d3e783c8 100644 --- a/plugins/input/postgis/resultset.hpp +++ b/plugins/input/postgis/resultset.hpp @@ -121,6 +121,11 @@ public: return PQftype(res_,col); return 0; } + + bool isNull(int index) const + { + return PQgetisnull(res_,pos_,index); + } const char* getValue(int index) const {