| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/drivers/MySQL/TeMySQL.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 *************************************************************************************/
    00026 #ifndef  __TERRALIB_INTERNAL_MYSQL_H
    00027 #define  __TERRALIB_INTERNAL_MYSQL_H
    00028
    00029 #include "TeMySQLDefines.h"
    00030
    00031 #include "../../kernel/TeTable.h"
    00032 #include "../../kernel/TeDatabase.h"
    00033 #include "../../kernel/TeDatabaseFactory.h"
    00034
    00035 #include "../../kernel/TeDatabaseIndex.h"
    00036
    00037 #ifdef WIN32
    00038 #include <winsock.h>
    00039 #endif
    00040
    00041 //#include <mysql.h>
    00042
    00043 //Forward declarations
    00044 struct st_mysql;
    00045 struct st_mysql_res;
    00046 //typedef char** MYSQL;
    00047
    00048
    00050 class TLMYSQL_DLL TeMySQL : public TeDatabase
    00051 {
    00052     friend class TeMySQLPortal;
    00053
    00054 public:
    00055
    00056            TeMySQL();
    00057
    00058            ~TeMySQL();
    00059
    00060            st_mysql*  getMySQL ()
    00061     {      return mysql_;        }
    00062
    00063            string  escapeSequence(const string& from);
    00064
    00065            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 = "");
    00066
    00067            bool connect (const string& host, const string& user, const string& password, const string& database, int port=-1);
    00068
    00069            bool showDatabases (const string& host, const string& user, const string& password, vector<string>& dbNames, int port=-1);
    00070
    00071            void close();
    00072
    00074            virtual bool listDatabases(std::vector<std::string> & databaseList);
    00075
    00077            virtual bool dropDatabase(const std::string & databaseName);
    00078
    00079            bool tableExist(const string& table);
    00080
    00081            bool columnExist(const string& table, const string& column, TeAttribute& attr);
    00082
    00083            bool addColumn (const string& table,TeAttributeRep &rep);
    00084
    00086            virtual bool deleteIndex(const string& tableName, const string& indexName);
    00087
    00089            virtual bool getIndexesFromTable(const string& tableName, std::vector<TeDatabaseIndex>& vecIndexes);
    00090
    00092            virtual bool alterTable(const string& tableName, TeAttributeRep& rep, const string& oldColName="");
    00093
    00094            bool defineIntegrity(void);
    00095
    00096            bool createRelation (const string& /* relName */, const string& /* table */, const string& /* fieldName */,
    00097                          const string& /* relatedTable */, const string& /* relatedField */, bool /* cascadeDeletion */)
    00098            { return true; }
    00099
    00100            TeDBRelationType existRelation(const string& /* tableName */, const string& /* relName */)
    00101
    00102            {          return TeNoRelation; }
    00103
    00104            bool execute ( const string &q);
    00105
    00106            bool createTable(const string& table, TeAttributeList &attr);
    00107
    00108            TeDatabasePortal* getPortal ();
    00109
    00110     string errorMessage ();
    00111     //    {           return string(mysql_error(mysql_));         }
    00112
    00113     int    errorNum ();
    00114     //    {           return mysql_errno(mysql_);      }
    00115
    00116     // specific TerraLib database access methods
    00117
    00118            bool insertProjection (TeProjection *proj);
    00119            bool insertLayer(TeLayer* layer);
    00120            bool insertProject(TeProject* project);
    00121            bool insertRepresentation(int layerId, TeRepresentation& rep);
    00122            bool insertTable(TeTable &table);
    00123            bool updateTable      (TeTable &table);
    00124            bool insertTableInfo (int layerId, TeTable &table, const string& user="");
    00125            bool insertRelationInfo(const int tableId, const string& tField,
    00126                                                                       const string& eTable, const string& eField, int& relId);
    00127            bool insertView (TeView *view);
    00128            bool insertViewTree (TeViewTree *tree);
    00129            bool insertTheme(TeAbstractTheme *theme);
    00130            bool insertThemeGroup (TeViewTree* tree);
    00131            bool generateLabelPositions (TeTheme *theme, const std::string& objectId = "");
    00132            bool insertThemeTable (int themeId, int tableId, int relationId, int tableOrder);
    00133            bool insertLegend (TeLegendEntry *legend);
    00134            bool insertPolygon    (const string& table, TePolygon &p);
    00135            bool insertPolygonSet(const string& table, TePolygonSet &ps);
    00136            bool updatePolygon    (const string& table, TePolygon &p);
    00137            bool insertLineSet    (const string& table, TeLineSet &ls);
    00138            bool insertLine (const string&  table, TeLine2D &l);
    00139            bool updateLine (const string& table, TeLine2D &l);
    00140            bool insertPoint (const string& table, TePoint &p);
    00141            bool insertText (const string& table, TeText &t);
    00142            bool insertArc (const string& table,TeArc &arc);
    00143            bool insertNode       (const string& table, TeNode &node);
    00144            bool insertCell       (const string& table, TeCell &c);
    00145            int  insertId();
    00146
    00147
    00148            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);
    00149
    00150            string getSQLTime(const TeTime& time) const;
    00151            string getSQLTemporalWhere(int time1, int time2, TeChronon chr, TeTemporalRelation rel,
    00152                                const string& initialTime, const string& finalTime);
    00153
    00154            bool inClauseValues(const string& query, const string& attribute, vector<string>& inClauseVector);
    00155
    00156            bool insertBlob (const string& tableName, const string& columnBlob, const string& whereClause, unsigned char* data, int size);
    00157
    00159            string concatValues(vector<string>& values, const string& unionString);
    00160
    00162            string toUpper(const string& value);
    00163
    00164            string getConcatFieldsExpression(const vector<string>& fNamesVec);
    00165
    00166            bool beginTransaction();
    00167     bool commitTransaction();
    00168     bool rollbackTransaction();
    00169
    00171            virtual std::string getClientEncoding();
    00172
    00174            virtual bool setClientEncoding(const std::string& characterSet);
    00175
    00177            virtual bool getEncodingList(std::vector<std::string>& vecEncodingList);
    00178
    00180            virtual bool getEncodingList(const std::string& host, const std::string& user, const std::string& password, const int& port, std::vector<std::string>& vecEncodingList);
    00181
    00182 private :
    00183
    00184         st_mysql*     mysql_;
    00185 char*   bufferBlob_;   // internal buffer used to write blob data
    00186 long       bufferBlobSize_; // size of the data already allocated
    00187
    00188 };
    00189
    00191 class TLMYSQL_DLL TeMySQLPortal  : public TeDatabasePortal
    00192 {
    00193 private:
    00194
    00195            st_mysql                         *mysql_;
    00196            st_mysql_res*                    result_;
    00197   char**              row_;
    00198
    00199 public :
    00200
    00201         // Constructor / Destructor
    00202
    00203     TeMySQLPortal ( TeMySQL *m);
    00204
    00205     TeMySQLPortal ( const TeMySQLPortal& p);
    00206
    00207     ~TeMySQLPortal ();
    00208
    00209     // Basic database methods
    00210
    00211     int  insertId();
    00212
    00213     bool query (const string &qry, TeCursorLocation l = TeSERVERSIDE, TeCursorType t = TeUNIDIRECTIONAL, TeCursorEditType e = TeREADONLY, TeCursorDataType dt = TeTEXTCURSOR );
    00214
    00215     bool fetchRow ();
    00216
    00217     bool fetchRow (int i);
    00218
    00219     void freeResult ();
    00220
    00221     string errorMessage ();
    00222
    00223     //{ return string(mysql_error(mysql_));} //: last error message()
    00224
    00225     int    errorNum ();
    00226
    00227     //{ return mysql_errno(mysql_);}
    00228
    00229     // specific SQL SELECT command methods
    00230
    00231     char*             getData (int i);
    00232     char*             getData (const string& s);
    00233     double            getDouble (int i);
    00234     double            getDouble (const string& s);
    00235     int                          getInt (int i);
    00236     int                          getInt (const string& s);
    00237     bool              getBool (const string& s);
    00238     bool              getBool (int i);
    00239
    00240     bool fetchGeometry(TePolygon& pol);
    00241     bool fetchGeometry(TeLine2D& line);
    00242     bool fetchGeometry(TeNode& n);
    00243     bool fetchGeometry(TePoint& p);
    00244     bool fetchGeometry(TePolygon& pol, const unsigned int& initIndex);
    00245     bool fetchGeometry(TeLine2D& line, const unsigned int& initIndex);
    00246     bool fetchGeometry(TeNode& n, const unsigned int& initIndex);
    00247     bool fetchGeometry(TePoint& p, const unsigned int& initIndex);
    00248
    00249     bool setData (int /* field */, string /* val */)
    00250     {return true; }
    00251     st_mysql_res* result()
    00252     {return result_; }
    00253
    00254     bool getBlob (const string& s, unsigned char* &data, long& size);
    00255     bool getRasterBlock(unsigned long& size, unsigned char* ptData);
    00256
    00257     TeTime getDate (int i);
    00258     TeTime getDate (const string& s);
    00259
    00260     string getDateAsString(int i);
    00261     string getDateAsString(const string& s);
    00262
    00263 protected:
    00264     TeLinearRing getLinearRing (int &ni);
    00265 };
    00266
    00267
    00273 class TLMYSQL_DLL TeMySQLFactory : public TeDatabaseFactory
    00274 {
    00275 public :
    00276
    00280     TeMySQLFactory() : TeDatabaseFactory( std::string( "MySQL" ) ) {};
    00281
    00285     ~TeMySQLFactory() {};
    00286
    00287 protected :
    00288
    00296   TeDatabase* build( const TeDatabaseFactoryParams& arg )
    00297   {
    00298       TeMySQL* instance_ptr = new TeMySQL();
    00299
    00300       if( arg.host_ != "" ) {
    00301           instance_ptr->connect( arg.host_, arg.user_, arg.password_,
    00302                                  arg.database_, arg.port_ );
    00303       }
    00304
    00305       return (TeDatabase*)instance_ptr;
    00306   }
    00307 };
    00308
    00309 namespace {
    00310     static TeMySQLFactory TeMySQLFactory_instance;
    00311 };
    00312
    00313 #endif
    00314

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines