AC_CHECK_LIB(pq, PQparameterStatus,AC_DEFINE(HAVE_PQPARAMETERSTATUS,1,[PostgreSQL 7.4 or later]))
AC_CHECK_LIB(pq, PQprotocolVersion,AC_DEFINE(HAVE_PQPROTOCOLVERSION,1,[PostgreSQL 7.4 or later]))
AC_CHECK_LIB(pq, PQtransactionStatus,AC_DEFINE(HAVE_PGTRANSACTIONSTATUS,1,[PostgreSQL 7.4 or later]))
+ AC_CHECK_LIB(pq, PQunescapeBytea,AC_DEFINE(HAVE_PQUNESCAPEBYTEA,1,[PostgreSQL 7.4 or later]))
+ AC_CHECK_LIB(pq, PQresultErrorField,AC_DEFINE(HAVE_PQRESULTERRORFIELD,1,[PostgreSQL 7.4 or later]))
AC_CHECK_LIB(pq, pg_encoding_to_char,AC_DEFINE(HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT,1,[Whether libpq is compiled with --enable-multibyte]))
LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS
return tmp;
}
-int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC) /* {{{ */
+int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *file, int line TSRMLS_DC) /* {{{ */
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
einfo->errmsg = NULL;
}
- switch (errcode) {
- default:
- strcpy(*pdo_err, "HY000");
- break;
+ if (sqlstate == NULL) {
+ strcpy(*pdo_err, "HY000");
+ }
+ else {
+ strcpy(*pdo_err, sqlstate);
}
if (errmsg) {
if (!(res = PQexec(H->server, sql))) {
/* fatal error */
- pdo_pgsql_error(dbh, PGRES_FATAL_ERROR);
+ pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
return 0;
} else {
ExecStatusType qs = PQresultStatus(res);
if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
- pdo_pgsql_error(dbh, qs);
+#if HAVE_PQRESULTERRORFIELD
+ char * sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
+ pdo_pgsql_error(dbh, qs, (const char *)sqlstate);
+ PQfreemem(sqlstate);
+#else
+ pdo_pgsql_error(dbh, qs, NULL);
+#endif
PQclear(res);
return 0;
}
}
if (PQstatus(H->server) != CONNECTION_OK) {
- pdo_pgsql_error(dbh, PGRES_FATAL_ERROR);
+ pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
goto cleanup;
}
status = PQresultStatus(S->result);
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
- pdo_pgsql_error_stmt(stmt, status);
+#if HAVE_PQRESULTERRORFIELD
+ char * sqlstate = PQresultErrorField(S->result, PG_DIAG_SQLSTATE);
+ pdo_pgsql_error_stmt(stmt, status, (const char *)sqlstate);
+#else
+ pdo_pgsql_error_stmt(stmt, status, NULL);
+#endif
+
return 0;
}
status = PQresultStatus(S->result);
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
- pdo_pgsql_error_stmt(stmt, status);
+#if HAVE_PQRESULTERRORFIELD
+ char * sqlstate = PQresultErrorField(S->result, PG_DIAG_SQLSTATE);
+ pdo_pgsql_error_stmt(stmt, status, (const char *)sqlstate);
+#else
+ pdo_pgsql_error_stmt(stmt, status, NULL);
+#endif
return 0;
}
}
/* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
- Renamed to php_pgsql_unescape_bytea() */
+ Renamed to php_pdo_pgsql_unescape_bytea() */
/*
* PQunescapeBytea - converts the null terminated string representation
* of a bytea, strtext, into binary, filling a buffer. It returns a
* 5 \'
* 6 \\
*/
-static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
+static unsigned char *php_pdo_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
{
size_t buflen;
unsigned char *buffer,
break;
case PDO_PARAM_LOB:
- tmp_ptr = php_pgsql_unescape_bytea(*ptr, &tmp_len);
- *ptr = estrndup(tmp_ptr, tmp_len);
+ *ptr = php_pdo_pgsql_unescape_bytea(*ptr, &tmp_len);
*len = tmp_len;
*caller_frees = 1;
- free(tmp_ptr);
+ break;
+ case PDO_PARAM_NULL:
+ case PDO_PARAM_STR:
+ case PDO_PARAM_STMT:
+ case PDO_PARAM_INPUT_OUTPUT:
break;
}
}
#include <libpq-fe.h>
+#define PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE "08006"
+
typedef struct {
const char *file;
int line;
extern pdo_driver_t pdo_pgsql_driver;
-extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *file, int line TSRMLS_DC);
-#define pdo_pgsql_error(d,e) _pdo_pgsql_error(d, NULL, e, __FILE__, __LINE__ TSRMLS_CC)
-#define pdo_pgsql_error_stmt(s,e) _pdo_pgsql_error(s->dbh, s, e, __FILE__, __LINE__ TSRMLS_CC)
+extern int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *file, int line TSRMLS_DC);
+#define pdo_pgsql_error(d,e,z) _pdo_pgsql_error(d, NULL, e, z, __FILE__, __LINE__ TSRMLS_CC)
+#define pdo_pgsql_error_stmt(s,e,z) _pdo_pgsql_error(s->dbh, s, e, z, __FILE__, __LINE__ TSRMLS_CC)
extern struct pdo_stmt_methods pgsql_stmt_methods;