00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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 }
00051 }
00052 }
00053
00054 #else // PQXX_PQ_IN_NAMESPACE
00055
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 }
00073 }
00074 }
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
00117
00119
00128 template<typename T> void from_string(const char Str[], T &Obj);
00129
00130 template<> void from_string(const char Str[], long &);
00131 template<> void from_string(const char Str[], unsigned long &);
00132 template<> void from_string(const char Str[], int &);
00133 template<> void from_string(const char Str[], unsigned int &);
00134 template<> void from_string(const char Str[], short &);
00135 template<> void from_string(const char Str[], unsigned short &);
00136 template<> void from_string(const char Str[], float &);
00137 template<> void from_string(const char Str[], double &);
00138 template<> void from_string(const char Str[], long double &);
00139 template<> void from_string(const char Str[], bool &);
00140
00141 template<> inline void from_string(const char Str[],PGSTD::string &Obj)
00142 { Obj = Str; }
00143
00144 template<>
00145 inline void from_string(const char Str[], PGSTD::stringstream &Obj)
00146 { Obj.clear(); Obj << Str; }
00147
00148 template<typename T>
00149 inline void from_string(const PGSTD::string &Str, T &Obj)
00150 { from_string(Str.c_str(), Obj); }
00151
00152 template<typename T>
00153 inline void from_string(const PGSTD::stringstream &Str, T &Obj)
00154 { from_string(Str.str(), Obj); }
00155
00156 template<> inline void
00157 from_string(const PGSTD::string &Str, PGSTD::string &Obj)
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 &);
00179 template<> PGSTD::string to_string(const unsigned short &);
00180 template<> PGSTD::string to_string(const int &);
00181 template<> PGSTD::string to_string(const unsigned int &);
00182 template<> PGSTD::string to_string(const long &);
00183 template<> PGSTD::string to_string(const unsigned long &);
00184 template<> PGSTD::string to_string(const float &);
00185 template<> PGSTD::string to_string(const double &);
00186 template<> PGSTD::string to_string(const long double &);
00187 template<> PGSTD::string to_string(const bool &);
00188
00189 inline PGSTD::string to_string(const char Obj[])
00190 { return PGSTD::string(Obj); }
00191
00192 inline PGSTD::string to_string(const PGSTD::stringstream &Obj)
00193 { return Obj.str(); }
00194
00195 inline PGSTD::string to_string(const PGSTD::string &Obj) {return Obj;}
00196
00197 template<> PGSTD::string to_string(const char &);
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() {}
00231 explicit items(const T &t) : CONT() { push_back(t); }
00232 items(const T &t1, const T &t2) : CONT()
00233 { push_back(t1); push_back(t2); }
00234 items(const T &t1, const T &t2, const T &t3) : CONT()
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()
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()
00239 {push_back(t1);push_back(t2);push_back(t3);push_back(t4);push_back(t5);}
00241 items(const CONT &c) : CONT(c) {}
00242
00244 items &operator()(const T &t)
00245 {
00246 push_back(t);
00247 return *this;
00248 }
00249 };
00250
00251
00252
00254 template<typename ITER> inline
00255 PGSTD::string separated_list(const PGSTD::string &sep,
00256 ITER begin,
00257 ITER end)
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)
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 }
00321
00323
00331 template<typename T> inline PGSTD::string ToString(const T &Obj)
00332 {
00333
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 }
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[]);
00441
00443
00453 PGSTD::string sqlesc(const char str[], size_t maxlen);
00454
00456
00462 PGSTD::string sqlesc(const PGSTD::string &);
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 &);
00593 PQAlloc &operator=(const PQAlloc &);
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; }
00614 const PGSTD::string &classname() const throw () {return m_Classname;}
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 }
00663 }
00664