MariaDB.xs

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