00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <map>
00021 #include <memory>
00022
00023 #include "pqxx/except"
00024 #include "pqxx/util"
00025
00026
00027
00028
00029
00030
00031
00032
00033
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);
00090
00092
00096 explicit connection_base(const char ConnInfo[]);
00097
00099 virtual ~connection_base() =0;
00100
00102 void disconnect() throw ();
00103
00105 bool is_open() const;
00106
00108
00116 template<typename TRANSACTOR>
00117 void perform(const TRANSACTOR &T, int Attempts=3);
00118
00119
00121
00130 PGSTD::auto_ptr<noticer> set_noticer(PGSTD::auto_ptr<noticer> N);
00131 noticer *get_noticer() const throw () { return m_Noticer.get(); }
00132
00134 void process_notice(const char[]) throw ();
00136 void process_notice(const PGSTD::string &msg) throw ()
00137 { process_notice(msg.c_str()); }
00138
00140 void trace(FILE *);
00141
00143 void get_notifs();
00144
00145
00146
00148 const char *dbname()
00149 { halfconnect(); return PQdb(m_Conn); }
00150
00152 const char *username()
00153 { halfconnect(); return PQuser(m_Conn); }
00154
00156 const char *hostname()
00157 { halfconnect(); return PQhost(m_Conn); }
00158
00160 const char *port()
00161 { halfconnect(); return PQport(m_Conn); }
00162
00164 const char *options() const throw ()
00165 { return m_ConnInfo.c_str(); }
00166
00167
00169
00176 int backendpid() const
00177 { return m_Conn ? PQbackendPID(m_Conn) : 0; }
00178
00180
00190 void activate() { Connect(); }
00191
00193
00201 void deactivate();
00202
00204
00210 void set_client_encoding(const PGSTD::string &Encoding)
00211 { set_variable("CLIENT_ENCODING", Encoding); }
00212
00214
00225 void set_variable(const PGSTD::string &Var,
00226 const PGSTD::string &Value);
00227
00228
00229 #ifdef PQXX_DEPRECATED_HEADERS
00230
00231 void Disconnect() throw () { disconnect(); }
00233 template<typename TRANSACTOR> void Perform(const TRANSACTOR &T, int A=3)
00234 { return perform(T,A); }
00236 PGSTD::auto_ptr<noticer> SetNoticer(PGSTD::auto_ptr<noticer> N)
00237 { return set_noticer(N); }
00239 noticer *GetNoticer() const throw ()
00240 { return get_noticer(); }
00242 void ProcessNotice(const char msg[]) throw () { return process_notice(msg); }
00244 void ProcessNotice(const PGSTD::string &msg) throw ()
00245 { return process_notice(msg); }
00247 void Trace(FILE *F) { trace(F); }
00249 void GetNotifs() { get_notifs(); }
00251 const char *DbName() { return dbname(); }
00253 const char *UserName() { return username(); }
00255 const char *HostName() { return hostname(); }
00257 const char *Port() { return port(); }
00259 const char *Options() const throw () { return options(); }
00261 int BackendPID() const { return backendpid(); }
00263 void Activate() { activate(); }
00265 void Deactivate() { deactivate(); }
00267 void SetClientEncoding(const PGSTD::string &E) { set_client_encoding(E); }
00269 void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00270 { set_variable(Var, Val); }
00271 #endif
00272
00273
00274 protected:
00276 void Connect();
00277
00279 virtual void startconnect() =0;
00280
00282 virtual void completeconnect() =0;
00283
00285 virtual void dropconnect() {}
00286
00288 PGconn *get_conn() const throw () { return m_Conn; }
00289
00291 void set_conn(PGconn *C) throw () { m_Conn = C; }
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 void halfconnect();
00302
00304 PGSTD::string m_ConnInfo;
00305
00307 PGconn *m_Conn;
00309 unique<transaction_base> m_Trans;
00310
00312 PGSTD::auto_ptr<noticer> m_Noticer;
00314 FILE *m_Trace;
00315
00316 typedef PGSTD::multimap<PGSTD::string, pqxx::trigger *> TriggerList;
00318 TriggerList m_Triggers;
00319
00321 PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00322
00323 friend class transaction_base;
00324 result Exec(const char[], int Retries=3, const char OnReconnect[]=0);
00325 void RegisterTransaction(transaction_base *);
00326 void UnregisterTransaction(transaction_base *) throw ();
00327 void MakeEmpty(result &, ExecStatusType=PGRES_EMPTY_QUERY);
00328 void BeginCopyRead(const PGSTD::string &Table);
00329 bool ReadCopyLine(PGSTD::string &);
00330 void BeginCopyWrite(const PGSTD::string &Table);
00331 void WriteCopyLine(const PGSTD::string &);
00332 void EndCopyWrite() throw ();
00333
00334 void EndCopy() throw ()
00335 #ifdef PQXX_HAVE_PQPUTCOPY
00336 {}
00337 #else
00338 ;
00339 #endif
00340
00341 void RawSetVar(const PGSTD::string &Var, const PGSTD::string &Value);
00342 void AddVariables(const PGSTD::map<PGSTD::string, PGSTD::string> &);
00343
00344 friend class largeobject;
00345 PGconn *RawConnection() const { return m_Conn; }
00346
00347 friend class trigger;
00348 void AddTrigger(trigger *);
00349 void RemoveTrigger(trigger *) throw ();
00350
00351
00352 connection_base(const connection_base &);
00353 connection_base &operator=(const connection_base &);
00354 };
00355
00356
00357 }
00358
00359
00360
00361 inline pqxx::connection_base::~connection_base()
00362 {
00363 close();
00364 }
00365
00366