PostgreSQL Source Code  git master
libpq-events.c File Reference
#include "postgres_fe.h"
#include "libpq-fe.h"
#include "libpq-int.h"
Include dependency graph for libpq-events.c:

Go to the source code of this file.

Functions

int PQregisterEventProc (PGconn *conn, PGEventProc proc, const char *name, void *passThrough)
 
int PQsetInstanceData (PGconn *conn, PGEventProc proc, void *data)
 
void * PQinstanceData (const PGconn *conn, PGEventProc proc)
 
int PQresultSetInstanceData (PGresult *result, PGEventProc proc, void *data)
 
void * PQresultInstanceData (const PGresult *result, PGEventProc proc)
 
int PQfireResultCreateEvents (PGconn *conn, PGresult *res)
 

Function Documentation

◆ PQfireResultCreateEvents()

int PQfireResultCreateEvents ( PGconn conn,
PGresult res 
)

Definition at line 185 of file libpq-events.c.

186 {
187  int result = true;
188  int i;
189 
190  if (!res)
191  return false;
192 
193  for (i = 0; i < res->nEvents; i++)
194  {
195  /* It's possible event was already fired, if so don't repeat it */
196  if (!res->events[i].resultInitialized)
197  {
199 
200  evt.conn = conn;
201  evt.result = res;
202  if (res->events[i].proc(PGEVT_RESULTCREATE, &evt,
203  res->events[i].passThrough))
204  res->events[i].resultInitialized = true;
205  else
206  result = false;
207  }
208  }
209 
210  return result;
211 }
int i
Definition: isn.c:73
@ PGEVT_RESULTCREATE
Definition: libpq-events.h:32
PGconn * conn
Definition: streamutil.c:55
void * passThrough
Definition: libpq-int.h:165
PGEventProc proc
Definition: libpq-int.h:163
bool resultInitialized
Definition: libpq-int.h:167
int nEvents
Definition: libpq-int.h:191
PGEvent * events
Definition: libpq-int.h:190

References conn, PGEventResultCreate::conn, pg_result::events, i, pg_result::nEvents, PGEvent::passThrough, PGEVT_RESULTCREATE, PGEvent::proc, res, PGEventResultCreate::result, and PGEvent::resultInitialized.

Referenced by PQgetResult().

◆ PQinstanceData()

void* PQinstanceData ( const PGconn conn,
PGEventProc  proc 
)

Definition at line 121 of file libpq-events.c.

122 {
123  int i;
124 
125  if (!conn || !proc)
126  return NULL;
127 
128  for (i = 0; i < conn->nEvents; i++)
129  {
130  if (conn->events[i].proc == proc)
131  return conn->events[i].data;
132  }
133 
134  return NULL;
135 }
void * data
Definition: libpq-int.h:166
int nEvents
Definition: libpq-int.h:434
PGEvent * events
Definition: libpq-int.h:433

References conn, PGEvent::data, pg_conn::events, i, pg_conn::nEvents, and PGEvent::proc.

◆ PQregisterEventProc()

int PQregisterEventProc ( PGconn conn,
PGEventProc  proc,
const char *  name,
void *  passThrough 
)

Definition at line 40 of file libpq-events.c.

42 {
43  int i;
44  PGEventRegister regevt;
45 
46  if (!proc || !conn || !name || !*name)
47  return false; /* bad arguments */
48 
49  for (i = 0; i < conn->nEvents; i++)
50  {
51  if (conn->events[i].proc == proc)
52  return false; /* already registered */
53  }
54 
56  {
57  PGEvent *e;
58  int newSize;
59 
60  newSize = conn->eventArraySize ? conn->eventArraySize * 2 : 8;
61  if (conn->events)
62  e = (PGEvent *) realloc(conn->events, newSize * sizeof(PGEvent));
63  else
64  e = (PGEvent *) malloc(newSize * sizeof(PGEvent));
65 
66  if (!e)
67  return false;
68 
69  conn->eventArraySize = newSize;
70  conn->events = e;
71  }
72 
73  conn->events[conn->nEvents].proc = proc;
74  conn->events[conn->nEvents].name = strdup(name);
75  if (!conn->events[conn->nEvents].name)
76  return false;
77  conn->events[conn->nEvents].passThrough = passThrough;
78  conn->events[conn->nEvents].data = NULL;
80  conn->nEvents++;
81 
82  regevt.conn = conn;
83  if (!proc(PGEVT_REGISTER, &regevt, passThrough))
84  {
85  conn->nEvents--;
87  return false;
88  }
89 
90  return true;
91 }
#define realloc(a, b)
Definition: header.h:60
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
@ PGEVT_REGISTER
Definition: libpq-events.h:29
e
Definition: preproc-init.c:82
char * name
Definition: libpq-int.h:164
int eventArraySize
Definition: libpq-int.h:435
const char * name

References conn, PGEventRegister::conn, PGEvent::data, pg_conn::eventArraySize, pg_conn::events, free, i, malloc, name, PGEvent::name, pg_conn::nEvents, PGEvent::passThrough, PGEVT_REGISTER, PGEvent::proc, realloc, and PGEvent::resultInitialized.

◆ PQresultInstanceData()

void* PQresultInstanceData ( const PGresult result,
PGEventProc  proc 
)

Definition at line 165 of file libpq-events.c.

166 {
167  int i;
168 
169  if (!result || !proc)
170  return NULL;
171 
172  for (i = 0; i < result->nEvents; i++)
173  if (result->events[i].proc == proc)
174  return result->events[i].data;
175 
176  return NULL;
177 }

References PGEvent::data, pg_result::events, i, pg_result::nEvents, and PGEvent::proc.

◆ PQresultSetInstanceData()

int PQresultSetInstanceData ( PGresult result,
PGEventProc  proc,
void *  data 
)

Definition at line 142 of file libpq-events.c.

143 {
144  int i;
145 
146  if (!result || !proc)
147  return false;
148 
149  for (i = 0; i < result->nEvents; i++)
150  {
151  if (result->events[i].proc == proc)
152  {
153  result->events[i].data = data;
154  return true;
155  }
156  }
157 
158  return false;
159 }
const void * data

References PGEvent::data, data, pg_result::events, i, pg_result::nEvents, and PGEvent::proc.

◆ PQsetInstanceData()

int PQsetInstanceData ( PGconn conn,
PGEventProc  proc,
void *  data 
)

Definition at line 98 of file libpq-events.c.

99 {
100  int i;
101 
102  if (!conn || !proc)
103  return false;
104 
105  for (i = 0; i < conn->nEvents; i++)
106  {
107  if (conn->events[i].proc == proc)
108  {
109  conn->events[i].data = data;
110  return true;
111  }
112  }
113 
114  return false;
115 }

References conn, PGEvent::data, data, pg_conn::events, i, pg_conn::nEvents, and PGEvent::proc.