⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgsqlconnection.h

📁 C++连接一写常用数据库的接口
💻 H
字号:
/* * PostgresqlConnection object defines the needed connection functions for the dbConnect PostgrSQL driver * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org * * This library is free software; you can redistribute it and/or *   modify it under the terms of the GNU Lesser General Public *   License as published by the Free Software Foundation; either *   version 2.1 of the License, or (at your option) any later version. * *   This library is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *   Lesser General Public License for more details. * *   You should have received a copy of the GNU Lesser General Public *   License along with this library; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US * * Notes: *  1)   * */#ifndef __PGSQL_CONNECTION_H__#define __PGSQL_CONNECTION_H__#define PGSQL_DRIVERNAME    "PostgreSQL"#include <string>#include <time.h>#include "dbconnectTypes.h"#include "baseConnection.h"#include "baseException.h"#include "simpleThreads.h"#include "libpq-fe.h"using namespace std;class PgsqlHandle : public BaseHandle{public:   // PostgreSQL Connection Handles;   PGconn   *__conn;   void     *queryObject;             // Query Object bound to the connection   // Constructor   PgsqlHandle()    :      __conn(NULL),      queryObject(NULL)    { };};class PostgresqlConnection : public BaseConnection{private:   struct PgsqlOptions  // Options that can be set by the configuration file.   {      bool        requireSSL;        // 'requireSSL' config file option under 'postgresql' section.      string      connectionTimeout; // 'connectionTimeout' config file option under 'postgresql' section.      string      port;              // 'port' config file option under 'postgresql' section.            // Constructor      PgsqlOptions()      :         requireSSL(false),         connectionTimeout("120"),         port("5432")      { }   };      // Collection types that can be freed within this object   enum CollectionType   {      CONNECTION_HANDLES   };   // Thread mutexes   SimpleThread_Mutex classMutex;   // Structure storing the options for postgresql from the dbconnect.cfg file.   PgsqlOptions _pgsqlOptions;      // Handles used by the driver   DBULONG       _numHandles;   PgsqlHandle **_handles;   /* Internal method to connect the handle to a PostgreSQL database.    *    * @param handleIndex     Index to the connection handle that must be connected.    */   void    _pgsqlConnect(         int index);      /* Internal method to disconnect the handle from a PostgreSQL database.    *    * @param handleIndex     Index to the connection handle that must be disconnected.    */   void    _pgsqlDisconnect(         int index);   /* Internal method to make sure the connection handle is still alive and reconnect if timed out.    *    * @param handleIndex     Index to the connection handle that must be pinged.    */   void    _pgsqlPing(         int index);   /* Internal method to query PostgreSQL. All query objects will use this.    *    * @param handleIndex     Index to the connection handle to use for the query.    * @param sqlStatement    Sql statment to query against the database.    *    * @return                Returns a pointer to the query result    */   PGresult*   _pgsqlQuery(         int           index,         const string& sqlStatement);   /* Internal method to free internally allocated memory    *    * @param type    The type representing which value to deallocate.    *    */   void _freeCollection(         CollectionType type);   public:   /* Constructor.    *    */   PostgresqlConnection(         int argc,          const char** argv);   /* Destructor.    *    */   ~PostgresqlConnection();   /* Create connections to the database.    *    * @param username         Database username.    * @param password         Password used for username.    * @param databaseName     Name of the database connecting to.    * @param host             Host the database resides on. Default "localhost"    * @param maxConnections   Maximum number of connections the object can pool at any one time. Default 1    * @param optParam1        Optional parameter that can be used by the driver.    * @param optParam2        Optional parameter that can be used by the driver.    *    */   void    connect(         const string &username,          const string &password="",          const string &databaseName="",          const string &host="localhost",          int          maxConnections=1,         int          minConnections=1,         const string &optParam1="",         const string &optParam2="");   /* Destroy all connections to the database.    *    * @param timeout          Timeout in seconds to wait for connections to become idle. Default 120.    */   void    disconnect(         time_t timeout=120);        /* Obtain a connection that is attached to a query object.    *    * @param timeout     Interval in seconds that the function should block for before throwing an exception.    *    * @return            Returns a void pointer to a BaseQuery object. (The pointer needs to be type casted.    */   void*   requestQueryConnection();      /* release a connection that is attached to a query object.    *    * @param queryConnection   Pointer of the query connection to release.    *    */   void   releaseQueryConnection(      void* queryObject);         // Define all friend classes   friend class PostgresqlQuery;};#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -