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

util.hxx

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/util.hxx
00005  *
00006  *   DESCRIPTION
00007  *      Various utility definitions for libpqxx
00008  *      DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/util instead.
00009  *
00010  * Copyright (c) 2001-2004, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  * See COPYING for copyright license.  If you did not receive a file called
00013  * COPYING with this source code, please notify the distributor of this mistake,
00014  * or contact the author.
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 #include "pqxx/libcompiler.h"
00019 #include "pqxx/config-public-libpq.h"
00020 
00021 #include <cstdio>
00022 #include <cctype>
00023 #include <sstream>
00024 #include <stdexcept>
00025 #include <string>
00026 #include <typeinfo>
00027 #include <vector>
00028 
00029 
00031 namespace pqxx
00032 {
00033 }
00034 
00035 
00036 #ifdef PQXX_PQ_IN_NAMESPACE
00037 // We want libpq in the pqxx::internal::pq namespace
00038 
00039 namespace pqxx
00040 {
00041 namespace internal
00042 {
00043 namespace pq
00044 {
00045 #define PQXXPQ pqxx::internal::pq
00046 extern "C"
00047 {
00048 #include "libpq-fe.h"
00049 }
00050 } // namespace pq
00051 } // namespace internal
00052 } // namespace pqxx
00053 
00054 #else   // PQXX_PQ_IN_NAMESPACE
00055 // We want libpq in the global namespace, with duplicates in pqxx::internal::pq
00056 
00057 extern "C"
00058 {
00059 #include "libpq-fe.h"
00060 }
00061 
00062 namespace pqxx
00063 {
00064 namespace internal
00065 {
00066 namespace pq
00067 {
00068 #define PQXXPQ
00069 typedef PQXXPQ::PGconn PGconn;
00070 typedef PQXXPQ::PGresult PGresult;
00071 
00072 } // namespace pq
00073 } // namespace internal
00074 } // namespace pqxx
00075 
00076 #endif  // PQXX_PQ_IN_NAMESPACE
00077 
00078 
00079 namespace pqxx
00080 {
00082 typedef PQXXPQ::Oid oid;
00083 
00085 const oid oid_none = 0;
00086 
00087 
00089 
00102 template<typename T> void error_unsupported_type_in_string_conversion(T);
00103 
00104 
00106 
00112 template<typename T> PGSTD::string error_ambiguous_string_conversion(T);
00113 
00114 
00115 
00116 // TODO: Implement date conversions
00117 
00119 
00128 template<typename T> void from_string(const char Str[], T &Obj);
00129 
00130 template<> void from_string(const char Str[], long &);                  //[t45]
00131 template<> void from_string(const char Str[], unsigned long &);         //[t45]
00132 template<> void from_string(const char Str[], int &);                   //[t45]
00133 template<> void from_string(const char Str[], unsigned int &);          //[t45]
00134 template<> void from_string(const char Str[], short &);                 //[t45]
00135 template<> void from_string(const char Str[], unsigned short &);        //[t45]
00136 template<> void from_string(const char Str[], float &);                 //[t46]
00137 template<> void from_string(const char Str[], double &);                //[t46]
00138 template<> void from_string(const char Str[], long double &);           //[t46]
00139 template<> void from_string(const char Str[], bool &);                  //[t76]
00140 
00141 template<> inline void from_string(const char Str[],PGSTD::string &Obj) //[t46]
00142         { Obj = Str; }
00143 
00144 template<> 
00145   inline void from_string(const char Str[], PGSTD::stringstream &Obj)   //[t0]
00146         { Obj.clear(); Obj << Str; }
00147 
00148 template<typename T> 
00149   inline void from_string(const PGSTD::string &Str, T &Obj)             //[t45]
00150         { from_string(Str.c_str(), Obj); }
00151 
00152 template<typename T>
00153   inline void from_string(const PGSTD::stringstream &Str, T &Obj)       //[t0]
00154         { from_string(Str.str(), Obj); }
00155 
00156 template<> inline void 
00157 from_string(const PGSTD::string &Str, PGSTD::string &Obj)               //[t46]
00158         { Obj = Str; }
00159 
00160 template<> inline void
00161 from_string(const PGSTD::string &, const char &Obj)
00162         { error_ambiguous_string_conversion(Obj); }
00163 template<> inline void
00164 from_string(const PGSTD::string &, const signed char &Obj)
00165         { error_ambiguous_string_conversion(Obj); }
00166 template<> inline void
00167 from_string(const PGSTD::string &, const unsigned char &Obj)
00168         { error_ambiguous_string_conversion(Obj); }
00169 
00170 
00172 
00176 template<typename T> PGSTD::string to_string(const T &);
00177 
00178 template<> PGSTD::string to_string(const short &);                      //[t76]
00179 template<> PGSTD::string to_string(const unsigned short &);             //[t76]
00180 template<> PGSTD::string to_string(const int &);                        //[t10]
00181 template<> PGSTD::string to_string(const unsigned int &);               //[t13]
00182 template<> PGSTD::string to_string(const long &);                       //[t18]
00183 template<> PGSTD::string to_string(const unsigned long &);              //[t20]
00184 template<> PGSTD::string to_string(const float &);                      //[t74]
00185 template<> PGSTD::string to_string(const double &);                     //[t74]
00186 template<> PGSTD::string to_string(const long double &);                //[t74]
00187 template<> PGSTD::string to_string(const bool &);                       //[t76]
00188 
00189 inline PGSTD::string to_string(const char Obj[])                        //[t14]
00190         { return PGSTD::string(Obj); }
00191 
00192 inline PGSTD::string to_string(const PGSTD::stringstream &Obj)          //[t0]
00193         { return Obj.str(); }
00194 
00195 inline PGSTD::string to_string(const PGSTD::string &Obj) {return Obj;}  //[t21]
00196 
00197 template<> PGSTD::string to_string(const char &);                       //[t21]
00198 
00199 
00200 template<> inline PGSTD::string to_string(const signed char &Obj)
00201         { return error_ambiguous_string_conversion(Obj); }
00202 template<> inline PGSTD::string to_string(const unsigned char &Obj)
00203         { return error_ambiguous_string_conversion(Obj); }
00204 
00205 
00207 
00224 template<typename T=PGSTD::string, typename CONT=PGSTD::vector<T> >
00225 class items : public CONT
00226 {
00227 public:
00229   items() : CONT() {}                                                   //[t80]
00231   explicit items(const T &t) : CONT() { push_back(t); }                 //[t0]
00232   items(const T &t1, const T &t2) : CONT()                              //[t80]
00233         { push_back(t1); push_back(t2); }
00234   items(const T &t1, const T &t2, const T &t3) : CONT()                 //[t0]
00235         { push_back(t1); push_back(t2); push_back(t3); }
00236   items(const T &t1, const T &t2, const T &t3, const T &t4) : CONT()    //[t0]
00237         { push_back(t1); push_back(t2); push_back(t3); push_back(t4); }
00238   items(const T&t1,const T&t2,const T&t3,const T&t4,const T&t5):CONT()  //[t0]
00239         {push_back(t1);push_back(t2);push_back(t3);push_back(t4);push_back(t5);}
00241   items(const CONT &c) : CONT(c) {}                                     //[t0]
00242 
00244   items &operator()(const T &t)                                         //[t80]
00245   {
00246     push_back(t);
00247     return *this;
00248   }
00249 };
00250 
00251 
00252 // TODO: Generalize--add transformation functor
00254 template<typename ITER> inline
00255 PGSTD::string separated_list(const PGSTD::string &sep,
00256     ITER begin,
00257     ITER end)                                                           //[t8]
00258 {
00259   PGSTD::string result;
00260   if (begin != end)
00261   {
00262     result = to_string(*begin);
00263     for (++begin; begin != end; ++begin)
00264     {
00265       result += sep;
00266       result += to_string(*begin);
00267     }
00268   }
00269   return result;
00270 }
00271 
00273 template<typename CONTAINER> inline
00274 PGSTD::string separated_list(const PGSTD::string &sep,
00275     const CONTAINER &c)                                                 //[t10]
00276 {
00277   return separated_list(sep, c.begin(), c.end());
00278 }
00279 
00280 
00282 
00291 namespace internal
00292 {
00294 
00302 template<typename T> inline const char *FmtString(T t)
00303 {
00304   error_unsupported_type_in_string_conversion(t);
00305   return 0;
00306 }
00307 
00308 template<> inline const char *FmtString(short)         { return "%hd"; }
00309 template<> inline const char *FmtString(unsigned short){ return "%hu"; }
00310 template<> inline const char *FmtString(int)           { return  "%i"; }
00311 template<> inline const char *FmtString(long)          { return "%li"; }
00312 template<> inline const char *FmtString(unsigned)      { return  "%u"; }
00313 template<> inline const char *FmtString(unsigned long) { return "%lu"; }
00314 template<> inline const char *FmtString(float)         { return  "%f"; }
00315 template<> inline const char *FmtString(double)        { return "%lf"; }
00316 template<> inline const char *FmtString(long double)   { return "%Lf"; }
00317 template<> inline const char *FmtString(char)          { return  "%c"; }
00318 template<> inline const char *FmtString(unsigned char) { return  "%c"; }
00319 
00320 } // namespace internal
00321 
00323 
00331 template<typename T> inline PGSTD::string ToString(const T &Obj)
00332 {
00333   // TODO: Find a decent way to determine max string length at compile time!
00334   char Buf[500];
00335   sprintf(Buf, internal::FmtString(Obj), Obj);
00336   return PGSTD::string(Buf);
00337 }
00338 
00339 
00340 template<> inline PGSTD::string ToString(const PGSTD::string &Obj) {return Obj;}
00341 template<> inline PGSTD::string ToString(const char *const &Obj) { return Obj; }
00342 template<> inline PGSTD::string ToString(char *const &Obj) { return Obj; }
00343 
00344 template<> inline PGSTD::string ToString(const unsigned char *const &Obj)
00345 {
00346   return reinterpret_cast<const char *>(Obj);
00347 }
00348 
00349 template<> inline PGSTD::string ToString(const bool &Obj) 
00350 { 
00351   return ToString(unsigned(Obj));
00352 }
00353 
00354 template<> inline PGSTD::string ToString(const short &Obj)
00355 {
00356   return ToString(int(Obj));
00357 }
00358 
00359 template<> inline PGSTD::string ToString(const unsigned short &Obj)
00360 {
00361   return ToString(unsigned(Obj));
00362 }
00363 
00364 
00366 
00374 template<typename T> inline void FromString(const char Str[], T &Obj)
00375 {
00376   if (!Str) throw PGSTD::runtime_error("Attempt to convert NULL string to " +
00377                                      PGSTD::string(typeid(T).name()));
00378 
00379   if (sscanf(Str, internal::FmtString(Obj), &Obj) != 1)
00380     throw PGSTD::runtime_error("Cannot convert value '" + 
00381                              PGSTD::string(Str) + 
00382                              "' to " + typeid(T).name());
00383 }
00384 
00385 
00386 namespace internal
00387 {
00389 
00391 void PQXX_LIBEXPORT FromString_string(const char Str[], PGSTD::string &Obj);
00392 
00394 
00396 void PQXX_LIBEXPORT FromString_ucharptr(const char Str[], 
00397         const unsigned char *&Obj);
00398 
00400 PGSTD::string PQXX_LIBEXPORT Quote_string(const PGSTD::string &Obj, 
00401         bool EmptyIsNull);
00402 
00404 PGSTD::string PQXX_LIBEXPORT Quote_charptr(const char Obj[], bool EmptyIsNull);
00405 } // namespace internal
00406 
00407 
00408 template<> inline void FromString(const char Str[], PGSTD::string &Obj)
00409 {
00410   internal::FromString_string(Str, Obj);
00411 }
00412 
00413 template<> inline void FromString(const char Str[], const char *&Obj)
00414 {
00415   if (!Str) throw PGSTD::runtime_error("Attempt to read NULL string");
00416   Obj = Str;
00417 }
00418 
00419 template<> inline void FromString(const char Str[], const unsigned char *&Obj)
00420 {
00421   internal::FromString_ucharptr(Str, Obj);
00422 }
00423 
00424 template<> inline void FromString(const char Str[], bool &Obj)
00425 {
00426   from_string(Str, Obj);
00427 }
00428 
00429 
00431 
00440 PGSTD::string sqlesc(const char str[]);                                 //[t0]
00441 
00443 
00453 PGSTD::string sqlesc(const char str[], size_t maxlen);                  //[t0]
00454 
00456 
00462 PGSTD::string sqlesc(const PGSTD::string &);                            //[t0]
00463 
00464 
00466 
00470 template<typename T> PGSTD::string Quote(const T &Obj, bool EmptyIsNull);
00471 
00472 
00474 
00476 template<> 
00477 inline PGSTD::string Quote(const PGSTD::string &Obj, bool EmptyIsNull)
00478 {
00479   return internal::Quote_string(Obj, EmptyIsNull);
00480 }
00481 
00483 
00485 template<> inline PGSTD::string Quote(const char *const & Obj, bool EmptyIsNull)
00486 {
00487   return internal::Quote_charptr(Obj, EmptyIsNull);
00488 }
00489 
00490 
00492 
00497 template<int LEN> inline PGSTD::string Quote(const char (&Obj)[LEN],
00498                                              bool EmptyIsNull)
00499 {
00500   return internal::Quote_charptr(Obj, EmptyIsNull);
00501 }
00502 
00503 
00504 template<typename T> inline PGSTD::string Quote(const T &Obj, bool EmptyIsNull)
00505 {
00506   return Quote(ToString(Obj), EmptyIsNull);
00507 }
00508 
00509 
00511 
00514 template<typename T> inline PGSTD::string Quote(T Obj)
00515 {
00516   return Quote(Obj, false);
00517 }
00518 
00519 
00520 namespace internal
00521 {
00522 void freepqmem(void *);
00523 void freenotif(PQXXPQ::PGnotify *);
00524 
00526 
00532 template<typename T> class PQAlloc
00533 {
00534   T *m_Obj;
00535 public:
00536   typedef T content_type;
00537 
00538   PQAlloc() : m_Obj(0) {}
00539 
00541   explicit PQAlloc(T *obj) : m_Obj(obj) {}
00542 
00543   ~PQAlloc() throw () { close(); }
00544 
00546 
00548   PQAlloc &operator=(T *obj) throw ()
00549   { 
00550     if (obj != m_Obj)
00551     {
00552       close();
00553       m_Obj = obj;
00554     }
00555     return *this;
00556   }
00557 
00559   operator bool() const throw () { return m_Obj != 0; }
00560 
00562   bool operator!() const throw () { return !m_Obj; }
00563 
00565 
00567   T *operator->() const throw (PGSTD::logic_error)
00568   {
00569     if (!m_Obj) throw PGSTD::logic_error("Null pointer dereferenced");
00570     return m_Obj;
00571   }
00572 
00574 
00576   T &operator*() const throw (PGSTD::logic_error) { return *operator->(); }
00577 
00579 
00581   T *c_ptr() const throw () { return m_Obj; }
00582 
00584   void close() throw () { if (m_Obj) freemem(); m_Obj = 0; }
00585 
00586 private:
00587   void freemem() throw ()
00588   {
00589     freepqmem(m_Obj);
00590   }
00591 
00592   PQAlloc(const PQAlloc &);             // Not allowed
00593   PQAlloc &operator=(const PQAlloc &);  // Not allowed
00594 };
00595 
00596 
00598 template<> inline void PQAlloc<PQXXPQ::PGnotify>::freemem() throw ()
00599 {
00600   freenotif(m_Obj);
00601 }
00602 
00603 
00604 class PQXX_LIBEXPORT namedclass
00605 {
00606 public:
00607   namedclass(const PGSTD::string &Name, const PGSTD::string &Classname) :
00608     m_Name(Name),
00609     m_Classname(Classname)
00610   {
00611   }
00612 
00613   const PGSTD::string &name() const throw () { return m_Name; }         //[t1]
00614   const PGSTD::string &classname() const throw () {return m_Classname;} //[t73]
00615   PGSTD::string description() const;
00616 
00617 private:
00618   PGSTD::string m_Name, m_Classname;
00619 };
00620 
00621 
00622 void CheckUniqueRegistration(const namedclass *New, const namedclass *Old);
00623 void CheckUniqueUnregistration(const namedclass *New, const namedclass *Old);
00624 
00625 
00627 
00630 template<typename GUEST>
00631 class unique
00632 {
00633 public:
00634   unique() : m_Guest(0) {}
00635 
00636   GUEST *get() const throw () { return m_Guest; }
00637 
00638   void Register(GUEST *G)
00639   {
00640     CheckUniqueRegistration(G, m_Guest);
00641     m_Guest = G;
00642   }
00643 
00644   void Unregister(GUEST *G)
00645   {
00646     CheckUniqueUnregistration(G, m_Guest);
00647     m_Guest = 0;
00648   }
00649 
00650 private:
00651   GUEST *m_Guest;
00652 
00654   unique(const unique &);
00656   unique &operator=(const unique &);
00657 };
00658 
00660 void sleep_seconds(int);
00661 
00662 } // namespace internal
00663 } // namespace pqxx
00664 

Generated on Thu Oct 14 20:43:29 2004 for libpqxx by  doxygen 1.3.9