Commit 3290e618 authored by Tom Lane's avatar Tom Lane

Add a PQfireResultCreateEvents function to allow applications to mimic the

sequence of operations that libpq goes through while creating a PGresult.
Also, remove ill-considered "const" decoration on parameters passed to
event procedures.
parent 4e57668d
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.262 2008/09/19 16:40:40 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.263 2008/09/19 20:06:13 tgl Exp $ -->
<chapter id="libpq"> <chapter id="libpq">
<title><application>libpq</application> - C Library</title> <title><application>libpq</application> - C Library</title>
...@@ -4592,10 +4592,11 @@ char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>); ...@@ -4592,10 +4592,11 @@ char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>);
<parameter>conn</parameter> is not null and <parameter>status</> <parameter>conn</parameter> is not null and <parameter>status</>
indicates an error, the current error message of the specified indicates an error, the current error message of the specified
connection is copied into the <structname>PGresult</structname>. connection is copied into the <structname>PGresult</structname>.
Also, if <parameter>conn</parameter> is not null, any event handlers Also, if <parameter>conn</parameter> is not null, any event procedures
registered in the connection are copied into the registered in the connection are copied into the
<structname>PGresult</structname> (but they don't get <structname>PGresult</structname>. (They do not get
<literal>PGEVT_RESULTCREATE</> calls). <literal>PGEVT_RESULTCREATE</> calls, but see
<function>PQfireResultCreateEvents</function>.)
Note that <function>PQclear</function> should eventually be called Note that <function>PQclear</function> should eventually be called
on the object, just as with a <structname>PGresult</structname> on the object, just as with a <structname>PGresult</structname>
returned by <application>libpq</application> itself. returned by <application>libpq</application> itself.
...@@ -4603,6 +4604,46 @@ char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>); ...@@ -4603,6 +4604,46 @@ char *pg_encoding_to_char(int <replaceable>encoding_id</replaceable>);
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term>
<function>PQfireResultCreateEvents</function>
<indexterm>
<primary>PQfireResultCreateEvents</primary>
</indexterm>
</term>
<listitem>
<para>
Fires a <literal>PGEVT_RESULTCREATE</literal> event (see <xref
linkend="libpq-events">) for each event procedure registered in the
<structname>PGresult</structname> object. Returns non-zero for success,
zero if any event procedure fails.
<synopsis>
int PQfireResultCreateEvents(PGconn *conn, PGresult *res);
</synopsis>
</para>
<para>
The <literal>conn</> argument is passed through to event procedures
but not used directly. It can be <literal>NULL</> if the event
procedures won't use it.
</para>
<para>
Event procedures that have already received a
<literal>PGEVT_RESULTCREATE</> or <literal>PGEVT_RESULTCOPY</> event
for this object are not fired again.
</para>
<para>
The main reason that this function is separate from
<function>PQmakeEmptyPGResult</function> is that it is often appropriate
to create a <structname>PGresult</structname> and fill it with data
before invoking the event procedures.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term> <term>
<function>PQcopyResult</function> <function>PQcopyResult</function>
...@@ -4904,7 +4945,7 @@ defaultNoticeProcessor(void *arg, const char *message) ...@@ -4904,7 +4945,7 @@ defaultNoticeProcessor(void *arg, const char *message)
<synopsis> <synopsis>
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventRegister; } PGEventRegister;
</synopsis> </synopsis>
...@@ -4937,7 +4978,7 @@ typedef struct ...@@ -4937,7 +4978,7 @@ typedef struct
<synopsis> <synopsis>
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventConnReset; } PGEventConnReset;
</synopsis> </synopsis>
...@@ -4967,7 +5008,7 @@ typedef struct ...@@ -4967,7 +5008,7 @@ typedef struct
<synopsis> <synopsis>
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventConnDestroy; } PGEventConnDestroy;
</synopsis> </synopsis>
...@@ -4995,7 +5036,7 @@ typedef struct ...@@ -4995,7 +5036,7 @@ typedef struct
<synopsis> <synopsis>
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
PGresult *result; PGresult *result;
} PGEventResultCreate; } PGEventResultCreate;
</synopsis> </synopsis>
...@@ -5063,7 +5104,7 @@ typedef struct ...@@ -5063,7 +5104,7 @@ typedef struct
<synopsis> <synopsis>
typedef struct typedef struct
{ {
const PGresult *result; PGresult *result;
} PGEventResultDestroy; } PGEventResultDestroy;
</synopsis> </synopsis>
......
# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.20 2008/09/17 04:31:08 tgl Exp $ # $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.21 2008/09/19 20:06:13 tgl Exp $
# Functions to be exported by libpq DLLs # Functions to be exported by libpq DLLs
PQconnectdb 1 PQconnectdb 1
PQsetdbLogin 2 PQsetdbLogin 2
...@@ -150,3 +150,4 @@ PQinstanceData 147 ...@@ -150,3 +150,4 @@ PQinstanceData 147
PQsetInstanceData 148 PQsetInstanceData 148
PQresultInstanceData 149 PQresultInstanceData 149
PQresultSetInstanceData 150 PQresultSetInstanceData 150
PQfireResultCreateEvents 151
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-events.c,v 1.2 2008/09/19 16:40:40 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-events.c,v 1.3 2008/09/19 20:06:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -175,3 +175,35 @@ PQresultInstanceData(const PGresult *result, PGEventProc proc) ...@@ -175,3 +175,35 @@ PQresultInstanceData(const PGresult *result, PGEventProc proc)
return NULL; return NULL;
} }
/*
* Fire RESULTCREATE events for an application-created PGresult.
*
* The conn argument can be NULL if event procedures won't use it.
*/
int
PQfireResultCreateEvents(PGconn *conn, PGresult *res)
{
int i;
if (!res)
return FALSE;
for (i = 0; i < res->nEvents; i++)
{
if (!res->events[i].resultInitialized)
{
PGEventResultCreate evt;
evt.conn = conn;
evt.result = res;
if (!res->events[i].proc(PGEVT_RESULTCREATE, &evt,
res->events[i].passThrough))
return FALSE;
res->events[i].resultInitialized = TRUE;
}
}
return TRUE;
}
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-events.h,v 1.1 2008/09/17 04:31:08 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-events.h,v 1.2 2008/09/19 20:06:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -36,22 +36,22 @@ typedef enum ...@@ -36,22 +36,22 @@ typedef enum
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventRegister; } PGEventRegister;
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventConnReset; } PGEventConnReset;
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
} PGEventConnDestroy; } PGEventConnDestroy;
typedef struct typedef struct
{ {
const PGconn *conn; PGconn *conn;
PGresult *result; PGresult *result;
} PGEventResultCreate; } PGEventResultCreate;
...@@ -63,7 +63,7 @@ typedef struct ...@@ -63,7 +63,7 @@ typedef struct
typedef struct typedef struct
{ {
const PGresult *result; PGresult *result;
} PGEventResultDestroy; } PGEventResultDestroy;
typedef int (*PGEventProc) (PGEventId evtId, void *evtInfo, void *passThrough); typedef int (*PGEventProc) (PGEventId evtId, void *evtInfo, void *passThrough);
...@@ -84,6 +84,9 @@ extern int PQresultSetInstanceData(PGresult *result, PGEventProc proc, void *dat ...@@ -84,6 +84,9 @@ extern int PQresultSetInstanceData(PGresult *result, PGEventProc proc, void *dat
/* Gets the PGresult instance data for the provided proc. */ /* Gets the PGresult instance data for the provided proc. */
extern void *PQresultInstanceData(const PGresult *result, PGEventProc proc); extern void *PQresultInstanceData(const PGresult *result, PGEventProc proc);
/* Fires RESULTCREATE events for an application-created PGresult. */
extern int PQfireResultCreateEvents(PGconn *conn, PGresult *res);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment