Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

connection_base.hxx

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/connection_base.hxx
00005  *
00006  *   DESCRIPTION
00007  *      definition of the pqxx::connection_base abstract base class.
00008  *   pqxx::connection_base encapsulates a frontend to backend connection
00009  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/connection_base instead.
00010  *
00011  * Copyright (c) 2001-2004, Jeroen T. Vermeulen <jtv@xs4all.nl>
00012  *
00013  * See COPYING for copyright license.  If you did not receive a file called
00014  * COPYING with this source code, please notify the distributor of this mistake,
00015  * or contact the author.
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 
00020 #include <map>
00021 #include <memory>
00022 
00023 #ifdef _WIN32
00024 #include <winsock2.h>   // for fd_set
00025 #endif  // _WIN32
00026 
00027 #include "pqxx/except"
00028 #include "pqxx/util"
00029 
00030 
00031 /* Use of the libpqxx library starts here.
00032  *
00033  * Everything that can be done with a database through libpqxx must go through
00034  * a connection object derived from connection_base.
00035  */
00036 
00037 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00038  */
00039 
00040 namespace pqxx
00041 {
00042 class result;
00043 class transaction_base;
00044 class trigger;
00045 
00046 
00048 
00052 struct PQXX_LIBEXPORT noticer : PGSTD::unary_function<const char[], void>
00053 {
00054   virtual ~noticer() throw () {}  
00055   virtual void operator()(const char Msg[]) throw () =0;
00056 };
00057 
00058 
00060 
00078 class PQXX_LIBEXPORT connection_base
00079 {
00080 public:
00082 
00087   explicit connection_base(const PGSTD::string &ConnInfo);              //[t2]
00088 
00090 
00094   explicit connection_base(const char ConnInfo[]);                      //[t2]
00095 
00097   virtual ~connection_base() =0;                                        //[t1]
00098 
00100   void disconnect() throw ();                                           //[t2]
00101 
00103   bool is_open() const throw ();                                        //[t1]
00104 
00106 
00114   template<typename TRANSACTOR> 
00115   void perform(const TRANSACTOR &T, int Attempts=3);                    //[t4]
00116 
00117 #ifdef PQXX_BROKEN_MEMBER_TEMPLATE_DEFAULT_ARG
00118   template<typename TRANSACTOR> void perform(TRANSACTOR &T, int Attempts);
00119   template<typename TRANSACTOR>
00120   void perform(const TRANSACTOR &T) { perform(T, 3); }
00121 #endif
00122 
00123   // TODO: Define a default noticer (mainly to help out Windows users)
00125 
00134   PGSTD::auto_ptr<noticer> set_noticer(PGSTD::auto_ptr<noticer> N) 
00135     throw ();                                                           //[t14]
00136   noticer *get_noticer() const throw () { return m_Noticer.get(); }     //[t14]
00137 
00139   void process_notice(const char[]) throw ();                           //[t14]
00141   void process_notice(const PGSTD::string &msg) throw ();               //[t14]
00142 
00144   void trace(FILE *) throw ();                                          //[t3]
00145 
00147 
00151   int get_notifs();                                                     //[t4]
00152 
00153   // Miscellaneous query functions (probably not needed very often)
00154  
00156   const char *dbname()                                                  //[t1]
00157         { halfconnect(); return PQdb(m_Conn); }
00158 
00160   const char *username()                                                //[t1]
00161         { halfconnect(); return  PQuser(m_Conn); }
00162 
00164   const char *hostname()                                                //[t1]
00165         { halfconnect(); return PQhost(m_Conn); }
00166 
00168   const char *port()                                                    //[t1]
00169         { halfconnect(); return PQport(m_Conn); }
00170 
00172   const char *options() const throw ()                                  //[t1]
00173         { return m_ConnInfo.c_str(); }
00174 
00175 
00177 
00184   int backendpid() const throw ()                                       //[t1]
00185         { return m_Conn ? PQbackendPID(m_Conn) : 0; }
00186 
00188 
00198   void activate() { Connect(); }                                        //[t12]
00199 
00201 
00209   void deactivate();                                                    //[t12]
00210 
00212 
00218   void set_client_encoding(const PGSTD::string &Encoding)               //[t7]
00219         { set_variable("CLIENT_ENCODING", Encoding); }
00220 
00222 
00236   void set_variable(const PGSTD::string &Var, 
00237                     const PGSTD::string &Value);                        //[t60]
00238 
00240 
00246   PGSTD::string get_variable(const PGSTD::string &);                    //[t60]
00247 
00249 
00252   int await_notification();                                             //[t78]
00253 
00255 
00258   int await_notification(long seconds, long microseconds);              //[t79]
00259 
00260 #ifdef PQXX_DEPRECATED_HEADERS
00261 
00262   void Disconnect() throw () { disconnect(); }
00264   template<typename TRANSACTOR> void Perform(const TRANSACTOR &T, int A=3)
00265         { return perform(T,A); }
00267   PGSTD::auto_ptr<noticer> SetNoticer(PGSTD::auto_ptr<noticer> N)
00268         { return set_noticer(N); }
00270   noticer *GetNoticer() const throw () 
00271         { return get_noticer(); }
00273   void ProcessNotice(const char msg[]) throw () { return process_notice(msg); }
00275   void ProcessNotice(const PGSTD::string &msg) throw () 
00276         { return process_notice(msg); }
00278   void Trace(FILE *F) { trace(F); }
00280   void GetNotifs() { get_notifs(); }
00282   const char *DbName() { return dbname(); }
00284   const char *UserName() { return username(); }
00286   const char *HostName() { return hostname(); }
00288   const char *Port() { return port(); }
00290   const char *Options() const throw () { return options(); }
00292   int BackendPID() const { return backendpid(); }
00294   void Activate() { activate(); }
00296   void Deactivate() { deactivate(); }
00298   void SetClientEncoding(const PGSTD::string &E) { set_client_encoding(E); }
00300   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00301         { set_variable(Var, Val); }
00302 #endif
00303 
00304 
00305 protected:
00307   void Connect();
00308 
00310   virtual void startconnect() =0;
00311 
00313   virtual void completeconnect() =0;
00314 
00316   virtual void dropconnect() throw () {}
00317 
00319   PGconn *get_conn() const throw () { return m_Conn; }
00320 
00322   void set_conn(PGconn *C) throw () { m_Conn = C; }
00323 
00324   void wait_read() const;
00325   void wait_read(long seconds, long microseconds) const;
00326   void wait_write() const;
00327 
00328 private:
00329   void SetupState();
00330   void InternalSetTrace() throw ();
00331   int Status() const { return PQstatus(m_Conn); }
00332   const char *ErrMsg() const;
00333   void Reset();
00334   void close() throw ();
00335   void RestoreVars();
00336   void halfconnect();
00337   int set_fdmask() const;
00338   void clear_fdmask() throw ();
00339   void go_sync();
00340   void go_async();
00341   PGSTD::string RawGetVar(const PGSTD::string &);
00342   void process_notice_raw(const char msg[]) throw ();
00343 
00344 
00346   PGSTD::string m_ConnInfo;
00347 
00349   PGconn *m_Conn;
00351   internal::unique<transaction_base> m_Trans;
00352 
00354   PGSTD::auto_ptr<noticer> m_Noticer;
00356   FILE *m_Trace;
00357 
00358   typedef PGSTD::multimap<PGSTD::string, pqxx::trigger *> TriggerList;
00360   TriggerList m_Triggers;
00361 
00363   PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00364 
00365   mutable fd_set m_fdmask;
00366 
00367   friend class transaction_base;
00368   result Exec(const char[], int Retries);
00369   void RegisterTransaction(transaction_base *);
00370   void UnregisterTransaction(transaction_base *) throw ();
00371   void MakeEmpty(result &, ExecStatusType=PGRES_EMPTY_QUERY);
00372   bool ReadCopyLine(PGSTD::string &);
00373   bool WriteCopyLine(const PGSTD::string &, bool async=false);
00374   void EndCopyWrite();
00375   void start_exec(const PGSTD::string &);
00376   PGresult *get_result();
00377 
00378   void RawSetVar(const PGSTD::string &Var, const PGSTD::string &Value);
00379   void AddVariables(const PGSTD::map<PGSTD::string, PGSTD::string> &);
00380 
00381   friend class largeobject;
00382   PGconn *RawConnection() const { return m_Conn; }
00383 
00384   friend class trigger;
00385   void AddTrigger(trigger *);
00386   void RemoveTrigger(trigger *) throw ();
00387 
00388   // Not allowed:
00389   connection_base(const connection_base &);
00390   connection_base &operator=(const connection_base &);
00391 };
00392 
00393 
00394 }
00395 
00396 
00397 // Put this here so on Windows, any noticer will be deleted in caller's context
00398 inline pqxx::connection_base::~connection_base()
00399 {
00400   close();
00401 }
00402 
00403 

Generated on Tue Apr 27 01:35:45 2004 for libpqxx by doxygen 1.3.6-20040222