00001 #ifndef __DBCONN_H_
00002 #define __DBCONN_H_
00003
00004 #include <libpq-fe.h>
00005 #include <libplayercore/playercore.h>
00006 #include <libplayercore/error.h>
00007 #include <vector>
00008 #include <string>
00009 #include <cstring>
00010 #include <cstddef>
00011
00012 #define MAX_PSQL_STRING 256
00013
00014 using namespace std;
00015
00016 typedef struct
00017 {
00018 double x0, y0, x1, y1;
00019 }BoundingBox;
00020
00021 class FeatureDataHolder
00022 {
00023 public:
00024 FeatureDataHolder() { memset(&feature_data, 0, sizeof feature_data); }
00025 FeatureDataHolder(const FeatureDataHolder & orig)
00026 {
00027 memset(&feature_data, 0, sizeof feature_data);
00028 name = orig.name;
00029 wkb = orig.wkb;
00030 attrib = orig.attrib;
00031 }
00032 virtual ~FeatureDataHolder();
00033 FeatureDataHolder(string name)
00034 {
00035 memset(&feature_data, 0, sizeof feature_data);
00036 this->name = name;
00037 }
00038 FeatureDataHolder(const player_vectormap_feature_data_t * feature)
00039 {
00040 memset(&feature_data, 0, sizeof feature_data);
00041 name = string(feature->name);
00042 attrib = string(feature->attrib);
00043 wkb.assign(feature->wkb, (feature->wkb) + (feature->wkb_count));
00044 }
00045
00046 const player_vectormap_feature_data_t* Convert();
00047
00048 string name;
00049 vector<uint8_t> wkb;
00050 string attrib;
00051 player_vectormap_feature_data_t feature_data;
00052 };
00053
00054 class LayerInfoHolder
00055 {
00056 public:
00057 LayerInfoHolder() { memset(&layer_info,0,sizeof(layer_info)); memset(&extent, 0, sizeof(extent)); };
00058 LayerInfoHolder(const LayerInfoHolder & orig)
00059 {
00060 memset(&layer_info,0,sizeof(layer_info));
00061 name = orig.name;
00062 extent = orig.extent;
00063 }
00064 LayerInfoHolder(string name)
00065 {
00066 memset(&layer_info,0,sizeof(layer_info));
00067 this->name = name;
00068 memset(&extent, 0, sizeof(extent));
00069 };
00070 virtual ~LayerInfoHolder()
00071 {
00072 free(layer_info.name);
00073 }
00074
00075 const player_vectormap_layer_info_t* Convert();
00076
00077 string name;
00078 BoundingBox extent;
00079 player_vectormap_layer_info_t layer_info;
00080 };
00081
00082 class LayerDataHolder
00083 {
00084 public:
00085 LayerDataHolder() { memset(&layer_data, 0, sizeof layer_data); }
00086 LayerDataHolder(const LayerDataHolder & orig)
00087 {
00088 memset(&layer_data, 0, sizeof layer_data);
00089 name = orig.name;
00090 features = orig.features;
00091 }
00092 LayerDataHolder(string name)
00093 {
00094 memset(&layer_data,0,sizeof(layer_data));
00095 this->name = name;
00096 }
00097 LayerDataHolder(const player_vectormap_layer_data_t * layer)
00098 {
00099 memset(&layer_data, 0, sizeof layer_data);
00100 name = string(layer->name);
00101 for (uint32_t ii = 0; ii < layer->features_count; ii++)
00102 {
00103 FeatureDataHolder fd(&(layer->features[ii]));
00104 features.push_back(fd);
00105 }
00106 }
00107 virtual ~LayerDataHolder();
00108
00109 const player_vectormap_layer_data_t* Convert();
00110
00111 vector<FeatureDataHolder> features;
00112 player_vectormap_layer_data_t layer_data;
00113 string name;
00114 };
00115
00116 class VectorMapInfoHolder
00117 {
00118 public:
00119 VectorMapInfoHolder() { memset(&info, 0, sizeof info); memset(&extent, 0, sizeof extent); };
00120 VectorMapInfoHolder(const VectorMapInfoHolder & orig)
00121 {
00122 memset(&info, 0, sizeof info);
00123 srid = orig.srid; layers = orig.layers; extent = orig.extent;
00124 }
00125 virtual ~VectorMapInfoHolder();
00126 VectorMapInfoHolder(uint32_t srid, BoundingBox extent)
00127 {
00128 this->srid = srid; this->extent = extent;
00129 memset(&info, 0, sizeof info);
00130 };
00131
00132 const player_vectormap_info_t* Convert();
00133
00134 uint32_t srid;
00135 vector<LayerInfoHolder> layers;
00136 BoundingBox extent;
00137 player_vectormap_info_t info;
00138 };
00139
00140 class PostgresConn
00141 {
00142 public:
00143 PostgresConn(){ conn = NULL; };
00144 virtual ~PostgresConn(){ if (Connected()) Disconnect(); };
00145 bool Connect(const char* dbname, const char* host, const char* user, const char* password, const char* port);
00146 bool Disconnect();
00147 bool Connected() { return (conn != NULL) && (PQstatus(conn) != CONNECTION_BAD); };
00148
00149 VectorMapInfoHolder GetVectorMapInfo(vector<string> layerNames);
00150 LayerInfoHolder GetLayerInfo(const char *layer_name);
00151 LayerDataHolder GetLayerData(const char *layer_name);
00152 int WriteLayerData(LayerDataHolder & data);
00153
00154 private:
00155 BoundingBox BinaryToBBox(const uint8_t *binary, uint32_t length);
00156 uint32_t Text2Bin(const char * text, unsigned char * bin, uint32_t maxlen);
00157 PGconn *conn;
00158
00159 };
00160
00161 #endif