MariaDB.xs

filter: Statementstmt 6.7% 106 / 1570 Branchbran 3.1% 38 / 1202 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 5.1% 144 / 2772 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/MariaDB.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "EVAPI.h"
6 #include <mysql.h>
7  
8 #include "ngx_queue.h"
9  
10 typedef struct ev_mariadb_s ev_mariadb_t;
11 typedef struct ev_mariadb_cb_s ev_mariadb_cb_t;
12 typedef struct ev_mariadb_send_s ev_mariadb_send_t;
13 typedef struct ev_mariadb_stmt_s ev_mariadb_stmt_t;
14  
15 typedef ev_mariadb_t* EV__MariaDB;
16 typedef struct ev_loop* EV__Loop;
17  
18 #define EV_MARIADB_MAGIC 0xDEADBEEF
19 #define EV_MARIADB_FREED 0xFEEDFACE
20  
21 enum ev_mariadb_state {
22 STATE_IDLE,
23 STATE_CONNECTING,
24 STATE_SEND,
25 STATE_READ_RESULT,
26 STATE_STORE_RESULT,
27 STATE_NEXT_RESULT,
28 STATE_PING,
29 STATE_CHANGE_USER,
30 STATE_SELECT_DB,
31 STATE_RESET_CONNECTION,
32 STATE_SET_CHARSET,
33 STATE_COMMIT,
34 STATE_ROLLBACK,
35 STATE_AUTOCOMMIT,
36 STATE_STMT_PREPARE, /* boundary: states >= here block query queueing */
37 STATE_STMT_EXECUTE,
38 STATE_STMT_STORE,
39 STATE_STMT_CLOSE,
40 STATE_STMT_RESET,
41 STATE_STMT_SEND_LONG_DATA,
42 STATE_REAL_QUERY,
43 STATE_STREAM_FETCH,
44 STATE_CLOSE,
45 };
46  
47 struct ev_mariadb_s {
48 unsigned int magic;
49 struct ev_loop *loop;
50 SV *loop_sv;
51 MYSQL *conn;
52  
53 ev_io rio, wio;
54 ev_timer timer;
55 int reading, writing, timing;
56 int fd;
57  
58 enum ev_mariadb_state state;
59 char *host, *user, *password, *database, *unix_socket;
60 unsigned int port;
61  
62 ngx_queue_t cb_queue; /* callbacks for sent queries (awaiting results) */
63 ngx_queue_t send_queue; /* queries waiting to be sent */
64 int pending_count; /* total: send_queue + cb_queue */
65 int send_count; /* queries sent, results not yet read */
66 int draining; /* draining multi-result extras */
67  
68 /* current operation context */
69 int op_ret;
70 MYSQL_RES *op_result;
71 MYSQL_STMT *op_stmt;
72 ev_mariadb_stmt_t *op_stmt_ctx; /* per-stmt wrapper for bind_params cleanup */
73 ev_mariadb_stmt_t *stmt_list; /* all allocated stmt wrappers */
74 IV next_stmt_id; /* monotonic counter for statement handle ids */
75 MYSQL *op_conn_ret;
76 my_bool op_bool_ret;
77 MYSQL_ROW op_row; /* for streaming fetch_row result */
78 SV *stream_cb; /* streaming per-row callback */
79 char *op_data_ptr; /* copied data buffer for send_long_data */
80  
81 SV *on_connect;
82 SV *on_error;
83  
84 int callback_depth;
85 pid_t connect_pid; /* PID at connect time, for fork detection */
86  
87 /* connection options (applied before mysql_real_connect_start) */
88 unsigned int connect_timeout;
89 unsigned int read_timeout;
90 unsigned int write_timeout;
91 int compress;
92 int multi_statements;
93 int found_rows;
94 char *charset;
95 char *init_command;
96 char *ssl_key;
97 char *ssl_cert;
98 char *ssl_ca;
99 char *ssl_capath;
100 char *ssl_cipher;
101 int ssl_verify_server_cert;
102 int utf8; /* auto-flag result strings as UTF-8 */
103 unsigned long client_flags;
104  
105 /* Pending cache values for in-flight change_user/select_db/set_charset.
106 Committed by on_utility_done on success, freed on failure. The strings
107 are also passed to the libmariadb start function so they remain valid
108 across the async op. */
109 char *pending_user;
110 char *pending_password;
111 char *pending_database;
112 char *pending_charset;
113 };
114  
115 struct ev_mariadb_cb_s {
116 SV *cb;
117 ngx_queue_t queue;
118 };
119  
120 struct ev_mariadb_send_s {
121 char *sql;
122 unsigned long sql_len;
123 SV *cb;
124 ngx_queue_t queue;
125 };
126  
127 struct ev_mariadb_stmt_s {
128 MYSQL_STMT *stmt;
129 MYSQL_BIND *bind_params;
130 int bind_param_count;
131 int closed; /* invalidated by cleanup_connection */
132 IV id; /* opaque handle id, monotonic, never 0 */
133 ev_mariadb_stmt_t *next; /* linked list of all stmts on this connection */
134 };
135  
136 #define MAX_PIPELINE_DEPTH 64
137 #define MAX_FREELIST_DEPTH 64
138  
139 #define COPY_ERROR(buf, src) do { \
140 strncpy((buf), (src), sizeof(buf) - 1); \
141 (buf)[sizeof(buf) - 1] = '\0'; \
142 } while (0)
143  
144 #define SET_STR_OPTION(field) do { \
145 if (self->field) Safefree(self->field); \
146 self->field = SvOK(value) ? safe_strdup(SvPV_nolen(value)) : NULL; \
147 } while (0)
148  
149 #define IS_FORKED(self) \
150 ((self)->connect_pid != 0 && (self)->connect_pid != getpid())
151  
152 /* Safefree is NULL-safe; the NULL assignment matters for callers where
153 self survives the call (cleanup_connection, reconnect via reset()). */
154 #define FREE_STR(p) do { Safefree(p); (p) = NULL; } while (0)
155  
156 #define CHECK_READY(self, cb) \
157 do { \
158 if (NULL == (self)->conn || (self)->state == STATE_CONNECTING) \
159 croak("not connected"); \
160 if (IS_FORKED(self)) \
161 croak("connection not valid after fork"); \
162 if ((self)->state != STATE_IDLE) \
163 croak("another operation is in progress"); \
164 if ((self)->send_count > 0) \
165 croak("cannot start operation while pipeline results are pending"); \
166 if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) \
167 croak("callback must be a CODE reference"); \
168 } while (0)
169  
170 #define CHECK_STMT(ctx) \
171 do { \
172 if (!(ctx)) \
173 croak("invalid statement handle"); \
174 if ((ctx)->closed) \
175 croak("statement handle is no longer valid (connection was reset)"); \
176 } while (0)
177  
178 static void io_cb(EV_P_ ev_io *w, int revents);
179 static void timer_cb(EV_P_ ev_timer *w, int revents);
180 static void continue_operation(ev_mariadb_t *self, int events);
181 static void pipeline_advance(ev_mariadb_t *self);
182 static void on_next_result_done(ev_mariadb_t *self);
183 static void start_reading(ev_mariadb_t *self);
184 static void stop_reading(ev_mariadb_t *self);
185 static void start_writing(ev_mariadb_t *self);
186 static void stop_writing(ev_mariadb_t *self);
187 static void free_stmt_bind_params(ev_mariadb_stmt_t *ctx);
188 static int is_utf8_charset(unsigned int charsetnr);
189 static void start_timer(ev_mariadb_t *self);
190 static void stop_timer(ev_mariadb_t *self);
191 static void update_watchers(ev_mariadb_t *self, int status);
192 static void emit_error(ev_mariadb_t *self, const char *msg);
193 static void cleanup_connection(ev_mariadb_t *self);
194 static int check_destroyed(ev_mariadb_t *self);
195 static void drain_multi_result(ev_mariadb_t *self);
196 static AV* build_field_names(MYSQL *conn, MYSQL_FIELD *fields, unsigned int ncols);
197 static void on_real_query_done(ev_mariadb_t *self);
198 static void on_stream_fetch_done(ev_mariadb_t *self);
199 static void on_close_done(ev_mariadb_t *self);
200  
201 0 static void maybe_pipeline(ev_mariadb_t *self) {
202 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
203 0 ▶ if (!ngx_queue_empty(&self->send_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->send_queue)) { 0 0 0
204 0 pipeline_advance(self);
205 0 return;
206 }
207 0 stop_reading(self);
208 0 stop_writing(self);
209 0 stop_timer(self);
210 }
211  
212 /* --- freelist for cb_queue entries --- */
213  
214 static ev_mariadb_cb_t *cbt_freelist = NULL;
215 static int cbt_freelist_size = 0;
216  
217 0 static ev_mariadb_cb_t* alloc_cbt(void) {
218 ev_mariadb_cb_t *cbt;
219 0 ▶ if (cbt_freelist) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt_freelist) { 0 0 0
220 0 cbt = cbt_freelist;
221 0 cbt_freelist = *(ev_mariadb_cb_t **)cbt;
222 0 cbt_freelist_size--;
223 } else {
224 0 Newx(cbt, 1, ev_mariadb_cb_t);
225 }
226 0 return cbt;
227 }
228  
229 0 static void release_cbt(ev_mariadb_cb_t *cbt) {
230 0 ▶ if (cbt_freelist_size >= MAX_FREELIST_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt_freelist_size >= MAX_FREELIST_DEPTH) { 0 0 0
231 0 Safefree(cbt);
232 0 return;
233 }
234 0 *(ev_mariadb_cb_t **)cbt = cbt_freelist;
235 0 cbt_freelist = cbt;
236 0 cbt_freelist_size++;
237 }
238  
239 /* --- freelist for send_queue entries --- */
240  
241 static ev_mariadb_send_t *send_freelist = NULL;
242 static int send_freelist_size = 0;
243  
244 0 static ev_mariadb_send_t* alloc_send(void) {
245 ev_mariadb_send_t *s;
246 0 ▶ if (send_freelist) {
Branch: 1 branch
BranchTrueFalseTotal
if (send_freelist) { 0 0 0
247 0 s = send_freelist;
248 0 send_freelist = *(ev_mariadb_send_t **)s;
249 0 send_freelist_size--;
250 } else {
251 0 Newx(s, 1, ev_mariadb_send_t);
252 }
253 0 return s;
254 }
255  
256 0 static void release_send(ev_mariadb_send_t *s) {
257 0 ▶ if (send_freelist_size >= MAX_FREELIST_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (send_freelist_size >= MAX_FREELIST_DEPTH) { 0 0 0
258 0 Safefree(s);
259 0 return;
260 }
261 0 *(ev_mariadb_send_t **)s = send_freelist;
262 0 send_freelist = s;
263 0 send_freelist_size++;
264 }
265  
266 /* Drain freelists at interpreter shutdown so Valgrind reports zero leaks. */
267 17 static void drain_freelists(pTHX_ void *unused) {
268 PERL_UNUSED_VAR(unused);
269 17 ▶ while (cbt_freelist) {
Branch: 1 branch
BranchTrueFalseTotal
while (cbt_freelist) { 0 17 17
270 0 ev_mariadb_cb_t *next = *(ev_mariadb_cb_t **)cbt_freelist;
271 0 Safefree(cbt_freelist);
272 0 cbt_freelist = next;
273 }
274 17 cbt_freelist_size = 0;
275 17 ▶ while (send_freelist) {
Branch: 1 branch
BranchTrueFalseTotal
while (send_freelist) { 0 17 17
276 0 ev_mariadb_send_t *next = *(ev_mariadb_send_t **)send_freelist;
277 0 Safefree(send_freelist);
278 0 send_freelist = next;
279 }
280 17 send_freelist_size = 0;
281 17 }
282  
283 0 static void push_send(ev_mariadb_t *self, const char *sql, STRLEN sql_len, SV *cb) {
284 char *sql_copy;
285 ev_mariadb_send_t *s;
286 0 Newx(sql_copy, sql_len + 1, char);
287 0 Copy(sql, sql_copy, sql_len + 1, char);
288  
289 0 s = alloc_send();
290 0 s->sql = sql_copy;
291 0 s->sql_len = (unsigned long)sql_len;
292 0 s->cb = SvREFCNT_inc(cb);
293 0 ngx_queue_insert_tail(&self->send_queue, &s->queue);
294 0 self->pending_count++;
295 0 }
296  
297 2 static void drain_queues_silent(ev_mariadb_t *self) {
298 2 ▶ while (!ngx_queue_empty(&self->send_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->send_queue)) { 0 2 2
299 0 ngx_queue_t *q = ngx_queue_head(&self->send_queue);
300 0 ev_mariadb_send_t *send = ngx_queue_data(q, ev_mariadb_send_t, queue);
301 0 ngx_queue_remove(q);
302 0 Safefree(send->sql);
303 0 SvREFCNT_dec(send->cb);
304 0 release_send(send);
305 0 self->pending_count--;
306 }
307 2 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 2 2
308 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
309 0 ev_mariadb_cb_t *cbt = ngx_queue_data(q, ev_mariadb_cb_t, queue);
310 0 ngx_queue_remove(q);
311 0 SvREFCNT_dec(cbt->cb);
312 0 release_cbt(cbt);
313 0 self->pending_count--;
314 }
315 2 }
316  
317 /* --- watcher helpers --- */
318  
319 0 static void start_reading(ev_mariadb_t *self) {
320 0 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 0 0 0
if (!self->reading && self->fd >= 0) { 0 0 0
321 0 ev_io_start(self->loop, &self->rio);
322 0 self->reading = 1;
323 }
324 0 }
325  
326 2 static void stop_reading(ev_mariadb_t *self) {
327 2 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 0 2 2
328 0 ev_io_stop(self->loop, &self->rio);
329 0 self->reading = 0;
330 }
331 2 }
332  
333 0 static void start_writing(ev_mariadb_t *self) {
334 0 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 0 0 0
if (!self->writing && self->fd >= 0) { 0 0 0
335 0 ev_io_start(self->loop, &self->wio);
336 0 self->writing = 1;
337 }
338 0 }
339  
340 2 static void stop_writing(ev_mariadb_t *self) {
341 2 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 0 2 2
342 0 ev_io_stop(self->loop, &self->wio);
343 0 self->writing = 0;
344 }
345 2 }
346  
347 0 static void start_timer(ev_mariadb_t *self) {
348 /* libmariadb may report a fresh timeout on each _cont; always re-arm.
349 ms == 0 means "fire as soon as possible" — use 1ms so the timer fires
350 on the next loop iteration rather than stalling. */
351 0 unsigned int ms = mysql_get_timeout_value_ms(self->conn);
352 0 ▶ if (ms == 0) ms = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ms == 0) ms = 1; 0 0 0
353 0 ▶ if (self->timing) ev_timer_stop(self->loop, &self->timer);
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) ev_timer_stop(self->loop, &self->timer); 0 0 0
354 0 ev_timer_set(&self->timer, ms / 1000.0, 0.0);
355 0 ev_timer_start(self->loop, &self->timer);
356 0 self->timing = 1;
357 0 }
358  
359 2 static void stop_timer(ev_mariadb_t *self) {
360 2 ▶ if (self->timing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->timing) { 0 2 2
361 0 ev_timer_stop(self->loop, &self->timer);
362 0 self->timing = 0;
363 }
364 2 }
365  
366 0 static void update_watchers(ev_mariadb_t *self, int status) {
367 0 ▶ if (status & (MYSQL_WAIT_READ | MYSQL_WAIT_EXCEPT)) start_reading(self); else stop_reading(self);
Branch: 1 branch
BranchTrueFalseTotal
if (status & (MYSQL_WAIT_READ | MYSQL_WAIT_EXCEPT)) start_reading(self); else stop_reading(self); 0 0 0
368 0 ▶ if (status & MYSQL_WAIT_WRITE) start_writing(self); else stop_writing(self);
Branch: 1 branch
BranchTrueFalseTotal
if (status & MYSQL_WAIT_WRITE) start_writing(self); else stop_writing(self); 0 0 0
369 0 ▶ if (status & MYSQL_WAIT_TIMEOUT) start_timer(self); else stop_timer(self);
Branch: 1 branch
BranchTrueFalseTotal
if (status & MYSQL_WAIT_TIMEOUT) start_timer(self); else stop_timer(self); 0 0 0
370 0 }
371  
372 0 static void init_io_watchers(ev_mariadb_t *self) {
373 0 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
374 0 self->rio.data = (void *)self;
375 0 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
376 0 self->wio.data = (void *)self;
377 0 }
378  
379 0 static int check_destroyed(ev_mariadb_t *self) {
380 0 ▶ if (self->magic == EV_MARIADB_FREED &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_MARIADB_FREED && 0 0 0
381 0 ▶ self->callback_depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
self->callback_depth == 0) { 0 0 0
382 0 Safefree(self);
383 0 return 1;
384 }
385 0 return 0;
386 }
387  
388 0 static void emit_error(ev_mariadb_t *self, const char *msg) {
389 SV *cb;
390 0 ▶ if (NULL == self->on_error) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->on_error) return; 0 0 0
391 0 cb = sv_2mortal(SvREFCNT_inc_simple_NN(self->on_error));
392  
393 0 dSP;
394 0 ENTER;
395 0 SAVETMPS;
396 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
397 0 ▶ XPUSHs(sv_2mortal(newSVpv(msg, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(msg, 0))); 0 0 0
398 0 PUTBACK;
399  
400 0 call_sv(cb, G_DISCARD | G_EVAL);
401 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
402 0 ▶ warn("EV::MariaDB: exception in error handler: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in error handler: %s", SvPV_nolen(ERRSV)); 0 0 0
403 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
404 }
405  
406 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
407 0 LEAVE;
408 }
409  
410 /* Pop and return the head callback from cb_queue. Caller must SvREFCNT_dec. */
411 0 static SV* pop_cb(ev_mariadb_t *self) {
412 ngx_queue_t *q;
413 ev_mariadb_cb_t *cbt;
414 SV *cb;
415  
416 0 ▶ if (ngx_queue_empty(&self->cb_queue)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return NULL; 0 0 0
417  
418 0 q = ngx_queue_head(&self->cb_queue);
419 0 cbt = ngx_queue_data(q, ev_mariadb_cb_t, queue);
420  
421 0 cb = cbt->cb;
422 0 ngx_queue_remove(q);
423 0 self->pending_count--;
424 0 release_cbt(cbt);
425  
426 0 return cb;
427 }
428  
429 /* Invoke a callback with (undef, errmsg). Decrements cb refcount. */
430 0 static void invoke_error_cb(SV *cb, const char *errmsg) {
431 0 dSP;
432 0 ENTER;
433 0 SAVETMPS;
434 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
435 0 PUSHs(&PL_sv_undef);
436 0 PUSHs(sv_2mortal(newSVpv(errmsg, 0)));
437 0 PUTBACK;
438 0 call_sv(cb, G_DISCARD | G_EVAL);
439 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
440 0 ▶ warn("EV::MariaDB: exception in callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in callback: %s", SvPV_nolen(ERRSV)); 0 0 0
441 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
442 }
443 0 SvREFCNT_dec(cb);
444 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
445 0 LEAVE;
446 0 }
447  
448 /* Invoke a callback with (undef) — EOF signal. Decrements cb refcount. */
449 0 static void invoke_eof_cb(SV *cb) {
450 0 dSP;
451 0 ENTER;
452 0 SAVETMPS;
453 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
454 0 PUSHs(&PL_sv_undef);
455 0 PUTBACK;
456 0 call_sv(cb, G_DISCARD | G_EVAL);
457 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
458 0 ▶ warn("EV::MariaDB: exception in stream callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in stream callback: %s", SvPV_nolen(ERRSV)); 0 0 0
459 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
460 }
461 0 SvREFCNT_dec(cb);
462 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
463 0 LEAVE;
464 0 }
465  
466 /* Invoke a callback SV with args already on the stack. Decrements refcount. */
467 0 static void invoke_cb(SV *cb) {
468 0 call_sv(cb, G_DISCARD | G_EVAL);
469 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
470 0 ▶ warn("EV::MariaDB: exception in callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in callback: %s", SvPV_nolen(ERRSV)); 0 0 0
471 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
472 }
473 0 SvREFCNT_dec(cb);
474 0 }
475  
476 /* Returns 1 if self was freed (caller must not touch self). */
477 0 static int deliver_result(ev_mariadb_t *self) {
478 0 MYSQL_RES *res = self->op_result;
479 0 SV *cb = pop_cb(self);
480  
481 0 ▶ if (cb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb == NULL) { 0 0 0
482 0 ▶ if (res) {
Branch: 1 branch
BranchTrueFalseTotal
if (res) { 0 0 0
483 0 mysql_free_result(res);
484 0 self->op_result = NULL;
485 }
486 0 return 0;
487 }
488  
489 0 self->callback_depth++;
490  
491 {
492 0 dSP;
493 0 ENTER;
494 0 SAVETMPS;
495 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
496  
497 0 ▶ if (res == NULL && mysql_field_count(self->conn) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (res == NULL && mysql_field_count(self->conn) > 0) { 0 0 0
if (res == NULL && mysql_field_count(self->conn) > 0) { 0 0 0
498 0 PUSHs(&PL_sv_undef);
499 0 PUSHs(sv_2mortal(newSVpv(mysql_error(self->conn), 0)));
500 }
501 0 ▶ else if (res != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
else if (res != NULL) { 0 0 0
502 0 my_ulonglong nrows = mysql_num_rows(res);
503 0 unsigned int ncols = mysql_num_fields(res);
504 0 MYSQL_FIELD *fields = mysql_fetch_fields(res);
505 0 AV *rows = newAV();
506 AV *fnames;
507 MYSQL_ROW row;
508 unsigned long *lengths;
509  
510 0 ▶ if (nrows > 0) av_extend(rows, (SSize_t)(nrows < (my_ulonglong)SSize_t_MAX ? nrows - 1 : SSize_t_MAX));
Branch: 2 branches
BranchTrueFalseTotal
if (nrows > 0) av_extend(rows, (SSize_t)(nrows < (my_ulonglong)SSize_t_MAX ? nrows - 1 : SSize_t_MAX)); 0 0 0
if (nrows > 0) av_extend(rows, (SSize_t)(nrows < (my_ulonglong)SSize_t_MAX ? nrows - 1 : SSize_t_MAX)); 0 0 0
511 0 ▶ while ((row = mysql_fetch_row(res)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = mysql_fetch_row(res)) != NULL) { 0 0 0
512 0 AV *r = newAV();
513 unsigned int c;
514 0 lengths = mysql_fetch_lengths(res);
515 0 ▶ if (ncols > 0) av_extend(r, ncols - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) av_extend(r, ncols - 1); 0 0 0
516 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
517 0 ▶ if (row[c] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (row[c] == NULL) { 0 0 0
518 0 av_push(r, newSV(0));
519 } else {
520 0 SV *val = newSVpvn(row[c], lengths[c]);
521 0 ▶ if (self->utf8 && is_utf8_charset(fields[c].charsetnr))
Branch: 2 branches
BranchTrueFalseTotal
if (self->utf8 && is_utf8_charset(fields[c].charsetnr)) 0 0 0
if (self->utf8 && is_utf8_charset(fields[c].charsetnr)) 0 0 0
522 0 SvUTF8_on(val);
523 0 av_push(r, val);
524 }
525 }
526 0 av_push(rows, newRV_noinc((SV*)r));
527 }
528 /* build field names before freeing result */
529 0 fnames = build_field_names(self->conn, fields, ncols);
530 0 mysql_free_result(res);
531 0 self->op_result = NULL;
532 0 PUSHs(sv_2mortal(newRV_noinc((SV*)rows)));
533 0 PUSHs(&PL_sv_undef);
534 0 PUSHs(sv_2mortal(newRV_noinc((SV*)fnames)));
535 }
536 else {
537 0 my_ulonglong affected = mysql_affected_rows(self->conn);
538 0 PUSHs(sv_2mortal(newSVuv((UV)affected)));
539 }
540  
541 0 PUTBACK;
542 0 invoke_cb(cb);
543 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
544 0 LEAVE;
545 }
546  
547 0 self->callback_depth--;
548 0 return check_destroyed(self);
549 }
550  
551 /* Returns 1 if self was freed. */
552 0 static int deliver_error(ev_mariadb_t *self, const char *errmsg) {
553 0 SV *cb = pop_cb(self);
554 0 ▶ if (cb == NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cb == NULL) return 0; 0 0 0
555  
556 0 self->callback_depth++;
557 0 invoke_error_cb(cb, errmsg);
558 0 self->callback_depth--;
559 0 return check_destroyed(self);
560 }
561  
562 /* Returns 1 if self was freed. Caller passes a non-mortal SV; ownership is
563 transferred — deliver_value will sv_2mortal inside its SAVETMPS scope so
564 FREETMPS correctly frees it (avoids mortal leak when sv_2mortal is called
565 before SAVETMPS). */
566 0 static int deliver_value(ev_mariadb_t *self, SV *val) {
567 0 SV *cb = pop_cb(self);
568 0 ▶ if (cb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb == NULL) { 0 0 0
569 0 SvREFCNT_dec(val);
570 0 return 0;
571 }
572  
573 0 self->callback_depth++;
574  
575 {
576 0 dSP;
577 0 ENTER;
578 0 SAVETMPS;
579 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
580 0 PUSHs(sv_2mortal(val));
581 0 PUTBACK;
582 0 invoke_cb(cb);
583 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
584 0 LEAVE;
585 }
586  
587 0 self->callback_depth--;
588 0 return check_destroyed(self);
589 }
590  
591 0 static void cleanup_connection(ev_mariadb_t *self) {
592 0 int saved_fd = self->fd;
593 0 ▶ int is_fork = IS_FORKED(self);
Branch: 2 branches
BranchTrueFalseTotal
int is_fork = IS_FORKED(self); 0 0 0
int is_fork = IS_FORKED(self); 0 0 0
594  
595 0 stop_reading(self);
596 0 stop_writing(self);
597 0 stop_timer(self);
598 0 self->fd = -1;
599 0 self->state = STATE_IDLE;
600 0 self->send_count = 0;
601 0 self->draining = 0;
602  
603 0 ▶ if (self->op_data_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_data_ptr) { 0 0 0
604 0 Safefree(self->op_data_ptr);
605 0 self->op_data_ptr = NULL;
606 }
607  
608 /* Discard any pending utility-op cache update — the op was cancelled. */
609 0 FREE_STR(self->pending_user);
610 0 FREE_STR(self->pending_password);
611 0 FREE_STR(self->pending_database);
612 0 FREE_STR(self->pending_charset);
613  
614 0 self->op_stmt_ctx = NULL;
615  
616 /* Invalidate all tracked stmt wrappers (don't free — user may hold handles) */
617 {
618 0 ev_mariadb_stmt_t *ctx = self->stmt_list;
619 0 ▶ while (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx) { 0 0 0
620 0 free_stmt_bind_params(ctx);
621 0 ▶ if (!is_fork && ctx->stmt) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_fork && ctx->stmt) { 0 0 0
if (!is_fork && ctx->stmt) { 0 0 0
622 0 ▶ if (ctx->stmt == self->op_stmt)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->stmt == self->op_stmt) 0 0 0
623 0 self->op_stmt = NULL; /* avoid double-close below */
624 0 mysql_stmt_close(ctx->stmt);
625 }
626 0 ctx->stmt = NULL;
627 0 ctx->closed = 1;
628 0 ctx = ctx->next;
629 }
630 /* keep stmt_list linked for close_stmt to unlink+free later */
631 }
632  
633 0 ▶ if (is_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_fork) { 0 0 0
634 0 self->op_result = NULL;
635 0 self->op_stmt = NULL;
636 0 self->conn = NULL;
637 } else {
638 0 ▶ if (self->op_result && saved_fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self->op_result && saved_fd >= 0) 0 0 0
if (self->op_result && saved_fd >= 0) 0 0 0
639 0 shutdown(saved_fd, SHUT_RDWR);
640 /* Close op_stmt if not tracked in stmt_list (e.g., in-flight prepare) */
641 0 ▶ if (self->op_stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_stmt) { 0 0 0
642 0 mysql_stmt_close(self->op_stmt);
643 }
644 0 self->op_stmt = NULL;
645 0 ▶ if (self->op_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result) { 0 0 0
646 0 mysql_free_result(self->op_result);
647 0 self->op_result = NULL;
648 }
649 0 ▶ if (self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->conn) { 0 0 0
650 0 MYSQL *conn = self->conn;
651 0 self->conn = NULL;
652 0 mysql_close(conn);
653 }
654 }
655 0 }
656  
657 2 static void cancel_pending(ev_mariadb_t *self, const char *errmsg) {
658 ngx_queue_t local_send;
659 ngx_queue_t local_cb;
660  
661 2 ngx_queue_init(&local_send);
662 2 ngx_queue_init(&local_cb);
663  
664 2 ▶ if (!ngx_queue_empty(&self->send_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->send_queue)) { 0 2 2
665 0 ngx_queue_add(&local_send, &self->send_queue);
666 0 ngx_queue_init(&self->send_queue);
667 }
668  
669 2 ▶ if (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->cb_queue)) { 0 2 2
670 0 ngx_queue_add(&local_cb, &self->cb_queue);
671 0 ngx_queue_init(&self->cb_queue);
672 }
673  
674 2 self->send_count = 0;
675 2 self->callback_depth++;
676  
677 /* cancel active stream if any */
678 2 ▶ if (self->stream_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->stream_cb) { 0 2 2
679 0 SV *cb = self->stream_cb;
680 0 self->stream_cb = NULL;
681 0 self->pending_count--;
682 0 invoke_error_cb(cb, errmsg);
683 }
684  
685 /* cancel unsent queries */
686 2 ▶ while (!ngx_queue_empty(&local_send)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_send)) { 0 2 2
687 0 ngx_queue_t *q = ngx_queue_head(&local_send);
688 0 ev_mariadb_send_t *send = ngx_queue_data(q, ev_mariadb_send_t, queue);
689 0 SV *cb = send->cb;
690 0 ngx_queue_remove(q);
691 0 Safefree(send->sql);
692 0 release_send(send);
693 0 self->pending_count--;
694  
695 0 invoke_error_cb(cb, errmsg);
696 }
697  
698 /* cancel sent-but-not-received */
699 2 ▶ while (!ngx_queue_empty(&local_cb)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_cb)) { 0 2 2
700 0 ngx_queue_t *q = ngx_queue_head(&local_cb);
701 0 ev_mariadb_cb_t *cbt = ngx_queue_data(q, ev_mariadb_cb_t, queue);
702 0 SV *cb = cbt->cb;
703 0 ngx_queue_remove(q);
704 0 self->pending_count--;
705 0 release_cbt(cbt);
706  
707 0 invoke_error_cb(cb, errmsg);
708 }
709  
710 2 self->callback_depth--;
711 2 }
712  
713 0 static void push_cb(ev_mariadb_t *self, SV *cb) {
714 0 ev_mariadb_cb_t *cbt = alloc_cbt();
715 0 cbt->cb = SvREFCNT_inc(cb);
716 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
717 0 self->pending_count++;
718 0 }
719  
720 /* Push cb to cb_queue, transferring ownership (no extra SvREFCNT_inc). */
721 0 static void push_cb_owned(ev_mariadb_t *self, SV *cb) {
722 0 ev_mariadb_cb_t *cbt = alloc_cbt();
723 0 cbt->cb = cb;
724 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
725 /* pending_count was incremented by push_send; ownership transfers from send_queue to cb_queue */
726 0 }
727  
728 2 static SV* handler_accessor(SV **slot, SV *handler, int has_arg) {
729 2 ▶ if (has_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_arg) { 2 0 2
730 2 ▶ if (NULL != *slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != *slot) { 0 2 2
731 0 SvREFCNT_dec(*slot);
732 0 *slot = NULL;
733 }
734 2 ▶ if (NULL != handler && SvOK(handler) &&
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != handler && SvOK(handler) && 2 0 2
if (NULL != handler && SvOK(handler) && 2 0 2
735 2 ▶ SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV) { 2 0 2
SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV) { 2 0 2
736 2 *slot = SvREFCNT_inc(handler);
737 }
738 }
739  
740 2 return (NULL != *slot)
741 2 ? SvREFCNT_inc(*slot)
742 4 ▶ : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_undef; 2 0 2
743 }
744  
745 /* --- Multi-result drain --- */
746  
747 0 static void drain_multi_result(ev_mariadb_t *self) {
748 int status;
749  
750 0 self->draining = 1;
751 0 status = mysql_next_result_start(&self->op_ret, self->conn);
752 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
753 0 self->state = STATE_NEXT_RESULT;
754 0 update_watchers(self, status);
755 0 return;
756 }
757 0 on_next_result_done(self);
758 }
759  
760 0 static void on_next_result_done(ev_mariadb_t *self) {
761 int status;
762  
763 for (;;) {
764 0 ▶ if (self->op_ret > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret > 0) { 0 0 0
765 /* error in secondary result set — report and stop draining */
766 0 self->draining = 0;
767 0 self->state = STATE_IDLE;
768 0 self->callback_depth++;
769 0 emit_error(self, mysql_error(self->conn));
770 0 self->callback_depth--;
771 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
772 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
773 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
774 0 drain_multi_result(self);
775 0 return;
776 }
777 0 pipeline_advance(self);
778 0 return;
779 }
780  
781 0 ▶ if (self->op_ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret == -1) { 0 0 0
782 /* no more results */
783 0 break;
784 }
785  
786 /* op_ret == 0: another result set is available */
787 0 ▶ if (mysql_field_count(self->conn) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mysql_field_count(self->conn) > 0) { 0 0 0
788 0 status = mysql_store_result_start(&self->op_result, self->conn);
789 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
790 0 self->state = STATE_STORE_RESULT;
791 0 update_watchers(self, status);
792 0 return;
793 }
794 /* synchronous store — free and continue drain */
795 0 ▶ if (self->op_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result) { 0 0 0
796 0 mysql_free_result(self->op_result);
797 0 self->op_result = NULL;
798 }
799 }
800  
801 /* DML result or sync store done — check for more */
802 0 ▶ if (!mysql_more_results(self->conn))
Branch: 1 branch
BranchTrueFalseTotal
if (!mysql_more_results(self->conn)) 0 0 0
803 0 break;
804  
805 0 status = mysql_next_result_start(&self->op_ret, self->conn);
806 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
807 0 self->state = STATE_NEXT_RESULT;
808 0 update_watchers(self, status);
809 0 return;
810 }
811 /* synchronous completion — loop instead of recursing */
812 }
813  
814 0 self->draining = 0;
815 0 self->state = STATE_IDLE;
816 0 pipeline_advance(self);
817 }
818  
819 /* --- Pipeline: send_query + read_query_result state machine --- */
820  
821 0 static void handle_send_failure(ev_mariadb_t *self, ev_mariadb_send_t *send) {
822 /* Any send failure tears down the connection below. Conservative: fires
823 even for client-side errors (e.g. CR_NET_PACKET_TOO_LARGE) where nothing
824 was sent and the session is still healthy. */
825 char errbuf[512];
826 0 SV *cb = send->cb;
827 0 Safefree(send->sql);
828 0 release_send(send);
829 0 self->pending_count--;
830  
831 0 COPY_ERROR(errbuf, mysql_error(self->conn));
832  
833 0 self->state = STATE_IDLE;
834 0 self->callback_depth++;
835 0 invoke_error_cb(cb, errbuf);
836 0 self->callback_depth--;
837 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
838 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
839 0 cancel_pending(self, "send failed");
840 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
841 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
842 0 drain_queues_silent(self);
843 0 cleanup_connection(self);
844 }
845  
846 0 static void on_send_done(ev_mariadb_t *self) {
847 0 ngx_queue_t *q = ngx_queue_head(&self->send_queue);
848 0 ev_mariadb_send_t *send = ngx_queue_data(q, ev_mariadb_send_t, queue);
849 0 ngx_queue_remove(q);
850  
851 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
852 0 handle_send_failure(self, send);
853 0 return;
854 }
855  
856 0 push_cb_owned(self, send->cb);
857 0 Safefree(send->sql);
858 0 release_send(send);
859 0 self->send_count++;
860  
861 0 self->state = STATE_IDLE;
862 0 pipeline_advance(self);
863 }
864  
865 0 static void on_read_result_done(ev_mariadb_t *self) {
866 int status;
867  
868 0 ▶ if (self->op_bool_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_bool_ret != 0) { 0 0 0
869 /* query returned an error */
870 0 self->send_count--;
871 0 self->state = STATE_IDLE;
872 0 ▶ if (deliver_error(self, mysql_error(self->conn))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_error(self->conn))) return; 0 0 0
873 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
874 /* drain any remaining result sets */
875 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
876 0 drain_multi_result(self);
877 0 return;
878 }
879 0 pipeline_advance(self);
880 0 return;
881 }
882  
883 0 ▶ if (mysql_field_count(self->conn) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mysql_field_count(self->conn) > 0) { 0 0 0
884 /* has result set — store it */
885 0 status = mysql_store_result_start(&self->op_result, self->conn);
886 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
887 0 self->state = STATE_STORE_RESULT;
888 0 update_watchers(self, status);
889 0 return;
890 }
891 /* synchronous store completion — fall through below */
892 }
893  
894 /* DML or synchronous store_result completion */
895 {
896 0 self->send_count--;
897 0 self->state = STATE_IDLE;
898 0 ▶ if (deliver_result(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_result(self)) return; 0 0 0
899 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
900 /* drain any extra result sets from multi-result queries */
901 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
902 0 drain_multi_result(self);
903 0 return;
904 }
905 0 pipeline_advance(self);
906 }
907 }
908  
909 /* Called when store_result_cont completes (pipeline text query path) */
910 0 static void on_store_result_done(ev_mariadb_t *self) {
911 0 ▶ if (self->draining) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->draining) { 0 0 0
912 0 MYSQL_RES *res = self->op_result;
913 0 self->op_result = NULL;
914 /* draining multi-result: free and continue */
915 0 ▶ if (res) mysql_free_result(res);
Branch: 1 branch
BranchTrueFalseTotal
if (res) mysql_free_result(res); 0 0 0
916 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
917 0 int status = mysql_next_result_start(&self->op_ret, self->conn);
918 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
919 0 self->state = STATE_NEXT_RESULT;
920 0 update_watchers(self, status);
921 0 return;
922 }
923 0 on_next_result_done(self);
924 0 return;
925 }
926 0 self->draining = 0;
927 0 self->state = STATE_IDLE;
928 0 pipeline_advance(self);
929 0 return;
930 }
931  
932 0 self->send_count--;
933 0 self->state = STATE_IDLE;
934  
935 0 ▶ if (deliver_result(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_result(self)) return; 0 0 0
936 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
937 /* drain any extra result sets */
938 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
939 0 drain_multi_result(self);
940 0 return;
941 }
942 0 pipeline_advance(self);
943 }
944  
945 /*
946 * Pipeline orchestrator. Called when state == IDLE.
947 * Phase 1: send all queued queries via mysql_send_query.
948 * Phase 2: read next result via mysql_read_query_result.
949 */
950 0 static void pipeline_advance(ev_mariadb_t *self) {
951 int status;
952  
953 /* Ensure clean watcher state — previous operations may have left
954 * watchers active after completing synchronously within their
955 * done handlers. Without this, subsequent operations that need
956 * the same watcher direction would skip ev_io_start. */
957 0 stop_reading(self);
958 0 stop_writing(self);
959 0 stop_timer(self);
960  
961 0 send_phase:
962 /* Phase 1: send up to MAX_PIPELINE_DEPTH queries */
963 0 ▶ while (!ngx_queue_empty(&self->send_queue) &&
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->send_queue) && 0 0 0
964 0 ▶ self->send_count < MAX_PIPELINE_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
self->send_count < MAX_PIPELINE_DEPTH) { 0 0 0
965 0 ngx_queue_t *q = ngx_queue_head(&self->send_queue);
966 0 ev_mariadb_send_t *send = ngx_queue_data(q, ev_mariadb_send_t, queue);
967  
968 0 status = mysql_send_query_start(&self->op_ret, self->conn,
969 0 send->sql, send->sql_len);
970  
971 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
972 /* need async IO to finish sending */
973 0 self->state = STATE_SEND;
974 0 update_watchers(self, status);
975 0 return;
976 }
977  
978 /* synchronous send completion */
979 0 ngx_queue_remove(q);
980  
981 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
982 0 handle_send_failure(self, send);
983 0 return;
984 }
985  
986 0 push_cb_owned(self, send->cb);
987 0 Safefree(send->sql);
988 0 release_send(send);
989 0 self->send_count++;
990 }
991  
992 /* Phase 2: read next result */
993 0 ▶ while (self->send_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->send_count > 0) { 0 0 0
994 0 status = mysql_read_query_result_start(&self->op_bool_ret, self->conn);
995  
996 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
997 0 self->state = STATE_READ_RESULT;
998 0 update_watchers(self, status);
999 0 return;
1000 }
1001  
1002 /* synchronous read completion */
1003 0 ▶ if (self->op_bool_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_bool_ret != 0) { 0 0 0
1004 /* query error */
1005 0 self->send_count--;
1006 0 ▶ if (deliver_error(self, mysql_error(self->conn))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_error(self->conn))) return; 0 0 0
1007 /* callback may have started a new operation */
1008 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1009 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
1010 0 drain_multi_result(self);
1011 0 return;
1012 }
1013 0 ▶ if (!ngx_queue_empty(&self->send_queue)) goto send_phase;
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->send_queue)) goto send_phase; 0 0 0
1014 0 continue;
1015 }
1016  
1017 0 ▶ if (mysql_field_count(self->conn) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mysql_field_count(self->conn) > 0) { 0 0 0
1018 /* has result set */
1019 0 status = mysql_store_result_start(&self->op_result, self->conn);
1020 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
1021 0 self->state = STATE_STORE_RESULT;
1022 0 update_watchers(self, status);
1023 0 return;
1024 }
1025 }
1026  
1027 {
1028 0 self->send_count--;
1029 0 ▶ if (deliver_result(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_result(self)) return; 0 0 0
1030 /* callback may have started a new operation */
1031 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1032 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
1033 0 drain_multi_result(self);
1034 0 return;
1035 }
1036 /* loop back to check send_queue (callback may have queued more) */
1037 0 ▶ if (!ngx_queue_empty(&self->send_queue)) goto send_phase;
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->send_queue)) goto send_phase; 0 0 0
1038 }
1039 }
1040  
1041 0 self->state = STATE_IDLE;
1042 }
1043  
1044 /* --- Connection --- */
1045  
1046 0 static void on_connect_done(ev_mariadb_t *self) {
1047 0 self->state = STATE_IDLE;
1048  
1049 0 ▶ if (self->op_conn_ret == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_conn_ret == NULL) { 0 0 0
1050 char errbuf[512];
1051 0 COPY_ERROR(errbuf, mysql_error(self->conn));
1052 0 self->callback_depth++;
1053 0 emit_error(self, errbuf);
1054 0 self->callback_depth--;
1055 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1056 /* on_error handler may have called reset(), starting a new connection;
1057 if state is no longer IDLE, don't touch the new connection */
1058 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1059 0 cancel_pending(self, errbuf);
1060 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1061 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1062 0 drain_queues_silent(self);
1063 0 cleanup_connection(self);
1064 0 return;
1065 }
1066  
1067 /* connected — reinit watchers for normal IO */
1068 0 stop_reading(self);
1069 0 stop_writing(self);
1070 0 stop_timer(self);
1071  
1072 0 self->fd = mysql_get_socket(self->conn);
1073  
1074 0 ▶ if (self->fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd < 0) { 0 0 0
1075 0 self->callback_depth++;
1076 0 emit_error(self, "mysql_get_socket returned invalid fd");
1077 0 self->callback_depth--;
1078 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1079 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1080 0 cancel_pending(self, "invalid fd");
1081 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1082 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
1083 0 drain_queues_silent(self);
1084 0 cleanup_connection(self);
1085 0 return;
1086 }
1087  
1088 0 init_io_watchers(self);
1089  
1090 0 ▶ if (NULL != self->on_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->on_connect) { 0 0 0
1091 0 SV *cb = sv_2mortal(SvREFCNT_inc_simple_NN(self->on_connect));
1092 0 self->callback_depth++;
1093  
1094 {
1095 0 dSP;
1096 0 ENTER;
1097 0 SAVETMPS;
1098 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1099 0 PUTBACK;
1100  
1101 0 call_sv(cb, G_DISCARD | G_EVAL);
1102 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
1103 0 ▶ warn("EV::MariaDB: exception in connect handler: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in connect handler: %s", SvPV_nolen(ERRSV)); 0 0 0
1104 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
1105 }
1106  
1107 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1108 0 LEAVE;
1109 }
1110  
1111 0 self->callback_depth--;
1112 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1113 }
1114  
1115 /* start pipeline if queries were queued during connection */
1116 0 maybe_pipeline(self);
1117 }
1118  
1119 /* --- Prepared statements --- */
1120  
1121 0 static void on_stmt_prepare_done(ev_mariadb_t *self) {
1122 0 MYSQL_STMT *stmt = self->op_stmt;
1123 0 self->op_stmt = NULL;
1124 0 self->state = STATE_IDLE;
1125  
1126 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
1127 char errbuf[512];
1128 0 COPY_ERROR(errbuf, mysql_stmt_error(stmt));
1129 0 mysql_stmt_close(stmt);
1130 0 ▶ if (deliver_error(self, errbuf)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, errbuf)) return; 0 0 0
1131 0 maybe_pipeline(self);
1132 0 return;
1133 }
1134  
1135 {
1136 ev_mariadb_stmt_t *ctx;
1137 0 Newxz(ctx, 1, ev_mariadb_stmt_t);
1138 0 ctx->stmt = stmt;
1139 0 ctx->id = ++self->next_stmt_id;
1140 0 ctx->next = self->stmt_list;
1141 0 self->stmt_list = ctx;
1142 0 ▶ if (deliver_value(self, newSViv(ctx->id))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(ctx->id))) return; 0 0 0
1143 }
1144 0 maybe_pipeline(self);
1145 }
1146  
1147 0 static void on_stmt_execute_done(ev_mariadb_t *self) {
1148 0 MYSQL_STMT *stmt = self->op_stmt;
1149 0 self->op_stmt = NULL;
1150 0 self->state = STATE_IDLE;
1151  
1152 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
1153 0 ▶ if (deliver_error(self, mysql_stmt_error(stmt))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_stmt_error(stmt))) return; 0 0 0
1154 0 maybe_pipeline(self);
1155 0 return;
1156 }
1157  
1158 {
1159 MYSQL_RES *meta;
1160 SV *cb;
1161 0 meta = mysql_stmt_result_metadata(stmt);
1162 0 self->op_result = meta;
1163 0 cb = pop_cb(self);
1164 0 ▶ if (cb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb == NULL) { 0 0 0
1165 0 ▶ if (self->op_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result) { 0 0 0
1166 0 mysql_stmt_free_result(stmt);
1167 0 mysql_free_result(self->op_result);
1168 0 self->op_result = NULL;
1169 }
1170 0 maybe_pipeline(self);
1171 0 return;
1172 }
1173  
1174 0 self->callback_depth++;
1175 {
1176 0 dSP;
1177 0 ENTER;
1178 0 SAVETMPS;
1179 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1180  
1181 0 ▶ if (self->op_result == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result == NULL) { 0 0 0
1182 0 my_ulonglong affected = mysql_stmt_affected_rows(stmt);
1183 0 PUSHs(sv_2mortal(newSVuv((UV)affected)));
1184 }
1185 else {
1186 0 unsigned int ncols = mysql_num_fields(self->op_result);
1187 0 MYSQL_FIELD *fields = mysql_fetch_fields(self->op_result);
1188 MYSQL_BIND *bind;
1189 unsigned long *lengths;
1190 my_bool *is_null;
1191 char **buffers;
1192 unsigned int c;
1193 0 AV *rows = newAV();
1194 int fetch_ret;
1195  
1196 0 Newxz(bind, ncols, MYSQL_BIND);
1197 0 SAVEFREEPV(bind);
1198 0 Newx(lengths, ncols, unsigned long);
1199 0 SAVEFREEPV(lengths);
1200 0 Newx(is_null, ncols, my_bool);
1201 0 SAVEFREEPV(is_null);
1202 0 Newx(buffers, ncols, char *);
1203 0 SAVEFREEPV(buffers);
1204  
1205 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
1206 0 unsigned long buflen = fields[c].max_length;
1207 0 ▶ if (buflen < 256) buflen = 256;
Branch: 1 branch
BranchTrueFalseTotal
if (buflen < 256) buflen = 256; 0 0 0
1208 0 Newx(buffers[c], buflen, char);
1209 0 SAVEFREEPV(buffers[c]);
1210 0 bind[c].buffer_type = MYSQL_TYPE_STRING;
1211 0 bind[c].buffer = buffers[c];
1212 0 bind[c].buffer_length = buflen;
1213 0 bind[c].length = &lengths[c];
1214 0 bind[c].is_null = &is_null[c];
1215 }
1216  
1217 0 ▶ if (mysql_stmt_bind_result(stmt, bind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mysql_stmt_bind_result(stmt, bind)) { 0 0 0
1218 0 mysql_stmt_free_result(stmt);
1219 0 mysql_free_result(self->op_result);
1220 0 self->op_result = NULL;
1221 0 SvREFCNT_dec((SV*)rows);
1222  
1223 0 PUSHs(&PL_sv_undef);
1224 0 PUSHs(sv_2mortal(newSVpv(mysql_stmt_error(stmt), 0)));
1225 0 goto invoke;
1226 }
1227  
1228 0 ▶ while ((fetch_ret = mysql_stmt_fetch(stmt)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fetch_ret = mysql_stmt_fetch(stmt)) == 0) { 0 0 0
1229 0 AV *row = newAV();
1230 0 ▶ if (ncols > 0) av_extend(row, ncols - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) av_extend(row, ncols - 1); 0 0 0
1231 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
1232 0 ▶ if (is_null[c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null[c]) { 0 0 0
1233 0 av_push(row, newSV(0));
1234 } else {
1235 0 SV *val = newSVpvn(buffers[c], lengths[c]);
1236 0 ▶ if (self->utf8 && is_utf8_charset(fields[c].charsetnr))
Branch: 2 branches
BranchTrueFalseTotal
if (self->utf8 && is_utf8_charset(fields[c].charsetnr)) 0 0 0
if (self->utf8 && is_utf8_charset(fields[c].charsetnr)) 0 0 0
1237 0 SvUTF8_on(val);
1238 0 av_push(row, val);
1239 }
1240 }
1241 0 av_push(rows, newRV_noinc((SV*)row));
1242 }
1243  
1244 0 ▶ if (fetch_ret != 0 && fetch_ret != MYSQL_NO_DATA) {
Branch: 2 branches
BranchTrueFalseTotal
if (fetch_ret != 0 && fetch_ret != MYSQL_NO_DATA) { 0 0 0
if (fetch_ret != 0 && fetch_ret != MYSQL_NO_DATA) { 0 0 0
1245 0 mysql_free_result(self->op_result);
1246 0 self->op_result = NULL;
1247 0 mysql_stmt_free_result(stmt);
1248 0 SvREFCNT_dec((SV*)rows);
1249 0 PUSHs(&PL_sv_undef);
1250 0 ▶ if (fetch_ret == MYSQL_DATA_TRUNCATED)
Branch: 1 branch
BranchTrueFalseTotal
if (fetch_ret == MYSQL_DATA_TRUNCATED) 0 0 0
1251 0 PUSHs(sv_2mortal(newSVpvs("data truncated")));
1252 else
1253 0 PUSHs(sv_2mortal(newSVpv(mysql_stmt_error(stmt), 0)));
1254 } else {
1255 0 AV *fnames = build_field_names(self->conn, fields, ncols);
1256 0 mysql_free_result(self->op_result);
1257 0 self->op_result = NULL;
1258 0 mysql_stmt_free_result(stmt);
1259 0 PUSHs(sv_2mortal(newRV_noinc((SV*)rows)));
1260 0 PUSHs(&PL_sv_undef);
1261 0 PUSHs(sv_2mortal(newRV_noinc((SV*)fnames)));
1262 }
1263 }
1264  
1265 0 invoke:
1266 0 PUTBACK;
1267 0 invoke_cb(cb);
1268 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1269 0 LEAVE;
1270 }
1271 0 self->callback_depth--;
1272 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1273 0 maybe_pipeline(self);
1274 }
1275 }
1276  
1277 0 static void on_stmt_store_done(ev_mariadb_t *self) {
1278 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
1279 0 MYSQL_STMT *stmt = self->op_stmt;
1280 0 self->op_stmt = NULL;
1281 0 self->state = STATE_IDLE;
1282 0 ▶ if (deliver_error(self, mysql_stmt_error(stmt))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_stmt_error(stmt))) return; 0 0 0
1283 0 maybe_pipeline(self);
1284 0 return;
1285 }
1286 0 on_stmt_execute_done(self);
1287 }
1288  
1289 /* Resolve an opaque statement-handle id to its live wrapper, or NULL if none
1290 matches (stale/closed/fabricated). Never dereferences the user's value. */
1291 0 static ev_mariadb_stmt_t *find_stmt(ev_mariadb_t *self, IV id) {
1292 0 ev_mariadb_stmt_t *ctx = self->stmt_list;
1293 0 ▶ while (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx) { 0 0 0
1294 0 ▶ if (ctx->id == id) return ctx;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->id == id) return ctx; 0 0 0
1295 0 ctx = ctx->next;
1296 }
1297 0 return NULL;
1298 }
1299  
1300 0 static void unlink_stmt(ev_mariadb_t *self, ev_mariadb_stmt_t *ctx) {
1301 0 ev_mariadb_stmt_t **pp = &self->stmt_list;
1302 0 ▶ while (*pp) {
Branch: 1 branch
BranchTrueFalseTotal
while (*pp) { 0 0 0
1303 0 ▶ if (*pp == ctx) { *pp = ctx->next; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (*pp == ctx) { *pp = ctx->next; return; } 0 0 0
1304 0 pp = &(*pp)->next;
1305 }
1306 }
1307  
1308 0 static void on_stmt_close_done(ev_mariadb_t *self) {
1309 0 self->op_stmt = NULL; /* stmt freed by mysql_stmt_close */
1310 0 ▶ if (self->op_stmt_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_stmt_ctx) { 0 0 0
1311 0 unlink_stmt(self, self->op_stmt_ctx);
1312 0 Safefree(self->op_stmt_ctx);
1313 0 self->op_stmt_ctx = NULL;
1314 }
1315 0 self->state = STATE_IDLE;
1316 0 ▶ if (self->op_bool_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_bool_ret != 0) { 0 0 0
1317 0 ▶ if (deliver_error(self, mysql_error(self->conn))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_error(self->conn))) return; 0 0 0
1318 } else {
1319 0 ▶ if (deliver_value(self, newSViv(1))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(1))) return; 0 0 0
1320 }
1321 0 maybe_pipeline(self);
1322 }
1323  
1324 0 static void on_stmt_reset_done(ev_mariadb_t *self) {
1325 0 MYSQL_STMT *stmt = self->op_stmt;
1326 0 self->op_stmt = NULL;
1327 0 self->state = STATE_IDLE;
1328 0 ▶ if (self->op_bool_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_bool_ret != 0) { 0 0 0
1329 0 ▶ if (deliver_error(self, mysql_stmt_error(stmt))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_stmt_error(stmt))) return; 0 0 0
1330 } else {
1331 0 ▶ if (deliver_value(self, newSViv(1))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(1))) return; 0 0 0
1332 }
1333 0 maybe_pipeline(self);
1334 }
1335  
1336 /* --- Async utility operation done handler --- */
1337  
1338 /* mysql_reset_connection / mysql_change_user drop all server-side prepared
1339 statements; invalidate every tracked wrapper so user code can't keep using
1340 them. Closes the local libmariadb stmt state and marks closed=1, mirroring
1341 cleanup_connection's pattern but without touching the live connection. */
1342 0 static void invalidate_all_stmts(ev_mariadb_t *self) {
1343 0 ev_mariadb_stmt_t *ctx = self->stmt_list;
1344 0 ▶ while (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx) { 0 0 0
1345 0 free_stmt_bind_params(ctx);
1346 0 ▶ if (ctx->stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->stmt) { 0 0 0
1347 0 mysql_stmt_close(ctx->stmt);
1348 0 ctx->stmt = NULL;
1349 }
1350 0 ctx->closed = 1;
1351 0 ctx = ctx->next;
1352 }
1353 0 self->op_stmt = NULL;
1354 0 self->op_stmt_ctx = NULL;
1355 0 }
1356  
1357 /* Commit (success) or discard (failure) any pending utility-op cache update. */
1358 0 static void resolve_pending_cache(ev_mariadb_t *self, int failed) {
1359 0 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 0 0 0
1360 0 FREE_STR(self->pending_user);
1361 0 FREE_STR(self->pending_password);
1362 0 FREE_STR(self->pending_database);
1363 0 FREE_STR(self->pending_charset);
1364 0 return;
1365 }
1366 0 ▶ if (self->pending_user) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pending_user) { 0 0 0
1367 0 Safefree(self->user);
1368 0 self->user = self->pending_user;
1369 0 self->pending_user = NULL;
1370 }
1371 0 ▶ if (self->pending_password) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pending_password) { 0 0 0
1372 0 Safefree(self->password);
1373 0 self->password = self->pending_password;
1374 0 self->pending_password = NULL;
1375 }
1376 0 ▶ if (self->pending_database) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pending_database) { 0 0 0
1377 0 Safefree(self->database);
1378 0 self->database = self->pending_database;
1379 0 self->pending_database = NULL;
1380 }
1381 0 ▶ if (self->pending_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pending_charset) { 0 0 0
1382 0 Safefree(self->charset);
1383 0 self->charset = self->pending_charset;
1384 0 self->pending_charset = NULL;
1385 }
1386 }
1387  
1388 0 static void on_utility_done(ev_mariadb_t *self, int failed) {
1389 0 enum ev_mariadb_state prev = self->state;
1390 0 self->state = STATE_IDLE;
1391 0 resolve_pending_cache(self, failed);
1392 0 ▶ if (!failed && (prev == STATE_RESET_CONNECTION || prev == STATE_CHANGE_USER))
Branch: 3 branches
BranchTrueFalseTotal
if (!failed && (prev == STATE_RESET_CONNECTION || prev == STATE_CHANGE_USER)) 0 0 0
if (!failed && (prev == STATE_RESET_CONNECTION || prev == STATE_CHANGE_USER)) 0 0 0
if (!failed && (prev == STATE_RESET_CONNECTION || prev == STATE_CHANGE_USER)) 0 0 0
1393 0 invalidate_all_stmts(self);
1394 0 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 0 0 0
1395 0 ▶ if (deliver_error(self, mysql_error(self->conn))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_error(self->conn))) return; 0 0 0
1396 } else {
1397 0 ▶ if (deliver_value(self, newSViv(1))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(1))) return; 0 0 0
1398 }
1399 0 maybe_pipeline(self);
1400 }
1401  
1402 /* Trailing dispatch shared by every utility op start/cont site:
1403 completed → on_utility_done; otherwise → arm watchers. */
1404 0 static void dispatch_utility(ev_mariadb_t *self, int status, int failed) {
1405 0 ▶ if (status == 0) on_utility_done(self, failed);
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) on_utility_done(self, failed); 0 0 0
1406 0 else update_watchers(self, status);
1407 0 }
1408  
1409 0 static void free_stmt_bind_params(ev_mariadb_stmt_t *ctx) {
1410 int i;
1411 0 ▶ if (!ctx->bind_params) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->bind_params) return; 0 0 0
1412 0 ▶ for (i = 0; i < ctx->bind_param_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->bind_param_count; i++) { 0 0 0
1413 0 Safefree(ctx->bind_params[i].buffer);
1414 }
1415 0 Safefree(ctx->bind_params);
1416 0 ctx->bind_params = NULL;
1417 0 ctx->bind_param_count = 0;
1418 }
1419  
1420 0 static void transition_to_stmt_store(ev_mariadb_t *self) {
1421 int status;
1422 0 self->op_stmt_ctx = NULL; /* bind_params live in wrapper, not freed here */
1423 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
1424 0 on_stmt_execute_done(self);
1425 } else {
1426 0 self->state = STATE_STMT_STORE;
1427 0 status = mysql_stmt_store_result_start(&self->op_ret, self->op_stmt);
1428 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1429 0 on_stmt_store_done(self);
1430 } else {
1431 0 update_watchers(self, status);
1432 }
1433 }
1434 0 }
1435  
1436 /* --- Streaming query (mysql_use_result + fetch_row) --- */
1437  
1438 0 static void stream_error(ev_mariadb_t *self) {
1439 0 SV *cb = self->stream_cb;
1440 char errbuf[512];
1441 0 COPY_ERROR(errbuf, mysql_error(self->conn));
1442 0 self->stream_cb = NULL;
1443 0 self->state = STATE_IDLE;
1444 0 self->pending_count--;
1445 0 self->callback_depth++;
1446 0 invoke_error_cb(cb, errbuf);
1447 0 self->callback_depth--;
1448 0 }
1449  
1450 0 static void on_real_query_done(ev_mariadb_t *self) {
1451 int status;
1452  
1453 0 ▶ if (self->op_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_ret != 0) { 0 0 0
1454 0 stream_error(self);
1455 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1456 0 maybe_pipeline(self);
1457 0 return;
1458 }
1459  
1460 0 self->op_result = mysql_use_result(self->conn);
1461 0 ▶ if (!self->op_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->op_result) { 0 0 0
1462 0 const char *err = mysql_error(self->conn);
1463 0 ▶ if (err && err[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (err && err[0]) { 0 0 0
if (err && err[0]) { 0 0 0
1464 0 stream_error(self);
1465 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1466 } else {
1467 /* DML or no-result query: deliver EOF to stream callback */
1468 0 SV *cb = self->stream_cb;
1469 0 self->stream_cb = NULL;
1470 0 self->state = STATE_IDLE;
1471 0 self->pending_count--;
1472 0 self->callback_depth++;
1473 0 invoke_eof_cb(cb);
1474 0 self->callback_depth--;
1475 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1476 }
1477 0 maybe_pipeline(self);
1478 0 return;
1479 }
1480  
1481 0 status = mysql_fetch_row_start(&self->op_row, self->op_result);
1482 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1483 0 on_stream_fetch_done(self);
1484 } else {
1485 0 self->state = STATE_STREAM_FETCH;
1486 0 update_watchers(self, status);
1487 }
1488 }
1489  
1490 0 static void on_stream_fetch_done(ev_mariadb_t *self) {
1491 0 ▶ MYSQL_FIELD *fields = (self->utf8 && self->op_result)
Branch: 1 branch
BranchTrueFalseTotal
MYSQL_FIELD *fields = (self->utf8 && self->op_result) 0 0 0
1492 0 ▶ ? mysql_fetch_fields(self->op_result) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? mysql_fetch_fields(self->op_result) : NULL; 0 0 0
1493  
1494 0 for (;;) {
1495 0 MYSQL_ROW row = self->op_row;
1496  
1497 0 ▶ if (row == NULL) break;
Branch: 1 branch
BranchTrueFalseTotal
if (row == NULL) break; 0 0 0
1498  
1499 /* deliver row to callback */
1500 {
1501 0 unsigned int ncols = mysql_num_fields(self->op_result);
1502 0 unsigned long *lengths = mysql_fetch_lengths(self->op_result);
1503 0 AV *r = newAV();
1504 unsigned int c;
1505  
1506 0 ▶ if (ncols > 0) av_extend(r, ncols - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) av_extend(r, ncols - 1); 0 0 0
1507 0 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 0 0 0
1508 0 ▶ if (row[c] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (row[c] == NULL) { 0 0 0
1509 0 av_push(r, newSV(0));
1510 } else {
1511 0 SV *val = newSVpvn(row[c], lengths[c]);
1512 0 ▶ if (fields && is_utf8_charset(fields[c].charsetnr))
Branch: 2 branches
BranchTrueFalseTotal
if (fields && is_utf8_charset(fields[c].charsetnr)) 0 0 0
if (fields && is_utf8_charset(fields[c].charsetnr)) 0 0 0
1513 0 SvUTF8_on(val);
1514 0 av_push(r, val);
1515 }
1516 }
1517  
1518 /* Detach stream_cb before invoking: prevents cancel_pending
1519 from double-firing the callback if finish() is called inside */
1520 {
1521 0 SV *saved_cb = self->stream_cb;
1522 0 self->stream_cb = NULL;
1523 0 self->pending_count--;
1524  
1525 0 self->callback_depth++;
1526 {
1527 0 SV *cb = sv_2mortal(SvREFCNT_inc_simple_NN(saved_cb));
1528 0 dSP;
1529 0 ENTER;
1530 0 SAVETMPS;
1531 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1532 0 PUSHs(sv_2mortal(newRV_noinc((SV*)r)));
1533 0 PUTBACK;
1534 0 call_sv(cb, G_DISCARD | G_EVAL);
1535 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
1536 0 ▶ warn("EV::MariaDB: exception in stream callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: exception in stream callback: %s", SvPV_nolen(ERRSV)); 0 0 0
1537 0 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 0 0 0
1538 }
1539 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1540 0 LEAVE;
1541 }
1542 0 self->callback_depth--;
1543 0 ▶ if (check_destroyed(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) { 0 0 0
1544 0 SvREFCNT_dec(saved_cb);
1545 0 return;
1546 }
1547  
1548 /* Was stream cancelled (finish/reset from callback)? */
1549 0 ▶ if (self->stream_cb != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->stream_cb != NULL) { 0 0 0
1550 /* Defence: unreachable via the public API — query_stream's
1551 CHECK_READY rejects the only way stream_cb could be
1552 reset while we're inside on_stream_fetch_done. Drop the
1553 saved cb so the (hypothetical) new stream owns the slot. */
1554 0 SvREFCNT_dec(saved_cb);
1555 0 ▶ } else if (self->conn == NULL || self->op_result == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (self->conn == NULL || self->op_result == NULL) { 0 0 0
} else if (self->conn == NULL || self->op_result == NULL) { 0 0 0
1556 /* connection torn down — stream is over */
1557 0 SvREFCNT_dec(saved_cb);
1558 0 return;
1559 } else {
1560 /* normal: restore for next row */
1561 0 self->stream_cb = saved_cb;
1562 0 self->pending_count++;
1563 }
1564 }
1565 }
1566  
1567 /* fetch next row */
1568 {
1569 0 int status = mysql_fetch_row_start(&self->op_row, self->op_result);
1570 0 ▶ if (status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != 0) { 0 0 0
1571 0 self->state = STATE_STREAM_FETCH;
1572 0 update_watchers(self, status);
1573 0 return;
1574 }
1575 }
1576 }
1577  
1578 /* row == NULL: EOF or error */
1579 {
1580 0 SV *cb = self->stream_cb;
1581 char errbuf[512];
1582 0 const char *err = mysql_error(self->conn);
1583 0 ▶ int has_error = (err && err[0]);
Branch: 2 branches
BranchTrueFalseTotal
int has_error = (err && err[0]); 0 0 0
int has_error = (err && err[0]); 0 0 0
1584 0 ▶ if (has_error) COPY_ERROR(errbuf, err);
Branch: 1 branch
BranchTrueFalseTotal
if (has_error) COPY_ERROR(errbuf, err); 0 0 0
1585  
1586 0 self->stream_cb = NULL;
1587 0 mysql_free_result(self->op_result);
1588 0 self->op_result = NULL;
1589 0 self->state = STATE_IDLE;
1590 0 self->pending_count--;
1591  
1592 0 self->callback_depth++;
1593 0 ▶ if (has_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_error) { 0 0 0
1594 0 invoke_error_cb(cb, errbuf);
1595 } else {
1596 0 invoke_eof_cb(cb);
1597 }
1598 0 self->callback_depth--;
1599 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1600 /* drain any secondary result sets from multi_statements */
1601 0 ▶ if (self->conn && mysql_more_results(self->conn)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
if (self->conn && mysql_more_results(self->conn)) { 0 0 0
1602 0 drain_multi_result(self);
1603 0 return;
1604 }
1605 0 maybe_pipeline(self);
1606 }
1607 }
1608  
1609 /* --- Async close --- */
1610  
1611 0 static void on_close_done(ev_mariadb_t *self) {
1612 SV *cb;
1613  
1614 /* conn was freed by mysql_close */
1615 0 self->conn = NULL;
1616 0 self->fd = -1;
1617 0 stop_reading(self);
1618 0 stop_writing(self);
1619 0 stop_timer(self);
1620 0 self->state = STATE_IDLE;
1621  
1622 /* pop our callback before cancel_pending fires errors for remaining items */
1623 0 cb = pop_cb(self);
1624  
1625 /* cancel any items queued from within a callback before close_async */
1626 0 cancel_pending(self, "connection closed");
1627 0 ▶ if (check_destroyed(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) { 0 0 0
1628 0 SvREFCNT_dec(cb);
1629 0 return;
1630 }
1631  
1632 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
1633 0 SV *val = newSViv(1);
1634 0 self->callback_depth++;
1635 {
1636 0 dSP;
1637 0 ENTER;
1638 0 SAVETMPS;
1639 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1640 0 PUSHs(sv_2mortal(val));
1641 0 PUTBACK;
1642 0 invoke_cb(cb);
1643 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1644 0 LEAVE;
1645 }
1646 0 self->callback_depth--;
1647 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1648 }
1649 /* no maybe_pipeline: conn is NULL, nothing can be sent */
1650 }
1651  
1652 /* --- Send long data done --- */
1653  
1654 0 static void on_send_long_data_done(ev_mariadb_t *self) {
1655 0 MYSQL_STMT *stmt = self->op_stmt;
1656  
1657 0 ▶ if (self->op_data_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_data_ptr) { 0 0 0
1658 0 Safefree(self->op_data_ptr);
1659 0 self->op_data_ptr = NULL;
1660 }
1661 0 self->op_stmt = NULL;
1662 0 self->state = STATE_IDLE;
1663  
1664 0 ▶ if (self->op_bool_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_bool_ret != 0) { 0 0 0
1665 0 ▶ if (deliver_error(self, mysql_stmt_error(stmt))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_error(self, mysql_stmt_error(stmt))) return; 0 0 0
1666 } else {
1667 0 ▶ if (deliver_value(self, newSViv(1))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(1))) return; 0 0 0
1668 }
1669 0 maybe_pipeline(self);
1670 }
1671  
1672 /* --- Main continuation dispatcher --- */
1673  
1674 0 static void continue_operation(ev_mariadb_t *self, int events) {
1675 int status;
1676  
1677 0 switch (self->state) {
1678 0 case STATE_CONNECTING:
1679 0 status = mysql_real_connect_cont(&self->op_conn_ret, self->conn, events);
1680 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1681 0 on_connect_done(self);
1682 } else {
1683 0 update_watchers(self, status);
1684 }
1685 0 break;
1686  
1687 0 case STATE_SEND:
1688 0 status = mysql_send_query_cont(&self->op_ret, self->conn, events);
1689 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1690 0 on_send_done(self);
1691 } else {
1692 0 update_watchers(self, status);
1693 }
1694 0 break;
1695  
1696 0 case STATE_READ_RESULT:
1697 0 status = mysql_read_query_result_cont(&self->op_bool_ret, self->conn, events);
1698 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1699 0 on_read_result_done(self);
1700 } else {
1701 0 update_watchers(self, status);
1702 }
1703 0 break;
1704  
1705 0 case STATE_STORE_RESULT:
1706 0 status = mysql_store_result_cont(&self->op_result, self->conn, events);
1707 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1708 0 on_store_result_done(self);
1709 } else {
1710 0 update_watchers(self, status);
1711 }
1712 0 break;
1713  
1714 0 case STATE_NEXT_RESULT:
1715 0 status = mysql_next_result_cont(&self->op_ret, self->conn, events);
1716 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1717 0 on_next_result_done(self);
1718 } else {
1719 0 update_watchers(self, status);
1720 }
1721 0 break;
1722  
1723 0 case STATE_STMT_PREPARE:
1724 0 status = mysql_stmt_prepare_cont(&self->op_ret, self->op_stmt, events);
1725 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1726 0 on_stmt_prepare_done(self);
1727 } else {
1728 0 update_watchers(self, status);
1729 }
1730 0 break;
1731  
1732 0 case STATE_STMT_EXECUTE:
1733 0 status = mysql_stmt_execute_cont(&self->op_ret, self->op_stmt, events);
1734 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1735 0 transition_to_stmt_store(self);
1736 } else {
1737 0 update_watchers(self, status);
1738 }
1739 0 break;
1740  
1741 0 case STATE_STMT_STORE:
1742 0 status = mysql_stmt_store_result_cont(&self->op_ret, self->op_stmt, events);
1743 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1744 0 on_stmt_store_done(self);
1745 } else {
1746 0 update_watchers(self, status);
1747 }
1748 0 break;
1749  
1750 0 case STATE_STMT_CLOSE:
1751 0 status = mysql_stmt_close_cont(&self->op_bool_ret, self->op_stmt, events);
1752 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1753 0 on_stmt_close_done(self);
1754 } else {
1755 0 update_watchers(self, status);
1756 }
1757 0 break;
1758  
1759 0 case STATE_STMT_RESET:
1760 0 status = mysql_stmt_reset_cont(&self->op_bool_ret, self->op_stmt, events);
1761 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1762 0 on_stmt_reset_done(self);
1763 } else {
1764 0 update_watchers(self, status);
1765 }
1766 0 break;
1767  
1768 0 case STATE_PING:
1769 0 status = mysql_ping_cont(&self->op_ret, self->conn, events);
1770 0 dispatch_utility(self, status, self->op_ret != 0);
1771 0 break;
1772  
1773 0 case STATE_CHANGE_USER:
1774 0 status = mysql_change_user_cont(&self->op_bool_ret, self->conn, events);
1775 0 dispatch_utility(self, status, self->op_bool_ret != 0);
1776 0 break;
1777  
1778 0 case STATE_SELECT_DB:
1779 0 status = mysql_select_db_cont(&self->op_ret, self->conn, events);
1780 0 dispatch_utility(self, status, self->op_ret != 0);
1781 0 break;
1782  
1783 0 case STATE_RESET_CONNECTION:
1784 0 status = mysql_reset_connection_cont(&self->op_ret, self->conn, events);
1785 0 dispatch_utility(self, status, self->op_ret != 0);
1786 0 break;
1787  
1788 0 case STATE_SET_CHARSET:
1789 0 status = mysql_set_character_set_cont(&self->op_ret, self->conn, events);
1790 0 dispatch_utility(self, status, self->op_ret != 0);
1791 0 break;
1792  
1793 0 case STATE_COMMIT:
1794 0 status = mysql_commit_cont(&self->op_bool_ret, self->conn, events);
1795 0 dispatch_utility(self, status, self->op_bool_ret != 0);
1796 0 break;
1797  
1798 0 case STATE_ROLLBACK:
1799 0 status = mysql_rollback_cont(&self->op_bool_ret, self->conn, events);
1800 0 dispatch_utility(self, status, self->op_bool_ret != 0);
1801 0 break;
1802  
1803 0 case STATE_AUTOCOMMIT:
1804 0 status = mysql_autocommit_cont(&self->op_bool_ret, self->conn, events);
1805 0 dispatch_utility(self, status, self->op_bool_ret != 0);
1806 0 break;
1807  
1808 0 case STATE_STMT_SEND_LONG_DATA:
1809 0 status = mysql_stmt_send_long_data_cont(&self->op_bool_ret, self->op_stmt, events);
1810 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1811 0 on_send_long_data_done(self);
1812 } else {
1813 0 update_watchers(self, status);
1814 }
1815 0 break;
1816  
1817 0 case STATE_REAL_QUERY:
1818 0 status = mysql_real_query_cont(&self->op_ret, self->conn, events);
1819 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1820 0 on_real_query_done(self);
1821 } else {
1822 0 update_watchers(self, status);
1823 }
1824 0 break;
1825  
1826 0 case STATE_STREAM_FETCH:
1827 0 status = mysql_fetch_row_cont(&self->op_row, self->op_result, events);
1828 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1829 0 on_stream_fetch_done(self);
1830 } else {
1831 0 update_watchers(self, status);
1832 }
1833 0 break;
1834  
1835 0 case STATE_CLOSE:
1836 0 status = mysql_close_cont(self->conn, events);
1837 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
1838 0 on_close_done(self);
1839 } else {
1840 0 update_watchers(self, status);
1841 }
1842 0 break;
1843  
1844 0 default:
1845 0 warn("EV::MariaDB: unexpected state %d in continue_operation", self->state);
1846 0 break;
1847 }
1848 0 }
1849  
1850 0 static void io_cb(EV_P_ ev_io *w, int revents) {
1851 0 ev_mariadb_t *self = (ev_mariadb_t *)w->data;
1852 0 int events = 0;
1853 (void)loop;
1854  
1855 0 ▶ if (self->magic != EV_MARIADB_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_MARIADB_MAGIC) return; 0 0 0
1856 0 ▶ if (NULL == self->conn) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->conn) return; 0 0 0
1857  
1858 0 ▶ if (revents & EV_READ) events |= MYSQL_WAIT_READ | MYSQL_WAIT_EXCEPT;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) events |= MYSQL_WAIT_READ | MYSQL_WAIT_EXCEPT; 0 0 0
1859 0 ▶ if (revents & EV_WRITE) events |= MYSQL_WAIT_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) events |= MYSQL_WAIT_WRITE; 0 0 0
1860  
1861 0 continue_operation(self, events);
1862 }
1863  
1864 0 static void timer_cb(EV_P_ ev_timer *w, int revents) {
1865 0 ev_mariadb_t *self = (ev_mariadb_t *)w->data;
1866 (void)loop;
1867 (void)revents;
1868  
1869 0 ▶ if (self->magic != EV_MARIADB_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_MARIADB_MAGIC) return; 0 0 0
1870 0 ▶ if (NULL == self->conn) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->conn) return; 0 0 0
1871  
1872 0 self->timing = 0;
1873 0 continue_operation(self, MYSQL_WAIT_TIMEOUT);
1874 }
1875  
1876 0 static char* safe_strdup(const char *s) {
1877 char *d;
1878 size_t len;
1879 0 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 0 0
1880 0 len = strlen(s);
1881 0 Newx(d, len + 1, char);
1882 0 Copy(s, d, len + 1, char);
1883 0 return d;
1884 }
1885  
1886 /* newSVpv on a non-empty string, &PL_sv_undef otherwise. Used by accessors
1887 that wrap a libmariadb getter returning a NUL-terminated string or NULL. */
1888 0 static SV* str_or_undef(const char *s) {
1889 0 ▶ return (s && s[0]) ? newSVpv(s, 0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
return (s && s[0]) ? newSVpv(s, 0) : &PL_sv_undef; 0 0 0
return (s && s[0]) ? newSVpv(s, 0) : &PL_sv_undef; 0 0 0
1890 }
1891  
1892 /* Free *slot then replace it with a fresh copy of src (or NULL). */
1893 0 static void replace_str(char **slot, const char *src) {
1894 0 Safefree(*slot);
1895 0 *slot = safe_strdup(src);
1896 0 }
1897  
1898 2 static void free_connect_strings(ev_mariadb_t *self) {
1899 2 FREE_STR(self->host);
1900 2 FREE_STR(self->user);
1901 2 FREE_STR(self->password);
1902 2 FREE_STR(self->database);
1903 2 FREE_STR(self->unix_socket);
1904 2 }
1905  
1906 2 static void free_option_strings(ev_mariadb_t *self) {
1907 2 FREE_STR(self->charset);
1908 2 FREE_STR(self->init_command);
1909 2 FREE_STR(self->ssl_key);
1910 2 FREE_STR(self->ssl_cert);
1911 2 FREE_STR(self->ssl_ca);
1912 2 FREE_STR(self->ssl_capath);
1913 2 FREE_STR(self->ssl_cipher);
1914 2 }
1915  
1916 0 static void apply_options(ev_mariadb_t *self) {
1917 0 MYSQL *conn = self->conn;
1918 0 unsigned long flags = self->client_flags;
1919  
1920 0 ▶ if (self->connect_timeout > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout > 0) 0 0 0
1921 0 mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &self->connect_timeout);
1922 0 ▶ if (self->read_timeout > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->read_timeout > 0) 0 0 0
1923 0 mysql_options(conn, MYSQL_OPT_READ_TIMEOUT, &self->read_timeout);
1924 0 ▶ if (self->write_timeout > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->write_timeout > 0) 0 0 0
1925 0 mysql_options(conn, MYSQL_OPT_WRITE_TIMEOUT, &self->write_timeout);
1926 0 ▶ if (self->compress)
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) 0 0 0
1927 0 mysql_options(conn, MYSQL_OPT_COMPRESS, NULL);
1928 0 ▶ if (self->charset)
Branch: 1 branch
BranchTrueFalseTotal
if (self->charset) 0 0 0
1929 0 mysql_options(conn, MYSQL_SET_CHARSET_NAME, self->charset);
1930 0 ▶ if (self->init_command)
Branch: 1 branch
BranchTrueFalseTotal
if (self->init_command) 0 0 0
1931 0 mysql_options(conn, MYSQL_INIT_COMMAND, self->init_command);
1932 0 ▶ if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher)
Branch: 5 branches
BranchTrueFalseTotal
if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher) 0 0 0
if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher) 0 0 0
if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher) 0 0 0
if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher) 0 0 0
if (self->ssl_ca || self->ssl_capath || self->ssl_cert || self->ssl_key || self->ssl_cipher) 0 0 0
1933 0 mysql_ssl_set(conn, self->ssl_key, self->ssl_cert, self->ssl_ca, self->ssl_capath, self->ssl_cipher);
1934 0 ▶ if (self->ssl_verify_server_cert) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ssl_verify_server_cert) { 0 0 0
1935 0 my_bool val = 1;
1936 0 mysql_options(conn, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &val);
1937 }
1938 0 ▶ if (self->multi_statements)
Branch: 1 branch
BranchTrueFalseTotal
if (self->multi_statements) 0 0 0
1939 0 flags |= CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS;
1940 0 ▶ if (self->found_rows)
Branch: 1 branch
BranchTrueFalseTotal
if (self->found_rows) 0 0 0
1941 0 flags |= CLIENT_FOUND_ROWS;
1942  
1943 0 self->client_flags = flags;
1944 0 }
1945  
1946 0 static int is_utf8_charset(unsigned int charsetnr) {
1947 /* utf8mb3 collations: 33, 83, 192-211 */
1948 /* utf8mb4 collations: 45, 46, 224-247, 255, 256-309 */
1949 /* MariaDB 10.10+ uca1400: utf8mb3 2048-2303, utf8mb4 2304-2559 */
1950 /* (2560+ are ucs2/utf16/utf32 uca1400 — NOT UTF-8) */
1951 0 ▶ return charsetnr == 33 || charsetnr == 83
Branch: 1 branch
BranchTrueFalseTotal
return charsetnr == 33 || charsetnr == 83 0 0 0
1952 0 ▶ || (charsetnr >= 192 && charsetnr <= 211)
Branch: 2 branches
BranchTrueFalseTotal
|| (charsetnr >= 192 && charsetnr <= 211) 0 0 0
|| (charsetnr >= 192 && charsetnr <= 211) 0 0 0
1953 0 ▶ || charsetnr == 45 || charsetnr == 46
Branch: 2 branches
BranchTrueFalseTotal
|| charsetnr == 45 || charsetnr == 46 0 0 0
|| charsetnr == 45 || charsetnr == 46 0 0 0
1954 0 ▶ || (charsetnr >= 224 && charsetnr <= 247)
Branch: 2 branches
BranchTrueFalseTotal
|| (charsetnr >= 224 && charsetnr <= 247) 0 0 0
|| (charsetnr >= 224 && charsetnr <= 247) 0 0 0
1955 0 ▶ || charsetnr == 255
Branch: 1 branch
BranchTrueFalseTotal
|| charsetnr == 255 0 0 0
1956 0 ▶ || (charsetnr >= 256 && charsetnr <= 309)
Branch: 2 branches
BranchTrueFalseTotal
|| (charsetnr >= 256 && charsetnr <= 309) 0 0 0
|| (charsetnr >= 256 && charsetnr <= 309) 0 0 0
1957 0 ▶ || (charsetnr >= 2048 && charsetnr <= 2559);
Branch: 3 branches
BranchTrueFalseTotal
|| (charsetnr >= 2048 && charsetnr <= 2559); 0 0 0
|| (charsetnr >= 2048 && charsetnr <= 2559); 0 0 0
|| (charsetnr >= 2048 && charsetnr <= 2559); 0 0 0
1958 }
1959  
1960 0 static int conn_charset_is_utf8(MYSQL *conn) {
1961 const char *cs;
1962 0 ▶ if (!conn) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!conn) return 0; 0 0 0
1963 0 cs = mysql_character_set_name(conn);
1964 0 ▶ return (cs && strncmp(cs, "utf8", 4) == 0);
Branch: 2 branches
BranchTrueFalseTotal
return (cs && strncmp(cs, "utf8", 4) == 0); 0 0 0
return (cs && strncmp(cs, "utf8", 4) == 0); 0 0 0
1965 }
1966  
1967 0 static AV* build_field_names(MYSQL *conn, MYSQL_FIELD *fields, unsigned int ncols) {
1968 0 AV *fnames = newAV();
1969 unsigned int i;
1970 0 int utf8_names = conn_charset_is_utf8(conn);
1971 0 ▶ if (ncols > 0) av_extend(fnames, ncols - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (ncols > 0) av_extend(fnames, ncols - 1); 0 0 0
1972 0 ▶ for (i = 0; i < ncols; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ncols; i++) { 0 0 0
1973 0 SV *name = newSVpvn(fields[i].name, fields[i].name_length);
1974 0 ▶ if (utf8_names)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8_names) 0 0 0
1975 0 SvUTF8_on(name);
1976 0 av_push(fnames, name);
1977 }
1978 0 return fnames;
1979 }
1980  
1981 0 static void setup_bind_params(ev_mariadb_stmt_t *ctx, AV *params) {
1982 0 int nparams = (int)(av_len(params) + 1);
1983 MYSQL_BIND *bp;
1984 int i;
1985  
1986 {
1987 0 unsigned long expected = mysql_stmt_param_count(ctx->stmt);
1988 0 ▶ if ((unsigned long)nparams != expected)
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned long)nparams != expected) 0 0 0
1989 0 croak("parameter count mismatch: got %d, expected %lu", nparams, expected);
1990 }
1991  
1992 0 ▶ if (nparams == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (nparams == 0) return; 0 0 0
1993  
1994 0 free_stmt_bind_params(ctx);
1995  
1996 0 Newxz(bp, nparams, MYSQL_BIND);
1997 0 ctx->bind_params = bp;
1998 0 ctx->bind_param_count = nparams;
1999  
2000 0 ▶ for (i = 0; i < nparams; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nparams; i++) { 0 0 0
2001 0 SV **svp = av_fetch(params, i, 0);
2002 0 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 0 0 0
if (svp && SvOK(*svp)) { 0 0 0
2003 0 ▶ if (SvIOK(*svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(*svp)) { 0 0 0
2004 long long *val;
2005 0 Newx(val, 1, long long);
2006 0 ▶ if (SvIsUV(*svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(*svp)) { 0 0 0
2007 0 *val = (long long)(unsigned long long)SvUV(*svp);
2008 0 bp[i].is_unsigned = 1;
2009 } else {
2010 0 *val = (long long)SvIV(*svp);
2011 0 bp[i].is_unsigned = 0;
2012 }
2013 0 bp[i].buffer_type = MYSQL_TYPE_LONGLONG;
2014 0 bp[i].buffer = (void *)val;
2015 0 ▶ } else if (SvNOK(*svp)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvNOK(*svp)) { 0 0 0
2016 double *val;
2017 0 Newx(val, 1, double);
2018 0 *val = (double)SvNV(*svp);
2019 0 bp[i].buffer_type = MYSQL_TYPE_DOUBLE;
2020 0 bp[i].buffer = (void *)val;
2021 } else {
2022 STRLEN len;
2023 0 const char *s = SvPV(*svp, len);
2024 char *copy;
2025 0 Newx(copy, len + 1, char);
2026 0 Copy(s, copy, len, char);
2027 0 copy[len] = '\0';
2028 0 bp[i].buffer_type = MYSQL_TYPE_STRING;
2029 0 bp[i].buffer = (void *)copy;
2030 0 bp[i].buffer_length = (unsigned long)len;
2031 0 bp[i].length = &bp[i].buffer_length;
2032 }
2033 } else {
2034 0 bp[i].buffer_type = MYSQL_TYPE_NULL;
2035 }
2036 }
2037  
2038 0 ▶ if (mysql_stmt_bind_param(ctx->stmt, bp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (mysql_stmt_bind_param(ctx->stmt, bp)) { 0 0 0
2039 char errbuf[512];
2040 0 COPY_ERROR(errbuf, mysql_stmt_error(ctx->stmt));
2041 0 free_stmt_bind_params(ctx);
2042 0 croak("%s", errbuf);
2043 }
2044 }
2045  
2046 0 static void start_connect(ev_mariadb_t *self) {
2047 int status;
2048  
2049 0 self->conn = mysql_init(NULL);
2050 0 ▶ if (NULL == self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->conn) { 0 0 0
2051 0 croak("mysql_init failed");
2052 }
2053  
2054 0 self->connect_pid = getpid();
2055 0 mysql_options(self->conn, MYSQL_OPT_NONBLOCK, 0);
2056 0 apply_options(self);
2057  
2058 0 self->state = STATE_CONNECTING;
2059 0 status = mysql_real_connect_start(&self->op_conn_ret, self->conn,
2060 0 self->host, self->user, self->password, self->database,
2061 0 self->port, self->unix_socket, self->client_flags);
2062  
2063 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2064 0 on_connect_done(self);
2065 } else {
2066 0 self->fd = mysql_get_socket(self->conn);
2067 0 ▶ if (self->fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd < 0) { 0 0 0
2068 0 mysql_close(self->conn);
2069 0 self->conn = NULL;
2070 0 self->state = STATE_IDLE;
2071 0 croak("mysql_get_socket returned invalid fd");
2072 }
2073  
2074 0 init_io_watchers(self);
2075  
2076 0 update_watchers(self, status);
2077 }
2078 0 }
2079  
2080 MODULE = EV::MariaDB PACKAGE = EV::MariaDB
2081  
2082 BOOT:
2083 {
2084 17 ▶ I_EV_API("EV::MariaDB");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::MariaDB"); 0 17 17
I_EV_API("EV::MariaDB"); 17 0 17
I_EV_API("EV::MariaDB"); 0 17 17
2085 17 call_atexit(drain_freelists, NULL);
2086 }
2087  
2088 EV::MariaDB
2089 _new(char *class, EV::Loop loop)
2090 CODE:
2091 {
2092 PERL_UNUSED_VAR(class);
2093 2 Newxz(RETVAL, 1, ev_mariadb_t);
2094 2 RETVAL->magic = EV_MARIADB_MAGIC;
2095 2 RETVAL->loop = loop;
2096 2 RETVAL->loop_sv = SvREFCNT_inc(ST(1)); /* keep the EV::Loop object alive */
2097 2 RETVAL->fd = -1;
2098 2 RETVAL->state = STATE_IDLE;
2099 2 ngx_queue_init(&RETVAL->cb_queue);
2100 2 ngx_queue_init(&RETVAL->send_queue);
2101  
2102 2 ev_init(&RETVAL->timer, timer_cb);
2103 2 RETVAL->timer.data = (void *)RETVAL;
2104 }
2105 OUTPUT:
2106 RETVAL
2107  
2108 void
2109 DESTROY(EV::MariaDB self)
2110 CODE:
2111 {
2112 2 ▶ if (self->magic != EV_MARIADB_MAGIC)
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_MARIADB_MAGIC) 0 2 2
2113 0 return;
2114  
2115 2 self->magic = EV_MARIADB_FREED;
2116  
2117 2 stop_reading(self);
2118 2 stop_writing(self);
2119 2 stop_timer(self);
2120  
2121 2 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 0 2 2
2122 /* global destruction — free C resources only; skip SvREFCNT_dec
2123 to avoid cascading destructors in torn-down interpreter */
2124 0 ▶ int is_fork = IS_FORKED(self);
Branch: 2 branches
BranchTrueFalseTotal
int is_fork = IS_FORKED(self); 0 0 0
int is_fork = IS_FORKED(self); 0 0 0
2125 0 ▶ while (!ngx_queue_empty(&self->send_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->send_queue)) { 0 0 0
2126 0 ngx_queue_t *q = ngx_queue_head(&self->send_queue);
2127 0 ev_mariadb_send_t *s = ngx_queue_data(q, ev_mariadb_send_t, queue);
2128 0 ngx_queue_remove(q);
2129 0 Safefree(s->sql);
2130 0 Safefree(s);
2131 }
2132 0 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 0 0
2133 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
2134 0 ev_mariadb_cb_t *cbt = ngx_queue_data(q, ev_mariadb_cb_t, queue);
2135 0 ngx_queue_remove(q);
2136 0 Safefree(cbt);
2137 }
2138 0 ▶ if (self->op_data_ptr) Safefree(self->op_data_ptr);
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_data_ptr) Safefree(self->op_data_ptr); 0 0 0
2139 0 Safefree(self->pending_user);
2140 0 Safefree(self->pending_password);
2141 0 Safefree(self->pending_database);
2142 0 Safefree(self->pending_charset);
2143 0 self->stream_cb = NULL;
2144 /* skip loop_sv dec: global destruction, like the other SVs here */
2145 {
2146 0 ev_mariadb_stmt_t *ctx = self->stmt_list;
2147 0 ▶ while (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx) { 0 0 0
2148 0 ev_mariadb_stmt_t *next = ctx->next;
2149 0 free_stmt_bind_params(ctx);
2150 0 ▶ if (!is_fork && ctx->stmt) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_fork && ctx->stmt) { 0 0 0
if (!is_fork && ctx->stmt) { 0 0 0
2151 0 ▶ if (ctx->stmt == self->op_stmt) self->op_stmt = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->stmt == self->op_stmt) self->op_stmt = NULL; 0 0 0
2152 0 mysql_stmt_close(ctx->stmt);
2153 }
2154 0 Safefree(ctx);
2155 0 ctx = next;
2156 }
2157 }
2158 0 ▶ if (!is_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_fork) { 0 0 0
2159 0 ▶ if (self->op_result) mysql_free_result(self->op_result);
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result) mysql_free_result(self->op_result); 0 0 0
2160 0 ▶ if (self->conn) mysql_close(self->conn);
Branch: 1 branch
BranchTrueFalseTotal
if (self->conn) mysql_close(self->conn); 0 0 0
2161 }
2162 0 free_connect_strings(self);
2163 0 free_option_strings(self);
2164 0 Safefree(self);
2165 0 return;
2166 }
2167  
2168 2 cancel_pending(self, "object destroyed");
2169  
2170 /* safety net: callbacks during cancel_pending could re-queue entries */
2171 2 drain_queues_silent(self);
2172  
2173 2 ▶ if (self->op_data_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_data_ptr) { 0 2 2
2174 0 Safefree(self->op_data_ptr);
2175 0 self->op_data_ptr = NULL;
2176 }
2177  
2178 2 FREE_STR(self->pending_user);
2179 2 FREE_STR(self->pending_password);
2180 2 FREE_STR(self->pending_database);
2181 2 FREE_STR(self->pending_charset);
2182  
2183 /* Free all tracked stmt wrappers and connection resources */
2184 {
2185 2 ▶ int is_fork = IS_FORKED(self);
Branch: 2 branches
BranchTrueFalseTotal
int is_fork = IS_FORKED(self); 0 2 2
int is_fork = IS_FORKED(self); 0 0 0
2186 2 ev_mariadb_stmt_t *ctx = self->stmt_list;
2187 2 ▶ while (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
while (ctx) { 0 2 2
2188 0 ev_mariadb_stmt_t *next = ctx->next;
2189 0 free_stmt_bind_params(ctx);
2190 0 ▶ if (!is_fork && ctx->stmt) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_fork && ctx->stmt) { 0 0 0
if (!is_fork && ctx->stmt) { 0 0 0
2191 /* op_stmt may equal a tracked stmt (e.g. STATE_STMT_RESET,
2192 STATE_STMT_STORE, STATE_STMT_SEND_LONG_DATA where op_stmt_ctx
2193 is NULL); avoid the double-close below. */
2194 0 ▶ if (ctx->stmt == self->op_stmt) self->op_stmt = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->stmt == self->op_stmt) self->op_stmt = NULL; 0 0 0
2195 0 mysql_stmt_close(ctx->stmt);
2196 }
2197 0 Safefree(ctx);
2198 0 ctx = next;
2199 }
2200 2 self->stmt_list = NULL;
2201 /* Close op_stmt only if it was never linked into stmt_list (in-flight prepare). */
2202 2 ▶ if (!is_fork && self->op_stmt && !self->op_stmt_ctx) {
Branch: 3 branches
BranchTrueFalseTotal
if (!is_fork && self->op_stmt && !self->op_stmt_ctx) { 2 0 2
if (!is_fork && self->op_stmt && !self->op_stmt_ctx) { 0 2 2
if (!is_fork && self->op_stmt && !self->op_stmt_ctx) { 0 0 0
2203 0 mysql_stmt_close(self->op_stmt);
2204 }
2205 2 self->op_stmt = NULL;
2206 2 self->op_stmt_ctx = NULL;
2207  
2208 2 ▶ if (is_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_fork) { 0 2 2
2209 0 self->op_result = NULL;
2210 0 self->conn = NULL;
2211 } else {
2212 2 ▶ if (self->op_result) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->op_result) { 0 2 2
2213 0 mysql_free_result(self->op_result);
2214 0 self->op_result = NULL;
2215 }
2216 {
2217 2 MYSQL *conn = self->conn;
2218 2 self->conn = NULL;
2219 2 ▶ if (conn) mysql_close(conn);
Branch: 1 branch
BranchTrueFalseTotal
if (conn) mysql_close(conn); 0 2 2
2220 }
2221 }
2222 }
2223  
2224 /* Drop handler SVs before zeroing loop/fd so any re-entrant destructors
2225 see NULL handler fields (and a still-valid loop while unwinding). */
2226 {
2227 SV *cb;
2228 2 ▶ if ((cb = self->on_connect)) { self->on_connect = NULL; SvREFCNT_dec(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if ((cb = self->on_connect)) { self->on_connect = NULL; SvREFCNT_dec(cb); } 0 2 2
2229 2 ▶ if ((cb = self->on_error)) { self->on_error = NULL; SvREFCNT_dec(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if ((cb = self->on_error)) { self->on_error = NULL; SvREFCNT_dec(cb); } 2 0 2
2230 }
2231  
2232 2 ▶ if (self->loop_sv) { SvREFCNT_dec(self->loop_sv); self->loop_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->loop_sv) { SvREFCNT_dec(self->loop_sv); self->loop_sv = NULL; } 2 0 2
2233 2 self->loop = NULL;
2234 2 self->fd = -1;
2235  
2236 2 free_connect_strings(self);
2237 2 free_option_strings(self);
2238  
2239 /* deferred free: check_destroyed will Safefree when depth hits 0 */
2240 2 ▶ if (self->callback_depth == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth == 0) 2 0 2
2241 2 Safefree(self);
2242 }
2243  
2244 void
2245 connect(EV::MariaDB self, const char *host, const char *user, const char *password, const char *database, unsigned int port = 3306, SV *unix_socket_sv = NULL)
2246 CODE:
2247 {
2248 0 ▶ if (NULL != self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->conn) { 0 0 0
2249 0 ▶ croak(self->state == STATE_CONNECTING
Branch: 1 branch
BranchTrueFalseTotal
croak(self->state == STATE_CONNECTING 0 0 0
2250 ? "connection already in progress"
2251 : "already connected");
2252 }
2253  
2254 0 free_connect_strings(self);
2255  
2256 0 self->host = safe_strdup(host);
2257 0 self->user = safe_strdup(user);
2258 0 self->password = safe_strdup(password);
2259 0 self->database = safe_strdup(database);
2260 0 self->port = port;
2261 0 self->unix_socket = NULL;
2262  
2263 0 ▶ if (unix_socket_sv && SvOK(unix_socket_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unix_socket_sv && SvOK(unix_socket_sv)) { 0 0 0
if (unix_socket_sv && SvOK(unix_socket_sv)) { 0 0 0
2264 0 self->unix_socket = safe_strdup(SvPV_nolen(unix_socket_sv));
2265 }
2266  
2267 0 start_connect(self);
2268 }
2269  
2270 void
2271 reset(EV::MariaDB self)
2272 CODE:
2273 {
2274 0 ▶ if (NULL == self->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->host) { 0 0 0
2275 0 croak("no previous connection to reset");
2276 }
2277  
2278 0 cleanup_connection(self);
2279 0 cancel_pending(self, "connection reset");
2280 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
2281 0 ▶ if (self->state != STATE_IDLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) return; 0 0 0
2282  
2283 0 start_connect(self);
2284 }
2285  
2286 void
2287 finish(EV::MariaDB self)
2288 CODE:
2289 {
2290 0 cleanup_connection(self);
2291 0 cancel_pending(self, "connection finished");
2292 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
2293 }
2294  
2295 SV*
2296 on_connect(EV::MariaDB self, SV *handler = NULL)
2297 CODE:
2298 {
2299 0 RETVAL = handler_accessor(&self->on_connect, handler, items > 1);
2300 }
2301 OUTPUT:
2302 RETVAL
2303  
2304 SV*
2305 on_error(EV::MariaDB self, SV *handler = NULL)
2306 CODE:
2307 {
2308 2 RETVAL = handler_accessor(&self->on_error, handler, items > 1);
2309 }
2310 OUTPUT:
2311 RETVAL
2312  
2313 void
2314 query(EV::MariaDB self, SV *sql_sv, SV *cb)
2315 CODE:
2316 {
2317 STRLEN sql_len;
2318 const char *sql;
2319  
2320 0 ▶ if (NULL == self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->conn) { 0 0 0
2321 0 croak("not connected");
2322 }
2323 0 ▶ if (IS_FORKED(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (IS_FORKED(self)) { 0 0 0
if (IS_FORKED(self)) { 0 0 0
2324 0 croak("connection not valid after fork");
2325 }
2326 0 ▶ if (self->state >= STATE_STMT_PREPARE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state >= STATE_STMT_PREPARE) { 0 0 0
2327 0 croak("cannot queue query: exclusive operation in progress");
2328 }
2329 0 ▶ if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { 0 0 0
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) { 0 0 0
2330 0 croak("callback must be a CODE reference");
2331 }
2332  
2333 0 sql = SvPV(sql_sv, sql_len);
2334 0 push_send(self, sql, sql_len, cb);
2335  
2336 0 ▶ if (self->state == STATE_IDLE && self->callback_depth == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self->state == STATE_IDLE && self->callback_depth == 0) 0 0 0
if (self->state == STATE_IDLE && self->callback_depth == 0) 0 0 0
2337 0 pipeline_advance(self);
2338 }
2339  
2340 void
2341 prepare(EV::MariaDB self, SV *sql_sv, SV *cb)
2342 PREINIT:
2343 STRLEN sql_len;
2344 const char *sql;
2345 CODE:
2346 {
2347 int status;
2348 MYSQL_STMT *stmt;
2349  
2350 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2351  
2352 0 stmt = mysql_stmt_init(self->conn);
2353 0 ▶ if (NULL == stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == stmt) { 0 0 0
2354 0 croak("mysql_stmt_init failed");
2355 }
2356 {
2357 0 my_bool update_max = 1;
2358 0 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &update_max);
2359 }
2360  
2361 0 sql = SvPV(sql_sv, sql_len);
2362 0 push_cb(self, cb);
2363 0 self->op_stmt = stmt;
2364  
2365 0 self->state = STATE_STMT_PREPARE;
2366 0 status = mysql_stmt_prepare_start(&self->op_ret, stmt, sql, (unsigned long)sql_len);
2367 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2368 0 on_stmt_prepare_done(self);
2369 } else {
2370 0 update_watchers(self, status);
2371 }
2372 }
2373  
2374 void
2375 execute(EV::MariaDB self, IV stmt_iv, SV *params_ref, SV *cb)
2376 PREINIT:
2377 ev_mariadb_stmt_t *ctx;
2378 int status;
2379 CODE:
2380 {
2381 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2382  
2383 0 ctx = find_stmt(self, stmt_iv);
2384 0 ▶ CHECK_STMT(ctx);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_STMT(ctx); 0 0 0
CHECK_STMT(ctx); 0 0 0
2385  
2386 0 ▶ if (SvOK(params_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(params_ref)) { 0 0 0
2387 0 ▶ if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV) 0 0 0
if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV) 0 0 0
2388 0 croak("params must be an ARRAY reference or undef");
2389 0 setup_bind_params(ctx, (AV *)SvRV(params_ref));
2390 }
2391  
2392 0 push_cb(self, cb);
2393 0 self->op_stmt = ctx->stmt;
2394 0 self->op_stmt_ctx = ctx;
2395  
2396 0 self->state = STATE_STMT_EXECUTE;
2397 0 status = mysql_stmt_execute_start(&self->op_ret, ctx->stmt);
2398 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2399 0 transition_to_stmt_store(self);
2400 } else {
2401 0 update_watchers(self, status);
2402 }
2403 }
2404  
2405 void
2406 bind_params(EV::MariaDB self, IV stmt_iv, SV *params_ref)
2407 PREINIT:
2408 ev_mariadb_stmt_t *ctx;
2409 CODE:
2410 {
2411 0 ▶ if (NULL == self->conn || self->state == STATE_CONNECTING)
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->conn || self->state == STATE_CONNECTING) 0 0 0
if (NULL == self->conn || self->state == STATE_CONNECTING) 0 0 0
2412 0 croak("not connected");
2413 0 ▶ if (IS_FORKED(self))
Branch: 2 branches
BranchTrueFalseTotal
if (IS_FORKED(self)) 0 0 0
if (IS_FORKED(self)) 0 0 0
2414 0 croak("connection not valid after fork");
2415 0 ▶ if (self->state != STATE_IDLE)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state != STATE_IDLE) 0 0 0
2416 0 croak("another operation is in progress");
2417 0 ▶ if (self->send_count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->send_count > 0) 0 0 0
2418 0 croak("cannot bind while pipeline results are pending");
2419 0 ▶ if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV) 0 0 0
if (!SvROK(params_ref) || SvTYPE(SvRV(params_ref)) != SVt_PVAV) 0 0 0
2420 0 croak("params must be an ARRAY reference");
2421  
2422 0 ctx = find_stmt(self, stmt_iv);
2423 0 ▶ CHECK_STMT(ctx);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_STMT(ctx); 0 0 0
CHECK_STMT(ctx); 0 0 0
2424 0 setup_bind_params(ctx, (AV *)SvRV(params_ref));
2425 }
2426  
2427 void
2428 close_stmt(EV::MariaDB self, IV stmt_iv, SV *cb)
2429 PREINIT:
2430 ev_mariadb_stmt_t *ctx;
2431 int status;
2432 CODE:
2433 {
2434 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2435  
2436 0 ctx = find_stmt(self, stmt_iv);
2437 0 ▶ if (!ctx) croak("invalid statement handle");
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) croak("invalid statement handle"); 0 0 0
2438 0 ▶ if (ctx->closed) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->closed) { 0 0 0
2439 /* already closed by cleanup_connection — just free the wrapper */
2440 0 unlink_stmt(self, ctx);
2441 0 Safefree(ctx);
2442 0 push_cb(self, cb);
2443 0 ▶ if (deliver_value(self, newSViv(1))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (deliver_value(self, newSViv(1))) return; 0 0 0
2444 0 maybe_pipeline(self);
2445 0 return;
2446 }
2447 0 free_stmt_bind_params(ctx);
2448 0 push_cb(self, cb);
2449 0 self->op_stmt = ctx->stmt;
2450 0 self->op_stmt_ctx = ctx;
2451  
2452 0 self->state = STATE_STMT_CLOSE;
2453 0 status = mysql_stmt_close_start(&self->op_bool_ret, ctx->stmt);
2454 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2455 0 on_stmt_close_done(self);
2456 } else {
2457 0 update_watchers(self, status);
2458 }
2459 }
2460  
2461 void
2462 stmt_reset(EV::MariaDB self, IV stmt_iv, SV *cb)
2463 PREINIT:
2464 ev_mariadb_stmt_t *ctx;
2465 int status;
2466 CODE:
2467 {
2468 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2469  
2470 0 ctx = find_stmt(self, stmt_iv);
2471 0 ▶ CHECK_STMT(ctx);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_STMT(ctx); 0 0 0
CHECK_STMT(ctx); 0 0 0
2472 0 push_cb(self, cb);
2473 0 self->op_stmt = ctx->stmt;
2474  
2475 0 self->state = STATE_STMT_RESET;
2476 0 status = mysql_stmt_reset_start(&self->op_bool_ret, ctx->stmt);
2477 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2478 0 on_stmt_reset_done(self);
2479 } else {
2480 0 update_watchers(self, status);
2481 }
2482 }
2483  
2484 void
2485 ping(EV::MariaDB self, SV *cb)
2486 PREINIT:
2487 int status;
2488 CODE:
2489 {
2490 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2491  
2492 0 push_cb(self, cb);
2493  
2494 0 self->state = STATE_PING;
2495 0 status = mysql_ping_start(&self->op_ret, self->conn);
2496 0 dispatch_utility(self, status, self->op_ret != 0);
2497 }
2498  
2499 void
2500 change_user(EV::MariaDB self, const char *user, const char *password, SV *db_sv, SV *cb)
2501 PREINIT:
2502 int status;
2503 const char *db;
2504 CODE:
2505 {
2506 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2507  
2508 /* undef $db keeps the current database (POD contract); NULL would drop it. */
2509 0 ▶ db = (SvOK(db_sv)) ? SvPV_nolen(db_sv) : self->database;
Branch: 1 branch
BranchTrueFalseTotal
db = (SvOK(db_sv)) ? SvPV_nolen(db_sv) : self->database; 0 0 0
2510  
2511 /* Stash the new credentials; on_utility_done commits them on success or
2512 discards on failure so reset() never sees credentials we couldn't use.
2513 Pass the pending pointers to libmariadb so the strings live across the
2514 async start/cont sequence (Perl args expire when the XS body returns). */
2515 0 replace_str(&self->pending_user, user);
2516 0 replace_str(&self->pending_password, password);
2517 0 ▶ if (db) replace_str(&self->pending_database, db);
Branch: 1 branch
BranchTrueFalseTotal
if (db) replace_str(&self->pending_database, db); 0 0 0
2518  
2519 0 push_cb(self, cb);
2520  
2521 0 self->state = STATE_CHANGE_USER;
2522 0 status = mysql_change_user_start(&self->op_bool_ret, self->conn,
2523 0 self->pending_user, self->pending_password, self->pending_database);
2524 0 dispatch_utility(self, status, self->op_bool_ret != 0);
2525 }
2526  
2527 void
2528 select_db(EV::MariaDB self, const char *db, SV *cb)
2529 PREINIT:
2530 int status;
2531 CODE:
2532 {
2533 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2534  
2535 /* Stash the new database; on_utility_done commits on success. */
2536 0 replace_str(&self->pending_database, db);
2537  
2538 0 push_cb(self, cb);
2539  
2540 0 self->state = STATE_SELECT_DB;
2541 0 status = mysql_select_db_start(&self->op_ret, self->conn, self->pending_database);
2542 0 dispatch_utility(self, status, self->op_ret != 0);
2543 }
2544  
2545 void
2546 reset_connection(EV::MariaDB self, SV *cb)
2547 PREINIT:
2548 int status;
2549 CODE:
2550 {
2551 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2552  
2553 0 push_cb(self, cb);
2554  
2555 0 self->state = STATE_RESET_CONNECTION;
2556 0 status = mysql_reset_connection_start(&self->op_ret, self->conn);
2557 0 dispatch_utility(self, status, self->op_ret != 0);
2558 }
2559  
2560 void
2561 set_charset(EV::MariaDB self, const char *charset, SV *cb)
2562 PREINIT:
2563 int status;
2564 CODE:
2565 {
2566 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2567  
2568 /* Stash the new charset; on_utility_done commits on success. */
2569 0 replace_str(&self->pending_charset, charset);
2570  
2571 0 push_cb(self, cb);
2572  
2573 0 self->state = STATE_SET_CHARSET;
2574 0 status = mysql_set_character_set_start(&self->op_ret, self->conn, self->pending_charset);
2575 0 dispatch_utility(self, status, self->op_ret != 0);
2576 }
2577  
2578 void
2579 commit(EV::MariaDB self, SV *cb)
2580 PREINIT:
2581 int status;
2582 CODE:
2583 {
2584 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2585  
2586 0 push_cb(self, cb);
2587  
2588 0 self->state = STATE_COMMIT;
2589 0 status = mysql_commit_start(&self->op_bool_ret, self->conn);
2590 0 dispatch_utility(self, status, self->op_bool_ret != 0);
2591 }
2592  
2593 void
2594 rollback(EV::MariaDB self, SV *cb)
2595 PREINIT:
2596 int status;
2597 CODE:
2598 {
2599 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2600  
2601 0 push_cb(self, cb);
2602  
2603 0 self->state = STATE_ROLLBACK;
2604 0 status = mysql_rollback_start(&self->op_bool_ret, self->conn);
2605 0 dispatch_utility(self, status, self->op_bool_ret != 0);
2606 }
2607  
2608 void
2609 autocommit(EV::MariaDB self, int mode, SV *cb)
2610 PREINIT:
2611 int status;
2612 CODE:
2613 {
2614 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2615  
2616 0 push_cb(self, cb);
2617  
2618 0 self->state = STATE_AUTOCOMMIT;
2619 0 status = mysql_autocommit_start(&self->op_bool_ret, self->conn, (my_bool)(mode ? 1 : 0));
2620 0 dispatch_utility(self, status, self->op_bool_ret != 0);
2621 }
2622  
2623 void
2624 query_stream(EV::MariaDB self, SV *sql_sv, SV *cb)
2625 PREINIT:
2626 STRLEN sql_len;
2627 const char *sql;
2628 int status;
2629 CODE:
2630 {
2631 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2632  
2633 0 sql = SvPV(sql_sv, sql_len);
2634 0 self->stream_cb = SvREFCNT_inc(cb);
2635 0 self->pending_count++;
2636  
2637 0 self->state = STATE_REAL_QUERY;
2638 0 status = mysql_real_query_start(&self->op_ret, self->conn, sql, (unsigned long)sql_len);
2639 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2640 0 on_real_query_done(self);
2641 } else {
2642 0 update_watchers(self, status);
2643 }
2644 }
2645  
2646 void
2647 close_async(EV::MariaDB self, SV *cb)
2648 PREINIT:
2649 int status;
2650 CODE:
2651 {
2652 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2653  
2654 0 push_cb(self, cb);
2655  
2656 0 self->state = STATE_CLOSE;
2657 0 status = mysql_close_start(self->conn);
2658 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2659 0 on_close_done(self);
2660 } else {
2661 0 update_watchers(self, status);
2662 }
2663 }
2664  
2665 void
2666 send_long_data(EV::MariaDB self, IV stmt_iv, unsigned int param_idx, SV *data_sv, SV *cb)
2667 PREINIT:
2668 ev_mariadb_stmt_t *ctx;
2669 STRLEN data_len;
2670 char *data;
2671 int status;
2672 CODE:
2673 {
2674 0 ▶ CHECK_READY(self, cb);
Branch: 8 branches
BranchTrueFalseTotal
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
CHECK_READY(self, cb); 0 0 0
2675  
2676 0 ctx = find_stmt(self, stmt_iv);
2677 0 ▶ CHECK_STMT(ctx);
Branch: 2 branches
BranchTrueFalseTotal
CHECK_STMT(ctx); 0 0 0
CHECK_STMT(ctx); 0 0 0
2678 {
2679 0 const char *src = SvPV(data_sv, data_len);
2680 0 ▶ Newx(data, data_len > 0 ? data_len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(data, data_len > 0 ? data_len : 1, char); 0 0 0
2681 0 Copy(src, data, data_len, char);
2682 }
2683 0 push_cb(self, cb);
2684 0 self->op_stmt = ctx->stmt;
2685 0 self->op_data_ptr = data;
2686  
2687 0 self->state = STATE_STMT_SEND_LONG_DATA;
2688 0 status = mysql_stmt_send_long_data_start(&self->op_bool_ret, ctx->stmt,
2689 param_idx, data, (unsigned long)data_len);
2690 0 ▶ if (status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 0) { 0 0 0
2691 0 on_send_long_data_done(self);
2692 } else {
2693 0 update_watchers(self, status);
2694 }
2695 }
2696  
2697 void
2698 _set_option(EV::MariaDB self, const char *key, SV *value)
2699 CODE:
2700 {
2701 0 ▶ if (strcmp(key, "connect_timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "connect_timeout") == 0) { 0 0 0
2702 0 self->connect_timeout = SvUV(value);
2703 0 ▶ } else if (strcmp(key, "read_timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "read_timeout") == 0) { 0 0 0
2704 0 self->read_timeout = SvUV(value);
2705 0 ▶ } else if (strcmp(key, "write_timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "write_timeout") == 0) { 0 0 0
2706 0 self->write_timeout = SvUV(value);
2707 0 ▶ } else if (strcmp(key, "compress") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "compress") == 0) { 0 0 0
2708 0 self->compress = SvTRUE(value) ? 1 : 0;
2709 0 ▶ } else if (strcmp(key, "multi_statements") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "multi_statements") == 0) { 0 0 0
2710 0 self->multi_statements = SvTRUE(value) ? 1 : 0;
2711 0 ▶ } else if (strcmp(key, "found_rows") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "found_rows") == 0) { 0 0 0
2712 0 self->found_rows = SvTRUE(value) ? 1 : 0;
2713 0 ▶ } else if (strcmp(key, "charset") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "charset") == 0) { 0 0 0
2714 0 ▶ SET_STR_OPTION(charset);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(charset); 0 0 0
SET_STR_OPTION(charset); 0 0 0
2715 0 ▶ } else if (strcmp(key, "init_command") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "init_command") == 0) { 0 0 0
2716 0 ▶ SET_STR_OPTION(init_command);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(init_command); 0 0 0
SET_STR_OPTION(init_command); 0 0 0
2717 0 ▶ } else if (strcmp(key, "ssl_key") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_key") == 0) { 0 0 0
2718 0 ▶ SET_STR_OPTION(ssl_key);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(ssl_key); 0 0 0
SET_STR_OPTION(ssl_key); 0 0 0
2719 0 ▶ } else if (strcmp(key, "ssl_cert") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_cert") == 0) { 0 0 0
2720 0 ▶ SET_STR_OPTION(ssl_cert);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(ssl_cert); 0 0 0
SET_STR_OPTION(ssl_cert); 0 0 0
2721 0 ▶ } else if (strcmp(key, "ssl_ca") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_ca") == 0) { 0 0 0
2722 0 ▶ SET_STR_OPTION(ssl_ca);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(ssl_ca); 0 0 0
SET_STR_OPTION(ssl_ca); 0 0 0
2723 0 ▶ } else if (strcmp(key, "ssl_capath") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_capath") == 0) { 0 0 0
2724 0 ▶ SET_STR_OPTION(ssl_capath);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(ssl_capath); 0 0 0
SET_STR_OPTION(ssl_capath); 0 0 0
2725 0 ▶ } else if (strcmp(key, "ssl_cipher") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_cipher") == 0) { 0 0 0
2726 0 ▶ SET_STR_OPTION(ssl_cipher);
Branch: 2 branches
BranchTrueFalseTotal
SET_STR_OPTION(ssl_cipher); 0 0 0
SET_STR_OPTION(ssl_cipher); 0 0 0
2727 0 ▶ } else if (strcmp(key, "ssl_verify_server_cert") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "ssl_verify_server_cert") == 0) { 0 0 0
2728 0 self->ssl_verify_server_cert = SvTRUE(value) ? 1 : 0;
2729 0 ▶ } else if (strcmp(key, "utf8") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "utf8") == 0) { 0 0 0
2730 0 self->utf8 = SvTRUE(value) ? 1 : 0;
2731 } else {
2732 0 croak("unknown option: %s", key);
2733 }
2734 }
2735  
2736 int
2737 is_connected(EV::MariaDB self)
2738 CODE:
2739 {
2740 1 ▶ RETVAL = (NULL != self->conn && self->state != STATE_CONNECTING) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn && self->state != STATE_CONNECTING) ? 1 : 0; 0 1 1
RETVAL = (NULL != self->conn && self->state != STATE_CONNECTING) ? 1 : 0; 0 0 0
RETVAL = (NULL != self->conn && self->state != STATE_CONNECTING) ? 1 : 0; 0 1 1
2741 }
2742 OUTPUT:
2743 RETVAL
2744  
2745 SV*
2746 error_message(EV::MariaDB self)
2747 CODE:
2748 {
2749 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_error(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_error(self->conn)) : &PL_sv_undef; 0 0 0
2750 }
2751 OUTPUT:
2752 RETVAL
2753  
2754 unsigned int
2755 error_number(EV::MariaDB self)
2756 CODE:
2757 {
2758 0 ▶ RETVAL = (NULL != self->conn) ? mysql_errno(self->conn) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn) ? mysql_errno(self->conn) : 0; 0 0 0
RETVAL = (NULL != self->conn) ? mysql_errno(self->conn) : 0; 0 0 0
2759 }
2760 OUTPUT:
2761 RETVAL
2762  
2763 SV*
2764 sqlstate(EV::MariaDB self)
2765 CODE:
2766 {
2767 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_sqlstate(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_sqlstate(self->conn)) : &PL_sv_undef; 0 0 0
2768 }
2769 OUTPUT:
2770 RETVAL
2771  
2772 SV*
2773 insert_id(EV::MariaDB self)
2774 CODE:
2775 {
2776 0 ▶ if (NULL != self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->conn) { 0 0 0
2777 0 my_ulonglong id = mysql_insert_id(self->conn);
2778 0 RETVAL = newSVuv((UV)id);
2779 }
2780 else {
2781 0 RETVAL = &PL_sv_undef;
2782 }
2783 }
2784 OUTPUT:
2785 RETVAL
2786  
2787 unsigned int
2788 warning_count(EV::MariaDB self)
2789 CODE:
2790 {
2791 0 ▶ RETVAL = (NULL != self->conn) ? mysql_warning_count(self->conn) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn) ? mysql_warning_count(self->conn) : 0; 0 0 0
RETVAL = (NULL != self->conn) ? mysql_warning_count(self->conn) : 0; 0 0 0
2792 }
2793 OUTPUT:
2794 RETVAL
2795  
2796 SV*
2797 info(EV::MariaDB self)
2798 CODE:
2799 {
2800 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_info(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_info(self->conn)) : &PL_sv_undef; 0 0 0
2801 }
2802 OUTPUT:
2803 RETVAL
2804  
2805 UV
2806 server_version(EV::MariaDB self)
2807 CODE:
2808 {
2809 0 ▶ RETVAL = (NULL != self->conn) ? (UV)mysql_get_server_version(self->conn) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn) ? (UV)mysql_get_server_version(self->conn) : 0; 0 0 0
RETVAL = (NULL != self->conn) ? (UV)mysql_get_server_version(self->conn) : 0; 0 0 0
2810 }
2811 OUTPUT:
2812 RETVAL
2813  
2814 SV*
2815 server_info(EV::MariaDB self)
2816 CODE:
2817 {
2818 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_get_server_info(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_get_server_info(self->conn)) : &PL_sv_undef; 0 0 0
2819 }
2820 OUTPUT:
2821 RETVAL
2822  
2823 UV
2824 thread_id(EV::MariaDB self)
2825 CODE:
2826 {
2827 0 ▶ RETVAL = (NULL != self->conn) ? (UV)mysql_thread_id(self->conn) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn) ? (UV)mysql_thread_id(self->conn) : 0; 0 0 0
RETVAL = (NULL != self->conn) ? (UV)mysql_thread_id(self->conn) : 0; 0 0 0
2828 }
2829 OUTPUT:
2830 RETVAL
2831  
2832 SV*
2833 host_info(EV::MariaDB self)
2834 CODE:
2835 {
2836 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_get_host_info(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_get_host_info(self->conn)) : &PL_sv_undef; 0 0 0
2837 }
2838 OUTPUT:
2839 RETVAL
2840  
2841 SV*
2842 character_set_name(EV::MariaDB self)
2843 CODE:
2844 {
2845 0 ▶ RETVAL = self->conn ? str_or_undef(mysql_character_set_name(self->conn)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->conn ? str_or_undef(mysql_character_set_name(self->conn)) : &PL_sv_undef; 0 0 0
2846 }
2847 OUTPUT:
2848 RETVAL
2849  
2850 int
2851 socket(EV::MariaDB self)
2852 CODE:
2853 {
2854 0 ▶ RETVAL = (NULL != self->conn) ? (int)mysql_get_socket(self->conn) : -1;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (NULL != self->conn) ? (int)mysql_get_socket(self->conn) : -1; 0 0 0
RETVAL = (NULL != self->conn) ? (int)mysql_get_socket(self->conn) : -1; 0 0 0
2855 }
2856 OUTPUT:
2857 RETVAL
2858  
2859 SV*
2860 affected_rows(EV::MariaDB self)
2861 CODE:
2862 {
2863 0 ▶ if (NULL != self->conn) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->conn) { 0 0 0
2864 0 my_ulonglong rows = mysql_affected_rows(self->conn);
2865 0 ▶ RETVAL = (rows == (my_ulonglong)-1) ? &PL_sv_undef : newSVuv((UV)rows);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (rows == (my_ulonglong)-1) ? &PL_sv_undef : newSVuv((UV)rows); 0 0 0
2866 }
2867 else {
2868 0 RETVAL = &PL_sv_undef;
2869 }
2870 }
2871 OUTPUT:
2872 RETVAL
2873  
2874 SV*
2875 escape(EV::MariaDB self, SV *str)
2876 PREINIT:
2877 STRLEN len;
2878 const char *s;
2879 unsigned long elen;
2880 CODE:
2881 {
2882 0 ▶ if (NULL == self->conn || self->state == STATE_CONNECTING || IS_FORKED(self))
Branch: 4 branches
BranchTrueFalseTotal
if (NULL == self->conn || self->state == STATE_CONNECTING || IS_FORKED(self)) 0 0 0
if (NULL == self->conn || self->state == STATE_CONNECTING || IS_FORKED(self)) 0 0 0
if (NULL == self->conn || self->state == STATE_CONNECTING || IS_FORKED(self)) 0 0 0
if (NULL == self->conn || self->state == STATE_CONNECTING || IS_FORKED(self)) 0 0 0
2883 0 croak("not connected");
2884 0 ▶ if (self->state == STATE_CLOSE)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == STATE_CLOSE) 0 0 0
2885 0 croak("connection is closing");
2886 0 s = SvPV(str, len);
2887 0 ▶ if (SvUTF8(str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(str)) { 0 0 0
2888 0 const char *cs = mysql_character_set_name(self->conn);
2889 0 ▶ if (!cs || (strncmp(cs, "utf8", 4) != 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cs || (strncmp(cs, "utf8", 4) != 0)) { 0 0 0
if (!cs || (strncmp(cs, "utf8", 4) != 0)) { 0 0 0
2890 0 ▶ warn("EV::MariaDB: escaping a UTF-8 string on a non-utf8 connection (%s) may cause corruption or injection vulnerabilities", cs ? cs : "unknown");
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::MariaDB: escaping a UTF-8 string on a non-utf8 connection (%s) may cause corruption or injection vulnerabilities", cs ? cs : "unknown"); 0 0 0
2891 }
2892 }
2893 0 ▶ if (len > (STRLEN)((~(STRLEN)0) / 2 - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (len > (STRLEN)((~(STRLEN)0) / 2 - 1)) 0 0 0
2894 0 croak("string too large to escape");
2895 0 RETVAL = newSV(len * 2 + 1);
2896 0 SvPOK_on(RETVAL);
2897 0 elen = mysql_real_escape_string(self->conn, SvPVX(RETVAL), s, (unsigned long)len);
2898 0 ▶ if (elen == (unsigned long)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (elen == (unsigned long)-1) { 0 0 0
2899 0 SvREFCNT_dec(RETVAL);
2900 0 croak("mysql_real_escape_string failed");
2901 }
2902 0 SvCUR_set(RETVAL, elen);
2903 0 *SvEND(RETVAL) = '\0';
2904 }
2905 OUTPUT:
2906 RETVAL
2907  
2908 int
2909 pending_count(EV::MariaDB self)
2910 CODE:
2911 {
2912 0 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 0 0 0
2913 }
2914 OUTPUT:
2915 RETVAL
2916  
2917 void
2918 skip_pending(EV::MariaDB self)
2919 CODE:
2920 {
2921 0 ▶ if (self->state != STATE_IDLE || self->send_count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->state != STATE_IDLE || self->send_count > 0) { 0 0 0
if (self->state != STATE_IDLE || self->send_count > 0) { 0 0 0
2922 0 cleanup_connection(self);
2923 }
2924 0 cancel_pending(self, "skipped");
2925 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
2926 }
2927  
2928 UV
2929 lib_version(char *class)
2930 CODE:
2931 {
2932 PERL_UNUSED_VAR(class);
2933 2 RETVAL = (UV)mysql_get_client_version();
2934 }
2935 OUTPUT:
2936 RETVAL
2937  
2938 SV*
2939 lib_info(char *class)
2940 CODE:
2941 {
2942 PERL_UNUSED_VAR(class);
2943 1 RETVAL = newSVpv(mysql_get_client_info(), 0);
2944 }
2945 OUTPUT:
2946 RETVAL
« blib/lib/EV/MariaDB.pm ↑ Summary