You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ro...@apache.org on 2005/02/05 21:35:38 UTC
svn commit: r151525 - in apr/apr-util/trunk: dbd/apr_dbd_pgsql.c
include/apr_dbd.h test/testdbd.c
Author: rooneg
Date: Sat Feb 5 12:35:36 2005
New Revision: 151525
URL: http://svn.apache.org/viewcvs?view=rev&rev=151525
Log:
Move transaction bookkeeping into the apr_dbd_t object, thus avoiding
the need to pass explicit transactions into most of the dbd functions.
* include/apr_dbd.h
(apr_dbd_driver_t): remove transaction argument from query, select,
pvquery, pvselect, pquery, pselect methods.
(apr_dbd_query,
apr_dbd_select,
apr_dbd_pquery,
apr_dbd_pselect): remove transaction arguments.
* dbd/apr_dbd_pgsql.c
(apr_dbd_t): forward declare, cache in-progress transaction.
(apr_dbd_transaction_t): store a pointer to the dbd instead of just
the PGconn.
(dbd_pgsql_select,
dbd_pgsql_query,
dbd_pgsql_pquery,
dbd_pgsql_pvquery,
dbd_pgsql_pselect,
dbd_pgsql_pvselect): remove transaction arguments, use transaction in
the dbd instead.
(dbd_pgsql_transaction): use transaction inside dbd, note where we need
to handle recursive transactions.
(dbd_pgsql_end_transaction): use transaction inside dbd, null it out
when we're done with it.
* test/testdbd.c
(create_table,
drop_table,
insert_rows,
invalid_op,
select_sequential,
select_random,
test_transactions,
test_pquery): stop passing transactions to functions that no longer
take them.
Modified:
apr/apr-util/trunk/dbd/apr_dbd_pgsql.c
apr/apr-util/trunk/include/apr_dbd.h
apr/apr-util/trunk/test/testdbd.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?view=diff&r1=151524&r2=151525
==============================================================================
--- apr/apr-util/trunk/dbd/apr_dbd_pgsql.c (original)
+++ apr/apr-util/trunk/dbd/apr_dbd_pgsql.c Sat Feb 5 12:35:36 2005
@@ -27,15 +27,18 @@
#define QUERY_MAX_ARGS 40
-typedef struct {
- PGconn *conn;
-} apr_dbd_t;
+typedef struct apr_dbd_t apr_dbd_t;
typedef struct {
int errnum;
- PGconn *handle;
+ apr_dbd_t *handle;
} apr_dbd_transaction_t;
+struct apr_dbd_t {
+ PGconn *conn;
+ apr_dbd_transaction_t *trans;
+};
+
typedef struct {
int random;
PGconn *handle;
@@ -63,14 +66,13 @@
#include "apr_dbd.h"
static int dbd_pgsql_select(apr_pool_t *pool, apr_dbd_t *sql,
- apr_dbd_transaction_t *trans,
apr_dbd_results_t **results,
const char *query, int seek)
{
PGresult *res;
int ret;
- if ( trans && trans->errnum ) {
- return trans->errnum;
+ if ( sql->trans && sql->trans->errnum ) {
+ return sql->trans->errnum;
}
if (seek) { /* synchronous query */
res = PQexec(sql->conn, query);
@@ -85,8 +87,8 @@
ret = PGRES_FATAL_ERROR;
}
if (ret != 0) {
- if (trans) {
- trans->errnum = ret;
+ if (sql->trans) {
+ sql->trans->errnum = ret;
}
return ret;
}
@@ -102,8 +104,8 @@
}
else {
if (PQsendQuery(sql->conn, query) == 0) {
- if (trans) {
- trans->errnum = 1;
+ if (sql->trans) {
+ sql->trans->errnum = 1;
}
return 1;
}
@@ -191,13 +193,12 @@
return PQerrorMessage(sql->conn);
}
-static int dbd_pgsql_query(apr_dbd_t *sql, apr_dbd_transaction_t *trans,
- int *nrows, const char *query)
+static int dbd_pgsql_query(apr_dbd_t *sql, int *nrows, const char *query)
{
PGresult *res;
int ret;
- if (trans && trans->errnum) {
- return trans->errnum;
+ if (sql->trans && sql->trans->errnum) {
+ return sql->trans->errnum;
}
res = PQexec(sql->conn, query);
if (res) {
@@ -212,8 +213,8 @@
else {
ret = PGRES_FATAL_ERROR;
}
- if (trans) {
- trans->errnum = ret;
+ if (sql->trans) {
+ sql->trans->errnum = ret;
}
return ret;
}
@@ -344,9 +345,8 @@
}
static int dbd_pgsql_pquery(apr_pool_t *pool, apr_dbd_t *sql,
- apr_dbd_transaction_t *trans, int *nrows,
- apr_dbd_prepared_t *statement, int nargs,
- const char **values)
+ int *nrows, apr_dbd_prepared_t *statement,
+ int nargs, const char **values)
{
int ret;
PGresult *res;
@@ -369,23 +369,22 @@
ret = PGRES_FATAL_ERROR;
}
- if (trans) {
- trans->errnum = ret;
+ if (sql->trans) {
+ sql->trans->errnum = ret;
}
return ret;
}
static int dbd_pgsql_pvquery(apr_pool_t *pool, apr_dbd_t *sql,
- apr_dbd_transaction_t *trans, int *nrows,
- apr_dbd_prepared_t *statement, ...)
+ int *nrows, apr_dbd_prepared_t *statement, ...)
{
const char *arg;
int nargs = 0;
va_list args;
const char *values[QUERY_MAX_ARGS];
- if (trans && trans->errnum) {
- return trans->errnum;
+ if (sql->trans && sql->trans->errnum) {
+ return sql->trans->errnum;
}
va_start(args, statement);
while ( arg = va_arg(args, const char*), arg ) {
@@ -397,11 +396,10 @@
}
va_end(args);
values[nargs] = NULL;
- return dbd_pgsql_pquery(pool, sql, trans, nrows, statement, nargs, values);
+ return dbd_pgsql_pquery(pool, sql, nrows, statement, nargs, values);
}
static int dbd_pgsql_pselect(apr_pool_t *pool, apr_dbd_t *sql,
- apr_dbd_transaction_t *trans,
apr_dbd_results_t **results,
apr_dbd_prepared_t *statement,
int seek, int nargs, const char **values)
@@ -431,8 +429,8 @@
ret = PGRES_FATAL_ERROR;
}
if (ret != 0) {
- if (trans) {
- trans->errnum = ret;
+ if (sql->trans) {
+ sql->trans->errnum = ret;
}
return ret;
}
@@ -456,8 +454,8 @@
values, 0, 0, 0);
}
if (rv == 0) {
- if (trans) {
- trans->errnum = 1;
+ if (sql->trans) {
+ sql->trans->errnum = 1;
}
return 1;
}
@@ -468,14 +466,13 @@
(*results)->handle = sql->conn;
}
- if (trans) {
- trans->errnum = ret;
+ if (sql->trans) {
+ sql->trans->errnum = ret;
}
return ret;
}
static int dbd_pgsql_pvselect(apr_pool_t *pool, apr_dbd_t *sql,
- apr_dbd_transaction_t *trans,
apr_dbd_results_t **results,
apr_dbd_prepared_t *statement,
int seek, ...)
@@ -485,8 +482,8 @@
va_list args;
const char *values[QUERY_MAX_ARGS];
- if (trans && trans->errnum) {
- return trans->errnum;
+ if (sql->trans && sql->trans->errnum) {
+ return sql->trans->errnum;
}
va_start(args, seek);
@@ -498,7 +495,7 @@
values[nargs++] = apr_pstrdup(pool, arg);
}
va_end(args);
- return dbd_pgsql_pselect(pool, sql, trans, results, statement,
+ return dbd_pgsql_pselect(pool, sql, results, statement,
seek, nargs, values) ;
}
@@ -506,7 +503,11 @@
apr_dbd_transaction_t **trans)
{
int ret = 0;
- PGresult *res = PQexec(handle->conn, "BEGIN TRANSACTION");
+ PGresult *res;
+
+ /* XXX handle recursive transactions here */
+
+ res = PQexec(handle->conn, "BEGIN TRANSACTION");
if (res) {
ret = PQresultStatus(res);
if (dbd_pgsql_is_success(ret)) {
@@ -516,7 +517,8 @@
}
}
PQclear(res);
- (*trans)->handle = handle->conn;
+ (*trans)->handle = handle;
+ handle->trans = *trans;
}
else {
ret = PGRES_FATAL_ERROR;
@@ -531,10 +533,10 @@
if (trans) {
if (trans->errnum) {
trans->errnum = 0;
- res = PQexec(trans->handle, "ROLLBACK");
+ res = PQexec(trans->handle->conn, "ROLLBACK");
}
else {
- res = PQexec(trans->handle, "COMMIT");
+ res = PQexec(trans->handle->conn, "COMMIT");
}
if (res) {
ret = PQresultStatus(res);
@@ -546,6 +548,7 @@
else {
ret = PGRES_FATAL_ERROR;
}
+ trans->handle->trans = NULL;
}
return ret;
}
Modified: apr/apr-util/trunk/include/apr_dbd.h
URL: http://svn.apache.org/viewcvs/apr/apr-util/trunk/include/apr_dbd.h?view=diff&r1=151524&r2=151525
==============================================================================
--- apr/apr-util/trunk/include/apr_dbd.h (original)
+++ apr/apr-util/trunk/include/apr_dbd.h Sat Feb 5 12:35:36 2005
@@ -116,19 +116,16 @@
/** query: execute an SQL query that doesn't return a result set
*
* @param handle - the connection
- * @param transaction - current transaction. May be null.
* @param nrows - number of rows affected.
* @param statement - the SQL statement to execute
* @return 0 for success or error code
*/
- int (*query)(apr_dbd_t *handle, apr_dbd_transaction_t *trans,
- int *nrows, const char *statement);
+ int (*query)(apr_dbd_t *handle, int *nrows, const char *statement);
/** select: execute an SQL query that returns a result set
*
* @param pool - pool to allocate the result set
* @param handle - the connection
- * @param transaction - current transaction. May be null.
* @param res - pointer to result set pointer. May point to NULL on entry
* @param statement - the SQL statement to execute
* @param random - 1 to support random access to results (seek any row);
@@ -136,8 +133,7 @@
* (async access - faster)
* @return 0 for success or error code
*/
- int (*select)(apr_pool_t *pool, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, apr_dbd_results_t **res,
+ int (*select)(apr_pool_t *pool, apr_dbd_t *handle, apr_dbd_results_t **res,
const char *statement, int random);
/** num_cols: get the number of columns in a results set
@@ -212,21 +208,18 @@
*
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param nrows - number of rows affected.
* @param statement - the prepared statement to execute
* @param ... - args to prepared statement
* @return 0 for success or error code
*/
- int (*pvquery)(apr_pool_t *pool, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, int *nrows,
- apr_dbd_prepared_t *statement, ...);
+ int (*pvquery)(apr_pool_t *pool, apr_dbd_t *handle, int *nrows,
+ apr_dbd_prepared_t *statement, ...);
/** pvselect: select using a prepared statement + args
*
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param res - pointer to query results. May point to NULL on entry
* @param statement - the prepared statement to execute
* @param random - Whether to support random-access to results
@@ -234,29 +227,26 @@
* @return 0 for success or error code
*/
int (*pvselect)(apr_pool_t *pool, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, apr_dbd_results_t **res,
+ apr_dbd_results_t **res,
apr_dbd_prepared_t *statement, int random, ...);
/** pquery: query using a prepared statement + args
*
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param nrows - number of rows affected.
* @param statement - the prepared statement to execute
* @param nargs - number of args to prepared statement
* @param args - args to prepared statement
* @return 0 for success or error code
*/
- int (*pquery)(apr_pool_t *pool, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, int *nrows,
+ int (*pquery)(apr_pool_t *pool, apr_dbd_t *handle, int *nrows,
apr_dbd_prepared_t *statement, int nargs, const char **args);
/** pselect: select using a prepared statement + args
*
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param res - pointer to query results. May point to NULL on entry
* @param statement - the prepared statement to execute
* @param random - Whether to support random-access to results
@@ -265,9 +255,8 @@
* @return 0 for success or error code
*/
int (*pselect)(apr_pool_t *pool, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, apr_dbd_results_t **res,
- apr_dbd_prepared_t *statement, int random, int nargs,
- const char **args);
+ apr_dbd_results_t **res, apr_dbd_prepared_t *statement,
+ int random, int nargs, const char **args);
} apr_dbd_driver_t;
@@ -408,17 +397,15 @@
*
* @param driver - the driver
* @param handle - the connection
- * @param transaction - current transaction. May be null.
* @param nrows - number of rows affected.
* @param statement - the SQL statement to execute
* @return 0 for success or error code
*/
APU_DECLARE(int) apr_dbd_query(apr_dbd_driver_t *driver, apr_dbd_t *handle,
- apr_dbd_transaction_t *trans, int *nrows,
- const char *statement);
+ int *nrows, const char *statement);
#else
-#define apr_dbd_query(driver,handle,trans,nrows,statement) \
- (driver)->query((handle),(trans),(nrows),(statement))
+#define apr_dbd_query(driver,handle,nrows,statement) \
+ (driver)->query((handle),(nrows),(statement))
#endif
#ifdef DOXYGEN
@@ -427,7 +414,6 @@
* @param driver - the driver
* @param pool - pool to allocate the result set
* @param handle - the connection
- * @param transaction - current transaction. May be null.
* @param res - pointer to result set pointer. May point to NULL on entry
* @param statement - the SQL statement to execute
* @param random - 1 to support random access to results (seek any row);
@@ -436,12 +422,11 @@
* @return 0 for success or error code
*/
APU_DECLARE(int) apr_dbd_select(apr_dbd_driver_t *driver, apr_pool_t *pool,
- apr_dbd_t *handle, apr_dbd_transaction_t *trans,
- apr_dbd_results_t *res, const char *statement,
- int random);
+ apr_dbd_t *handle, apr_dbd_results_t *res,
+ const char *statement, int random);
#else
-#define apr_dbd_select(driver,pool,handle,trans,res,statement,random) \
- (driver)->select((pool),(handle),(trans),(res),(statement),(random))
+#define apr_dbd_select(driver,pool,handle,res,statement,random) \
+ (driver)->select((pool),(handle),(res),(statement),(random))
#endif
#ifdef DOXYGEN
@@ -572,7 +557,6 @@
* @param driver - the driver
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param nrows - number of rows affected.
* @param statement - the prepared statement to execute
* @param nargs - number of args to prepared statement
@@ -580,12 +564,12 @@
* @return 0 for success or error code
*/
APU_DECLARE(int) apr_dbd_pquery(apr_dbd_driver_t *driver, apr_pool_t *pool,
- apr_dbd_t *handle, apr_dbd_transaction_t *trans,
- int *nrows, apr_dbd_prepared_t *statement,
- int nargs, const char **args);
+ apr_dbd_t *handle, int *nrows,
+ apr_dbd_prepared_t *statement, int nargs,
+ const char **args);
#else
-#define apr_dbd_pquery(driver,pool,handle,trans,nrows,statement,nargs,args) \
- (driver)->pquery((pool),(handle),(trans),(nrows),(statement), \
+#define apr_dbd_pquery(driver,pool,handle,nrows,statement,nargs,args) \
+ (driver)->pquery((pool),(handle),(nrows),(statement), \
(nargs),(args))
#endif
@@ -595,7 +579,6 @@
* @param driver - the driver
* @param pool - working pool
* @param handle - the connection
- * @param trans - current transaction. May be null.
* @param res - pointer to query results. May point to NULL on entry
* @param statement - the prepared statement to execute
* @param random - Whether to support random-access to results
@@ -604,13 +587,12 @@
* @return 0 for success or error code
*/
APU_DECLARE(int) apr_dbd_pselect(apr_dbd_driver_t *driver, apr_pool_t *pool,
- apr_dbd_t *handle, apr_dbd_transaction_t *trans,
- apr_dbd_results_t **res,
+ apr_dbd_t *handle, apr_dbd_results_t **res,
apr_dbd_prepared_t *statement, int random,
int nargs, const char **args);
#else
-#define apr_dbd_pselect(driver,pool,handle,trans,res,statement,random,nargs,args) \
- (driver)->pselect((pool),(handle),(trans),(res),(statement), \
+#define apr_dbd_pselect(driver,pool,handle,res,statement,random,nargs,args) \
+ (driver)->pselect((pool),(handle),(res),(statement), \
(random),(nargs),(args))
#endif
Modified: apr/apr-util/trunk/test/testdbd.c
URL: http://svn.apache.org/viewcvs/apr/apr-util/trunk/test/testdbd.c?view=diff&r1=151524&r2=151525
==============================================================================
--- apr/apr-util/trunk/test/testdbd.c (original)
+++ apr/apr-util/trunk/test/testdbd.c Sat Feb 5 12:35:36 2005
@@ -40,7 +40,7 @@
"col1 varchar(40) not null,"
"col2 varchar(40),"
"col3 integer)" ;
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
return rv;
}
static int drop_table(apr_pool_t* pool, apr_dbd_t* handle,
@@ -49,7 +49,7 @@
int rv = 0;
int nrows;
const char *statement = "DROP TABLE apr_dbd_test" ;
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
return rv;
}
static int insert_rows(apr_pool_t* pool, apr_dbd_t* handle,
@@ -66,7 +66,7 @@
"INSERT into apr_dbd_test values ('qwerty', 'foo', 0);"
"INSERT into apr_dbd_test values ('asdfgh', 'bar', 1);"
;
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
if (rv) {
const char* stmt[] = {
"INSERT into apr_dbd_test (col1) values ('foo');",
@@ -79,7 +79,7 @@
printf("Compound insert failed; trying statements one-by-one\n") ;
for (i=0; stmt[i] != NULL; ++i) {
statement = stmt[i];
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
if (rv) {
nerrors++;
}
@@ -96,11 +96,11 @@
int rv = 0;
int nrows;
const char *statement = "INSERT into apr_dbd_test1 (col2) values ('foo')" ;
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
printf("invalid op returned %d (should be nonzero). Error msg follows\n", rv);
printf("'%s'\n", apr_dbd_error(driver, handle, rv));
statement = "INSERT into apr_dbd_test (col1, col2) values ('bar', 'foo')" ;
- rv = apr_dbd_query(driver, handle, NULL, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
printf("valid op returned %d (should be zero; error shouldn't affect subsequent ops)\n", rv);
return rv;
}
@@ -114,7 +114,7 @@
const char* statement = "SELECT * FROM apr_dbd_test ORDER BY col1, col2";
apr_dbd_results_t *res = NULL;
apr_dbd_row_t *row = NULL;
- rv = apr_dbd_select(driver,pool,handle,NULL,&res,statement,0);
+ rv = apr_dbd_select(driver,pool,handle,&res,statement,0);
if (rv) {
printf("Select failed: %s", apr_dbd_error(driver, handle, rv));
return rv;
@@ -145,7 +145,7 @@
const char* statement = "SELECT * FROM apr_dbd_test ORDER BY col1, col2";
apr_dbd_results_t *res = NULL;
apr_dbd_row_t *row = NULL;
- rv = apr_dbd_select(driver,pool,handle,NULL,&res,statement,1);
+ rv = apr_dbd_select(driver,pool,handle,&res,statement,1);
if (rv) {
printf("Select failed: %s", apr_dbd_error(driver, handle, rv));
return rv;
@@ -209,7 +209,7 @@
return rv;
}
statement = "UPDATE apr_dbd_test SET col2 = 'failed'";
- rv = apr_dbd_query(driver, handle, trans, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
if (rv) {
printf("Update failed: '%s'\n", apr_dbd_error(driver, handle, rv));
apr_dbd_transaction_end(driver, pool, trans);
@@ -218,12 +218,12 @@
printf("%d rows updated\n", nrows);
statement = "INSERT INTO apr_dbd_test1 (col3) values (3)";
- rv = apr_dbd_query(driver, handle, trans, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
if (!rv) {
printf("Oops, invalid op succeeded but shouldn't!\n");
}
statement = "INSERT INTO apr_dbd_test values ('zzz', 'aaa', 3)";
- rv = apr_dbd_query(driver, handle, trans, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
printf("Valid insert returned %d. Should be nonzero (fail) because transaction is bad\n", rv) ;
rv = apr_dbd_transaction_end(driver, pool, trans);
@@ -245,7 +245,7 @@
return rv;
}
statement = "UPDATE apr_dbd_test SET col2 = 'success'";
- rv = apr_dbd_query(driver, handle, trans, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
if (rv) {
printf("Update failed: '%s'\n", apr_dbd_error(driver, handle, rv));
apr_dbd_transaction_end(driver, pool, trans);
@@ -253,7 +253,7 @@
}
printf("%d rows updated\n", nrows);
statement = "INSERT INTO apr_dbd_test values ('aaa', 'zzz', 3)";
- rv = apr_dbd_query(driver, handle, trans, &nrows, statement);
+ rv = apr_dbd_query(driver, handle, &nrows, statement);
printf("Valid insert returned %d. Should be zero (OK)\n", rv) ;
rv = apr_dbd_transaction_end(driver, pool, trans);
if (rv) {
@@ -284,7 +284,7 @@
apr_dbd_error(driver, handle, rv));
return rv;
}
- rv = driver->pvselect(pool, handle, NULL, &res, statement, 0, "3", NULL);
+ rv = driver->pvselect(pool, handle, &res, statement, 0, "3", NULL);
if (rv) {
printf("Exec of prepared statement failed!\n%s\n",
apr_dbd_error(driver, handle, rv));
@@ -327,7 +327,7 @@
return rv;
}
apr_dbd_transaction_start(driver, pool, handle, &trans);
- rv = driver->pvquery(pool, handle, trans, &nrows, statement,
+ rv = driver->pvquery(pool, handle, &nrows, statement,
"prepared", "insert", "2", NULL);
apr_dbd_transaction_end(driver, pool, trans);
if (rv) {