23 #define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) {ch1,ch2,ch3,ch4,ch5}
25 #define PGSQL_STATECODE_LEN 5
26 #include <utils/errcodes.h>
38 const char PgSqlConnection::DUPLICATE_KEY[] = ERRCODE_UNIQUE_VIOLATION;
40 PgSqlResult::PgSqlResult(PGresult *result)
41 : result_(result), rows_(0), cols_(0) {
50 rows_ = PQntuples(result);
51 cols_ = PQnfields(result);
57 if (row < 0 || row >= rows_) {
59 <<
", out of range: 0.." << rows_);
71 if (col < 0 || col >= cols_) {
73 <<
", out of range: 0.." << cols_);
85 const char* label = NULL;
88 label = PQfname(result_, col);
90 std::ostringstream os;
91 os <<
"Unknown column:" << col;
99 : conn_(conn), committed_(false) {
119 if (PQstatus(
conn_) == CONNECTION_OK) {
121 if(PQresultStatus(r) != PGRES_COMMAND_OK) {
124 .arg(PQerrorMessage(
conn_));
135 if(PQresultStatus(r) != PGRES_COMMAND_OK) {
137 << statement.
text <<
", reason: " << PQerrorMessage(
conn_));
146 tagged_statement != end_statement; ++tagged_statement) {
153 string dbconnparameters;
154 string shost =
"localhost";
161 dbconnparameters +=
"host = '" + shost +
"'" ;
171 if (sport.size() > 0) {
172 unsigned int port = 0;
176 port = boost::lexical_cast<unsigned int>(sport);
185 if (port > numeric_limits<uint16_t>::max()) {
191 std::ostringstream oss;
193 dbconnparameters +=
" port = " + oss.str();
200 dbconnparameters +=
" user = '" + suser +
"'";
208 dbconnparameters +=
" password = '" + spassword +
"'";
216 dbconnparameters +=
" dbname = '" + sname +
"'";
231 if (stimeout.size() > 0) {
235 connect_timeout = boost::lexical_cast<unsigned int>(stimeout);
252 if ((connect_timeout == 0) ||
253 (connect_timeout > numeric_limits<int>::max())) {
255 stimeout <<
") must be an integer greater than 0");
259 std::ostringstream oss;
260 oss << connect_timeout;
261 dbconnparameters +=
" connect_timeout = " + oss.str();
265 PGconn* new_conn = PQconnectdb(dbconnparameters.c_str());
270 if (PQstatus(new_conn) != CONNECTION_OK) {
273 std::string error_message = PQerrorMessage(new_conn);
284 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
286 return ((sqlstate != NULL) &&
293 int s = PQresultStatus(r);
294 if (s != PGRES_COMMAND_OK && s != PGRES_TUPLES_OK) {
299 const char* sqlstate = PQresultErrorField(r, PG_DIAG_SQLSTATE);
300 if ((sqlstate == NULL) ||
301 ((memcmp(sqlstate,
"08", 2) == 0) ||
302 (memcmp(sqlstate,
"53", 2) == 0) ||
303 (memcmp(sqlstate,
"54", 2) == 0) ||
304 (memcmp(sqlstate,
"57", 2) == 0) ||
305 (memcmp(sqlstate,
"58", 2) == 0))) {
308 .arg(PQerrorMessage(
conn_))
309 .arg(sqlstate ? sqlstate :
"<sqlstate null>");
320 "fatal database errror or connectivity lost");
324 const char* error_message = PQerrorMessage(
conn_);
326 << statement.
name <<
", status: " << s
327 <<
"sqlstate:[ " << (sqlstate ? sqlstate :
"<null>")
328 <<
"], reason: " << error_message);
336 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
337 const char* error_message = PQerrorMessage(
conn_);
347 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
348 const char* error_message = PQerrorMessage(
conn_);
357 if (PQresultStatus(r) != PGRES_COMMAND_OK) {
358 const char* error_message = PQerrorMessage(
conn_);