![]() |
TerraLib 4.1
|
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_ADODB_H 00027 #define __TERRALIB_INTERNAL_ADODB_H 00028 00029 #include <TeDatabaseFactory.h> 00030 00031 #ifdef WIN32 00032 #pragma warning ( disable: 4192 ) 00033 #pragma warning ( disable: 4146 ) 00034 #include <atldbcli.h> 00035 #include <iostream> 00036 #include <comdef.h> 00037 00038 /* 00039 * Lets use the import directive to create some smart pointers for us 00040 */ 00041 #import "msado26.tlb" rename("EOF", "IsEOF") rename("BOF", "IsBOF") 00042 #import "msadox.dll" 00043 #import "oledb32.dll" rename_namespace("OLEDB") 00044 #else 00045 #error ONLY FOR WINDOWS OPERATIONAL SYSTEM 00046 #endif 00047 #include <TeDatabase.h> 00048 00049 00050 #ifdef AFX_DLL 00051 #define EXPORT_WIN __declspec( dllexport ) 00052 #else 00053 #define EXPORT_WIN 00054 #endif 00055 00056 #include "TeAdoDefines.h" 00057 00059 class TLADO_DLL TeAdo : public TeDatabase 00060 { 00061 friend class TeAdoPortal; 00062 00063 public: 00064 TeAdo(); 00065 ~TeAdo(); 00066 00067 // Methods to create and connect to a ADO database 00068 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 = ""); 00069 virtual bool connect (const string& host, const string& user, const string& password, const string& database, int port = -1); 00071 virtual bool connect (const string& fullDatabaseName = ""); 00072 // bool connect (); 00073 virtual void connection (ADODB::_ConnectionPtr conn); 00074 string connectionString() { return connectionString_; } 00075 00076 // Close connection 00077 void close() ; 00078 00079 // Submit a SQL statement 00080 bool execute ( const string &sql); 00081 string escapeSequence(const string& from); 00082 00083 // Methods to manipulate a table 00084 bool tableExist(const string& table); 00085 bool listTables(vector<string>& tableList); 00086 bool columnExist(const string& table, const string& column,TeAttribute& attr); 00087 bool allowEmptyString(const string& tableName, const string& column); 00088 bool getAttributeList(const string& tableName,TeAttributeList& attList); 00089 00090 virtual bool createTable(const string& table, TeAttributeList &attr); 00091 virtual bool addColumn(const string& table,TeAttributeRep &rep); 00092 virtual bool alterTable (const string& table, TeAttributeRep &rep, const string& oldColName=""); 00093 virtual bool alterTable (const string& oldTableName, const string& newTableName); 00094 00095 virtual bool deleteColumn (const string& table, const string& colName); 00096 virtual bool createRelation (const string& relationName, 00097 const string& foreignTbl, const string& FTKey, 00098 const string& relatedTbl, const string& RTKey, 00099 bool cascadeDeletion); 00100 00101 TeDBRelationType existRelation(const string& tableName, const string& relName); 00102 00103 // Insert/Update methods 00104 bool insertProject(TeProject *project); 00105 00106 // Insert an attribute table 00107 bool insertTable(TeTable &table); 00108 bool insertBlob (const string& tableName, const string& columnBlob, const string& whereClause, unsigned char* data, int size); 00109 00110 bool insertTableInfo (int layerId, TeTable &table, const string& user=""); 00111 bool insertRelationInfo(const int tableId, const string& tField, 00112 const string& eTable, const string& eField, int& relId); 00113 bool insertLayer(TeLayer *layer); 00114 bool insertRepresentation (int layerId, TeRepresentation& rep); 00115 bool insertView (TeView *view); 00116 virtual bool insertTheme (TeAbstractTheme *theme); 00117 virtual bool insertThemeGroup (TeViewTree* tree); 00118 bool insertThemeTable (int themeId, int tableId, int relationId, int tableOrder); 00119 00120 bool insertViewTree (TeViewTree *tree); 00121 bool insertLegend (TeLegendEntry *legend); 00122 bool insertProjection (TeProjection *proj); 00123 00124 virtual bool insertPolygonSet (const string& table, TePolygonSet &ps); 00125 virtual bool insertPolygon (const string& table, TePolygon &p); 00126 bool updatePolygon (const string& table, TePolygon &p); 00127 00128 bool insertLineSet (const string& table, TeLineSet &ls); 00129 bool insertLine (const string& table, TeLine2D &l); 00130 bool updateLine (const string& table, TeLine2D &l); 00131 00132 bool insertPointSet (const string& table, TePointSet &ps); 00133 bool insertPoint (const string& table, TePoint &p); 00134 bool updatePoint (const string& table, TePoint &p); 00135 00136 bool insertTextSet (const string& table, TeTextSet &ts); 00137 bool insertText (const string& table, TeText &t); 00138 00139 bool insertArcSet (const string& table, TeArcSet &as); 00140 bool insertArc (const string& table,TeArc &arc); 00141 00142 bool insertNodeSet (const string& table, TeNodeSet &ns); 00143 bool insertNode (const string& table, TeNode &node); 00144 bool updateNode (const string& table, TeNode &node); 00145 00146 bool insertCellSet (const string& table, TeCellSet &cs); 00147 bool insertCell (const string& table, TeCell &c); 00148 00149 bool ByteArrayToVariant(VARIANT &varArray, unsigned char* bytes, int nbytes); 00150 bool LineToVariant(VARIANT &varArray, TeLine2D& line); 00151 bool LinearRingToVariant(VARIANT &varArray, TeLinearRing& ring); 00152 bool PointsToVariant(VARIANT &varArray, double* points, int npoints); 00153 00154 TeDatabasePortal* getPortal (); 00155 00156 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); 00157 00159 string getSQLTime(const TeTime& time) const; 00160 string getSQLTemporalWhere(int time1, int time2, TeChronon chr, TeTemporalRelation rel, 00161 const string& initialTime, const string& finalTime); 00162 00164 string concatValues(vector<string>& values, const string& unionString); 00165 00167 string toUpper(const string& value); 00168 00169 //informations about the system datetime format 00170 string systemDateTimeFormat_; 00171 string systemIndAM_; 00172 string systemIndPM_; 00173 string systemDateSep_; 00174 string systemTimeSep_; 00175 00176 bool updateBBox(const string& tableName, const string& idName, int idValue, const TeBox& box); 00177 bool updateLayerBox(TeLayer* layer); 00178 bool updateLayer(TeLayer* layer); 00179 00180 static string systemDateTimeFormat(string& indAM, string& indPM, string& sepD, string& sepT); 00181 00185 00186 virtual bool beginTransaction(); 00187 00189 virtual bool commitTransaction(); 00190 00192 virtual bool rollbackTransaction(); 00194 00195 protected : 00196 ADODB::_ConnectionPtr connection_; 00197 string connectionString_; 00198 }; 00199 00201 class TLADO_DLL TeAdoPortal : public TeDatabasePortal 00202 { 00203 protected: 00204 00205 _bstr_t bvalue_; 00206 string error_message_; 00207 long error_num_; 00208 long curRow_; 00209 00210 TeLinearRing getLinearRing(); 00211 00212 public : 00213 // Constructor / Destructor 00214 00215 TeAdoPortal (); 00216 TeAdoPortal (TeDatabase* pDatabase); 00217 ~TeAdoPortal (); 00218 00219 // Queries 00220 bool isConnected (); 00221 bool isEOF(); 00222 00223 // Move operations 00224 bool moveFirst(); 00225 bool moveNext(); 00226 00227 ADODB::_ConnectionPtr connection_; 00228 ADODB::_RecordsetPtr recset_; 00229 00230 00231 // Basic database methods 00232 bool query ( const string &qry, TeCursorLocation l = TeSERVERSIDE, TeCursorType t = TeBIDIRECTIONAL, TeCursorEditType e = TeREADWRITE, TeCursorDataType dt = TeTEXTCURSOR ); 00233 bool fetchRow (); 00234 bool fetchRow (int i); 00235 void freeResult (); 00236 00237 // specific TerraLib database access methods 00238 bool fetchGeometry(TePolygon& pol); 00239 bool fetchGeometry(TePolygon& pol, const unsigned int& initIndex); 00240 bool fetchGeometry(TeLine2D& line); 00241 bool fetchGeometry(TeLine2D& line, const unsigned int& initIndex); 00242 bool fetchGeometry(TeNode& n); 00243 bool fetchGeometry(TeNode& n, const unsigned int& initIndex ); 00244 bool fetchGeometry(TePoint& p); 00245 bool fetchGeometry(TePoint& p, const unsigned int& initIndex); 00246 00247 char* getData (int i); 00248 char* getData (const string& s); 00249 double getDouble (int i); 00250 double getDouble (const string& s); 00251 int getInt (int i); 00252 int getInt (const string& s); 00253 bool getBool (const string& s); 00254 bool getBool (int i); 00255 bool getBlob (const string& s, unsigned char* &data, long& size); 00256 bool getBlob (int i, unsigned char* &data, long& size); 00257 00258 bool BlobToLine(TeLine2D& line); 00259 bool getRasterBlock(unsigned long& size, unsigned char* ptData); 00260 00261 TeTime getDate (int i); 00262 TeTime getDate (const string& s); 00263 00265 string getDateAsString(int i); 00266 string getDateAsString(const string& s); 00267 00268 }; 00269 00275 class TLADO_DLL TeAdoFactory : public TeDatabaseFactory 00276 { 00277 public : 00278 00282 TeAdoFactory() : TeDatabaseFactory( std::string( "Ado" ) ) {}; 00283 00287 ~TeAdoFactory() {}; 00288 00289 protected : 00290 00298 TeDatabase* build( const TeDatabaseFactoryParams& arg ) 00299 { 00300 TeAdo* instance_ptr = new TeAdo(); 00301 00302 if( arg.database_ != "" ) { 00303 instance_ptr->connect( arg.host_, arg.user_, arg.password_, 00304 arg.database_, arg.port_ ); 00305 } 00306 00307 return (TeDatabase*)instance_ptr; 00308 } 00309 }; 00310 00311 namespace { 00312 static TeAdoFactory TeAdoFactory_instance; 00313 }; 00314 00315 #endif