TeOCICursor.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 *************************************************************************************/
00023 /*! \file TeOCICursor.h
00024         \brief This file contains the class OCICursor that represents a concept of record set to ORACLE DBMS using OCI (Oracle Call Interface) library. 
00025 */
00026 
00027 #ifndef OCICursor_H
00028 #define OCICursor_H
00029 
00030 #include <TeOCIConnect.h>
00031 #include <vector>
00032 #include <TeCoord2D.h>
00033 
00034 #include "TeOracleDefines.h"
00035 
00036 using namespace std;
00037 
00038 #define TYPE_OWNER                              "MDSYS"
00039 #define SDO_ORDINATE_ARRAY      TYPE_OWNER".SDO_ORDINATE_ARRAY"
00040 #define SDO_ELEM_INFO_ARRAY     TYPE_OWNER".SDO_ELEM_INFO_ARRAY"
00041 #define SDO_GEOMETRY                    TYPE_OWNER".SDO_GEOMETRY"
00042 
00043 //! Number of rows to be fetched into memory from database server
00044 #define MAX_ROWS                                500
00045 
00046 //! Max size to be allocated in the client side to store blob data (in bytes)
00047 #define MAX_BLOB_LENGTH                 5000000 //5 MegaBytes (the max blob length is 4 Gigabytes)
00048 
00049 //! Structure to represent a point as the SDO_POINT type of the Oracle Spatial 
00050 struct TLORACLE_DLL sdo_point_type
00051 {
00052    OCINumber x;
00053    OCINumber y;
00054    OCINumber z;
00055 };
00056 
00057 typedef struct sdo_point_type sdo_point_type;
00058 
00059 //! Structure to represent a geometry as the SDO_GEOMETRY type of the Oracle Spatial 
00060 struct TLORACLE_DLL sdo_geometry
00061 {
00062    OCINumber       sdo_gtype;
00063    OCINumber       sdo_srid;
00064    sdo_point_type  sdo_point;
00065    OCIArray        *sdo_elem_info;
00066    OCIArray        *sdo_ordinates;
00067 };
00068 
00069 typedef struct sdo_geometry SDO_GEOMETRY_TYPE;
00070 
00071 //! Structure to represent a point indicator as the SDO_POINT type of the Oracle Spatial 
00072 struct TLORACLE_DLL sdo_point_type_ind
00073 {
00074    OCIInd _atomic;
00075    OCIInd x;
00076    OCIInd y;
00077    OCIInd z;
00078 };
00079 
00080 typedef struct sdo_point_type_ind sdo_point_type_ind;
00081 
00082 //! Structure to represent a geometry indicator as the SDO_GEOMETRY type of the Oracle Spatial 
00083 struct TLORACLE_DLL SDO_GEOMETRY_ind
00084 {
00085    OCIInd                    _atomic;
00086    OCIInd                    sdo_gtype;
00087    OCIInd                    sdo_srid;
00088    struct sdo_point_type_ind sdo_point;
00089    OCIInd                    sdo_elem_info;
00090    OCIInd                    sdo_ordinates;
00091 };
00092 
00093 typedef struct SDO_GEOMETRY_ind SDO_GEOMETRY_ind;
00094 
00095 //! Structure to handle OCI data types
00096 struct TLORACLE_DLL sb2ind
00097 {
00098         sb2             sbind[MAX_ROWS];
00099 };
00100 typedef struct sb2ind   indarray;
00101 
00102 /*! \class TeOCICursor
00103         \brief A class that implements a concept of record set to a ORACLE DBMS.
00104 
00105         This class contains attributes and methods to handle a record set 
00106         to ORACLE DBMS using OCI (Oracle Call Interface) library.
00107 
00108         A record set is generated by a query on the data in the database server.
00109         The class OCICursor is used by the drivers TeOCIOracle and TeOCIOracleSpatial that
00110         are responsible for the link between ORACLE DBMS and TerraLib.
00111         \sa 
00112         TeOCIConnect
00113 */
00114 class TLORACLE_DLL TeOCICursor
00115 {
00116 protected:
00117         TeOCIConnection*        connection_;            //!< A pointer to a opened connection
00118         OCIStmt*                        stmthpToQuery_;         //!< OCI statement handle to query
00119         OCIDescribe*            dschp_;                         //!< OCI environment describe handle - describes objects
00120         bool                            isOpen_;                        //!< indicates if the cursor is opened or not (if the stmthpToQuery was located)
00121         string                          errorMessage_;          //!< error message returned by the ORACLE DBMS  
00122                                 
00123         /** @name Buffers to store data in memory - client side
00124         */
00125         //@{    
00126         vector<void *>                  buffers_;       //!< buffer to store simple data type (varchar, number, date, etc)
00127         vector <indarray>               ind_;           //!< buffer to store null indicator
00128                                 
00129         SDO_GEOMETRY_TYPE*              global_geom_obj_[MAX_ROWS];     //!< buffer to store spatial object  
00130         SDO_GEOMETRY_ind*               global_geom_ind_[MAX_ROWS];     //!< buffer to store spatial object indicator  
00131 
00132         vector<unsigned char*>  lobBuffer_;                     //! vector of buffers to store blob
00133         bool                                    hasBlob_;                       //! Flag that indicates if the cursor has blob data type
00134         unsigned long                   maxBuflen_;                     //! Max memory the will be allocated to store blob data
00135     int                                         maxRows_;                       //! Number of rows to be fetched into memory from database server
00136 
00137         vector<OCIDefine *>             defines_;               //!< OCIDefine objects to link buffers in client side and the statement handle
00138         OCIArray*                               ordinates_;             //!< store the ordinates to be used in a query (bind)
00139         //@}
00140 
00141         string                                  fieldValue_;    //!< temporary variable 
00142 
00143         /** @name Information to handle the records or rows stored in memory (buffers) 
00144         */
00145         //@{    
00146         short                           row_Index_;             //!< relative current row index in the memory buffer
00147         int                                     row_Cur_;               //!< absolute current row index in the memory buffer
00148         int                                     rows_Fetched_;  //!< number of rows fetched from database
00149         int                                     rows_Mem_;              //!< number of rows on memory buffer - client side
00150         bool                            last_Row_;              //!< if the current row is the last
00151         //@}
00152         
00153         /** @name Information about columns of the record set in memory
00154         */
00155         //@{    
00156         int                                     numColls_;  //!< number of columns
00157         vector<string>          colName_;       //!< name of the columns
00158         vector<int>                     colType_;       //!< type of the columns
00159         vector<int>                     colSize_;       //!< size of the columns
00160         vector<int>                     colScale_;  //!< scale of the columns
00161         vector<string>          colBlobName_; //!< name of the BLOB columns only
00162         //@}
00163 
00164 public:
00165         
00166         //! Constructor 
00167         TeOCICursor(TeOCIConnection* conn, const unsigned long& maxBlobSize=0, const unsigned long& maxRowNum=0 );
00168 
00169         //! Destructor
00170         ~TeOCICursor() { close(); }
00171 
00172         //! Opens the cursor locating memory and initializing the enviroment 
00173         bool open();
00174                 
00175         //! Returns the error message 
00176         string  getErrorMessage() { return errorMessage_;}
00177 
00178         //! Checks if the cursor is opened
00179         bool isOpen()   { return isOpen_; }
00180 
00181         //! Returns the connection to the database server  
00182         TeOCIConnection*        conn()          { return connection_; }
00183 
00184         //! Returns the current row index 
00185         int     currentRow() { return row_Cur_; }
00186 
00187         //! Closes the cursor
00188         void close();
00189         
00190         //! Defines the OCI handles by position
00191         void defineByPos(int pos, void* value, int size, void* indicator, int type);
00192 
00193         //! Executes the SQL statement set previously 
00194         bool            execute();
00195 
00196         //! Fetchs a number of rows 
00197         bool            fetch(int rows);
00198 
00199         //! Executes a query defined by a SQL statement that returns a record set as result
00200         bool            query(const string& query);
00201 
00202         //! Executes a query defined by a SQL statement that uses SDO_GEOMETRY type and returns a record set as result
00203         bool            querySDO(const string& query);
00204 
00205         //! Moves to the first row in the record set 
00206         bool            moveFirst();
00207 
00208         //! Moves to the next row in the record set
00209         bool            moveNext();
00210 
00211         //! Moves to a specific position in the record set
00212         bool            moveTo(int pos);
00213 
00214         //! Moves to the last row in the record set
00215         bool            moveLast();
00216         
00217         /** @name Methods to return information about the columns and rows in the record set
00218         */
00219         //@{ 
00220         //! Returns the column type (SQLT) of a specific column
00221         int                     colType (int colnumber);
00222         //! Returns the column name of a specific column
00223         string          colName (int colnumber);
00224         //! Returns the column size of a specific column
00225         int                     colSize (int colnumber);
00226         //! Returns the column scale (number of digits after decimal point) of a specific column
00227         int                     colScale (int colnumber);
00228         //! Returns the columns number in the client side
00229         int                     numCol(void);
00230         //! Loads all column descriptions (name, type, size and scale)
00231         void            loadCollDescription (); 
00232         //! Returns the rows number in the client side
00233         int                     numRows(void);
00234         //@}
00235 
00236         //! Appends the new value to the internal ordinate vector
00237         bool            appendOrdinates(const double& val);
00238         
00239         //! Binds the internal ordinate array with the SQL statement
00240         bool            bindOrdinates();
00241         
00242         //! Sets and prepares the SQL statement to be executed
00243         bool            prepare(const string& stmt);
00244         
00245         //! Returns the query type based on the SQL statement set previously
00246         int                     queryType(); 
00247 
00248         //! Locates memory to store the record set in the client side
00249         bool            allocateCursor(void);
00250 
00251         //! Gets the i-th field value from record set
00252         char*           getFieldValue(int i);
00253 
00254         /** @name Methods to deal with SDO_GEOMETRY, SDO_ELEM_INFO and SDO_ORDINATES types of the Oracle Spatial
00255         */
00256         //@{    
00257         //! Gets the size of the SDO_ELEM_INFO array of the record set current row  
00258         int                     getDimArraySize();
00259         //! Gets the i-th element of the SDO_ELEM_INFO array of the record set current row 
00260         bool            getDimElement(int i,int &elem);
00261         //! Gets the number of ordinates in the SDO_ORDINATES array of the record set current row 
00262         int                     getNumberOrdinates(void);
00263         //! Gets the i-th coordinate of the SDO_ORDINATES array of the record set current row 
00264         bool            getCoordinates(int i,TeCoord2D& coord);
00265         //! Gets all coordinates of the SDO_ORDINATES array of the record set current row 
00266         bool            getCoordinates(vector<TeCoord2D>& result);
00267         //! Gets the geometry type of the SDO_GEOMETRY type of the record set current row 
00268         int                     getGeometryType();
00269         //! Gets the spatial reference of the SDO_GEOMETRY type of the record set current row 
00270         int                     getSpatialReferenceId();
00271         //! Gets a coordinate (x and y) of the SDO_POINT type of the record set current row 
00272         bool            getXYZcoord (double& x, double& y);
00273         //@}
00274 
00275         //! Clears the memory and structures located by the record set
00276         void            freeResult(void);
00277 
00278         /** @name Methods to deal with long binary (blob) type
00279         */
00280         //@{    
00281         //! Reads a long binary (blob) column (blobCol) from database server and returns it as a unsigned char vector
00282         bool            readBlob(unsigned char* buffer, unsigned int& bufsize, unsigned int& blobCol);
00283         //! Reads a long binary (blob) column (blobCol) from database server and returns it as a unsigned char vector
00284         bool            readBlob(unsigned char* buffer, unsigned int& bufsize, const string& blobCol);
00285         //! Reads a long binary (blob) from database server and returns it as a double vector
00286         bool            readBlob(double *buffer, unsigned int& bufsize);
00287         //! Gets a pointer to buffer of blob
00288         bool            readBlob(double **buffer);
00289         //! Returns the size (in unsigend int) of a specific blob column
00290         unsigned int    getBlobSize(const string& blobCol);
00291         //@}
00292 
00293 protected:
00294         //! Checks the error associated to error OCI handle
00295         bool            checkError(sword status);
00296 };
00297 
00298 #endif 
00299 
00300 

Generated on Sun Jul 29 04:01:22 2012 for TerraLib - Development Source by  doxygen 1.5.3