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

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-2003, 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 #include "pqxx/except"
00024 #include "pqxx/util"
00025 
00026 
00027 /* Use of the libpqxx library starts here.
00028  *
00029  * Everything that can be done with a database through libpqxx must go through
00030  * a connection object derived from connection_base.
00031  */
00032 
00033 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00034  */
00035 
00036 namespace pqxx
00037 {
00038 class result;
00039 class transaction_base;
00040 class trigger;
00041 
00043 
00047 struct PQXX_LIBEXPORT noticer : PGSTD::unary_function<const char[], void>
00048 {
00049   virtual ~noticer() {}  
00050   virtual void operator()(const char Msg[]) throw () =0;
00051 };
00052 
00053 
00055 template<> inline PGSTD::string Classname(const transaction_base *) 
00056 { 
00057   return "transaction_base"; 
00058 }
00059 
00060 
00062 
00080 class PQXX_LIBEXPORT connection_base
00081 {
00082 public:
00084 
00089   explicit connection_base(const PGSTD::string &ConnInfo);              //[t2]
00090 
00092 
00096   explicit connection_base(const char ConnInfo[]);                      //[t2]
00097 
00099   virtual ~connection_base() =0;                                        //[t1]
00100 
00101 #ifdef PQXX_DEPRECATED_HEADERS
00102 
00103   void Disconnect() throw () { disconnect(); }
00104 #endif
00105 
00107   void disconnect() throw ();                                           //[t2]
00108 
00110   bool is_open() const;                                                 //[t1]
00111 
00112 #ifdef PQXX_DEPRECATED_HEADERS
00113 
00114   template<typename TRANSACTOR> void Perform(const TRANSACTOR &T, int A=3)
00115         { return perform(T,A); }
00116 #endif
00117 
00119 
00127   template<typename TRANSACTOR> 
00128   void perform(const TRANSACTOR &T, int Attempts=3);                    //[t4]
00129 
00130 #ifdef PQXX_DEPRECATED_HEADERS
00131 
00132   PGSTD::auto_ptr<noticer> SetNoticer(PGSTD::auto_ptr<noticer> N)
00133         { return set_noticer(N); }
00135   noticer *GetNoticer() const throw () 
00136         { return get_noticer(); }
00138   void ProcessNotice(const char msg[]) throw () { return process_notice(msg); }
00140   void ProcessNotice(const PGSTD::string &msg) throw () 
00141         { return process_notice(msg); }
00142 #endif
00143 
00144   // TODO: Define a default noticer (mainly to help out Windows users)
00146 
00155   PGSTD::auto_ptr<noticer> set_noticer(PGSTD::auto_ptr<noticer> N);     //[t14]
00156   noticer *get_noticer() const throw () { return m_Noticer.get(); }     //[t14]
00157 
00159   void process_notice(const char[]) throw ();                           //[t14]
00161   void process_notice(const PGSTD::string &msg) throw ()                //[t14]
00162         { process_notice(msg.c_str()); }
00163 
00164 #ifdef PQXX_DEPRECATED_HEADERS
00165 
00166   void Trace(FILE *F) { trace(F); }
00167 
00169   void GetNotifs() { get_notifs(); }
00170 #endif
00171 
00173   void trace(FILE *);                                                   //[t3]
00174 
00176   void get_notifs();                                                    //[t4]
00177 
00178   // Miscellaneous query functions (probably not needed very often)
00179  
00180 #ifdef PQXX_DEPRECATED_HEADERS
00181 
00182   const char *DbName() { return dbname(); }
00184   const char *UserName() { return username(); }
00186   const char *HostName() { return hostname(); }
00188   const char *Port() { return port(); }
00190   const char *Options() const throw () { return options(); }
00192   int BackendPID() const { return backendpid(); }
00193 #endif
00194 
00196   const char *dbname()                                                  //[t1]
00197         { activate(); return PQdb(m_Conn); }
00198 
00200   const char *username()                                                //[t1]
00201         { activate(); return  PQuser(m_Conn); }
00202 
00204   const char *hostname()                                                //[t1]
00205         { activate(); return PQhost(m_Conn); }
00206 
00208   const char *port()                                                    //[t1]
00209         { activate(); return PQport(m_Conn); }
00210 
00212   const char *options() const throw ()                                  //[t1]
00213         { return m_ConnInfo.c_str(); }
00214 
00215 
00217 
00224   int backendpid() const                                                //[t1]
00225         { return m_Conn ? PQbackendPID(m_Conn) : 0; }
00226 
00227 
00228 #ifdef PQXX_DEPRECATED_HEADERS
00229 
00230   void Activate() { activate(); }
00232   void Deactivate() { deactivate(); }
00234   void SetClientEncoding(const PGSTD::string &E) { set_client_encoding(E); }
00236   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00237         { set_variable(Var, Val); }
00238 #endif
00239 
00241 
00251   void activate() { if (!m_Conn) Connect(); }                           //[t12]
00252 
00254 
00262   void deactivate();                                                    //[t12]
00263 
00265 
00271   void set_client_encoding(const PGSTD::string &Encoding)               //[t7]
00272         { set_variable("CLIENT_ENCODING", Encoding); }
00273 
00275 
00286   void set_variable(const PGSTD::string &Var, 
00287                     const PGSTD::string &Value);                        //[t60]
00288 
00289 protected:
00291   void Connect();
00292 
00293 private:
00294   void SetupState();
00295   void InternalSetTrace();
00296   int Status() const { return PQstatus(m_Conn); }
00297   const char *ErrMsg() const;
00298   void Reset(const char OnReconnect[]=0);
00299   void close() throw ();
00300   void RestoreVars();
00301 
00303   PGSTD::string m_ConnInfo;
00305   PGconn *m_Conn;
00307   unique<transaction_base> m_Trans;
00308 
00310   PGSTD::auto_ptr<noticer> m_Noticer;
00312   FILE *m_Trace;
00313 
00314   typedef PGSTD::multimap<PGSTD::string, pqxx::trigger *> TriggerList;
00316   TriggerList m_Triggers;
00317 
00319   PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00320 
00321   friend class transaction_base;
00322   result Exec(const char[], int Retries=3, const char OnReconnect[]=0);
00323   void RegisterTransaction(transaction_base *);
00324   void UnregisterTransaction(transaction_base *) throw ();
00325   void MakeEmpty(result &, ExecStatusType=PGRES_EMPTY_QUERY);
00326   void BeginCopyRead(const PGSTD::string &Table);
00327   bool ReadCopyLine(PGSTD::string &);
00328   void BeginCopyWrite(const PGSTD::string &Table);
00329   void WriteCopyLine(const PGSTD::string &);
00330   void EndCopy() throw ();
00331   void RawSetVar(const PGSTD::string &Var, const PGSTD::string &Value);
00332   void AddVariables(const PGSTD::map<PGSTD::string, PGSTD::string> &);
00333 
00334   friend class largeobject;
00335   PGconn *RawConnection() const { return m_Conn; }
00336 
00337   friend class trigger;
00338   void AddTrigger(trigger *);
00339   void RemoveTrigger(trigger *) throw ();
00340 
00341   // Not allowed:
00342   connection_base(const connection_base &);
00343   connection_base &operator=(const connection_base &);
00344 };
00345 
00346 
00347 }
00348 
00349 
00350 // Put this here so on Windows, any noticer will be deleted in caller's context
00351 inline pqxx::connection_base::~connection_base()
00352 {
00353   close();
00354 }
00355 
00356 

Generated on Sun Oct 5 05:38:07 2003 for libpqxx by doxygen 1.3.2