/home/martin/workspace/OpenStreetNav/src/psql/PqTypeWrap.h
Go to the documentation of this file.
00001 /*
00002  * PqTypeWrap.h
00003  *
00004  *  Created on: Nov 22, 2011
00005  *      Author: martin
00006  */
00007 
00008 #ifndef PQTYPEWRAP_H_
00009 #define PQTYPEWRAP_H_
00010 
00011 #include <libpqtypes.h>
00012 #include <stdint.h>
00013 #include <string>
00014 #include <vector>
00015 
00016 namespace psql
00017 {
00018 
00019 void zero_get_check(int);
00020 void zero_put_check(int);
00021 
00022 template<typename T>
00023 class PqTypeWrap
00024 {
00025 };
00026 
00027 template<>
00028 class PqTypeWrap<double>
00029 {
00030 public:
00031     double get(PGresult* res, int row, int col)
00032     {
00033         PGfloat8 d;
00034         zero_get_check(PQgetf(res, row, "%float8", col, &d));
00035         return d;
00036     }
00037 
00038     void put(PGparam* param, double val)
00039     {
00040         zero_put_check(PQputf(param, "%float8", val));
00041     }
00042 };
00043 
00044 template<>
00045 class PqTypeWrap<int>
00046 {
00047 public:
00048     int get(PGresult* res, int row, int col)
00049     {
00050         PGint4 i;
00051         zero_get_check(PQgetf(res, row, "%int4", col, &i));
00052         return i;
00053     }
00054     void put(PGparam* param, int val)
00055     {
00056         zero_put_check(PQputf(param, "%int4", val));
00057     }
00058 };
00059 
00060 template<>
00061 class PqTypeWrap<int64_t>
00062 {
00063 public:
00064     int64_t get(PGresult* res, int row, int col)
00065     {
00066         PGint8 i;
00067         zero_get_check(PQgetf(res, row, "%int8", col, &i));
00068         return i;
00069     }
00070     void put(PGparam* param, int64_t val)
00071     {
00072         zero_put_check(PQputf(param, "%int8", val));
00073     }
00074 };
00075 
00076 template<>
00077 class PqTypeWrap<std::vector<char> >
00078 {
00079 public:
00080     std::vector<char> get(PGresult* res, int row, int col)
00081     {
00082         PGbytea ret;
00083         zero_get_check(PQgetf(res, row, "%bytea", col, &ret));
00084         std::vector<char> v(ret.len);
00085         for (int i = 0; i < ret.len; ++i)
00086         {
00087             v[i] = ret.data[i];
00088         }
00089         return v;
00090     }
00091     void put(PGparam* param, std::vector<char>& vect)
00092     {
00093         PGbytea bytea;
00094         bytea.len = vect.size();
00095         bytea.data = &vect[0];
00096         zero_put_check(PQputf(param, "%bytea", &bytea));
00097     }
00098 };
00099 
00100 template<>
00101 class PqTypeWrap<std::string>
00102 {
00103 public:
00104     std::string get(PGresult* res, int row, int col)
00105     {
00106         PGtext txt;
00107         zero_get_check(PQgetf(res, row, "%text", col, &txt));
00108         return std::string(txt);
00109     }
00110     void put(PGparam* param, std::string& str)
00111     {
00112         zero_put_check(PQputf(param, "%text", str.c_str()));
00113     }
00114 };
00115 
00116 } /* namespace display */
00117 #endif /* PQTYPEWRAP_H_ */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines