backends/postgresql/error.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2011 Gevorg Voskanyan
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #define SOCI_POSTGRESQL_SOURCE
9 #include "soci-postgresql.h"
10 #include "error.h"
11 #include <cstring>
12 #include <cassert>
13 
14 using namespace soci;
15 using namespace soci::details;
16 
18  std::string const & msg, char const *sqlst)
19  : soci_error(msg)
20 {
21  assert(std::strlen(sqlst) == 5);
22  std::memcpy(sqlstate_, sqlst, 5);
23 }
24 
26 {
27  return std::string(sqlstate_, 5);
28 }
29 
30 void
32 {
33  static_cast<void>(check_for_data(errMsg));
34 }
35 
36 bool
38 {
39  ExecStatusType const status = PQresultStatus(result_);
40  switch (status)
41  {
42  case PGRES_EMPTY_QUERY:
43  case PGRES_COMMAND_OK:
44  // No data but don't throw neither.
45  return false;
46 
47  case PGRES_TUPLES_OK:
48  return true;
49 
50  default:
51  // Some of the other status codes are not really errors but we're
52  // not prepared to handle them right now and shouldn't ever receive
53  // them so throw nevertheless
54  break;
55  }
56 
57  std::string msg(errMsg);
58  const char* const pqError = PQresultErrorMessage(result_);
59  if (pqError && *pqError)
60  {
61  msg += " ";
62  msg += pqError;
63  }
64 
65  const char* sqlstate = PQresultErrorField(result_, PG_DIAG_SQLSTATE);
66  const char* const blank_sql_state = " ";
67  if (!sqlstate)
68  {
69  sqlstate = blank_sql_state;
70  }
71 
72  throw postgresql_soci_error(msg, sqlstate);
73 }
bool check_for_data(char const *errMsg) const
void check_for_errors(char const *errMsg) const
postgresql_soci_error(std::string const &msg, char const *sqlst)


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40