| 1 |
|
| #ifndef DBIL_NATIVE_H |
| 2 |
|
| #define DBIL_NATIVE_H |
| 3 |
|
| |
| 4 |
|
| /* Backend A - native fd async, for socket drivers with a non-blocking execute |
| 5 |
|
| * (DBD::Pg first; mysql/MariaDB slot in beside later). |
| 6 |
|
| * |
| 7 |
|
| * True single-threaded async: fire the query with {pg_async => PG_ASYNC}, |
| 8 |
|
| * register the connection's socket ($dbh->{pg_socket}) with the loop, and on |
| 9 |
|
| * readability poll $dbh->pg_ready; when ready, $dbh->pg_result collects, rows |
| 10 |
|
| * are fetched from the async statement handle, and the future settles. No |
| 11 |
|
| * workers, no serialization - but exactly ONE query in flight per connection |
| 12 |
|
| * (a libpq limit), so further queries FIFO-queue on the connection until the |
| 13 |
|
| * pool of native connections arrives in phase 04. |
| 14 |
|
| * |
| 15 |
|
| * Everything reaches DBD::Pg through call_method / attribute FETCH, so this |
| 16 |
|
| * file compiles with no libpq or DBD::Pg present; capability sniffing keeps |
| 17 |
|
| * the whole backend unreachable unless DBD::Pg's async surface really exists. |
| 18 |
|
| * |
| 19 |
|
| * Depends on dbil_run.h (dbil_h_fetch) and dbil_pool.h (closures, dbil_vt). */ |
| 20 |
|
| |
| 21 |
|
| /* ---- the async-constant / capability sniff --------------------------------- */ |
| 22 |
|
| |
| 23 |
|
| static IV DBIL_PG_ASYNC = 0; |
| 24 |
|
| static int DBIL_PG_ASYNC_KNOWN = 0; |
| 25 |
|
| |
| 26 |
|
| /* resolve DBD::Pg::PG_ASYNC once; 0 (and known) when absent */ |
| 27 |
10 |
| static IV dbil_pg_async_const(pTHX) { |
| 28 |
10 |
▶ | if (DBIL_PG_ASYNC_KNOWN) return DBIL_PG_ASYNC; |
Branch: 1 branch
| Branch | True | False | Total |
| if (DBIL_PG_ASYNC_KNOWN) return DBIL_PG_ASYNC; |
9 |
1 |
10 |
|
| 29 |
1 |
| DBIL_PG_ASYNC_KNOWN = 1; |
| 30 |
|
| { |
| 31 |
1 |
| CV *cv = get_cv("DBD::Pg::PG_ASYNC", 0); |
| 32 |
1 |
▶ | if (cv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) { |
1 |
0 |
1 |
|
| 33 |
1 |
| dSP; int n; |
| 34 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
1 |
1 |
|
| 35 |
1 |
| n = call_sv((SV *)cv, G_SCALAR | G_EVAL); |
| 36 |
1 |
| SPAGAIN; |
| 37 |
1 |
▶ | if (!SvTRUE(ERRSV) && n > 0) DBIL_PG_ASYNC = POPi; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n > 0) DBIL_PG_ASYNC = POPi; |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && n > 0) DBIL_PG_ASYNC = POPi; |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && n > 0) DBIL_PG_ASYNC = POPi; |
1 |
0 |
1 |
|
| 38 |
0 |
▶ | else if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 39 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 40 |
|
| } |
| 41 |
|
| } |
| 42 |
1 |
| return DBIL_PG_ASYNC; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| /* is the driver's native-async surface actually loaded + usable? */ |
| 46 |
1 |
| static int dbil_native_available(pTHX_ const char *drv) { |
| 47 |
1 |
▶ | if (strEQ(drv, "Pg")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(drv, "Pg")) |
1 |
0 |
1 |
|
| 48 |
1 |
| return dbil_pg_async_const(aTHX) != 0; |
| 49 |
|
| /* mysql/MariaDB: deferred until their surface is wired in */ |
| 50 |
0 |
| return 0; |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| /* ---- native connection state ------------------------------------------------ */ |
| 54 |
|
| |
| 55 |
|
| typedef struct dbil_native { |
| 56 |
|
| pid_t pid; /* the process that owns this libpq connection */ |
| 57 |
|
| SV *dbh; /* the (parent-held) connection (+1) */ |
| 58 |
|
| SV *loop; /* loop adapter (+1) */ |
| 59 |
|
| dbil_vt *vt; /* C seam when the adapter has one */ |
| 60 |
|
| int fd; /* pg_socket, registered with the loop; -1 = not yet */ |
| 61 |
|
| int dead; /* connection-level failure: stop accepting */ |
| 62 |
|
| SV *inflight; /* future of the running query (+1) or NULL */ |
| 63 |
|
| SV *sth; /* its async statement handle (+1) or NULL */ |
| 64 |
|
| int is_query; /* SELECT-style: fetch rows on completion */ |
| 65 |
|
| AV *queue; /* pending [is_query, sql, bindref, future, ...] */ |
| 66 |
|
| } dbil_native; |
| 67 |
|
| |
| 68 |
|
| static void dbil_native_pump(pTHX_ dbil_native *nc); /* fire next; below */ |
| 69 |
|
| |
| 70 |
|
| /* dbil_errstr lives in dbil_run.h now: every backend needs it */ |
| 71 |
|
| |
| 72 |
|
| /* finish the in-flight query: collect pg_result, fetch rows, settle */ |
| 73 |
8 |
| static void dbil_native_complete(pTHX_ dbil_native *nc) { |
| 74 |
8 |
| SV *fut = nc->inflight; |
| 75 |
8 |
| SV *sth = nc->sth; |
| 76 |
8 |
| IV rows = 0; |
| 77 |
8 |
| int ok = 1; |
| 78 |
8 |
| SV *err = NULL; |
| 79 |
|
| |
| 80 |
8 |
| nc->inflight = NULL; |
| 81 |
8 |
| nc->sth = NULL; |
| 82 |
|
| |
| 83 |
|
| { /* $dbh->pg_result: rows-affected, false on error */ |
| 84 |
8 |
| dSP; int n; |
| 85 |
8 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
8 |
8 |
|
| 86 |
8 |
▶ | EXTEND(SP, 1); PUSHs(nc->dbh); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(nc->dbh); PUTBACK; |
0 |
8 |
8 |
|
| 87 |
8 |
| n = call_method("pg_result", G_SCALAR | G_EVAL); |
| 88 |
8 |
| SPAGAIN; |
| 89 |
8 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
8 |
0 |
8 |
| if (SvTRUE(ERRSV)) { |
0 |
8 |
8 |
|
| 90 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 91 |
0 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 93 |
0 |
| ok = 0; err = sv_2mortal(e); |
| 94 |
|
| } else { |
| 95 |
8 |
▶ | SV *r = (n > 0) ? POPs : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *r = (n > 0) ? POPs : NULL; |
8 |
0 |
8 |
|
| 96 |
8 |
▶ | if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
Branch: 8 branches
| Branch | True | False | Total |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
8 |
0 |
8 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
8 |
0 |
8 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
0 |
8 |
8 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
0 |
0 |
0 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
8 |
0 |
8 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
1 |
7 |
8 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
1 |
0 |
1 |
| if (!r || !SvOK(r) || (SvPOK(r) && !SvCUR(r)) || (!SvPOK(r) && !SvTRUE(r) && SvIV(r) == 0 && !SvNOK(r))) { |
1 |
0 |
1 |
|
| 97 |
|
| /* a plain false return = error; grab errstr after LEAVE */ |
| 98 |
1 |
| ok = SvTRUE(r) ? 1 : 0; |
| 99 |
|
| } |
| 100 |
8 |
▶ | if (r && SvOK(r)) rows = SvIV(r); |
Branch: 2 branches
| Branch | True | False | Total |
| if (r && SvOK(r)) rows = SvIV(r); |
8 |
0 |
8 |
| if (r && SvOK(r)) rows = SvIV(r); |
8 |
0 |
8 |
|
| 101 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 102 |
8 |
▶ | if (!ok) err = dbil_errstr(aTHX_ nc->dbh, "pg_result failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) err = dbil_errstr(aTHX_ nc->dbh, "pg_result failed"); |
1 |
7 |
8 |
|
| 103 |
|
| } |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
8 |
▶ | if (fut) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fut) { |
8 |
0 |
8 |
|
| 107 |
8 |
▶ | if (!ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { |
1 |
7 |
8 |
|
| 108 |
1 |
| dbil_future_settle_fail(aTHX_ fut, err); |
| 109 |
|
| } |
| 110 |
13 |
▶ | else if (nc->is_query && sth) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (nc->is_query && sth) { |
6 |
1 |
7 |
| else if (nc->is_query && sth) { |
6 |
0 |
6 |
|
| 111 |
6 |
| SV *nfld = dbil_h_fetch(aTHX_ sth, "NUM_OF_FIELDS"); |
| 112 |
6 |
▶ | IV nf = (nfld && SvOK(nfld)) ? SvIV(nfld) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| IV nf = (nfld && SvOK(nfld)) ? SvIV(nfld) : 0; |
6 |
0 |
6 |
| IV nf = (nfld && SvOK(nfld)) ? SvIV(nfld) : 0; |
6 |
0 |
6 |
|
| 113 |
6 |
| HV *h = newHV(); |
| 114 |
6 |
▶ | if (nf > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nf > 0) { |
6 |
0 |
6 |
|
| 115 |
6 |
| SV *names = dbil_h_fetch(aTHX_ sth, "NAME"); |
| 116 |
6 |
| SV *fa = NULL; |
| 117 |
|
| { |
| 118 |
6 |
| dSP; int n; |
| 119 |
6 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
6 |
6 |
|
| 120 |
6 |
▶ | EXTEND(SP, 1); PUSHs(sth); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(sth); PUTBACK; |
0 |
6 |
6 |
|
| 121 |
6 |
| n = call_method("fetchall_arrayref", G_SCALAR | G_EVAL); |
| 122 |
6 |
| SPAGAIN; |
| 123 |
6 |
▶ | if (!SvTRUE(ERRSV) && n > 0) { SV *r = POPs; if (SvROK(r)) fa = SvREFCNT_inc(r); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n > 0) { SV *r = POPs; if (SvROK(r)) fa = SvREFCNT_inc(r); } |
6 |
0 |
6 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *r = POPs; if (SvROK(r)) fa = SvREFCNT_inc(r); } |
6 |
0 |
6 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *r = POPs; if (SvROK(r)) fa = SvREFCNT_inc(r); } |
6 |
0 |
6 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *r = POPs; if (SvROK(r)) fa = SvREFCNT_inc(r); } |
6 |
0 |
6 |
|
| 124 |
0 |
▶ | else if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 125 |
6 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 126 |
|
| } |
| 127 |
6 |
▶ | (void)hv_stores(h, "rows", fa ? fa : newRV_noinc((SV *)newAV())); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "rows", fa ? fa : newRV_noinc((SV *)newAV())); |
0 |
6 |
6 |
|
| 128 |
|
| { |
| 129 |
6 |
| AV *cols = newAV(); |
| 130 |
6 |
▶ | if (names && SvROK(names) && SvTYPE(SvRV(names)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (names && SvROK(names) && SvTYPE(SvRV(names)) == SVt_PVAV) { |
6 |
0 |
6 |
| if (names && SvROK(names) && SvTYPE(SvRV(names)) == SVt_PVAV) { |
6 |
0 |
6 |
| if (names && SvROK(names) && SvTYPE(SvRV(names)) == SVt_PVAV) { |
6 |
0 |
6 |
|
| 131 |
6 |
| AV *na = (AV *)SvRV(names); |
| 132 |
6 |
| SSize_t j, m = av_len(na) + 1; |
| 133 |
18 |
▶ | for (j = 0; j < m; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < m; j++) { |
12 |
6 |
18 |
|
| 134 |
12 |
| SV **c = av_fetch(na, j, 0); |
| 135 |
12 |
▶ | av_push(cols, (c && *c) ? newSVsv(*c) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(cols, (c && *c) ? newSVsv(*c) : newSV(0)); |
12 |
0 |
12 |
| av_push(cols, (c && *c) ? newSVsv(*c) : newSV(0)); |
12 |
0 |
12 |
|
| 136 |
|
| } |
| 137 |
|
| } |
| 138 |
6 |
| (void)hv_stores(h, "columns", newRV_noinc((SV *)cols)); |
| 139 |
|
| } |
| 140 |
|
| } else { |
| 141 |
0 |
| (void)hv_stores(h, "rows", newRV_noinc((SV *)newAV())); |
| 142 |
0 |
| (void)hv_stores(h, "columns", newRV_noinc((SV *)newAV())); |
| 143 |
|
| } |
| 144 |
6 |
| dbil_future_settle_done1(aTHX_ fut, sv_2mortal(newRV_noinc((SV *)h))); |
| 145 |
|
| } |
| 146 |
|
| else { |
| 147 |
1 |
| HV *h = newHV(); |
| 148 |
1 |
| (void)hv_stores(h, "rows_affected", newSViv(rows > 0 ? rows : 0)); |
| 149 |
1 |
| dbil_future_settle_done1(aTHX_ fut, sv_2mortal(newRV_noinc((SV *)h))); |
| 150 |
|
| } |
| 151 |
8 |
| SvREFCNT_dec(fut); |
| 152 |
|
| } |
| 153 |
8 |
▶ | if (sth) SvREFCNT_dec(sth); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sth) SvREFCNT_dec(sth); |
8 |
0 |
8 |
|
| 154 |
|
| |
| 155 |
8 |
| dbil_native_pump(aTHX_ nc); /* fire the next queued query, if any */ |
| 156 |
8 |
| } |
| 157 |
|
| |
| 158 |
|
| /* readable on the pg socket: is the async result ready yet? */ |
| 159 |
16 |
| static void dbil_native_on_readable(pTHX_ dbil_native *nc) { |
| 160 |
16 |
| int ready = 0; |
| 161 |
16 |
▶ | if (!nc->inflight) return; /* spurious wakeup */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nc->inflight) return; /* spurious wakeup */ |
0 |
16 |
16 |
|
| 162 |
|
| { |
| 163 |
16 |
| dSP; int n; |
| 164 |
16 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
16 |
16 |
|
| 165 |
16 |
▶ | EXTEND(SP, 1); PUSHs(nc->dbh); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(nc->dbh); PUTBACK; |
0 |
16 |
16 |
|
| 166 |
16 |
| n = call_method("pg_ready", G_SCALAR | G_EVAL); |
| 167 |
16 |
| SPAGAIN; |
| 168 |
|
| /* SvTRUE is a multi-evaluating macro before 5.30, so SvTRUE(POPs) |
| 169 |
|
| * pops once per expansion and walks SP below the stack base. Pop |
| 170 |
|
| * into a variable and test that. */ |
| 171 |
16 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
16 |
0 |
16 |
|
| 172 |
16 |
| SV *r = POPs; |
| 173 |
16 |
▶ | if (!SvTRUE(ERRSV)) ready = SvTRUE(r) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) ready = SvTRUE(r) ? 1 : 0; |
16 |
0 |
16 |
| if (!SvTRUE(ERRSV)) ready = SvTRUE(r) ? 1 : 0; |
16 |
0 |
16 |
|
| 174 |
|
| } |
| 175 |
16 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
16 |
0 |
16 |
|
| 176 |
|
| } |
| 177 |
16 |
▶ | if (ready) dbil_native_complete(aTHX_ nc); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ready) dbil_native_complete(aTHX_ nc); |
8 |
8 |
16 |
|
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| /* the two seam callbacks */ |
| 181 |
0 |
| static void dbil_native_reader_c(pTHX_ int fd, int mask, void *ud) { |
| 182 |
|
| PERL_UNUSED_VAR(fd); PERL_UNUSED_VAR(mask); |
| 183 |
0 |
| dbil_native_on_readable(aTHX_ (dbil_native *)ud); |
| 184 |
0 |
| } |
| 185 |
|
| XS_INTERNAL(dbil_native_reader_cb); |
| 186 |
8 |
| XS_INTERNAL(dbil_native_reader_cb) { |
| 187 |
8 |
| dXSARGS; |
| 188 |
8 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 189 |
|
| PERL_UNUSED_VAR(items); |
| 190 |
8 |
▶ | if (cap) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap) |
8 |
0 |
8 |
|
| 191 |
8 |
| dbil_native_on_readable(aTHX_ |
| 192 |
8 |
| INT2PTR(dbil_native *, SvIV(*av_fetch(cap, 0, 0)))); |
| 193 |
8 |
| XSRETURN_EMPTY; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| /* register the pg socket with the loop (once) */ |
| 197 |
8 |
| static void dbil_native_watch(pTHX_ dbil_native *nc) { |
| 198 |
|
| SV *sock; |
| 199 |
8 |
▶ | if (nc->fd >= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->fd >= 0) return; |
7 |
1 |
8 |
|
| 200 |
1 |
| sock = dbil_h_fetch(aTHX_ nc->dbh, "pg_socket"); |
| 201 |
1 |
▶ | if (!sock || !SvOK(sock)) { nc->fd = -1; return; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sock || !SvOK(sock)) { nc->fd = -1; return; } |
1 |
0 |
1 |
| if (!sock || !SvOK(sock)) { nc->fd = -1; return; } |
0 |
1 |
1 |
|
| 202 |
1 |
| nc->fd = (int)SvIV(sock); |
| 203 |
1 |
▶ | if (nc->vt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->vt) { |
0 |
1 |
1 |
|
| 204 |
0 |
| nc->vt->add_reader(aTHX_ nc->vt->ctx, nc->fd, dbil_native_reader_c, nc); |
| 205 |
|
| } else { |
| 206 |
1 |
| AV *cap = newAV(); |
| 207 |
|
| SV *cb; |
| 208 |
1 |
| av_push(cap, newSViv(PTR2IV(nc))); |
| 209 |
1 |
| cb = sv_2mortal(dbil_closure(aTHX_ dbil_native_reader_cb, cap)); |
| 210 |
|
| { |
| 211 |
1 |
| dSP; |
| 212 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
1 |
1 |
|
| 213 |
1 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
1 |
1 |
|
| 214 |
1 |
| PUSHs(nc->loop); mPUSHi(nc->fd); PUSHs(cb); |
| 215 |
1 |
| PUTBACK; |
| 216 |
1 |
| call_method("add_reader", G_VOID | G_DISCARD | G_EVAL); |
| 217 |
1 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 218 |
|
| } |
| 219 |
|
| } |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* fire one query asynchronously; on immediate failure settle the future */ |
| 223 |
9 |
| static void dbil_native_fire(pTHX_ dbil_native *nc, int is_query, SV *sql, |
| 224 |
|
| SV *bindref, SV *future) { |
| 225 |
9 |
| SV *sth = NULL; |
| 226 |
|
| |
| 227 |
|
| { /* $sth = $dbh->prepare($sql, { pg_async => PG_ASYNC }) */ |
| 228 |
9 |
| HV *attr = newHV(); |
| 229 |
|
| SV *attr_rv; |
| 230 |
9 |
| (void)hv_stores(attr, "pg_async", newSViv(dbil_pg_async_const(aTHX))); |
| 231 |
9 |
| attr_rv = sv_2mortal(newRV_noinc((SV *)attr)); |
| 232 |
|
| { |
| 233 |
9 |
| dSP; int n; |
| 234 |
9 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
9 |
9 |
|
| 235 |
9 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
9 |
9 |
|
| 236 |
9 |
| PUSHs(nc->dbh); PUSHs(sql); PUSHs(attr_rv); |
| 237 |
9 |
| PUTBACK; |
| 238 |
9 |
| n = call_method("prepare", G_SCALAR | G_EVAL); |
| 239 |
9 |
| SPAGAIN; |
| 240 |
9 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
9 |
0 |
9 |
| if (SvTRUE(ERRSV)) { |
1 |
8 |
9 |
|
| 241 |
1 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
1 |
0 |
1 |
|
| 242 |
1 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
1 |
0 |
1 |
|
| 243 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 244 |
1 |
| dbil_future_settle_fail(aTHX_ future, sv_2mortal(e)); |
| 245 |
1 |
| SvREFCNT_dec(future); |
| 246 |
1 |
| return; |
| 247 |
|
| } |
| 248 |
8 |
▶ | if (n > 0) { SV *s = POPs; if (SvROK(s)) sth = SvREFCNT_inc(s); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0) { SV *s = POPs; if (SvROK(s)) sth = SvREFCNT_inc(s); } |
8 |
0 |
8 |
| if (n > 0) { SV *s = POPs; if (SvROK(s)) sth = SvREFCNT_inc(s); } |
8 |
0 |
8 |
|
| 249 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 250 |
|
| } |
| 251 |
|
| } |
| 252 |
8 |
▶ | if (!sth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sth) { |
0 |
8 |
8 |
|
| 253 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 254 |
|
| dbil_errstr(aTHX_ nc->dbh, "async prepare failed")); |
| 255 |
0 |
| SvREFCNT_dec(future); |
| 256 |
0 |
| return; |
| 257 |
|
| } |
| 258 |
|
| |
| 259 |
|
| { /* $sth->execute(@bind) - returns immediately under pg_async */ |
| 260 |
8 |
▶ | AV *bind = (bindref && SvROK(bindref)) ? (AV *)SvRV(bindref) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| AV *bind = (bindref && SvROK(bindref)) ? (AV *)SvRV(bindref) : NULL; |
8 |
0 |
8 |
| AV *bind = (bindref && SvROK(bindref)) ? (AV *)SvRV(bindref) : NULL; |
8 |
0 |
8 |
|
| 261 |
8 |
▶ | SSize_t nb = bind ? av_len(bind) + 1 : 0, i; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t nb = bind ? av_len(bind) + 1 : 0, i; |
8 |
0 |
8 |
|
| 262 |
8 |
| dSP; int n, exec_failed = 0; |
| 263 |
8 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
8 |
8 |
|
| 264 |
8 |
▶ | EXTEND(SP, 1 + nb); PUSHs(sth); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, 1 + nb); PUSHs(sth); |
8 |
0 |
8 |
| EXTEND(SP, 1 + nb); PUSHs(sth); |
0 |
8 |
8 |
|
| 265 |
8 |
▶ | for (i = 0; i < nb; i++) { SV **e = av_fetch(bind, i, 0); PUSHs(e && *e ? *e : &PL_sv_undef); } |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i < nb; i++) { SV **e = av_fetch(bind, i, 0); PUSHs(e && *e ? *e : &PL_sv_undef); } |
0 |
0 |
0 |
| for (i = 0; i < nb; i++) { SV **e = av_fetch(bind, i, 0); PUSHs(e && *e ? *e : &PL_sv_undef); } |
0 |
0 |
0 |
| for (i = 0; i < nb; i++) { SV **e = av_fetch(bind, i, 0); PUSHs(e && *e ? *e : &PL_sv_undef); } |
0 |
8 |
8 |
|
| 266 |
8 |
| PUTBACK; |
| 267 |
8 |
| n = call_method("execute", G_SCALAR | G_EVAL); |
| 268 |
8 |
| SPAGAIN; |
| 269 |
8 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
8 |
0 |
8 |
| if (SvTRUE(ERRSV)) { |
0 |
8 |
8 |
|
| 270 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 271 |
0 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 272 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 273 |
0 |
| SvREFCNT_dec(sth); |
| 274 |
0 |
| dbil_future_settle_fail(aTHX_ future, sv_2mortal(e)); |
| 275 |
0 |
| SvREFCNT_dec(future); |
| 276 |
0 |
| return; |
| 277 |
|
| } |
| 278 |
|
| /* a false return is the driver refusing the statement before it |
| 279 |
|
| * went to the server; there will be no pg_result to wait for */ |
| 280 |
8 |
▶ | if (n > 0) { SV *r = POPs; if (!SvTRUE(r)) exec_failed = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0) { SV *r = POPs; if (!SvTRUE(r)) exec_failed = 1; } |
8 |
0 |
8 |
| if (n > 0) { SV *r = POPs; if (!SvTRUE(r)) exec_failed = 1; } |
0 |
8 |
8 |
|
| 281 |
0 |
| else exec_failed = 1; |
| 282 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 283 |
8 |
▶ | if (exec_failed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (exec_failed) { |
0 |
8 |
8 |
|
| 284 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 285 |
|
| dbil_errstr(aTHX_ sth, "async execute failed")); |
| 286 |
0 |
| SvREFCNT_dec(sth); |
| 287 |
0 |
| SvREFCNT_dec(future); |
| 288 |
0 |
| return; |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
8 |
| nc->inflight = future; /* takes the +1 we were given */ |
| 293 |
8 |
| nc->sth = sth; |
| 294 |
8 |
| nc->is_query = is_query; |
| 295 |
8 |
| dbil_native_watch(aTHX_ nc); |
| 296 |
|
| /* if the result raced us and is already ready, complete now rather than |
| 297 |
|
| * waiting for a readability edge we may have missed */ |
| 298 |
8 |
| dbil_native_on_readable(aTHX_ nc); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| /* fire the next queued request if the connection is idle */ |
| 302 |
8 |
| static void dbil_native_pump(pTHX_ dbil_native *nc) { |
| 303 |
8 |
▶ | if (nc->inflight || nc->dead) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (nc->inflight || nc->dead) return; |
8 |
0 |
8 |
| if (nc->inflight || nc->dead) return; |
0 |
8 |
8 |
|
| 304 |
8 |
▶ | if (av_len(nc->queue) >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(nc->queue) >= 3) { |
2 |
6 |
8 |
|
| 305 |
2 |
| SV *isq = av_shift(nc->queue); |
| 306 |
2 |
| SV *sql = av_shift(nc->queue); |
| 307 |
2 |
| SV *bref = av_shift(nc->queue); |
| 308 |
2 |
| SV *fut = av_shift(nc->queue); |
| 309 |
2 |
| dbil_native_fire(aTHX_ nc, (int)SvIV(isq), sv_2mortal(sql), |
| 310 |
|
| sv_2mortal(bref), fut); /* fire takes fut's +1 */ |
| 311 |
2 |
| SvREFCNT_dec(isq); |
| 312 |
|
| } |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| /* run one statement on the native connection; returns the future (+1) */ |
| 316 |
9 |
| static SV *dbil_native_run(pTHX_ dbil_native *nc, int is_query, SV *sql, AV *bind) { |
| 317 |
9 |
| SV *future = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 318 |
9 |
▶ | if (nc->dead) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->dead) { |
0 |
9 |
9 |
|
| 319 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 320 |
|
| sv_2mortal(newSVpvs("native connection is closed"))); |
| 321 |
0 |
| return future; |
| 322 |
|
| } |
| 323 |
9 |
▶ | if (nc->inflight) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->inflight) { |
2 |
7 |
9 |
|
| 324 |
|
| /* one in-flight per connection (libpq): queue */ |
| 325 |
2 |
| av_push(nc->queue, newSViv(is_query)); |
| 326 |
2 |
| av_push(nc->queue, newSVsv(sql)); |
| 327 |
2 |
| av_push(nc->queue, dbil_bind_rv(aTHX_ bind)); |
| 328 |
2 |
| av_push(nc->queue, SvREFCNT_inc(future)); |
| 329 |
2 |
| return future; |
| 330 |
|
| } |
| 331 |
7 |
| dbil_native_fire(aTHX_ nc, is_query, sql, |
| 332 |
|
| sv_2mortal(dbil_bind_rv(aTHX_ bind)), |
| 333 |
7 |
| SvREFCNT_inc(future)); |
| 334 |
7 |
| return future; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* ---- lifecycle --------------------------------------------------------------- */ |
| 338 |
|
| |
| 339 |
|
| /* A libpq connection cannot be shared across a fork: two processes writing the |
| 340 |
|
| * same socket interleave protocol messages and neither can recover. So the |
| 341 |
|
| * owning pid is recorded and an inherited state is dropped rather than used. */ |
| 342 |
9 |
| static int dbil_native_owned(const dbil_native *nc) { |
| 343 |
9 |
▶ | return nc && nc->pid == getpid(); |
Branch: 2 branches
| Branch | True | False | Total |
| return nc && nc->pid == getpid(); |
9 |
0 |
9 |
| return nc && nc->pid == getpid(); |
9 |
0 |
9 |
|
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
1 |
| static dbil_native *dbil_native_new(pTHX_ SV *dbh, SV *loop) { |
| 347 |
|
| dbil_native *nc; |
| 348 |
1 |
| Newxz(nc, 1, dbil_native); |
| 349 |
1 |
| nc->pid = getpid(); |
| 350 |
1 |
| nc->dbh = SvREFCNT_inc(dbh); |
| 351 |
1 |
| nc->loop = SvREFCNT_inc(loop); |
| 352 |
1 |
| nc->vt = dbil_vt_of(aTHX_ loop); |
| 353 |
1 |
| nc->fd = -1; |
| 354 |
1 |
| nc->queue = newAV(); |
| 355 |
1 |
| return nc; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
|
| /* Release inherited native state: drop our references and free the struct, |
| 359 |
|
| * touching neither the loop (the registration is the parent's) nor the socket |
| 360 |
|
| * (closing it would tear down the parent's connection). */ |
| 361 |
0 |
| static void dbil_native_disown(pTHX_ dbil_native *nc) { |
| 362 |
0 |
▶ | if (!nc) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nc) return; |
0 |
0 |
0 |
|
| 363 |
0 |
▶ | if (nc->inflight) SvREFCNT_dec(nc->inflight); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->inflight) SvREFCNT_dec(nc->inflight); |
0 |
0 |
0 |
|
| 364 |
0 |
▶ | if (nc->sth) SvREFCNT_dec(nc->sth); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->sth) SvREFCNT_dec(nc->sth); |
0 |
0 |
0 |
|
| 365 |
0 |
▶ | if (nc->queue) SvREFCNT_dec((SV *)nc->queue); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->queue) SvREFCNT_dec((SV *)nc->queue); |
0 |
0 |
0 |
|
| 366 |
0 |
▶ | if (nc->dbh) SvREFCNT_dec(nc->dbh); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->dbh) SvREFCNT_dec(nc->dbh); |
0 |
0 |
0 |
|
| 367 |
0 |
▶ | if (nc->loop) SvREFCNT_dec(nc->loop); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->loop) SvREFCNT_dec(nc->loop); |
0 |
0 |
0 |
|
| 368 |
0 |
| Safefree(nc); |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
1 |
| static void dbil_native_free(pTHX_ dbil_native *nc) { |
| 372 |
1 |
▶ | if (!nc) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nc) return; |
0 |
1 |
1 |
|
| 373 |
1 |
▶ | if (!dbil_native_owned(nc)) { dbil_native_disown(aTHX_ nc); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dbil_native_owned(nc)) { dbil_native_disown(aTHX_ nc); return; } |
0 |
1 |
1 |
|
| 374 |
1 |
▶ | if (nc->fd >= 0 && !PL_dirty) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nc->fd >= 0 && !PL_dirty) { |
1 |
0 |
1 |
| if (nc->fd >= 0 && !PL_dirty) { |
1 |
0 |
1 |
|
| 375 |
1 |
▶ | if (nc->vt) nc->vt->remove(aTHX_ nc->vt->ctx, nc->fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->vt) nc->vt->remove(aTHX_ nc->vt->ctx, nc->fd); |
0 |
1 |
1 |
|
| 376 |
|
| else { |
| 377 |
1 |
| dSP; |
| 378 |
1 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
1 |
1 |
|
| 379 |
1 |
▶ | EXTEND(SP, 2); PUSHs(nc->loop); mPUSHi(nc->fd); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); PUSHs(nc->loop); mPUSHi(nc->fd); PUTBACK; |
0 |
1 |
1 |
|
| 380 |
1 |
| call_method("remove", G_VOID | G_DISCARD | G_EVAL); |
| 381 |
1 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 382 |
|
| } |
| 383 |
|
| } |
| 384 |
1 |
▶ | if (nc->inflight) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->inflight) { |
0 |
1 |
1 |
|
| 385 |
0 |
| dbil_future_settle_fail(aTHX_ nc->inflight, |
| 386 |
|
| sv_2mortal(newSVpvs("connection closed with query in flight"))); |
| 387 |
0 |
| SvREFCNT_dec(nc->inflight); |
| 388 |
|
| } |
| 389 |
1 |
▶ | if (nc->sth) SvREFCNT_dec(nc->sth); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->sth) SvREFCNT_dec(nc->sth); |
0 |
1 |
1 |
|
| 390 |
|
| { /* fail anything still queued */ |
| 391 |
1 |
▶ | while (av_len(nc->queue) >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(nc->queue) >= 3) { |
0 |
1 |
1 |
|
| 392 |
0 |
| SV *isq = av_shift(nc->queue), *sql = av_shift(nc->queue); |
| 393 |
0 |
| SV *br = av_shift(nc->queue), *fut = av_shift(nc->queue); |
| 394 |
0 |
| SvREFCNT_dec(isq); SvREFCNT_dec(sql); SvREFCNT_dec(br); |
| 395 |
0 |
| dbil_future_settle_fail(aTHX_ fut, |
| 396 |
|
| sv_2mortal(newSVpvs("connection closed"))); |
| 397 |
0 |
| SvREFCNT_dec(fut); |
| 398 |
|
| } |
| 399 |
|
| } |
| 400 |
1 |
| SvREFCNT_dec((SV *)nc->queue); |
| 401 |
1 |
| SvREFCNT_dec(nc->dbh); |
| 402 |
1 |
| SvREFCNT_dec(nc->loop); |
| 403 |
1 |
| Safefree(nc); |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
|
| #endif /* DBIL_NATIVE_H */ |