libpqxx
connection_base.hxx
1 
13 #ifndef PQXX_H_CONNECTION_BASE
14 #define PQXX_H_CONNECTION_BASE
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 
19 #include <bitset>
20 #include <list>
21 #include <map>
22 #include <memory>
23 
24 #include "pqxx/errorhandler.hxx"
25 #include "pqxx/except.hxx"
26 #include "pqxx/prepared_statement.hxx"
27 #include "pqxx/strconv.hxx"
28 #include "pqxx/util.hxx"
29 #include "pqxx/version.hxx"
30 
31 
32 /* Use of the libpqxx library starts here.
33  *
34  * Everything that can be done with a database through libpqxx must go through
35  * a connection object derived from connection_base.
36  */
37 
38 /* Methods tested in eg. self-test program test1 are marked with "//[t01]"
39  */
40 
41 namespace pqxx
42 {
43 namespace internal
44 {
46 class sql_cursor;
47 
49 {
50 public:
52 
53  void add(int n) noexcept { m_counter += n; }
54  void clear() noexcept { m_counter = 0; }
55  int get() const noexcept { return m_counter; }
56 
57 private:
58  int m_counter = 0;
59 };
60 
61 }
62 
63 
65 
81 std::string PQXX_LIBEXPORT encrypt_password( //[t00]
82  const std::string &user,
83  const std::string &password);
84 
85 
86 namespace internal
87 {
88 namespace gate
89 {
90 class connection_dbtransaction;
91 class connection_errorhandler;
92 class connection_largeobject;
93 class connection_notification_receiver;
94 class connection_parameterized_invocation;
95 class connection_pipeline;
96 class connection_prepare_invocation;
97 class connection_reactivation_avoidance_exemption;
98 class connection_sql_cursor;
99 class connection_transaction;
100 class const_connection_largeobject;
101 } // namespace pqxx::internal::gate
102 } // namespace pqxx::internal
103 
104 
106 
139 class PQXX_LIBEXPORT connection_base
140 {
141 public:
143  void disconnect() noexcept; //[t02]
144 
146 
150  bool PQXX_PURE is_open() const noexcept; //[t01]
151 
162 
173  void activate(); //[t12]
174 
176 
184  void deactivate(); //[t12]
185 
187 
231  void inhibit_reactivation(bool inhibit) //[t86]
232  { m_inhibit_reactivation=inhibit; }
233 
235 
240  void simulate_failure(); //[t94]
242 
244  void process_notice(const char[]) noexcept; //[t14]
246  void process_notice(const std::string &) noexcept; //[t14]
247 
249  void trace(std::FILE *) noexcept; //[t03]
250 
259 
263  const char *dbname(); //[t01]
264 
266 
269  const char *username(); //[t01]
270 
272 
275  const char *hostname(); //[t01]
276 
278 
281  const char *port(); //[t01]
282 
284 
293  int PQXX_PURE backendpid() const noexcept; //[t01]
294 
296 
311  int PQXX_PURE sock() const noexcept; //[t87]
312 
321 
323 
327  {
330  };
331 
332 
334 
350  bool supports(capability c) const noexcept //[t88]
351  { return m_caps.test(c); }
352 
354 
362  int PQXX_PURE protocol_version() const noexcept; //[t01]
363 
365 
377  int PQXX_PURE server_version() const noexcept; //[t01]
379 
381 
387  void set_client_encoding(const std::string &Encoding) //[t07]
388  { set_variable("CLIENT_ENCODING", Encoding); }
389 
391 
407  void set_variable( //[t60]
408  const std::string &Var,
409  const std::string &Value);
410 
412 
419  std::string get_variable(const std::string &); //[t60]
421 
422 
427 
440  int get_notifs(); //[t04]
441 
442 
444 
450  int await_notification(); //[t78]
451 
453 
459  int await_notification(long seconds, long microseconds); //[t79]
461 
462 
487 
521  void prepare(const std::string &name, const std::string &definition);
522 
524 
530  void prepare(const std::string &definition);
531 
533  void unprepare(const std::string &name);
534 
536 
546  void prepare_now(const std::string &name);
547 
552 
564  template<typename TRANSACTOR>
565  void perform(const TRANSACTOR &T, int Attempts); //[t04]
566 
568 
574  template<typename TRANSACTOR>
575  void perform(const TRANSACTOR &T) { perform(T, 3); }
576 
578 
581  std::string adorn_name(const std::string &); //[90]
582 
587  std::string esc(const char str[]);
589 
591  std::string esc(const char str[], size_t maxlen);
592 
594  std::string esc(const std::string &str);
595 
597  std::string esc_raw(const unsigned char str[], size_t len);
598 
600 
603  std::string unesc_raw(const std::string &text)
604  { return unesc_raw(text.c_str()); }
605 
607 
610  std::string unesc_raw(const char *text);
611 
613  std::string quote_raw(const unsigned char str[], size_t len);
614 
616  std::string quote_name(const std::string &identifier);
617 
619 
620  template<typename T>
621  std::string quote(const T &t)
622  {
623  if (string_traits<T>::is_null(t)) return "NULL";
624  return "'" + this->esc(to_string(t)) + "'";
625  }
626 
627  std::string quote(const binarystring &);
629 
631  void cancel_query();
632 
635  {
636  // These values must match those in libpq's PGVerbosity enum.
637  terse=0,
638  normal=1,
639  verbose=2
640  };
641 
643 
651  void set_verbosity(error_verbosity verbosity) noexcept;
653  error_verbosity get_verbosity() const noexcept {return m_verbosity;}
654 
656 
668  std::vector<errorhandler *> get_errorhandlers() const;
669 
670 protected:
672  m_policy(pol)
673  {
674  // Check library version. The check_library_version template is declared
675  // for any library version, but only actually defined for the version of
676  // the libpqxx binary against which the code is linked.
677  //
678  // If the library binary is a different version than the one declared in
679  // these headers, then this call will fail to link: there will be no
680  // definition for the function with these exact template parameter values.
681  // There will be a definition, but the version in the parameter values will
682  // be different.
683  //
684  // There is no particular reason to do this here in this constructor, except
685  // to ensure that every meaningful libpqxx client will execute it. The call
686  // must be in the execution path somewhere or the compiler won't try to link
687  // it. We can't use it to initialise a global or class-static variable,
688  // because a smart compiler might resolve it at compile time.
689  //
690  // On the other hand, we don't want to make a useless function call too
691  // often for performance reasons. A local static variable is initialised
692  // only on the definition's first execution. Compilers will be well
693  // optimised for this behaviour, so there's a minimal one-time cost.
694  static const auto version_ok =
696  ignore_unused(version_ok);
697 
698  clearcaps();
699  }
700  void init();
701 
702  void close() noexcept;
703  void wait_read() const;
704  void wait_read(long seconds, long microseconds) const;
705  void wait_write() const;
706 
707 private:
708 
709  result make_result(internal::pq::PGresult *rhs, const std::string &query);
710 
711  void clearcaps() noexcept;
712  void PQXX_PRIVATE set_up_state();
713  void PQXX_PRIVATE check_result(const result &);
714 
715  void PQXX_PRIVATE internal_set_trace() noexcept;
716  int PQXX_PRIVATE PQXX_PURE status() const noexcept;
717 
718  friend class internal::gate::const_connection_largeobject;
719  const char * PQXX_PURE err_msg() const noexcept;
720 
721  void PQXX_PRIVATE reset();
722  std::string PQXX_PRIVATE raw_get_var(const std::string &);
723  void PQXX_PRIVATE process_notice_raw(const char msg[]) noexcept;
724 
725  void read_capabilities();
726 
727  prepare::internal::prepared_def &find_prepared(const std::string &);
728 
729  prepare::internal::prepared_def &register_prepared(const std::string &);
730 
731  friend class internal::gate::connection_prepare_invocation;
733  result prepared_exec(
734  const std::string &,
735  const char *const[],
736  const int[],
737  const int[],
738  int);
739  result exec_prepared(const std::string &statement, const internal::params &);
740  bool prepared_exists(const std::string &) const;
741 
743  internal::pq::PGconn *m_conn = nullptr;
744 
745  connectionpolicy &m_policy;
746 
748  internal::unique<transaction_base> m_trans;
749 
750  std::list<errorhandler *> m_errorhandlers;
751 
753  std::FILE *m_trace = nullptr;
754 
755  using receiver_list =
756  std::multimap<std::string, pqxx::notification_receiver *>;
758  receiver_list m_receivers;
759 
761  std::map<std::string, std::string> m_vars;
762 
763  using PSMap = std::map<std::string, prepare::internal::prepared_def>;
765  PSMap m_prepared;
766 
768  int m_serverversion = 0;
769 
771  internal::reactivation_avoidance_counter m_reactivation_avoidance;
772 
774  int m_unique_id = 0;
775 
777  bool m_completed = false;
778 
780  bool m_inhibit_reactivation = false;
781 
783  std::bitset<cap_end> m_caps;
784 
786  error_verbosity m_verbosity = normal;
787 
788  friend class internal::gate::connection_errorhandler;
789  void PQXX_PRIVATE register_errorhandler(errorhandler *);
790  void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
791 
792  friend class internal::gate::connection_transaction;
793  result PQXX_PRIVATE exec(const char[], int Retries);
794  void PQXX_PRIVATE register_transaction(transaction_base *);
795  void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
796  bool PQXX_PRIVATE read_copy_line(std::string &);
797  void PQXX_PRIVATE write_copy_line(const std::string &);
798  void PQXX_PRIVATE end_copy_write();
799  void PQXX_PRIVATE raw_set_var(const std::string &, const std::string &);
800  void PQXX_PRIVATE add_variables(const std::map<std::string, std::string> &);
801 
802  friend class internal::gate::connection_largeobject;
803  internal::pq::PGconn *raw_connection() const { return m_conn; }
804 
805  friend class internal::gate::connection_notification_receiver;
806  void add_receiver(notification_receiver *);
807  void remove_receiver(notification_receiver *) noexcept;
808 
809  friend class internal::gate::connection_pipeline;
810  void PQXX_PRIVATE start_exec(const std::string &);
811  bool PQXX_PRIVATE consume_input() noexcept;
812  bool PQXX_PRIVATE is_busy() const noexcept;
813  int PQXX_PRIVATE encoding_code();
814  internal::pq::PGresult *get_result();
815 
816  friend class internal::gate::connection_dbtransaction;
817 
818  friend class internal::gate::connection_sql_cursor;
819  void add_reactivation_avoidance_count(int);
820 
821  friend class internal::gate::connection_reactivation_avoidance_exemption;
822 
823  friend class internal::gate::connection_parameterized_invocation;
825  result parameterized_exec(
826  const std::string &query,
827  const char *const params[],
828  const int paramlengths[],
829  const int binaries[],
830  int nparams);
831 
832  result exec_params(
833  const std::string &query,
834  const internal::params &args);
835 
836  connection_base(const connection_base &) =delete;
837  connection_base &operator=(const connection_base &) =delete;
838 };
839 
840 
841 namespace internal
842 {
843 
846 {
847 public:
850 
851  void close_connection() noexcept { m_open = false; }
852 
853 private:
854  connection_base &m_home;
855  int m_count;
856  bool m_open;
857 };
858 
859 
860 void wait_read(const internal::pq::PGconn *);
861 void wait_read(const internal::pq::PGconn *, long seconds, long microseconds);
862 void wait_write(const internal::pq::PGconn *);
863 } // namespace pqxx::internal
864 
865 } // namespace pqxx
866 
867 #include "pqxx/compiler-internal-post.hxx"
868 
869 #endif
void wait_write(const internal::pq::PGconn *)
Definition: connection_base.cxx:1201
Binary data corresponding to PostgreSQL&#39;s "BYTEA" binary-string type.
Definition: binarystring.hxx:53
Result set containing data returned by a query or command.
Definition: result.hxx:65
std::string unesc_raw(const std::string &text)
Unescape binary data, e.g. from a table field or notification payload.
Definition: connection_base.hxx:603
Definition: connectionpolicy.hxx:31
std::string quote(const T &t)
Represent object as SQL string, including quoting & escaping.
Definition: connection_base.hxx:621
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
Definition: connection_base.hxx:48
void add(int n) noexcept
Definition: connection_base.hxx:53
void wait_read(const internal::pq::PGconn *)
Definition: connection_base.cxx:1181
bool supports(capability c) const noexcept
Does this connection seem to support the given capability?
Definition: connection_base.hxx:350
Cursor with SQL positioning semantics.
Definition: cursor.hxx:164
error_verbosity
Error verbosity levels.
Definition: connection_base.hxx:634
Not a capability value; end-of-enumeration marker.
Definition: connection_base.hxx:329
Base class for error-handler callbacks.
Definition: errorhandler.hxx:49
std::string encrypt_password(const std::string &user, const std::string &password)
Encrypt password for given user.
Definition: connection_base.cxx:83
void perform(const TRANSACTOR &T)
Definition: connection_base.hxx:575
connection_base(connectionpolicy &pol)
Definition: connection_base.hxx:671
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Scoped exemption to reactivation avoidance.
Definition: connection_base.hxx:845
int check_library_version< PQXX_VERSION_MAJOR, PQXX_VERSION_MINOR >() noexcept
Definition: version.cxx:13
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
void clear() noexcept
Definition: connection_base.hxx:54
void close_connection() noexcept
Definition: connection_base.hxx:851
connection_base abstract base class; represents a connection to a database.
Definition: connection_base.hxx:139
void inhibit_reactivation(bool inhibit)
Disallow (or permit) connection recovery.
Definition: connection_base.hxx:231
STL namespace.
Traits class for use in string conversions.
Definition: strconv.hxx:52
void set_client_encoding(const std::string &Encoding)
Set client-side character encoding.
Definition: connection_base.hxx:387
Definition: notification.hxx:55
capability
Session capabilities.
Definition: connection_base.hxx:326
error_verbosity get_verbosity() const noexcept
Retrieve current error verbosity.
Definition: connection_base.hxx:653
auto perform(const TRANSACTION_CALLBACK &callback, int attempts=3) -> decltype(callback())
Simple way to execute a transaction with automatic retry.
Definition: transactor.hxx:100
void ignore_unused(T)
Suppress compiler warning about an unused item.
Definition: util.hxx:37