Author: turnstep
Date: Sat Feb 16 19:18:02 2008
New Revision: 10753

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/Pg.h
   DBD-Pg/trunk/TODO
   DBD-Pg/trunk/dbdimp.c

Log:
Finish threaded Perl support.


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes        (original)
+++ DBD-Pg/trunk/Changes        Sat Feb 16 19:18:02 2008
@@ -3,6 +3,7 @@
 2.0.1
 
        - Use version.pm [CPAN bug #33206] [GSM]
+       - Add PERL_NO_GET_CONTEXT #define to improve performance on threaded 
Perls
        - Remove '//' style comments to make strict ANSI compilers happy.
                (Trevor Inman) [CPAN bug #33089]
        - Force client encoding of UTF8 for some tests.

Modified: DBD-Pg/trunk/Pg.h
==============================================================================
--- DBD-Pg/trunk/Pg.h   (original)
+++ DBD-Pg/trunk/Pg.h   Sat Feb 16 19:18:02 2008
@@ -23,6 +23,13 @@
 
 #include "libpq-fe.h"
 
+#ifndef INV_READ
+#define INV_READ 0x00040000
+#endif
+#ifndef INV_WRITE
+#define INV_WRITE 0x00020000
+#endif
+
 #ifdef BUFSIZ
 #undef BUFSIZ
 #endif

Modified: DBD-Pg/trunk/TODO
==============================================================================
--- DBD-Pg/trunk/TODO   (original)
+++ DBD-Pg/trunk/TODO   Sat Feb 16 19:18:02 2008
@@ -17,3 +17,4 @@
 - Remove libpq dependency
 - Handle and/or better tests for different encoding, especially those not 
    supported as a server encoding (e.g. BIG5)
+- Make a CPAN bundle

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c       (original)
+++ DBD-Pg/trunk/dbdimp.c       Sat Feb 16 19:18:02 2008
@@ -78,22 +78,23 @@
        (SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV &&                             
        \
         SvRMAGICAL(SvRV(h)) && (SvMAGIC(SvRV(h)))->mg_type == 'P')
 
-static void pg_error(SV *h, ExecStatusType error_num, const char *error_msg);
+static void pg_error(pTHX_ SV *h, ExecStatusType error_num, const char 
*error_msg);
 static void pg_warn (void * arg, const char * message);
-static ExecStatusType _result(imp_dbh_t *imp_dbh, const char *sql);
-static ExecStatusType _sqlstate(imp_dbh_t *imp_dbh, PGresult *result);
-static int dbd_db_rollback_commit (SV *dbh, imp_dbh_t *imp_dbh, char * action);
-static void dbd_st_split_statement (imp_sth_t *imp_sth, int version, char 
*statement);
-static int dbd_st_prepare_statement (SV *sth, imp_sth_t *imp_sth);
-static int is_high_bit_set(const unsigned char *val, STRLEN size);
-static int dbd_st_deallocate_statement(SV *sth, imp_sth_t *imp_sth);
-static PGTransactionStatusType dbd_db_txn_status (imp_dbh_t *imp_dbh);
-static int pg_db_start_txn (SV *dbh, imp_dbh_t *imp_dbh);
-static int handle_old_async(SV * handle, imp_dbh_t * imp_dbh, int asyncflag);
+static ExecStatusType _result(pTHX_ imp_dbh_t *imp_dbh, const char *sql);
+static ExecStatusType _sqlstate(pTHX_ imp_dbh_t *imp_dbh, PGresult *result);
+static int dbd_db_rollback_commit (pTHX_ SV *dbh, imp_dbh_t *imp_dbh, char * 
action);
+static void dbd_st_split_statement (pTHX_ imp_sth_t *imp_sth, int version, 
char *statement);
+static int dbd_st_prepare_statement (pTHX_ SV *sth, imp_sth_t *imp_sth);
+static int is_high_bit_set(pTHX_ const unsigned char *val, STRLEN size);
+static int dbd_st_deallocate_statement(pTHX_ SV *sth, imp_sth_t *imp_sth);
+static PGTransactionStatusType dbd_db_txn_status (pTHX_ imp_dbh_t *imp_dbh);
+static int pg_db_start_txn (pTHX_ SV *dbh, imp_dbh_t *imp_dbh);
+static int handle_old_async(pTHX_ SV * handle, imp_dbh_t * imp_dbh, int 
asyncflag);
 
 /* ================================================================== */
 void dbd_init (dbistate_t *dbistate)
 {
+       dTHX;
        DBIS = dbistate;
 }
 
@@ -102,6 +103,7 @@
 int dbd_db_login (SV * dbh, imp_dbh_t * imp_dbh, char * dbname, char * uid, 
char * pwd)
 {
 
+       dTHX;
        char *         conn_str;
        char *         dest;
        bool           inquote = DBDPG_FALSE;
@@ -190,7 +192,7 @@
        /* Check to see that the backend connection was successfully made */
        connstatus = PQstatus(imp_dbh->conn);
        if (CONNECTION_OK != connstatus) {
-               pg_error(dbh, (ExecStatusType)connstatus, 
PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, (ExecStatusType)connstatus, 
PQerrorMessage(imp_dbh->conn));
                strncpy(imp_dbh->sqlstate, "08006", 6); /* "CONNECTION FAILURE" 
*/
                PQfinish(imp_dbh->conn);
                sv_free((SV *)imp_dbh->savepoints);
@@ -214,7 +216,7 @@
                ExecStatusType status;
 
                result = PQexec(imp_dbh->conn, "SELECT version(), 'DBD::Pg'");
-               status = _sqlstate(imp_dbh, result);
+               status = _sqlstate(aTHX_ imp_dbh, result);
 
                if (!result || PGRES_TUPLES_OK != status || 
(0==PQntuples(result))) {
                        if (dbis->debug >= 1)
@@ -264,7 +266,7 @@
 /* ================================================================== */
 
 /* Database specific error handling. */
-static void pg_error (SV * h, ExecStatusType error_num, const char * error_msg)
+static void pg_error (pTHX_ SV * h, ExecStatusType error_num, const char * 
error_msg)
 {
        D_imp_xxh(h);
        char *      err;
@@ -298,6 +300,7 @@
 /* Turn database notices into perl warnings for proper handling. */
 static void pg_warn (void * arg, const char * message)
 {
+       dTHX;
        D_imp_dbh( sv_2mortal(newRV((SV*)arg)) );
 
        if (dbis->debug >= 4)
@@ -313,7 +316,7 @@
 
 /* ================================================================== */
 /* Quick command executor used throughout this file */
-static ExecStatusType _result(imp_dbh_t * imp_dbh, const char * sql)
+static ExecStatusType _result(pTHX_ imp_dbh_t * imp_dbh, const char * sql)
 {
        PGresult *     result;
        ExecStatusType status;
@@ -326,7 +329,7 @@
 
        result = PQexec(imp_dbh->conn, sql);
 
-       status = _sqlstate(imp_dbh, result);
+       status = _sqlstate(aTHX_ imp_dbh, result);
 
        PQclear(result);
 
@@ -337,7 +340,7 @@
 
 /* ================================================================== */
 /* Set the SQLSTATE based on a result, returns the status */
-static ExecStatusType _sqlstate(imp_dbh_t * imp_dbh, PGresult * result)
+static ExecStatusType _sqlstate(pTHX_ imp_dbh_t * imp_dbh, PGresult * result)
 {
        ExecStatusType status   = PGRES_FATAL_ERROR; /* until proven otherwise 
*/
        bool           stateset = DBDPG_FALSE;
@@ -384,7 +387,7 @@
                (void)PerlIO_printf(DBILOGFP, "dbdpg: _sqlstate set 
imp_dbh->sqlstate to %s\n", imp_dbh->sqlstate);
 
        if (dbis->debug >= 7)
-               (void)PerlIO_printf(DBILOGFP, "dbdpg: _sqlstate txn_status is 
%d\n", dbd_db_txn_status(imp_dbh));
+               (void)PerlIO_printf(DBILOGFP, "dbdpg: _sqlstate txn_status is 
%d\n", dbd_db_txn_status(aTHX_ imp_dbh));
 
 
        return status;
@@ -395,6 +398,7 @@
 /* ================================================================== */
 int dbd_db_ping (SV * dbh)
 {
+       dTHX;
        D_imp_dbh(dbh);
        PGTransactionStatusType tstatus;
        ExecStatusType          status;
@@ -405,7 +409,7 @@
        if (NULL == imp_dbh->conn)
                return -1;
 
-       tstatus = dbd_db_txn_status(imp_dbh);
+       tstatus = dbd_db_txn_status(aTHX_ imp_dbh);
 
        if (dbis->debug >= 6)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_db_ping txn_status is 
%d\n", tstatus);
@@ -418,7 +422,7 @@
 
        /* Even though it may be reported as normal, we have to make sure by 
issuing a command */
 
-       status = _result(imp_dbh, "SELECT 'DBD::Pg ping test'");
+       status = _result(aTHX_ imp_dbh, "SELECT 'DBD::Pg ping test'");
 
        if (PGRES_TUPLES_OK == status)
                return 1;
@@ -429,7 +433,7 @@
 
 
 /* ================================================================== */
-static PGTransactionStatusType dbd_db_txn_status (imp_dbh_t * imp_dbh)
+static PGTransactionStatusType dbd_db_txn_status (pTHX_ imp_dbh_t * imp_dbh)
 {
        return PQtransactionStatus(imp_dbh->conn);
 
@@ -439,7 +443,7 @@
 /* rollback and commit share so much code they get one function: */
 
 /* ================================================================== */
-static int dbd_db_rollback_commit (SV * dbh, imp_dbh_t * imp_dbh, char * 
action)
+static int dbd_db_rollback_commit (pTHX_ SV * dbh, imp_dbh_t * imp_dbh, char * 
action)
 {
        PGTransactionStatusType tstatus;
        ExecStatusType          status;
@@ -457,7 +461,7 @@
        /* We only perform these actions if we need to. For newer servers, we 
           ask it for the status directly and double-check things */
 
-       tstatus = dbd_db_txn_status(imp_dbh);
+       tstatus = dbd_db_txn_status(aTHX_ imp_dbh);
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_db_%s txn_status is 
%d\n", action, tstatus);
 
@@ -495,13 +499,13 @@
        if (!imp_dbh->done_begin)
                return 1;
 
-       status = _result(imp_dbh, action);
+       status = _result(aTHX_ imp_dbh, action);
                
        /* Set this early, for scripts that continue despite the error below */
        imp_dbh->done_begin = DBDPG_FALSE;
 
        if (PGRES_COMMAND_OK != status) {
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                return 0;
        }
 
@@ -516,20 +520,22 @@
 /* ================================================================== */
 int dbd_db_commit (SV * dbh, imp_dbh_t * imp_dbh)
 {
-       return dbd_db_rollback_commit(dbh, imp_dbh, "commit");
+       dTHX;
+       return dbd_db_rollback_commit(aTHX_ dbh, imp_dbh, "commit");
 }
 
 /* ================================================================== */
 int dbd_db_rollback (SV * dbh, imp_dbh_t * imp_dbh)
 {
-       return dbd_db_rollback_commit(dbh, imp_dbh, "rollback");
+       dTHX;
+       return dbd_db_rollback_commit(aTHX_ dbh, imp_dbh, "rollback");
 }
 
 
 /* ================================================================== */
 int dbd_db_disconnect (SV * dbh, imp_dbh_t * imp_dbh)
 {
-       
+       dTHX;
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_db_disconnect\n");
 
@@ -559,6 +565,7 @@
 /* ================================================================== */
 void dbd_db_destroy (SV * dbh, imp_dbh_t * imp_dbh)
 {
+       dTHX;
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_db_destroy\n");
 
@@ -583,6 +590,7 @@
 /* ================================================================== */
 SV * dbd_db_FETCH_attrib (SV * dbh, imp_dbh_t * imp_dbh, SV * keysv)
 {
+       dTHX;
        STRLEN kl;
        char * key = SvPV(keysv,kl);
        SV *   retsv = Nullsv;
@@ -706,6 +714,7 @@
 /* ================================================================== */
 int dbd_db_STORE_attrib (SV * dbh, imp_dbh_t * imp_dbh, SV * keysv, SV * 
valuesv)
 {
+       dTHX;
        STRLEN       kl;
        char *       key = SvPV(keysv,kl);
        unsigned int newval = SvTRUE(valuesv);
@@ -808,6 +817,7 @@
 /* ================================================================== */
 SV * dbd_st_FETCH_attrib (SV * sth, imp_sth_t * imp_sth, SV * keysv)
 {
+       dTHX;
        STRLEN            kl;
        char *            key = SvPV(keysv,kl);
        SV *              retsv = Nullsv;
@@ -930,7 +940,7 @@
                                fieldname = PQfname(imp_sth->result, fields);
                                sv_fieldname = newSVpv(fieldname,0);
 #ifdef is_utf8_string
-                               if (is_high_bit_set((unsigned char *)fieldname, 
strlen(fieldname)) && is_utf8_string((unsigned char *)fieldname, 
strlen(fieldname)))
+                               if (is_high_bit_set(aTHX_ (unsigned char 
*)fieldname, strlen(fieldname)) && is_utf8_string((unsigned char *)fieldname, 
strlen(fieldname)))
                                        SvUTF8_on(sv_fieldname);
 #endif
                                (void)av_store(av, fields, sv_fieldname);
@@ -1092,6 +1102,7 @@
 /* ================================================================== */
 int dbd_st_STORE_attrib (SV * sth, imp_sth_t * imp_sth, SV * keysv, SV * 
valuesv)
 {
+       dTHX;
        STRLEN kl;
        char * key = SvPV(keysv,kl);
        STRLEN vl;
@@ -1155,7 +1166,8 @@
 /* ================================================================== */
 int dbd_discon_all (SV * drh, imp_drh_t * imp_drh)
 {
-       
+       dTHX;
+
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_discon_all drh=%d\n", 
drh);
 
@@ -1174,7 +1186,7 @@
 /* ================================================================== */
 int dbd_db_getfd (SV * dbh, imp_dbh_t * imp_dbh)
 {
-
+       dTHX;
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_db_getfd dbh=%d\n", 
dbh);
 
@@ -1186,6 +1198,7 @@
 /* ================================================================== */
 SV * dbd_db_pg_notifies (SV * dbh, imp_dbh_t * imp_dbh)
 {
+       dTHX;
        int        status;
        PGnotify * notify;
        AV *       ret;
@@ -1196,7 +1209,7 @@
 
        status = PQconsumeInput(imp_dbh->conn);
        if (0 == status) { 
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                return &sv_undef;
        }
 
@@ -1221,6 +1234,7 @@
 /* ================================================================== */
 int dbd_st_prepare (SV * sth, imp_sth_t * imp_sth, char * statement, SV * 
attribs)
 {
+       dTHX;
        D_imp_dbh_from_sth;
        STRLEN mypos=0, wordstart, newsize; /* Used to find and set firstword */
        SV **svp; /* To help parse the arguments */
@@ -1311,7 +1325,7 @@
        statement -= mypos; /* Rewind statement */
 
        /* Break the statement into segments by placeholder */
-       dbd_st_split_statement(imp_sth, imp_dbh->pg_server_version, statement);
+       dbd_st_split_statement(aTHX_ imp_sth, imp_dbh->pg_server_version, 
statement);
 
        /*
          We prepare it right away if:
@@ -1337,7 +1351,7 @@
                if (dbis->debug >= 5)
                        (void)PerlIO_printf(DBILOGFP, "dbdpg: Running an 
immediate prepare\n");
 
-               if (dbd_st_prepare_statement(sth, imp_sth)!=0) {
+               if (dbd_st_prepare_statement(aTHX_ sth, imp_sth)!=0) {
                        croak (PQerrorMessage(imp_dbh->conn));
                }
        }
@@ -1350,7 +1364,7 @@
 
 
 /* ================================================================== */
-static void dbd_st_split_statement (imp_sth_t * imp_sth, int version, char * 
statement)
+static void dbd_st_split_statement (pTHX_ imp_sth_t * imp_sth, int version, 
char * statement)
 {
 
        /* Builds the "segment" and "placeholder" structures for a statement 
handle */
@@ -1831,9 +1845,8 @@
 
 
 /* ================================================================== */
-static int dbd_st_prepare_statement (SV * sth, imp_sth_t * imp_sth)
+static int dbd_st_prepare_statement (pTHX_ SV * sth, imp_sth_t * imp_sth)
 {
-
        D_imp_dbh_from_sth;
        char *       statement;
        unsigned int x;
@@ -1930,7 +1943,7 @@
                (void)PerlIO_printf(DBILOGFP, "dbdpg: Prepared statement 
(%s)\n", statement);
 
        if (oldprepare) {
-               status = _result(imp_dbh, statement);
+               status = _result(aTHX_ imp_dbh, statement);
        }
        else {
                int params = 0;
@@ -1955,7 +1968,7 @@
        }
        Safefree(statement);
        if (PGRES_COMMAND_OK != status) {
-               pg_error(sth, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ sth, status, PQerrorMessage(imp_dbh->conn));
                return -2;
        }
 
@@ -1971,7 +1984,7 @@
 /* ================================================================== */
 int dbd_bind_ph (SV * sth, imp_sth_t * imp_sth, SV * ph_name, SV * newvalue, 
IV sql_type, SV * attribs, int is_inout, IV maxlen)
 {
-
+       dTHX;
        D_imp_dbh_from_sth;
        char * name = Nullch;
        STRLEN name_len;
@@ -2169,7 +2182,7 @@
                if (dbis->debug >= 5)
                        (void)PerlIO_printf(DBILOGFP, "dbdpg: Binding has 
forced a re-prepare\n");
                /* Deallocate sets the prepare_name to NULL */
-               if (dbd_st_deallocate_statement(sth, imp_sth)!=0) {
+               if (dbd_st_deallocate_statement(aTHX_ sth, imp_sth)!=0) {
                        /* Deallocation failed. Let's mark it and move on */
                        Safefree(imp_sth->prepare_name);
                        imp_sth->prepare_name = NULL;
@@ -2191,6 +2204,7 @@
 /* ================================================================== */
 SV * pg_stringify_array(SV *input, const char * array_delim, int 
server_version) {
 
+       dTHX;
        AV * toparr;
        AV * currarr;
        AV * lastarr;
@@ -2320,7 +2334,8 @@
 } /* end of pg_stringify_array */
 
 /* ================================================================== */
-static SV * pg_destringify_array(imp_dbh_t *imp_dbh, unsigned char * input, 
sql_type_info_t * coltype) {
+static SV * pg_destringify_array(pTHX_ imp_dbh_t *imp_dbh, unsigned char * 
input, sql_type_info_t * coltype)
+{
 
        AV*    av;              /* The main array we are returning a reference 
to */
        AV*    currentav;       /* The current array level */
@@ -2399,7 +2414,7 @@
 #ifdef is_utf8_string
                                        if (imp_dbh->pg_enable_utf8) {
                                                SvUTF8_off(sv);
-                                               if (is_high_bit_set((unsigned 
char *)string, section_size) && is_utf8_string((unsigned char*)string, 
section_size)) {
+                                               if (is_high_bit_set(aTHX_ 
(unsigned char *)string, section_size) && is_utf8_string((unsigned 
char*)string, section_size)) {
                                                        SvUTF8_on(sv);
                                                }
                                        }
@@ -2447,6 +2462,7 @@
 /* ================================================================== */
 int pg_quickexec (SV * dbh, const char * sql, int asyncflag)
 {
+       dTHX;
        D_imp_dbh(dbh);
        PGresult *              result;
        ExecStatusType          status = PGRES_FATAL_ERROR; /* Assume the worst 
*/
@@ -2474,16 +2490,16 @@
        /* If we are still waiting on an async, handle it */
        if (imp_dbh->async_status) {
          if (dbis->debug >= 4) (void)PerlIO_printf(DBILOGFP, "dbdpg: handling 
old async\n");
-         rows = handle_old_async(dbh, imp_dbh, asyncflag);
+         rows = handle_old_async(aTHX_ dbh, imp_dbh, asyncflag);
          if (rows)
                return rows;
        }
 
        /* If not autocommit, start a new transaction */
        if (!imp_dbh->done_begin && !DBIc_has(imp_dbh, DBIcf_AutoCommit)) {
-               status = _result(imp_dbh, "begin");
+               status = _result(aTHX_ imp_dbh, "begin");
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                        return -2;
                }
                imp_dbh->done_begin = DBDPG_TRUE;
@@ -2494,7 +2510,7 @@
          if (dbis->debug >= 4) (void)PerlIO_printf(DBILOGFP, "dbdpg: Going 
asychronous with do()\n");
          if (! PQsendQuery(imp_dbh->conn, sql)) {
                if (dbis->debug >= 4) (void)PerlIO_printf(DBILOGFP, "dbdpg: 
PQsendQuery failed\n");
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                return -2;
          }
          imp_dbh->async_status = 1;
@@ -2507,7 +2523,7 @@
                (void)PerlIO_printf(DBILOGFP, "%s\n\n", sql);
 
        result = PQexec(imp_dbh->conn, sql);
-       status = _sqlstate(imp_dbh, result);
+       status = _sqlstate(aTHX_ imp_dbh, result);
 
        imp_dbh->copystate = 0; /* Assume not in copy mode until told otherwise 
*/
 
@@ -2533,12 +2549,12 @@
        case PGRES_BAD_RESPONSE:
        case PGRES_NONFATAL_ERROR:
                rows = -2;
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                break;
        case PGRES_FATAL_ERROR:
        default:
                rows = -2;
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                break;
        }
 
@@ -2571,6 +2587,7 @@
 /* Return value <= -2:error, >=0:ok row count, (-1=unknown count) */
 int dbd_st_execute (SV * sth, imp_sth_t * imp_sth)
 {
+       dTHX;
        D_imp_dbh_from_sth;
        ph_t *        currph;
        int           status = -1;
@@ -2598,7 +2615,7 @@
        if (imp_sth->numphs!=0) {
                for (currph=imp_sth->ph; NULL != currph; currph=currph->nextph) 
{
                        if (NULL == currph->bind_type) {
-                               pg_error(sth, PGRES_FATAL_ERROR, "execute 
called with an unbound placeholder");
+                               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "execute 
called with an unbound placeholder");
                                return -2;
                        }
                        if (currph->isinout) {
@@ -2616,16 +2633,16 @@
                (void)PerlIO_printf
                  (DBILOGFP, "dbdpg: Attempting to handle existing async 
transaction\n");
          }       
-         ret = handle_old_async(sth, imp_dbh, imp_sth->async_flag);
+         ret = handle_old_async(aTHX_ sth, imp_dbh, imp_sth->async_flag);
          if (ret)
                return ret;
        }
 
        /* If not autocommit, start a new transaction */
        if (!imp_dbh->done_begin && !DBIc_has(imp_dbh, DBIcf_AutoCommit)) {
-               status = _result(imp_dbh, "begin");
+               status = _result(aTHX_ imp_dbh, "begin");
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(sth, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ sth, status, 
PQerrorMessage(imp_dbh->conn));
                        return -2;
                }
                imp_dbh->done_begin = DBDPG_TRUE;
@@ -2743,7 +2760,7 @@
                                if (dbis->debug >= 5)
                                        (void)PerlIO_printf(DBILOGFP, "dbdpg: 
Re-preparing statement\n");
                        }
-                       if (dbd_st_prepare_statement(sth, imp_sth)!=0) {
+                       if (dbd_st_prepare_statement(aTHX_ sth, imp_sth)!=0) {
                                Safefree(paramValues);
                                Safefree(paramLengths);
                                Safefree(paramFormats);
@@ -2918,7 +2935,7 @@
                return 0;
        }
 
-       status = _sqlstate(imp_dbh, imp_sth->result);
+       status = _sqlstate(aTHX_ imp_dbh, imp_sth->result);
 
        imp_dbh->copystate = 0; /* Assume not in copy mode until told otherwise 
*/
        if (PGRES_TUPLES_OK == status) {
@@ -2969,7 +2986,7 @@
        else {
                if (dbis->debug >= 5)
                        (void)PerlIO_printf(DBILOGFP, "dbdpg: Invalid status 
returned (%d)\n", status);
-               pg_error(sth, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ sth, status, PQerrorMessage(imp_dbh->conn));
                return -2;
        }
        
@@ -2983,7 +3000,7 @@
 
 
 /* ================================================================== */
-static int is_high_bit_set(const unsigned char * val, STRLEN size)
+static int is_high_bit_set(pTHX_ const unsigned char * val, STRLEN size)
 {
        while (*val && size--)
                if (*val++ & 0x80) return 1;
@@ -2994,6 +3011,7 @@
 /* ================================================================== */
 AV * dbd_st_fetch (SV * sth, imp_sth_t * imp_sth)
 {
+       dTHX;
        D_imp_dbh_from_sth;
        sql_type_info_t * type_info;
        int               num_fields;
@@ -3010,7 +3028,7 @@
 
        /* Check that execute() was executed successfully */
        if ( !DBIc_ACTIVE(imp_sth) ) {
-               pg_error(sth, PGRES_NONFATAL_ERROR, "no statement 
executing\n");        
+               pg_error(aTHX_ sth, PGRES_NONFATAL_ERROR, "no statement 
executing\n");  
                return Nullav;
        }
        
@@ -3060,7 +3078,7 @@
                        if (type_info
                                && 0 == strncmp(type_info->arrayout, "array", 5)
                                && imp_dbh->expand_array) {
-                               AvARRAY(av)[i] = pg_destringify_array(imp_dbh, 
value, type_info);
+                               AvARRAY(av)[i] = pg_destringify_array(aTHX_ 
imp_dbh, value, type_info);
                        }
                        else {
                                if (type_info) {
@@ -3092,7 +3110,7 @@
                                case PG_TEXT:
                                case PG_BPCHAR:
                                case PG_VARCHAR:
-                                       if (is_high_bit_set(value, value_len) 
&& is_utf8_string((unsigned char*)value, value_len)) {
+                                       if (is_high_bit_set(aTHX_ value, 
value_len) && is_utf8_string((unsigned char*)value, value_len)) {
                                                SvUTF8_on(sv);
                                        }
                                        break;
@@ -3132,7 +3150,7 @@
 
 /* ================================================================== */
 /* Pop off savepoints to the specified savepoint name */
-static void pg_db_free_savepoints_to (imp_dbh_t * imp_dbh, const char 
*savepoint)
+static void pg_db_free_savepoints_to (pTHX_ imp_dbh_t * imp_dbh, const char 
*savepoint)
 {
        I32 i;
        for (i = av_len(imp_dbh->savepoints); i >= 0; i--) {
@@ -3149,6 +3167,7 @@
 /* ================================================================== */
 int dbd_st_rows (SV * sth, imp_sth_t * imp_sth)
 {
+       dTHX;
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: dbd_st_rows sth=%d\n", 
sth);
        
@@ -3159,8 +3178,8 @@
 
 /* ================================================================== */
 int dbd_st_finish (SV * sth, imp_sth_t * imp_sth)
-{
-       
+{      
+       dTHX;
        D_imp_dbh_from_sth;
 
        if (dbis->debug >= 4)
@@ -3176,7 +3195,7 @@
        /* Are we in the middle of an async for this statement handle? */
        if (imp_dbh->async_status) {
          if (imp_sth->async_status) {
-               handle_old_async(sth, imp_dbh, PG_OLDQUERY_WAIT);
+               handle_old_async(aTHX_ sth, imp_dbh, PG_OLDQUERY_WAIT);
          }
        }
 
@@ -3190,7 +3209,7 @@
 
 
 /* ================================================================== */
-static int dbd_st_deallocate_statement (SV * sth, imp_sth_t * imp_sth)
+static int dbd_st_deallocate_statement (pTHX_ SV * sth, imp_sth_t * imp_sth)
 {
        D_imp_dbh_from_sth;
        char                    tempsqlstate[6];
@@ -3207,7 +3226,7 @@
        tempsqlstate[0] = '\0';
 
        /* What is our status? */
-       tstatus = dbd_db_txn_status(imp_dbh);
+       tstatus = dbd_db_txn_status(aTHX_ imp_dbh);
        if (dbis->debug >= 5)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: txn_status is %d\n", 
tstatus);
 
@@ -3226,11 +3245,11 @@
                                        (void)PerlIO_printf(DBILOGFP, "dbdpg: 
Rolling back to savepoint %s\n", SvPV_nolen(sp));
                                sprintf(cmd, "rollback to %s", SvPV_nolen(sp));
                                strncpy(tempsqlstate, imp_dbh->sqlstate, 
strlen(imp_dbh->sqlstate)+1);
-                               status = _result(imp_dbh, cmd);
+                               status = _result(aTHX_ imp_dbh, cmd);
                                Safefree(cmd);
                        }
                        else {
-                               status = _result(imp_dbh, "ROLLBACK");
+                               status = _result(aTHX_ imp_dbh, "ROLLBACK");
                                imp_dbh->done_begin = DBDPG_FALSE;
                        }
                }
@@ -3249,10 +3268,10 @@
        if (dbis->debug >= 5)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: Deallocating (%s)\n", 
imp_sth->prepare_name);
 
-       status = _result(imp_dbh, stmt);
+       status = _result(aTHX_ imp_dbh, stmt);
        Safefree(stmt);
        if (PGRES_COMMAND_OK != status) {
-               pg_error(sth, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ sth, status, PQerrorMessage(imp_dbh->conn));
                return 2;
        }
 
@@ -3270,6 +3289,7 @@
 /* ================================================================== */
 void dbd_st_destroy (SV * sth, imp_sth_t * imp_sth)
 {
+       dTHX;
        D_imp_dbh_from_sth;
        seg_t * currseg;
        seg_t * nextseg;
@@ -3292,13 +3312,13 @@
        }
 
        if (imp_dbh->async_status) {
-         handle_old_async(sth, imp_dbh, PG_OLDQUERY_WAIT);
+         handle_old_async(aTHX_ sth, imp_dbh, PG_OLDQUERY_WAIT);
        }
 
        /* Deallocate only if we named this statement ourselves and we still 
have a good connection */
        /* On rare occasions, dbd_db_destroy is called first and we can no 
longer rely on imp_dbh */
        if (imp_sth->prepared_by_us && DBIc_ACTIVE(imp_dbh)) {
-               if (dbd_st_deallocate_statement(sth, imp_sth)!=0) {
+               if (dbd_st_deallocate_statement(aTHX_ sth, imp_sth)!=0) {
                        if (dbis->debug >= 4)
                                (void)PerlIO_printf(DBILOGFP, "dbdpg: Could not 
deallocate\n");
                }
@@ -3349,6 +3369,7 @@
 int
 pg_db_putline (SV * dbh, const char * buffer)
 {
+       dTHX;
        D_imp_dbh(dbh);
        int copystatus;
 
@@ -3364,7 +3385,7 @@
 
        copystatus = PQputCopyData(imp_dbh->conn, buffer, (int)strlen(buffer));
        if (-1 == copystatus) {
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                return 0;
        }
        else if (1 != copystatus) {
@@ -3378,6 +3399,7 @@
 int
 pg_db_getline (SV * dbh, SV * svbuf, int length)
 {
+       dTHX;
        D_imp_dbh(dbh);
        int    copystatus;
        char * tempbuf;
@@ -3406,7 +3428,7 @@
                return -1;
        }
        else if (copystatus < 1) {
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
        }
        else {
                sv_setpv(svbuf, tempbuf);
@@ -3421,6 +3443,7 @@
 int
 pg_db_getcopydata (SV * dbh, SV * dataline, int async)
 {
+       dTHX;
        D_imp_dbh(dbh);
        int    copystatus;
        char * tempbuf;
@@ -3442,7 +3465,7 @@
        }
        else if (0 == copystatus) { /* async and still in progress: consume and 
return */
                if (!PQconsumeInput(imp_dbh->conn)) {
-                       pg_error(dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                        return -2;
                }
        }
@@ -3452,14 +3475,14 @@
                SvCUR_set(dataline, 0);
                imp_dbh->copystate=0;
                result = PQgetResult(imp_dbh->conn);
-               status = _sqlstate(imp_dbh, result);
+               status = _sqlstate(aTHX_ imp_dbh, result);
                PQclear(result);
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                }
        }
        else {
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
        }
        return copystatus;
 }
@@ -3468,6 +3491,7 @@
 int
 pg_db_putcopydata (SV * dbh, SV * dataline)
 {
+       dTHX;
        D_imp_dbh(dbh);
        int copystatus;
 
@@ -3490,7 +3514,7 @@
        else if (0 == copystatus) { /* non-blocking mode only */
        }
        else {
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
        }
 
        return copystatus == 1 ? 1 : 0;
@@ -3503,6 +3527,7 @@
        /* If in COPY_IN mode, terminate the COPYing */
        /* Returns 1 on success, otherwise 0 (plus a probably warning/error) */
 
+       dTHX;
        D_imp_dbh(dbh);
        int            copystatus;
 
@@ -3528,10 +3553,10 @@
                ExecStatusType status;
                imp_dbh->copystate = 0;
                result = PQgetResult(imp_dbh->conn);
-               status = _sqlstate(imp_dbh, result);
+               status = _sqlstate(aTHX_ imp_dbh, result);
                PQclear(result);
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                        return 0;
                }
                return 1;
@@ -3540,7 +3565,7 @@
                return 0;
        }
        else {
-               pg_error(dbh, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                return 0;
        }
 }
@@ -3549,6 +3574,7 @@
 /* ================================================================== */
 int pg_db_endcopy (SV * dbh)
 {
+       dTHX;
        D_imp_dbh(dbh);
        int            copystatus;
        PGresult *     result;
@@ -3565,17 +3591,17 @@
                        (void)PerlIO_printf(DBILOGFP, "dbdpg: Running 
PQputCopyEnd\n");
                copystatus = PQputCopyEnd(imp_dbh->conn, NULL);
                if (-1 == copystatus) {
-                       pg_error(dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                        return 1;
                }
                else if (1 != copystatus)
                        croak("PQputCopyEnd returned a value of %d\n", 
copystatus);
                /* Get the final result of the copy */
                result = PQgetResult(imp_dbh->conn);
-               status = _sqlstate(imp_dbh, result);
+               status = _sqlstate(aTHX_ imp_dbh, result);
                PQclear(result);
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                        return 1;
                }
                copystatus = 0;
@@ -3592,6 +3618,7 @@
 /* ================================================================== */
 void pg_db_pg_server_trace (SV * dbh, FILE * fh)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3604,6 +3631,7 @@
 /* ================================================================== */
 void pg_db_pg_server_untrace (SV * dbh)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3616,6 +3644,7 @@
 /* ================================================================== */
 int pg_db_savepoint (SV * dbh, imp_dbh_t * imp_dbh, char * savepoint)
 {
+       dTHX;
        int    status;
        char * action;
 
@@ -3635,19 +3664,19 @@
 
        /* Start a new transaction if this is the first command */
        if (!imp_dbh->done_begin) {
-               status = _result(imp_dbh, "begin");
+               status = _result(aTHX_ imp_dbh, "begin");
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                        return -2;
                }
                imp_dbh->done_begin = DBDPG_TRUE;
        }
 
-       status = _result(imp_dbh, action);
+       status = _result(aTHX_ imp_dbh, action);
        Safefree(action);
 
        if (PGRES_COMMAND_OK != status) {
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                return 0;
        }
 
@@ -3659,6 +3688,7 @@
 /* ================================================================== */
 int pg_db_rollback_to (SV * dbh, imp_dbh_t * imp_dbh, const char *savepoint)
 {
+       dTHX;
        int    status;
        char * action;
 
@@ -3676,15 +3706,15 @@
        if ((NULL == imp_dbh->conn) || (DBIc_has(imp_dbh, DBIcf_AutoCommit)))
                return 0;
 
-       status = _result(imp_dbh, action);
+       status = _result(aTHX_ imp_dbh, action);
        Safefree(action);
 
        if (PGRES_COMMAND_OK != status) {
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                return 0;
        }
 
-       pg_db_free_savepoints_to(imp_dbh, savepoint);
+       pg_db_free_savepoints_to(aTHX_ imp_dbh, savepoint);
        return 1;
 }
 
@@ -3692,6 +3722,7 @@
 /* ================================================================== */
 int pg_db_release (SV * dbh, imp_dbh_t * imp_dbh, char * savepoint)
 {
+       dTHX;
        int    status;
        char * action;
 
@@ -3709,29 +3740,29 @@
        if ((NULL == imp_dbh->conn) || (DBIc_has(imp_dbh, DBIcf_AutoCommit)))
                return 0;
 
-       status = _result(imp_dbh, action);
+       status = _result(aTHX_ imp_dbh, action);
        Safefree(action);
 
        if (PGRES_COMMAND_OK != status) {
-               pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ dbh, status, PQerrorMessage(imp_dbh->conn));
                return 0;
        }
 
-       pg_db_free_savepoints_to(imp_dbh, savepoint);
+       pg_db_free_savepoints_to(aTHX_ imp_dbh, savepoint);
        return 1;
 }
 
 
 /* ================================================================== */
 /* Used to ensure we are in a txn, e.g. the lo_ functions below */
-static int pg_db_start_txn (SV * dbh, imp_dbh_t * imp_dbh)
+static int pg_db_start_txn (pTHX_ SV * dbh, imp_dbh_t * imp_dbh)
 {
        int status = -1;
        /* If not autocommit, start a new transaction */
        if (!imp_dbh->done_begin && !DBIc_has(imp_dbh, DBIcf_AutoCommit)) {
-               status = _result(imp_dbh, "begin");
+               status = _result(aTHX_ imp_dbh, "begin");
                if (PGRES_COMMAND_OK != status) {
-                       pg_error(dbh, status, PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ dbh, status, 
PQerrorMessage(imp_dbh->conn));
                        return 0;
                }
                imp_dbh->done_begin = DBDPG_TRUE;
@@ -3745,12 +3776,13 @@
 /* ================================================================== */
 unsigned int pg_db_lo_creat (SV * dbh, int mode)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_db_lo_creat (%d)\n", 
mode);
 
-       if (!pg_db_start_txn(dbh,imp_dbh))
+       if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
                return 0; /* No other option, because lo_creat returns an Oid */
 
        return lo_creat(imp_dbh->conn, mode); /* 0 on error */
@@ -3759,12 +3791,13 @@
 /* ================================================================== */
 int pg_db_lo_open (SV * dbh, unsigned int lobjId, int mode)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_db_lo_open (%d) 
(%d)\n", lobjId, mode);
 
-       if (!pg_db_start_txn(dbh,imp_dbh))
+       if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
                return -2;
 
        return lo_open(imp_dbh->conn, lobjId, mode); /* -1 on error */
@@ -3773,6 +3806,7 @@
 /* ================================================================== */
 int pg_db_lo_close (SV * dbh, int fd)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3784,6 +3818,7 @@
 /* ================================================================== */
 int pg_db_lo_read (SV * dbh, int fd, char * buf, size_t len)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3795,6 +3830,7 @@
 /* ================================================================== */
 int pg_db_lo_write (SV * dbh, int fd, char * buf, size_t len)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3806,6 +3842,7 @@
 /* ================================================================== */
 int pg_db_lo_lseek (SV * dbh, int fd, int offset, int whence)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3817,6 +3854,7 @@
 /* ================================================================== */
 int pg_db_lo_tell (SV * dbh, int fd)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
@@ -3828,12 +3866,13 @@
 /* ================================================================== */
 int pg_db_lo_unlink (SV * dbh, unsigned int lobjId)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_db_lo_unlink (%d)\n", 
lobjId);
 
-       if (!pg_db_start_txn(dbh,imp_dbh))
+       if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
                return -2;
 
        return lo_unlink(imp_dbh->conn, lobjId); /* 1 on success, -1 on failure 
*/
@@ -3842,12 +3881,13 @@
 /* ================================================================== */
 unsigned int pg_db_lo_import (SV * dbh, char * filename)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_db_lo_import (%s)\n", 
filename);
 
-       if (!pg_db_start_txn(dbh,imp_dbh))
+       if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
                return 0; /* No other option, because lo_import returns an Oid 
*/
 
        return lo_import(imp_dbh->conn, filename); /* 0 on error */
@@ -3856,12 +3896,13 @@
 /* ================================================================== */
 int pg_db_lo_export (SV * dbh, unsigned int lobjId, char * filename)
 {
+       dTHX;
        D_imp_dbh(dbh);
 
        if (dbis->debug >= 4)
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_db_lo_export id:(%d) 
file:(%s)\n", lobjId, filename);
 
-       if (!pg_db_start_txn(dbh,imp_dbh))
+       if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
                return -2;
 
        return lo_export(imp_dbh->conn, lobjId, filename); /* 1 on success, -1 
on failure */
@@ -3871,6 +3912,7 @@
 /* ================================================================== */
 int dbd_st_blob_read (SV * sth, imp_sth_t * imp_sth, int lobjId, long offset, 
long len, SV * destrv, long destoffset)
 {
+       dTHX;
        D_imp_dbh_from_sth;
 
        int    ret, lobj_fd, nbytes;
@@ -3883,23 +3925,23 @@
 
        /* safety checks */
        if (lobjId <= 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: lobjId <= 
0");
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: 
lobjId <= 0");
                return 0;
        }
        if (offset < 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: offset < 
0");
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: 
offset < 0");
                return 0;
        }
        if (len < 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: len < 0");
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: len < 
0");
                return 0;
        }
        if (! SvROK(destrv)) {
-               pg_error(sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: destrv not 
a reference");
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: 
destrv not a reference");
                return 0;
        }
        if (destoffset < 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: destoffset 
< 0");
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, "dbd_st_blob_read: 
destoffset < 0");
                return 0;
        }
        
@@ -3912,7 +3954,7 @@
        /* open large object */
        lobj_fd = lo_open(imp_dbh->conn, (unsigned)lobjId, INV_READ);
        if (lobj_fd < 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                return 0;
        }
        
@@ -3920,7 +3962,7 @@
        if (offset > 0) {
                ret = lo_lseek(imp_dbh->conn, lobj_fd, (int)offset, SEEK_SET);
                if (ret < 0) {
-                       pg_error(sth, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
+                       pg_error(aTHX_ sth, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                        return 0;
                }
        }
@@ -3947,7 +3989,7 @@
        /* close large object */
        ret = lo_close(imp_dbh->conn, lobj_fd);
        if (ret < 0) {
-               pg_error(sth, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ sth, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                return 0;
        }
        
@@ -3961,6 +4003,7 @@
 /* Return the result of an asynchronous query, waiting if needed */
 int dbdpg_result (SV *h, imp_dbh_t *imp_dbh)
 {
+       dTHX;
        PGresult *result;
        ExecStatusType status = PGRES_FATAL_ERROR;
        int rows;
@@ -3969,7 +4012,7 @@
        if (dbis->debug >= 4) { (void)PerlIO_printf(DBILOGFP, "dbdpg: 
dbdpg_result\n"); }
 
        if (1 != imp_dbh->async_status) {
-               pg_error(h, PGRES_FATAL_ERROR, "No asynchronous query is 
running\n");
+               pg_error(aTHX_ h, PGRES_FATAL_ERROR, "No asynchronous query is 
running\n");
                return -2;
        }       
 
@@ -3977,7 +4020,7 @@
 
        while ((result = PQgetResult(imp_dbh->conn)) != NULL) {
          /* TODO: Better multiple result-set handling */
-         status = _sqlstate(imp_dbh, result);
+         status = _sqlstate(aTHX_ imp_dbh, result);
          switch (status) {
          case PGRES_TUPLES_OK:
                rows = PQntuples(result);
@@ -4007,12 +4050,12 @@
          case PGRES_BAD_RESPONSE:
          case PGRES_NONFATAL_ERROR:
                rows = -2;
-               pg_error(h, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ h, status, PQerrorMessage(imp_dbh->conn));
                break;
          case PGRES_FATAL_ERROR:
          default:
                rows = -2;
-               pg_error(h, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ h, status, PQerrorMessage(imp_dbh->conn));
                break;
          }
 
@@ -4051,17 +4094,18 @@
 
 int dbdpg_ready(SV *h, imp_dbh_t *imp_dbh)
 {
+       dTHX;
        if (dbis->debug >= 4) {
                (void)PerlIO_printf(DBILOGFP, "dbdpg: pg_st_ready, async_status 
is %d\n",imp_dbh->async_status);
        }
 
        if (0 == imp_dbh->async_status) {
-               pg_error(h, PGRES_FATAL_ERROR, "No asynchronous query is 
running\n");
+               pg_error(aTHX_ h, PGRES_FATAL_ERROR, "No asynchronous query is 
running\n");
                return -1;
        }       
 
        if (!PQconsumeInput(imp_dbh->conn)) {
-         pg_error(h, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
+         pg_error(aTHX_ h, PGRES_FATAL_ERROR, PQerrorMessage(imp_dbh->conn));
          return -2;
        }
 
@@ -4081,6 +4125,7 @@
 
 int dbdpg_cancel(SV *h, imp_dbh_t *imp_dbh)
 {
+       dTHX;
        PGcancel *cancel;
        char errbuf[256];
        PGresult *result;
@@ -4089,12 +4134,12 @@
        if (dbis->debug >= 4) { (void)PerlIO_printf(DBILOGFP, "dbdpg: 
dbdpg_cancel, async=%d\n", imp_dbh->async_status); }
 
        if (0 == imp_dbh->async_status) {
-         pg_error(h, PGRES_FATAL_ERROR, "No asynchronous query is running");
+         pg_error(aTHX_ h, PGRES_FATAL_ERROR, "No asynchronous query is 
running");
          return DBDPG_FALSE;
        }
 
        if (-1 == imp_dbh->async_status) {
-         pg_error(h, PGRES_FATAL_ERROR, "Asychronous query has already been 
cancelled");
+         pg_error(aTHX_ h, PGRES_FATAL_ERROR, "Asychronous query has already 
been cancelled");
          return DBDPG_FALSE;
        }
 
@@ -4105,7 +4150,7 @@
        if (! PQcancel(cancel,errbuf,sizeof(errbuf))) {
          PQfreeCancel(cancel);
          if (dbis->debug >= 1) { (void)PerlIO_printf(DBILOGFP, "dbdpg: 
PQcancel failed: %s\n", errbuf); }
-         pg_error(h, PGRES_FATAL_ERROR, "PQcancel failed");
+         pg_error(aTHX_ h, PGRES_FATAL_ERROR, "PQcancel failed");
          return DBDPG_FALSE;
        }
        PQfreeCancel(cancel);
@@ -4118,11 +4163,11 @@
        /* Read in the result - assume only one */
        result = PQgetResult(imp_dbh->conn);
        if (!result) {
-         pg_error(h, PGRES_FATAL_ERROR, "Failed to get a result after 
PQcancel");
+         pg_error(aTHX_ h, PGRES_FATAL_ERROR, "Failed to get a result after 
PQcancel");
          return DBDPG_FALSE;
        }
 
-       status = _sqlstate(imp_dbh, result);
+       status = _sqlstate(aTHX_ imp_dbh, result);
 
        /* If we actually cancelled a running query, perform a rollback */
        if (0 == strncmp(imp_dbh->sqlstate, "57014", 5)) {
@@ -4134,7 +4179,7 @@
        /* If we got any other error, make sure we report it */
        if (0 != strncmp(imp_dbh->sqlstate, "00000", 5)) {
          if (dbis->debug >= 0) { (void)PerlIO_printf(DBILOGFP, "dbdpg: Query 
was not cancelled: was already finished\n"); }
-         pg_error(h, status, PQerrorMessage(imp_dbh->conn));
+         pg_error(aTHX_ h, status, PQerrorMessage(imp_dbh->conn));
        }
        
        return DBDPG_FALSE;
@@ -4144,7 +4189,8 @@
 
 int dbdpg_cancel_sth(SV *sth, imp_sth_t *imp_sth)
 {
-        D_imp_dbh_from_sth;
+       dTHX;
+       D_imp_dbh_from_sth;
 
        const bool cancel_result = dbdpg_cancel(sth, imp_dbh);
 
@@ -4160,7 +4206,7 @@
 or by waiting for a result.
 
  */
-static int handle_old_async(SV * handle, imp_dbh_t * imp_dbh, int asyncflag)
+static int handle_old_async(pTHX_ SV * handle, imp_dbh_t * imp_dbh, int 
asyncflag)
 {
 
   PGresult *result;
@@ -4180,7 +4226,7 @@
          cresult = PQcancel(cancel,errbuf,255);
          if (! cresult) {
                if (dbis->debug >= 1) { (void)PerlIO_printf(DBILOGFP, "dbdpg: 
PQcancel failed: %s\n", errbuf); }
-               pg_error(handle, PGRES_FATAL_ERROR, "Could not cancel previous 
command");
+               pg_error(aTHX_ handle, PGRES_FATAL_ERROR, "Could not cancel 
previous command");
                return -2;
          }
          PQfreeCancel(cancel);
@@ -4196,28 +4242,28 @@
        /* Finish up the outstanding query and throw out the result, unless an 
error */
        if (dbis->debug >= 1) { (void)PerlIO_printf(DBILOGFP, "dbdpg: Waiting 
for old async command to finish\n"); }
        while ((result = PQgetResult(imp_dbh->conn)) != NULL) {
-         status = _sqlstate(imp_dbh, result);
+         status = _sqlstate(aTHX_ imp_dbh, result);
          PQclear(result);
          if (status == PGRES_COPY_IN) { /* In theory, this should be caught by 
copystate, but we'll be careful */
                if (-1 == PQputCopyEnd(imp_dbh->conn, NULL)) {
-                 pg_error(handle, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
+                 pg_error(aTHX_ handle, PGRES_FATAL_ERROR, 
PQerrorMessage(imp_dbh->conn));
                  return -2;
                }
          }
          else if (status == PGRES_COPY_OUT) { /* Won't be as nice with this 
one */
-               pg_error(handle, PGRES_FATAL_ERROR, "Must finish copying 
first");
+               pg_error(aTHX_ handle, PGRES_FATAL_ERROR, "Must finish copying 
first");
                return -2;
          }
          else if (status != PGRES_EMPTY_QUERY
                           && status != PGRES_COMMAND_OK
                           && status != PGRES_TUPLES_OK) {
-               pg_error(handle, status, PQerrorMessage(imp_dbh->conn));
+               pg_error(aTHX_ handle, status, PQerrorMessage(imp_dbh->conn));
                return -2;
          }
        }
   }
   else {
-       pg_error(handle, PGRES_FATAL_ERROR, "Cannot execute until previous 
async query has finished");
+       pg_error(aTHX_ handle, PGRES_FATAL_ERROR, "Cannot execute until 
previous async query has finished");
        return -2;
   }
 

Reply via email to