Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

postgres.h

Go to the documentation of this file.
00001 
00002 // Name:        wx/dbi/postgres.h
00003 // Purpose:     
00004 // Author:      Joe Yates
00005 // Created:     2004-11-17
00006 // Copyright:   (c) Joe Yates
00007 // License:     BSD license (see the file 'LICENSE.txt')
00009 
00010 #ifndef __wx_dbi_postgres_h__
00011 #define __wx_dbi_postgres_h__
00012 
00013 #if _MSC_VER > 1000
00014 #pragma once
00015 #endif // _MSC_VER > 1000
00016 
00017 #ifdef wxDBI_POSTGRES
00018 
00019 #include "wx/wx.h"
00020 
00021 #include <wx/dbi.h>
00022 #include <postgres/libpq-fe.h>
00023 
00024 namespace wx
00025 {
00026 
00027 namespace DBI
00028 {
00029 
00030 class Postgres : public Database
00031   {
00032   // friends, classes, enums and types
00033   // PgStatement nees access to the Pg connection
00034   friend class PgStatement;
00035 
00036   // Constructors/destructors
00037   public:
00041     Postgres();
00042 
00046     ~Postgres()
00047       {
00048       Disconnect();
00049       }
00050 
00052   // 'Database' Implementation
00053 
00054   // Operators
00055 
00056   // Methods
00057   public:
00058     // Connection
00059 
00063     void    Connect(const ConnectionInfo& cni);
00064     void    Disconnect();
00065     inline bool IsConnected()
00066       {
00067       return (conn != NULL)? true : false;
00068       }
00069 
00070     // Transactions
00071     void Begin();
00072     void Rollback();
00073     void Commit();
00074 
00075     // Queries
00076     Statement *       Prepare(wxString const& sQuery);
00077 
00078     // Database info
00079     wxArrayString     GetDatabases();
00080     bool              DatabaseExists(const wxString& sDatabaseName);
00081     wxArrayString     GetTables();
00082     bool              TableExists(const wxString& sTableName);
00083 
00085   // PgDatabase-specific methods
00086   public:
00087     static wxString   GetPgTypeFromVariantType(const wxString& sType);
00088     static Oid        GetPgOidFromVariantType(const wxString& sType);
00089 
00090   private:
00091     long  GenerateStatementId();
00092 
00094   // Members
00095   private:
00096     PGconn     *  conn;
00097     StatementCache stc;
00098   };
00099 
00100 class PgStatement : public Statement
00101   {
00102   // friends, classes, enums and types
00103   private:
00104 
00108     class PgParameters
00109       {
00110       public:
00111         PgParameters(const ArrayRecord& raParameters);
00112         ~PgParameters();
00113 
00114       public:
00115         int GetParamCount()
00116           { return nParamCount; }
00117         const Oid * GetParamTypes()
00118           { return paramTypes; }
00119         const char * const * GetParamValues()
00120           { return paramValues; }
00121         int * GetParamLengths()
00122           { return paramLengths; }
00123         int * GetParamFormats()
00124           { return paramFormats; }
00125 
00126       private:
00127         int     nParamCount;
00128         Oid *   paramTypes;
00129         char ** paramValues;
00130         int *   paramLengths;
00131         int *   paramFormats;
00132       };
00133 
00134   public:
00135     PgStatement(Database * pdbOwner, const wxString& sNewQuery);
00136     ~PgStatement();
00137 
00138   public:
00139     bool  DataAvailable();
00140     void  ExecutePrepared(const ArrayRecord& raParameters);
00141     void  ExecuteTemporary(const ArrayRecord& raParameters = ArrayRecord());
00142 
00150     static void ExecuteTemporary
00151       (
00152       Postgres * pdb,
00153       const wxString& sQuery,
00154       const ArrayRecord& raParameters = ArrayRecord(),
00155       PGresult ** ppgr = NULL
00156       );
00157 
00158   protected:
00159     int   GetFieldCount();
00160     void  MoveNext();
00161     void  GetFieldValue
00162       (
00163       int             nIndex,
00164       wxVariant&      v
00165       );
00166     wxString GetFieldName
00167       (
00168       int         nIndex
00169       );
00170 
00171   private:
00180     void        PrepareQuery(const ArrayRecord& raParameters);
00181     void        CleanUpResult();
00182     void        ThrowPgError(wxString sError = wxT("%s"));
00183 
00184   private:
00185     Postgres *  pg;
00186     PGresult *  pgr; // Value being non-null also indicated that the query has been prepared
00187     long        nCurrentRecord;
00188 
00194     wxString    sQueryHandle;
00195   };
00196 
00197 } //namespace DBI
00198 
00199 } //namespace wx
00200 
00201 #endif // def wxDBI_POSTGRES
00202 
00203 #endif // ndef __wx_dbi_postgres_h__

Generated on Wed Jan 25 08:13:09 2006 for Sherpa wxWidgets Classes by doxygen 1.3.6