From eff159c3b9a4ea250e20a413b5919efbea36be2e Mon Sep 17 00:00:00 2001
From: Dimitar Misev <misev@rasdaman.com>
Date: Fri, 21 Feb 2014 10:29:13 +0100
Subject: [PATCH] ticket:661 - fix compilation with PG < 9
---
applications/rasgeo/RasdamanHelper2.cc | 49 ++++++++++++++++++++++++++++----
applications/rasgeo/RasdamanHelper2.hh | 7 +++++
2 files changed, 51 insertions(+), 5 deletions(-)
diff --git a/applications/rasgeo/RasdamanHelper2.cc b/applications/rasgeo/RasdamanHelper2.cc
index 8ba0957..2ca2804 100644
a
|
b
|
|
45 | 45 | #include "gdal_priv.h" |
46 | 46 | #include "gdal_rat.h" |
47 | 47 | |
| 48 | #include <pg_config.h> |
| 49 | #include <postgresql/libpq-fe.h> |
| 50 | |
48 | 51 | // some string constants |
49 | 52 | #define PSPREFIX "ps" |
50 | 53 | #define crsURIprefix "%SECORE_URL%/crs/" |
… |
… |
RasdamanHelper2::writeExtraMetadata(long oid,
|
1577 | 1580 | query.str(""); |
1578 | 1581 | PQclear(res); |
1579 | 1582 | |
1580 | | char* litstr = PQescapeLiteral(const_cast<PGconn*>(petaconn), |
| 1583 | char* litstr = escapeLiteral(const_cast<PGconn*>(petaconn), |
1581 | 1584 | values[k].c_str(), values[k].size()); |
1582 | 1585 | |
1583 | 1586 | // write the name of the table into the ps_extra_metadata table |
… |
… |
RasdamanHelper2::queryImageOIDs(const std::string& kvp)
|
1738 | 1741 | query << "select distinct k0.oid from "; |
1739 | 1742 | for (int k=0; k < availkeys.size(); ++k) |
1740 | 1743 | { |
1741 | | char* key = PQescapeLiteral(conn, availkeys[k].c_str(), availkeys[k].size()); |
1742 | | char* val = PQescapeLiteral(conn, availvals[k].c_str(), availvals[k].size()); |
| 1744 | char* key = escapeLiteral(conn, availkeys[k].c_str(), availkeys[k].size()); |
| 1745 | char* val = escapeLiteral(conn, availvals[k].c_str(), availvals[k].size()); |
1743 | 1746 | |
1744 | 1747 | if (k > 0) |
1745 | 1748 | { |
… |
… |
RasdamanHelper2::writeRAT(const std::string& filename,
|
2261 | 2264 | for (c=0; c < ncols; c++) |
2262 | 2265 | { |
2263 | 2266 | std::string gdalcolname = pRAT->GetNameOfCol(c); |
2264 | | char* colname = PQescapeIdentifier(const_cast<PGconn*>(conn), |
| 2267 | char* colname = escapeIdentifier(const_cast<PGconn*>(conn), |
2265 | 2268 | gdalcolname.c_str(), gdalcolname.size()); |
2266 | 2269 | |
2267 | 2270 | coltypes.push_back(pRAT->GetTypeOfCol(c)); |
… |
… |
RasdamanHelper2::writeRAT(const std::string& filename,
|
2320 | 2323 | case GFT_String: |
2321 | 2324 | { |
2322 | 2325 | std::string valstr = pRAT->GetValueAsString(r,c); |
2323 | | char* litstr = PQescapeLiteral(const_cast<PGconn*>(conn), |
| 2326 | char* litstr = escapeLiteral(const_cast<PGconn*>(conn), |
2324 | 2327 | valstr.c_str(), valstr.size()); |
2325 | 2328 | query << litstr; |
2326 | 2329 | PQfreemem((void*)litstr); |
… |
… |
RasdamanHelper2::getDataTypeString(r_Type::r_Type_Id type)
|
3672 | 3675 | } |
3673 | 3676 | return stype; |
3674 | 3677 | } |
| 3678 | |
| 3679 | char* |
| 3680 | RasdamanHelper2::escapeLiteral(PGconn* conn, char* str, size_t strsize) throw (r_Error) |
| 3681 | { |
| 3682 | #if PG_VERSION_NUM >= 90000 |
| 3683 | return PQescapeLiteral(conn, str, strsize); |
| 3684 | #elif PG_VERSION_NUM >= 80000 |
| 3685 | char* to = (char*) malloc(strsize * 2 + 1); |
| 3686 | if (to) |
| 3687 | { |
| 3688 | int error; |
| 3689 | PQescapeStringConn(conn, to, str, strsize, &error); |
| 3690 | if (error) |
| 3691 | { |
| 3692 | std::cerr << "RasdamanHelper2::escapeLiteral - failed escaping string." << std::endl; |
| 3693 | throw r_Error(r_Error::r_Error_General); |
| 3694 | } |
| 3695 | } |
| 3696 | else |
| 3697 | { |
| 3698 | std::cerr << "RasdamanHelper2::escapeLiteral - failed allocating memory." << std::endl; |
| 3699 | throw r_Error(r_Error::r_Error_MemoryAllocation); |
| 3700 | } |
| 3701 | return to; |
| 3702 | #endif |
| 3703 | } |
| 3704 | |
| 3705 | char* |
| 3706 | RasdamanHelper2::escapeIdentifier(PGconn* conn, char* str, size_t strsize) throw (r_Error) |
| 3707 | { |
| 3708 | #if PG_VERSION_NUM >= 90000 |
| 3709 | return PQescapeIdentifier(conn, str, strsize); |
| 3710 | #elif PG_VERSION_NUM >= 80000 |
| 3711 | return escapeLiteral(conn, str, strsize); |
| 3712 | #endif |
| 3713 | } |
| 3714 | No newline at end of file |
diff --git a/applications/rasgeo/RasdamanHelper2.hh b/applications/rasgeo/RasdamanHelper2.hh
index 53cecb4..6698f34 100644
a
|
b
|
protected:
|
445 | 445 | /* just to make PG-heavy code a little bit easier to read ...*/ |
446 | 446 | bool PGDATAFAILED(std::string fun, std::string msg, PGresult* res); |
447 | 447 | bool PGFAILED(std::string fun, std::string msg, PGresult* res); |
| 448 | |
| 449 | private: |
| 450 | /// proxy to PQEscapeLiteral |
| 451 | char* escapeLiteral(PGconn* conn, char* str, size_t strsize) throw (r_Error); |
| 452 | |
| 453 | /// proxy to PQEscapeIdentifier |
| 454 | char* escapeIdentifier(PGconn* conn, char* str, size_t strsize) throw (r_Error); |
448 | 455 | |
449 | 456 | }; |
450 | 457 | |