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 
30 
31 /* Use of the libpqxx library starts here.
32  *
33  * Everything that can be done with a database through libpqxx must go through
34  * a connection object derived from connection_base.
35  */
36 
37 /* Methods tested in eg. self-test program test1 are marked with "//[t01]"
38  */
39 
40 namespace pqxx
41 {
42 namespace internal
43 {
45 class sql_cursor;
46 
48 {
49 public:
51 
52  void add(int n) noexcept { m_counter += n; }
53  void clear() noexcept { m_counter = 0; }
54  int get() const noexcept { return m_counter; }
55 
56 private:
57  int m_counter = 0;
58 };
59 
60 }
61 
62 
64 
80 std::string PQXX_LIBEXPORT encrypt_password( //[t00]
81  const std::string &user,
82  const std::string &password);
83 
84 
85 namespace internal
86 {
87 namespace gate
88 {
89 class connection_dbtransaction;
90 class connection_errorhandler;
91 class connection_largeobject;
92 class connection_notification_receiver;
93 class connection_parameterized_invocation;
94 class connection_pipeline;
95 class connection_prepare_invocation;
96 class connection_reactivation_avoidance_exemption;
97 class connection_sql_cursor;
98 class connection_transaction;
99 class const_connection_largeobject;
100 } // namespace pqxx::internal::gate
101 } // namespace pqxx::internal
102 
103 
105 
138 class PQXX_LIBEXPORT connection_base
139 {
140 public:
142  void disconnect() noexcept; //[t02]
143 
145 
149  bool PQXX_PURE is_open() const noexcept; //[t01]
150 
161 
172  void activate(); //[t12]
173 
175 
183  void deactivate(); //[t12]
184 
186 
230  void inhibit_reactivation(bool inhibit) //[t86]
231  { m_inhibit_reactivation=inhibit; }
232 
234 
239  void simulate_failure(); //[t94]
241 
243  void process_notice(const char[]) noexcept; //[t14]
245  void process_notice(const std::string &) noexcept; //[t14]
246 
248  void trace(std::FILE *) noexcept; //[t03]
249 
258 
262  const char *dbname(); //[t01]
263 
265 
268  const char *username(); //[t01]
269 
271 
274  const char *hostname(); //[t01]
275 
277 
280  const char *port(); //[t01]
281 
283 
292  int PQXX_PURE backendpid() const noexcept; //[t01]
293 
295 
310  int PQXX_PURE sock() const noexcept; //[t87]
311 
320 
322 
326  {
329  };
330 
331 
333 
349  bool supports(capability c) const noexcept //[t88]
350  { return m_caps.test(c); }
351 
353 
361  int PQXX_PURE protocol_version() const noexcept; //[t01]
362 
364 
376  int PQXX_PURE server_version() const noexcept; //[t01]
378 
380 
386  void set_client_encoding(const std::string &Encoding) //[t07]
387  { set_variable("CLIENT_ENCODING", Encoding); }
388 
390 
406  void set_variable( //[t60]
407  const std::string &Var,
408  const std::string &Value);
409 
411 
418  std::string get_variable(const std::string &); //[t60]
420 
421 
426 
439  int get_notifs(); //[t04]
440 
441 
443 
449  int await_notification(); //[t78]
450 
452 
458  int await_notification(long seconds, long microseconds); //[t79]
460 
461 
486 
520  void prepare(const std::string &name, const std::string &definition);
521 
523 
529  void prepare(const std::string &definition);
530 
532  void unprepare(const std::string &name);
533 
535 
545  void prepare_now(const std::string &name);
546 
551 
563  template<typename TRANSACTOR>
564  void perform(const TRANSACTOR &T, int Attempts); //[t04]
565 
567 
573  template<typename TRANSACTOR>
574  void perform(const TRANSACTOR &T) { perform(T, 3); }
575 
577 
580  std::string adorn_name(const std::string &); //[90]
581 
586  std::string esc(const char str[]);
588 
590  std::string esc(const char str[], size_t maxlen);
591 
593  std::string esc(const std::string &str);
594 
596  std::string esc_raw(const unsigned char str[], size_t len);
597 
599 
602  std::string unesc_raw(const std::string &text)
603  { return unesc_raw(text.c_str()); }
604 
606 
609  std::string unesc_raw(const char *text);
610 
612  std::string quote_raw(const unsigned char str[], size_t len);
613 
615  std::string quote_name(const std::string &identifier);
616 
618 
619  template<typename T>
620  std::string quote(const T &t)
621  {
622  if (string_traits<T>::is_null(t)) return "NULL";
623  return "'" + this->esc(to_string(t)) + "'";
624  }
625 
626  std::string quote(const binarystring &);
628 
630  void cancel_query();
631 
634  {
635  // These values must match those in libpq's PGVerbosity enum.
636  terse=0,
637  normal=1,
638  verbose=2
639  };
640 
642 
650  void set_verbosity(error_verbosity verbosity) noexcept;
652  error_verbosity get_verbosity() const noexcept {return m_verbosity;}
653 
655 
667  std::vector<errorhandler *> get_errorhandlers() const;
668 
669 protected:
670  explicit connection_base(connectionpolicy &);
671  void init();
672 
673  void close() noexcept;
674  void wait_read() const;
675  void wait_read(long seconds, long microseconds) const;
676  void wait_write() const;
677 
678 private:
679 
680  result make_result(internal::pq::PGresult *rhs, const std::string &query);
681 
682  void PQXX_PRIVATE clearcaps() noexcept;
683  void PQXX_PRIVATE set_up_state();
684  void PQXX_PRIVATE check_result(const result &);
685 
686  void PQXX_PRIVATE internal_set_trace() noexcept;
687  int PQXX_PRIVATE PQXX_PURE status() const noexcept;
688 
689  friend class internal::gate::const_connection_largeobject;
690  const char * PQXX_PURE err_msg() const noexcept;
691 
692  void PQXX_PRIVATE reset();
693  std::string PQXX_PRIVATE raw_get_var(const std::string &);
694  void PQXX_PRIVATE process_notice_raw(const char msg[]) noexcept;
695 
696  void read_capabilities();
697 
698  prepare::internal::prepared_def &find_prepared(const std::string &);
699 
700  prepare::internal::prepared_def &register_prepared(const std::string &);
701 
702  friend class internal::gate::connection_prepare_invocation;
704  result prepared_exec(
705  const std::string &,
706  const char *const[],
707  const int[],
708  const int[],
709  int);
710  result exec_prepared(const std::string &statement, const internal::params &);
711  bool prepared_exists(const std::string &) const;
712 
714  internal::pq::PGconn *m_conn = nullptr;
715 
716  connectionpolicy &m_policy;
717 
719  internal::unique<transaction_base> m_trans;
720 
721  std::list<errorhandler *> m_errorhandlers;
722 
724  std::FILE *m_trace = nullptr;
725 
726  using receiver_list =
727  std::multimap<std::string, pqxx::notification_receiver *>;
729  receiver_list m_receivers;
730 
732  std::map<std::string, std::string> m_vars;
733 
734  using PSMap = std::map<std::string, prepare::internal::prepared_def>;
736  PSMap m_prepared;
737 
739  int m_serverversion = 0;
740 
742  internal::reactivation_avoidance_counter m_reactivation_avoidance;
743 
745  int m_unique_id = 0;
746 
748  bool m_completed = false;
749 
751  bool m_inhibit_reactivation = false;
752 
754  std::bitset<cap_end> m_caps;
755 
757  error_verbosity m_verbosity = normal;
758 
759  friend class internal::gate::connection_errorhandler;
760  void PQXX_PRIVATE register_errorhandler(errorhandler *);
761  void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
762 
763  friend class internal::gate::connection_transaction;
764  result PQXX_PRIVATE exec(const char[], int Retries);
765  void PQXX_PRIVATE register_transaction(transaction_base *);
766  void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
767  bool PQXX_PRIVATE read_copy_line(std::string &);
768  void PQXX_PRIVATE write_copy_line(const std::string &);
769  void PQXX_PRIVATE end_copy_write();
770  void PQXX_PRIVATE raw_set_var(const std::string &, const std::string &);
771  void PQXX_PRIVATE add_variables(const std::map<std::string, std::string> &);
772 
773  friend class internal::gate::connection_largeobject;
774  internal::pq::PGconn *raw_connection() const { return m_conn; }
775 
776  friend class internal::gate::connection_notification_receiver;
777  void add_receiver(notification_receiver *);
778  void remove_receiver(notification_receiver *) noexcept;
779 
780  friend class internal::gate::connection_pipeline;
781  void PQXX_PRIVATE start_exec(const std::string &);
782  bool PQXX_PRIVATE consume_input() noexcept;
783  bool PQXX_PRIVATE is_busy() const noexcept;
784  int PQXX_PRIVATE encoding_code();
785  internal::pq::PGresult *get_result();
786 
787  friend class internal::gate::connection_dbtransaction;
788 
789  friend class internal::gate::connection_sql_cursor;
790  void add_reactivation_avoidance_count(int);
791 
792  friend class internal::gate::connection_reactivation_avoidance_exemption;
793 
794  friend class internal::gate::connection_parameterized_invocation;
796  result parameterized_exec(
797  const std::string &query,
798  const char *const params[],
799  const int paramlengths[],
800  const int binaries[],
801  int nparams);
802 
803  result exec_params(
804  const std::string &query,
805  const internal::params &args);
806 
807  connection_base(const connection_base &) =delete;
808  connection_base &operator=(const connection_base &) =delete;
809 };
810 
811 
812 namespace internal
813 {
814 
817 {
818 public:
821 
822  void close_connection() noexcept { m_open = false; }
823 
824 private:
825  connection_base &m_home;
826  int m_count;
827  bool m_open;
828 };
829 
830 
831 void wait_read(const internal::pq::PGconn *);
832 void wait_read(const internal::pq::PGconn *, long seconds, long microseconds);
833 void wait_write(const internal::pq::PGconn *);
834 } // namespace pqxx::internal
835 
836 } // namespace pqxx
837 
838 #include "pqxx/compiler-internal-post.hxx"
839 
840 #endif
Binary data corresponding to PostgreSQL&#39;s "BYTEA" binary-string type.
Definition: binarystring.hxx:53
Definition: connectionpolicy.hxx:31
Definition: connection_base.hxx:47
void perform(const TRANSACTOR &T)
Definition: connection_base.hxx:574
void add(int n) noexcept
Definition: connection_base.hxx:52
Cursor with SQL positioning semantics.
Definition: cursor.hxx:164
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Scoped exemption to reactivation avoidance.
Definition: connection_base.hxx:816
capability
Session capabilities.
Definition: connection_base.hxx:325
void wait_read(const internal::pq::PGconn *)
Base class for error-handler callbacks.
Definition: errorhandler.hxx:49
auto perform(const TRANSACTION_CALLBACK &callback, int attempts=3) -> decltype(callback())
Simple way to execute a transaction with automatic retry.
Definition: transactor.hxx:100
error_verbosity
Error verbosity levels.
Definition: connection_base.hxx:633
void wait_write(const internal::pq::PGconn *)
Result set containing data returned by a query or command.
Definition: result.hxx:67
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
Definition: notification.hxx:53
error_verbosity get_verbosity() const noexcept
Retrieve current error verbosity.
Definition: connection_base.hxx:652
STL namespace.
void set_client_encoding(const std::string &Encoding)
Set client-side character encoding.
Definition: connection_base.hxx:386
std::string quote(const T &t)
Represent object as SQL string, including quoting & escaping.
Definition: connection_base.hxx:620
std::string unesc_raw(const std::string &text)
Unescape binary data, e.g. from a table field or notification payload.
Definition: connection_base.hxx:602
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
std::string encrypt_password(const std::string &user, const std::string &password)
Encrypt password for given user.
Definition: connection_base.cxx:82
connection_base abstract base class; represents a connection to a database.
Definition: connection_base.hxx:138
void clear() noexcept
Definition: connection_base.hxx:53
Not a capability value; end-of-enumeration marker.
Definition: connection_base.hxx:328
bool supports(capability c) const noexcept
Does this connection seem to support the given capability?
Definition: connection_base.hxx:349
void inhibit_reactivation(bool inhibit)
Disallow (or permit) connection recovery.
Definition: connection_base.hxx:230
Traits class for use in string conversions.
Definition: strconv.hxx:39
void close_connection() noexcept
Definition: connection_base.hxx:822