Return-Path: Delivered-To: apmail-apr-commits-archive@www.apache.org Received: (qmail 67389 invoked from network); 5 Feb 2005 19:00:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 5 Feb 2005 19:00:14 -0000 Received: (qmail 25737 invoked by uid 500); 5 Feb 2005 19:00:14 -0000 Delivered-To: apmail-apr-commits-archive@apr.apache.org Received: (qmail 25640 invoked by uid 500); 5 Feb 2005 19:00:13 -0000 Mailing-List: contact commits-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: dev@apr.apache.org Delivered-To: mailing list commits@apr.apache.org Received: (qmail 25625 invoked by uid 99); 5 Feb 2005 19:00:13 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Sat, 05 Feb 2005 11:00:13 -0800 Received: (qmail 67377 invoked by uid 65534); 5 Feb 2005 19:00:12 -0000 Message-ID: <20050205190012.67376.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Sat, 05 Feb 2005 19:00:12 -0000 Subject: svn commit: r151517 - apr/apr-util/trunk/dbd/apr_dbd_pgsql.c To: commits@apr.apache.org From: rooneg@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: rooneg Date: Sat Feb 5 11:00:10 2005 New Revision: 151517 URL: http://svn.apache.org/viewcvs?view=3Drev&rev=3D151517 Log: Start preparations to move the transaction handling code inside the apr_dbd_t structure. * dbd/apr_dbd_pgsql.c (apr_dbd_t): make this a structure that contains the connection, as a first step towards eventually putting the transaction data in here. (apr_dbd_transaction): cache a pointer to a PGconn, not an apr_dbt_t. (dbd_pgsql_select, dbd_pgsql_error, dbd_pgsql_query, dbd_pgsql_prepare, dbd_pgsql_pquery, dbd_pgsql_pselect, dbd_pgsql_transaction, dbd_pgsql_close, dbd_pgsql_check_conn, dbd_pgsql_select_db): account for the fact that the apr_dbd_t is no longer just a typedef for PGconn. (dbd_pgsql_open): ditto, plus catch errors that come from having a bogus params string, otherwise we're likely to crash inside PQreset. Modified: apr/apr-util/trunk/dbd/apr_dbd_pgsql.c Modified: apr/apr-util/trunk/dbd/apr_dbd_pgsql.c URL: http://svn.apache.org/viewcvs/apr/apr-util/trunk/dbd/apr_dbd_pgsql.c?v= iew=3Ddiff&r1=3D151516&r2=3D151517 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- apr/apr-util/trunk/dbd/apr_dbd_pgsql.c (original) +++ apr/apr-util/trunk/dbd/apr_dbd_pgsql.c Sat Feb 5 11:00:10 2005 @@ -14,6 +14,8 @@ * limitations under the License. */ =20 +#include "apu.h" + #if APU_HAVE_PGSQL =20 #include @@ -25,10 +27,13 @@ =20 #define QUERY_MAX_ARGS 40 =20 -typedef PGconn apr_dbd_t; +typedef struct { + PGconn *conn; +} apr_dbd_t; + typedef struct { int errnum; - apr_dbd_t *handle; + PGconn *handle; } apr_dbd_transaction; =20 typedef struct { @@ -68,7 +73,7 @@ return trans->errnum; } if (seek) { /* synchronous query */ - res =3D PQexec(sql, query); + res =3D PQexec(sql->conn, query); if (res) { ret =3D PQresultStatus(res); if (dbd_pgsql_is_success(ret)) { @@ -96,7 +101,7 @@ apr_pool_cleanup_null); } else { - if (PQsendQuery(sql, query) =3D=3D 0) { + if (PQsendQuery(sql->conn, query) =3D=3D 0) { if (trans) { trans->errnum =3D 1; } @@ -106,7 +111,7 @@ *results =3D apr_pcalloc(pool, sizeof(apr_dbd_results)); } (*results)->random =3D seek; - (*results)->handle =3D sql; + (*results)->handle =3D sql->conn; } return 0; } @@ -183,7 +188,7 @@ =20 static const char *dbd_pgsql_error(apr_dbd_t *sql, int n) { - return PQerrorMessage(sql); + return PQerrorMessage(sql->conn); } =20 static int dbd_pgsql_query(apr_dbd_t *sql, apr_dbd_transaction *trans, @@ -194,7 +199,7 @@ if (trans && trans->errnum) { return trans->errnum; } - res =3D PQexec(sql, query); + res =3D PQexec(sql->conn, query); if (res) { ret =3D PQresultStatus(res); if (dbd_pgsql_is_success(ret)) { @@ -321,7 +326,7 @@ sqlptr +=3D strlen(pgquery); *sqlptr =3D 0; =20 - res =3D PQexec(sql, sqlcmd); + res =3D PQexec(sql->conn, sqlcmd); if ( res ) { ret =3D PQresultStatus(res); if (dbd_pgsql_is_success(ret)) { @@ -346,10 +351,12 @@ int ret; PGresult *res; if (statement->prepared) { - res =3D PQexecPrepared(sql, statement->name, nargs, values, 0, 0, = 0); + res =3D PQexecPrepared(sql->conn, statement->name, nargs, values, = 0, 0, + 0); } else { - res =3D PQexecParams(sql, statement->name, nargs, 0, values, 0, 0,= 0); + res =3D PQexecParams(sql->conn, statement->name, nargs, 0, values,= 0, 0, + 0); } if (res) { ret =3D PQresultStatus(res); @@ -404,10 +411,12 @@ int ret =3D 0; if (seek) { /* synchronous query */ if (statement->prepared) { - res =3D PQexecPrepared(sql, statement->name, nargs, values, 0,= 0, 0); + res =3D PQexecPrepared(sql->conn, statement->name, nargs, valu= es, 0, + 0, 0); } else { - res =3D PQexecParams(sql, statement->name, nargs, 0, values, 0= , 0, 0); + res =3D PQexecParams(sql->conn, statement->name, nargs, 0, val= ues, 0, + 0, 0); } if (res) { ret =3D PQresultStatus(res); @@ -439,12 +448,12 @@ } else { if (statement->prepared) { - rv =3D PQsendQueryPrepared(sql, statement->name, nargs, values= ,0,0, - 0); + rv =3D PQsendQueryPrepared(sql->conn, statement->name, nargs, = values, + 0, 0, 0); } else { - rv =3D PQsendQueryParams(sql, statement->name, nargs, 0, value= s,0,0, - 0); + rv =3D PQsendQueryParams(sql->conn, statement->name, nargs, 0, + values, 0, 0, 0); } if (rv =3D=3D 0) { if (trans) { @@ -456,7 +465,7 @@ *results =3D apr_pcalloc(pool, sizeof(apr_dbd_results)); } (*results)->random =3D seek; - (*results)->handle =3D sql; + (*results)->handle =3D sql->conn; } =20 if (trans) { @@ -497,7 +506,7 @@ apr_dbd_transaction **trans) { int ret =3D 0; - PGresult *res =3D PQexec(handle, "BEGIN TRANSACTION"); + PGresult *res =3D PQexec(handle->conn, "BEGIN TRANSACTION"); if (res) { ret =3D PQresultStatus(res); if (dbd_pgsql_is_success(ret)) { @@ -507,7 +516,7 @@ } } PQclear(res); - (*trans)->handle =3D handle; + (*trans)->handle =3D handle->conn; } else { ret =3D PGRES_FATAL_ERROR; @@ -543,21 +552,38 @@ =20 static apr_dbd_t *dbd_pgsql_open(apr_pool_t *pool, const char *params) { - return PQconnectdb(params); + apr_dbd_t *sql; + =20 + PGconn *conn =3D PQconnectdb(params); + + /* if there's an error in the connect string or something we get + * back a * bogus connection object, and things like PQreset are + * liable to segfault, so just close it out now. it would be nice + * if we could give an indication of why we failed to connect... */ + if (PQstatus(conn) !=3D CONNECTION_OK) { + PQfinish(conn); + return NULL; + } + + sql =3D apr_pcalloc (pool, sizeof (*sql)); + + sql->conn =3D conn; + + return sql; } =20 static apr_status_t dbd_pgsql_close(apr_dbd_t *handle) { - PQfinish(handle); + PQfinish(handle->conn); return APR_SUCCESS; } =20 static apr_status_t dbd_pgsql_check_conn(apr_pool_t *pool, apr_dbd_t *handle) { - if (PQstatus(handle) !=3D CONNECTION_OK) { - PQreset(handle); - if (PQstatus(handle) !=3D CONNECTION_OK) { + if (PQstatus(handle->conn) !=3D CONNECTION_OK) { + PQreset(handle->conn); + if (PQstatus(handle->conn) !=3D CONNECTION_OK) { return APR_EGENERAL; } } @@ -572,7 +598,7 @@ =20 static void *dbd_pgsql_native(apr_dbd_t *handle) { - return handle; + return handle->conn; } =20 static int dbd_pgsql_num_cols(apr_dbd_results* res)