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
);
36 zend_fcall_info_args_clear(&cb
->fci
, 0);
39 return ZEND_HASH_APPLY_KEEP
;
42 static void php_pqconn_event_connreset(PGEventConnReset
*event
)
44 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
49 if ((zevhs
= zend_hash_str_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRL("reset")))) {
50 zval args
, connection
;
53 php_pq_object_to_zval(data
->obj
, &connection
);
54 add_next_index_zval(&args
, &connection
);
55 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs
), apply_event
, &args
);
61 static void php_pqconn_event_resultcreate(PGEventResultCreate
*event
)
63 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
66 php_pqres_object_t
*obj
= php_pqres_init_instance_data(event
->result
, data
->obj
);
70 if ((zevhs
= zend_hash_str_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRL("result")))) {
71 zval args
, connection
, res
;
74 php_pq_object_to_zval(data
->obj
, &connection
);
75 add_next_index_zval(&args
, &connection
);
76 php_pq_object_to_zval(obj
, &res
);
77 add_next_index_zval(&args
, &res
);
78 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs
), apply_event
, &args
);
83 if (data
->obj
->intern
->onevent
.fci
.size
> 0) {
86 php_pq_object_to_zval(obj
, &res
);
87 zend_fcall_info_argn(&data
->obj
->intern
->onevent
.fci
, 1, &res
);
88 zend_fcall_info_call(&data
->obj
->intern
->onevent
.fci
, &data
->obj
->intern
->onevent
.fcc
, NULL
, NULL
);
95 static void php_pqconn_event_resultdestroy(PGEventResultDestroy
*event
)
97 php_pqres_object_t
*obj
= PQresultInstanceData(event
->result
, php_pqconn_event
);
100 obj
->intern
->res
= NULL
;
101 assert(GC_REFCOUNT(&obj
->zo
));
102 php_pq_object_delref(obj
);
106 int php_pqconn_event(PGEventId id
, void *e
, void *data
)
109 case PGEVT_CONNRESET
:
110 php_pqconn_event_connreset(e
);
112 case PGEVT_RESULTCREATE
:
113 php_pqconn_event_resultcreate(e
);
115 case PGEVT_RESULTDESTROY
:
116 php_pqconn_event_resultdestroy(e
);
125 php_pqconn_event_data_t
*php_pqconn_event_data_init(php_pqconn_object_t
*obj
)
127 php_pqconn_event_data_t
*data
= emalloc(sizeof(*data
));
135 void php_pqconn_notice_recv(void *p
, const PGresult
*res
)
137 php_pqconn_event_data_t
*data
= p
;
142 if ((zevhs
= zend_hash_str_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRL("notice")))) {
143 zval args
, connection
;
146 php_pq_object_to_zval(data
->obj
, &connection
);
147 add_next_index_zval(&args
, &connection
);
148 add_next_index_string(&args
, PHP_PQresultErrorMessage(res
));
149 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs
), apply_event
, &args
);
150 zval_ptr_dtor(&args
);
155 void php_pqconn_notice_ignore(void *p
, const PGresult
*res
)
164 * vim600: noet sw=4 ts=4 fdm=marker
165 * vim<600: noet sw=4 ts=4