2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include <libpq-events.h>
22 #include "php_pq_misc.h"
23 #include "php_pq_object.h"
24 #include "php_pqconn_event.h"
25 #include "php_pqres.h"
27 static int apply_event(zval
*p
, void *a
)
29 php_pq_callback_t
*cb
= Z_PTR_P(p
);
34 zend_fcall_info_args(&cb
->fci
, args
);
35 zend_fcall_info_call(&cb
->fci
, &cb
->fcc
, &rv
, NULL
);
38 return ZEND_HASH_APPLY_KEEP
;
41 static void php_pqconn_event_connreset(PGEventConnReset
*event
)
43 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
48 if ((evhs
= zend_hash_str_find_ptr(&data
->obj
->intern
->eventhandlers
, ZEND_STRS("reset")))) {
49 zval args
, connection
;
52 php_pq_object_to_zval(data
->obj
, &connection
);
53 add_next_index_zval(&args
, &connection
);
54 zend_hash_apply_with_argument(evhs
, apply_event
, &args
);
60 static void php_pqconn_event_resultcreate(PGEventResultCreate
*event
)
62 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
65 php_pqres_object_t
*obj
;
68 php_pqres_init_instance_data(event
->result
, data
->obj
, &obj
);
71 if ((evhs
= zend_hash_str_find_ptr(&data
->obj
->intern
->eventhandlers
, ZEND_STRL("result")))) {
72 zval args
, connection
, res
;
75 php_pq_object_to_zval(data
->obj
, &connection
);
76 add_next_index_zval(&args
, &connection
);
77 php_pq_object_to_zval(obj
, &res
);
78 add_next_index_zval(&args
, &res
);
79 zend_hash_apply_with_argument(evhs
, apply_event
, &args
);
84 if (data
->obj
->intern
->onevent
.fci
.size
> 0) {
87 php_pq_object_to_zval(obj
, &res
);
88 zend_fcall_info_argn(&data
->obj
->intern
->onevent
.fci
, 1, &res
);
89 zend_fcall_info_call(&data
->obj
->intern
->onevent
.fci
, &data
->obj
->intern
->onevent
.fcc
, NULL
, NULL
);
96 static void php_pqconn_event_resultdestroy(PGEventResultDestroy
*event
)
98 php_pqres_object_t
*obj
= PQresultInstanceData(event
->result
, php_pqconn_event
);
101 obj
->intern
->res
= NULL
;
105 int php_pqconn_event(PGEventId id
, void *e
, void *data
)
108 case PGEVT_CONNRESET
:
109 php_pqconn_event_connreset(e
);
111 case PGEVT_RESULTCREATE
:
112 php_pqconn_event_resultcreate(e
);
114 case PGEVT_RESULTDESTROY
:
115 php_pqconn_event_resultdestroy(e
);
124 php_pqconn_event_data_t
*php_pqconn_event_data_init(php_pqconn_object_t
*obj
)
126 php_pqconn_event_data_t
*data
= emalloc(sizeof(*data
));
133 void php_pqconn_notice_recv(void *p
, const PGresult
*res
)
135 php_pqconn_event_data_t
*data
= p
;
140 if ((evhs
= zend_hash_str_find_ptr(&data
->obj
->intern
->eventhandlers
, ZEND_STRL("notice")))) {
141 zval args
, connection
;
144 php_pq_object_to_zval(data
->obj
, &connection
);
145 add_next_index_zval(&args
, &connection
);
146 add_next_index_string(&args
, PHP_PQresultErrorMessage(res
));
147 zend_hash_apply_with_argument(evhs
, apply_event
, &args
);
148 zval_ptr_dtor(&args
);
153 void php_pqconn_notice_ignore(void *p
, const PGresult
*res
)
162 * vim600: noet sw=4 ts=4 fdm=marker
163 * vim<600: noet sw=4 ts=4