TerraLib 4.1
E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/drivers/PostgreSQL/TePostgreSQL.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00028 /*
00029  * TODO: 01. Implementar o alterTable que nao pode simplesmente alterar o tipo da coluna
00030  *                     02. No metodo query do portal, determinar os campos que fazem parte da chave primaria
00031  *
00032  */
00033 
00034 #ifndef  __TERRALIB_INTERNAL_POSTGRESQL_H
00035 #define  __TERRALIB_INTERNAL_POSTGRESQL_H
00036 
00037 #include "../../kernel/TeDatabase.h"
00038 #include "../../kernel/TeDatabaseFactory.h"
00039 #include "TePostgreSQLDefines.h"
00040 
00041 class TePostgreSQLPortal;
00042 class TePGConnection;
00043 class TePGRecordset;
00044 
00045 struct pg_conn;
00046 
00048 
00057 class TLPOSTGRESQL_DLL TePostgreSQL : public TeDatabase
00058 {
00059            friend class TePostgreSQLPortal;
00060 
00061            public:
00062                       
00064                       TePostgreSQL();
00065 
00067                       virtual ~TePostgreSQL();
00068 
00073 
00075         virtual void setConnection(TeConnection* c);
00076 
00080         virtual TeConnection* getConnection();
00081 
00083 
00085                       string errorMessage();
00086 
00088                       virtual bool newDatabase(const string& database, const string& user, const string& password, const string& host, const int& port = -1, bool terralibModel=true, const std::string& characterSet = "");
00089 
00091                       virtual bool connect(const string& host, const string& user, const string& password, const string& database, int port = -1);
00092 
00093                       virtual bool connect(pg_conn* conn);
00094 
00096                       virtual bool showDatabases(const string& host, const string& user, const string& password, vector<string>& dbNames, int port = -1);
00097 
00099                       void close(void);
00100 
00101                       bool listTables(vector<string>& tableList);
00102 
00104                       virtual bool listDatabases(std::vector<std::string> &databaseList);
00105 
00107                       virtual bool dropDatabase(const std::string &databaseName);
00108 
00110                       bool tableExist(const string& table);
00111 
00113                       virtual bool deleteTable (const string& table);
00114 
00116                bool columnExist(const string& table, const string& column, TeAttribute& attr);
00117 
00119                       virtual bool createTable(const string& table, TeAttributeList &attr);
00120 
00122                       virtual bool addColumn(const string& table, TeAttributeRep &rep);
00123 
00125                       bool createRelation(const string& relName, const string& table, const string& fieldName, const string& relatedTable, const string& relatedField, bool cascadeDeletion);
00126 
00128                       TeDBRelationType existRelation(const string& tableName, const string& relName);
00129 
00131                       bool execute(const string &sql);
00132 
00134                       virtual TeDatabasePortal* getPortal();
00135 
00137                       bool insertTable(TeTable &table);
00138 
00140                       bool updateTable      (TeTable &table);
00141                       
00143                       bool insertRelationInfo(const int tableId, const string& tField, const string& rTable, const string& rField, int& relId);
00144 
00146                bool insertTableInfo(int layerId, TeTable& table, const string& user = "");
00147 
00149                       bool alterTable(const string& tableName, TeAttributeRep& rep, const string& oldColName="");
00150 
00152                       bool alterTable(const string& oldTableName, const string& newTableName);
00153 
00155                       bool insertBlob (const string& tableName, const string& columnBlob, const string& whereClause, unsigned char* data, int size);
00156                       
00158                       bool insertProjection(TeProjection *proj);
00159 
00161                       bool insertLayer(TeLayer *layer);
00162 
00164                       bool insertProject(TeProject *proj);
00165 
00167                       bool insertRepresentation(int layerId, TeRepresentation& rep);
00168 
00170                       bool insertView(TeView *view);
00171 
00173                       bool insertViewTree(TeViewTree *tree);
00174 
00176                       bool insertThemeGroup(TeViewTree* tree);
00177 
00179                       bool insertTheme(TeAbstractTheme *theme);
00180 
00182                       bool insertThemeTable(int themeId, int tableId, int relationId, int tableOrder);
00183 
00185                       virtual bool generateLabelPositions(TeTheme *theme, const std::string& objectId = "");
00186 
00188                       bool insertLegend(TeLegendEntry *legend);              
00189 
00191                       virtual bool loadPolygonSet(const string& table, TeBox& box, TePolygonSet& ps);
00192 
00194                       virtual TeDatabasePortal* loadPolygonSet(const string& table, TeBox& box);
00195 
00197                virtual bool locatePolygon(const string& table, TeCoord2D& pt, TePolygon& polygon, const double& tol = 0.0);                    
00198 
00200                       virtual bool locatePolygonSet  (const string& table, TeCoord2D &pt, double tol, TePolygonSet &polygons);
00201 
00203                       virtual bool insertPolygon(const string& table, TePolygon& p);
00204 
00206                       virtual bool updatePolygon(const string& table, TePolygon& p);
00207 
00208                       virtual bool loadLineSet(const string& table, TeBox& box, TeLineSet& linSet);
00209 
00210                       virtual TeDatabasePortal* loadLineSet(const string& table, TeBox& box);
00211 
00213                       virtual bool insertLine(const string& table, TeLine2D& l);
00214 
00216                       virtual bool updateLine(const string& table, TeLine2D& l);
00217 
00219                       virtual bool locateLine(const string& table, TeCoord2D& pt, TeLine2D& line, const double& tol = 0.0);
00220 
00222                       virtual bool insertPoint(const string& table, TePoint& p);
00223 
00224                       virtual bool updatePoint(const string& table, TePoint& p);
00225 
00227                       virtual bool locatePoint(const string& table, TeCoord2D& pt, TePoint& point, const double& tol = 0.0);
00228 
00230                       bool insertText(const string& table, TeText& t);
00231 
00233                       bool insertArc(const string& table,TeArc& arc);
00234 
00236                       virtual bool insertNode(const string& table, TeNode& node);
00237                       virtual bool updateNode(const string& table, TeNode &node);
00238 
00240                       virtual bool insertCell(const string& table, TeCell& c);
00241                       virtual bool updateCell(const string& table, TeCell &c);
00242 
00243                       // Locates a cell
00244                       virtual bool locateCell(const string& table, TeCoord2D& pt, TeCell& c, const double& tol = 0.0);
00245 
00247                       virtual bool insertRasterBlock(const string& table, const string& blockId, const TeCoord2D& ll, const TeCoord2D& ur, unsigned char* buf,unsigned long size, int band = 0, unsigned int res = 1, unsigned int subband = 0);
00248 
00250                       string getSQLStatistics(TeGroupingAttr& attrs);
00251 
00253                       string getSQLTemporalWhere(TeTimeInterval& timeInterval, TeTemporalRelation timeOperator, const string& initialTime, const string& finalTime);
00254 
00256                       string getSQLTemporalWhere(int time1, int time2, TeChronon chr, TeTemporalRelation rel, const string& initialTime, const string& finalTime);
00257 
00259                       string getAutoNumberSQL(const string& table);
00260 
00262                       string getSQLTime(const TeTime& t) const;
00263 
00265                string concatValues(vector<string>& values, const string& unionString);
00266 
00268                string toUpper(const string& value);
00269 
00271         string leftString(const string& name, const int& length);
00272 
00274                       virtual bool createSpatialIndex(const string& table, const string& column, TeSpatialIndexType type = TeRTREE, short level = 0, short tile = 0);
00275 
00277                       virtual string getSQLBoxWhere(const TeBox& box, const TeGeomRep rep, const std::string& tableName);
00278 
00280                       virtual string getSQLBoxWhere(const string& table1, const string& table2, TeGeomRep rep2, TeGeomRep rep1 = TePOLYGONS);
00281 
00283                       virtual string getSQLBoxSelect(const string& tableName, TeGeomRep rep);
00284 
00286                       virtual bool getMBRGeom(string tableGeom, string object_id, TeBox& box, string colGeom);
00287 
00289                       virtual bool getMBRSelectedObjects(string geomTable, string colGeom, string fromClause, string whereClause, string afterWhereClause, TeGeomRep repType, TeBox& bout, const double& tol = 0.0);
00290 
00292                       virtual string getSpatialIdxColumn(TeGeomRep rep);
00293 
00295                       bool beginTransaction(); 
00296 
00298                       bool commitTransaction();
00299 
00301                       bool rollbackTransaction();
00302 
00304                       virtual std::string getClientEncoding();
00305 
00307                       virtual bool setClientEncoding(const std::string& characterSet);
00308 
00310                       virtual bool getEncodingList(std::vector<std::string>& vecEncodingList);
00311                        
00313                       virtual bool getEncodingList(const std::string& host, const std::string& user, const std::string& password, const int& port, std::vector<std::string>& vecEncodingList);
00314 
00316                       virtual bool getIndexesFromTable(const string& tableName, std::vector<TeDatabaseIndex>& vecIndexes);
00317 
00319                       virtual bool getAttributeList(const string& tableName,TeAttributeList& attList);
00320 
00321            protected:
00322 
00324                       virtual void getInsertRasterBlock(const string& table, const string& blockId, const TeCoord2D& ll, const TeCoord2D& ur, const int& band, const unsigned int& res, const unsigned int& subband, char* buf, const unsigned long& size, string& sql);
00325 
00327                       virtual void getUpdateRasterBlock(const string& table, const string& blockId, const TeCoord2D& ll, const TeCoord2D& ur, const int& band, const unsigned int& res, const unsigned int& subband, char* buf, const unsigned long& size, string& sql);
00328                       
00330                       string escapeSequence(const string& from);
00331                       
00332 
00334                       bool realConnect(const string& host, const string& user, const string& password, const string& database, int port = -1);
00335 
00337                       TePGConnection* tepg_connection_;
00338         TePGConnection* tepg_connection_ref;
00339 
00341                 std::string gistBoxOps_;
00342 
00344                       int transactionCounter_;
00345 
00346 };
00347 
00348 
00350 
00353 class TLPOSTGRESQL_DLL TePostgreSQLPortal : public TeDatabasePortal
00354 {
00355            public:
00356 
00358                       TePostgreSQLPortal();
00359 
00361                       TePostgreSQLPortal(TeDatabase *pDatabase);
00362                       
00364                       virtual ~TePostgreSQLPortal();
00365 
00367                       virtual bool query(const string &qry, TeCursorLocation l = TeSERVERSIDE, TeCursorType t = TeUNIDIRECTIONAL, TeCursorEditType e = TeREADONLY, TeCursorDataType dt = TeTEXTCURSOR);
00368 
00370                       bool fetchRow();
00371 
00373                       bool fetchRow(int i);
00374 
00376                       void freeResult();
00377 
00379                       string errorMessage();
00380 
00382                       char* getData(int i);
00383 
00385                       char* getData(const string& s);
00386 
00388                       double getDouble(int i);
00389 
00391                       double getDouble(const string& s);
00392 
00394                       int        getInt(int i);
00395 
00397                       int        getInt(const string& s);
00398 
00400                       bool getBool(const string& s);
00401 
00403                       bool getBool(int i);
00404 
00406                TeTime getDate(int i);
00407 
00409                TeTime getDate(const string& s);
00410 
00412                       string getDateAsString(int i);
00413 
00415                       string getDateAsString(const string& s);
00416 
00418                       bool getBlob(const string& s, unsigned char*& data, long& size);
00419 
00420                       virtual bool getRasterBlock(unsigned long& size, unsigned char* ptData);
00421 
00422                       virtual bool fetchGeometry(TePolygon& pol);
00423                       virtual bool fetchGeometry(TeLine2D& line);
00424                       virtual bool fetchGeometry(TeNode& n);
00425                       virtual bool fetchGeometry(TePoint& p);
00426                       virtual    bool fetchGeometry (TeCell& cell);
00427                       virtual bool fetchGeometry(TePolygon& pol, const unsigned int& initIndex);
00428                       virtual bool fetchGeometry(TeLine2D& line, const unsigned int& initIndex);
00429                       virtual bool fetchGeometry(TeNode& n, const unsigned int& initIndex);
00430                       virtual bool fetchGeometry(TePoint& p, const unsigned int& initIndex);
00431                       virtual    bool fetchGeometry (TeCell& cell, const unsigned int& initIndex);
00432 
00433 
00434            protected:
00435 
00437                       TeLinearRing getLinearRing(int& numberOfHoles);
00438 
00440                       TeLinearRing getLinearRing(int& numberOfHoles, const unsigned int& i);
00441 
00443                       string escapeSequence(const string& from);
00444 
00445                       // Protected data members
00446                       TePGConnection *con_;
00447                       TePGRecordset* tepg_recordset_;
00448 
00449                       long curRow_;                    
00450 
00451 };
00452 
00453 
00459 class TLPOSTGRESQL_DLL TePostgreSQLFactory : public TeDatabaseFactory
00460 {
00461   public :
00462       
00466     TePostgreSQLFactory() : TeDatabaseFactory( 
00467       std::string( "PostgreSQL" ) ) {};      
00468       
00472     ~TePostgreSQLFactory() {};
00473       
00474   protected :  
00475   
00483     TeDatabase* build( const TeDatabaseFactoryParams& arg )
00484     {
00485       TePostgreSQL* instance_ptr = new TePostgreSQL();
00486       
00487       if( arg.host_ != "" ) {
00488         instance_ptr->connect( arg.host_, arg.user_, arg.password_,
00489           arg.database_, arg.port_ );
00490       }
00491   
00492       return (TeDatabase*)instance_ptr;
00493     }
00494 };
00495 
00496 namespace {
00497   static TePostgreSQLFactory TePostgreSQLFactory_instance;
00498 }; 
00499 
00500 #endif     // __TERRALIB_INTERNAL_TePOSTGRESQL_H
00501 
00502 /*
00503  *  Updates:
00504  *      - 2007/03/26: Mario Rocco Added new method - string leftString(const string& name, const int& length);
00505  */
00506 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines