xs/model.xs

filter: Statementstmt 98.4% 125 / 127 Branchbran 58.8% 79 / 136 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 204 / 263 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/auth.xs ↑ Summary include/punk/punk_validate.h »
1 MODULE = Punk PACKAGE = Punk::Model
2  
3 PROTOTYPES: DISABLE
4  
5 # use Punk::Model - push the base class onto the caller's @ISA and install
6 # its declaration keywords, each a magic CV carrying that class's metadata.
7 void
8 import(class, ...)
9 SV *class
10 CODE:
11 {
12 81 ▶ const char *caller = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
const char *caller = CopSTASHPV(PL_curcop); 81 0 81
const char *caller = CopSTASHPV(PL_curcop); 81 0 81
const char *caller = CopSTASHPV(PL_curcop); 81 0 81
const char *caller = CopSTASHPV(PL_curcop); 0 81 81
const char *caller = CopSTASHPV(PL_curcop); 0 0 0
const char *caller = CopSTASHPV(PL_curcop); 81 0 81
const char *caller = CopSTASHPV(PL_curcop); 0 81 81
13 SV *caller_sv;
14 HV *meta;
15 SV *meta_rv;
16 PERL_UNUSED_VAR(class);
17 PERL_UNUSED_VAR(items);
18 81 ▶ if (!caller || strEQ(caller, "main")) XSRETURN_EMPTY; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!caller || strEQ(caller, "main")) XSRETURN_EMPTY; /* # uncoverable condition left count:1 class:ignore_covered_err */ 81 0 81
if (!caller || strEQ(caller, "main")) XSRETURN_EMPTY; /* # uncoverable condition left count:1 class:ignore_covered_err */ 11 70 81
19 70 caller_sv = sv_2mortal(newSVpv(caller, 0));
20  
21 /* a second `use` in the same class: nothing to do. Asked of the
22 * class's OWN metadata - an inherited one means this is a subclass,
23 * which does need its own entry to declare into. */
24 70 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 70 70
25 # uncoverable condition left count:1 class:ignore_covered_err
26 70 if (pm_meta_own(aTHX_ caller_sv)) XSRETURN_EMPTY;
27 70  
28 70 meta = newHV();
29 70 (void)hv_stores(meta, "table", newSV(0));
30 70 (void)hv_stores(meta, "fields", newRV_noinc((SV *)newAV()));
31 70 (void)hv_stores(meta, "field", newRV_noinc((SV *)newHV()));
32 70 (void)hv_stores(meta, "primary", newSV(0));
33 70 (void)hv_stores(meta, "validate", newSV(0));
34 70 (void)hv_stores(meta, "database", newSVpvs("default"));
35 meta_rv = newRV_noinc((SV *)meta);
36 (void)hv_store_ent(pm_registry(aTHX), caller_sv, meta_rv, 0);
37  
38 70 ▶ { /* @Caller::ISA = ('Punk::Model'), unless a parent already put a
Branch: 1 branch
BranchTrueFalseTotal
{ /* @Caller::ISA = ('Punk::Model'), unless a parent already put a 70 0 70
39 70 * model there - pushing it twice would be harmless but untidy. */
40 70 # uncoverable branch false
41 # uncoverable condition right count:1 class:ignore_covered_err
42 if (!sv_derived_from(caller_sv, "Punk::Model")) {
43 AV *isa = get_av(form("%s::ISA", caller), GV_ADD);
44 av_push(isa, newSVpvs("Punk::Model"));
45 }
46 }
47  
48 { /* the keywords, each carrying [ $meta ] */
49 static const struct { const char *name; XSUBADDR_t body; } kw[] = {
50 { "table", pm_kw_table },
51 { "field", pm_kw_field },
52 { "database", pm_kw_database },
53 350 ▶ { "validate", pm_kw_validate },
Branch: 1 branch
BranchTrueFalseTotal
{ "validate", pm_kw_validate }, 280 70 350
54 280 { NULL, NULL }
55 };
56 280 int i;
57 280 for (i = 0; kw[i].name; i++) {
58 AV *cap = newAV();
59 280 SV *code;
60 av_push(cap, newSVsv(meta_rv));
61 280 code = punk_closure(aTHX_ kw[i].body, cap);
62 280 {
63 GV *gv = gv_fetchpv(form("%s::%s", caller, kw[i].name),
64 GV_ADD, SVt_PVCV);
65 sv_setsv((SV *)gv, code);
66 70 SvREFCNT_dec(code);
67 }
68 }
69 }
70 XSRETURN_EMPTY;
71 }
72  
73 # The finalised metadata hashref, or undef when the class declared no
74 # table. Punk::App calls _punk_model_meta at compile to test a class.
75 SV *
76 _meta(class, ...)
77 SV *class
78 ALIAS:
79 _punk_model_meta = 1
80 CODE:
81 {
82 101 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 101 101
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
83 # uncoverable condition left count:2 class:ignore_covered_err
84 # uncoverable condition right count:2 class:ignore_covered_err
85 101 ▶ SV *pkg = (items > 1 && SvOK(ST(1))) ? ST(1) : class;
Branch: 1 branch
BranchTrueFalseTotal
SV *pkg = (items > 1 && SvOK(ST(1))) ? ST(1) : class; 0 101 101
86 101 HV *m;
87 202 ▶ PERL_UNUSED_VAR(ix);
Branch: 1 branch
BranchTrueFalseTotal
PERL_UNUSED_VAR(ix); 100 1 101
88 202 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 101 0 101
89 # uncoverable condition left count:1 class:ignore_covered_err
90 if (SvROK(pkg)) pkg = sv_2mortal(newSVpv(sv_reftype(SvRV(pkg), 1), 0));
91 m = pm_meta_hv(aTHX_ pkg);
92 RETVAL = (m && pm_meta_finalize(aTHX_ m))
93 ? newRV_inc((SV *)m) : newSV(0);
94 }
95 OUTPUT:
96 RETVAL
97  
98 # The per-worker instance: resolve the backend, hand it the table shape,
99 # compile the create/update validators. Called once per model per worker.
100 SV *
101 _instantiate(class, ...)
102 SV *class
103 CODE:
104 {
105 71 ▶ HV *dbopts = (items > 1 && SvROK(ST(1))
Branch: 1 branch
BranchTrueFalseTotal
HV *dbopts = (items > 1 && SvROK(ST(1)) 71 0 71
106 71 ▶ && SvTYPE(SvRV(ST(1))) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(1))) == SVt_PVHV) 71 0 71
107 142 ▶ ? (HV *)SvRV(ST(1)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(ST(1)) : NULL; 71 0 71
108 71 HV *m = pm_meta_hv(aTHX_ class);
109 HV *self;
110 SV *backend_class, *backend;
111 SV *should;
112  
113 71 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 71 0 71
# uncoverable branch true 0 71 71
114 0 # uncoverable condition left count:1 class:ignore_covered_err
115 # uncoverable condition left count:2 class:ignore_covered_err
116 if (!(m && pm_meta_finalize(aTHX_ m)))
117 croak("Punk::Model: '%s' declares no table", SvPV_nolen(class)); /* # uncoverable statement */
118  
119 71 ▶ { /* backend => 'Class', defaulting to the shipped DBI one.
Branch: 1 branch
BranchTrueFalseTotal
{ /* backend => 'Class', defaulting to the shipped DBI one. 71 0 71
120 53 ▶ * backend => 'Punk::Model::DBIx::Loop' selects the non-blocking
Branch: 1 branch
BranchTrueFalseTotal
* backend => 'Punk::Model::DBIx::Loop' selects the non-blocking 53 0 53
121 71 ▶ * backend, whose methods return futures instead of values. */
Branch: 1 branch
BranchTrueFalseTotal
* backend, whose methods return futures instead of values. */ 53 18 71
122 SV *b = dbopts ? pm_get(aTHX_ dbopts, "backend") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
123 backend_class = (b && SvOK(b)) ? sv_2mortal(newSVsv(b))
124 71 : sv_2mortal(newSVpvs("Punk::Model::DBI"));
125 71 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 70 1 71
} 0 70 70
126 1 ▶ { /* load it unless it is already there */
Branch: 1 branch
BranchTrueFalseTotal
{ /* load it unless it is already there */ 0 0 0
127 0 ▶ HV *stash = gv_stashsv(backend_class, 0);
Branch: 2 branches
BranchTrueFalseTotal
HV *stash = gv_stashsv(backend_class, 0); 0 0 0
HV *stash = gv_stashsv(backend_class, 0); 0 0 0
128 if (!(stash && gv_fetchmethod_autoload(stash, "new", 0))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
129 # uncoverable branch true
130 # uncoverable branch false
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition right count:1 class:ignore_covered_err
133 if (!pk_require_once(aTHX_ SvPV_nolen(backend_class), FALSE))
134 70 croak("Punk::Model: backend '%s' failed to load: %s", /* # uncoverable statement */
135 70 SvPV_nolen(backend_class), SvPV_nolen(ERRSV)); /* # uncoverable statement */
136 }
137 70 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 70 0 70
138  
139 70 { /* $backend_class->new(database => \%conn, table, primary,
140 205 ▶ * columns) - the connection options minus our own key */
Branch: 1 branch
BranchTrueFalseTotal
* columns) - the connection options minus our own key */ 135 70 205
141 135 ▶ HV *conn = newHV();
Branch: 2 branches
BranchTrueFalseTotal
HV *conn = newHV(); 135 0 135
HV *conn = newHV(); 0 135 135
142 135 ▶ dSP;
Branch: 2 branches
BranchTrueFalseTotal
dSP; 68 67 135
dSP; 52 16 68
143 52 int count;
144 83 # uncoverable branch false class:ignore_covered_err
145 # uncoverable condition right count:1 class:ignore_covered_err
146 if (dbopts) {
147 70 HE *he;
148 70 ▶ hv_iterinit(dbopts);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(dbopts); 0 70 70
149 70 ▶ while ((he = hv_iternext(dbopts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(dbopts))) { 0 70 70
150 70 SV *k = HeSVKEY_force(he);
151 70 if (SvCUR(k) == 7 && memEQ(SvPVX(k), "backend", 7))
152 70 continue;
153 70 (void)hv_store_ent(conn, k, newSVsv(HeVAL(he)), 0);
154 70 }
155 70 }
156 70 ENTER; SAVETMPS;
157 70 PUSHMARK(SP);
158 70 EXTEND(SP, 11);
159 PUSHs(backend_class);
160 mPUSHs(newSVpvs("database"));
161 70 ▶ mPUSHs(newRV_noinc((SV *)conn));
Branch: 2 branches
BranchTrueFalseTotal
mPUSHs(newRV_noinc((SV *)conn)); 43 27 70
mPUSHs(newRV_noinc((SV *)conn)); 43 0 43
162 43 mPUSHs(newSVpvs("table"));
163 43 mPUSHs(newSVsv(pm_get(aTHX_ m, "table")));
164 mPUSHs(newSVpvs("primary"));
165 70 mPUSHs(newSVsv(pm_get(aTHX_ m, "primary")));
166 70 mPUSHs(newSVpvs("columns"));
167 70 mPUSHs(newSVsv(pm_get(aTHX_ m, "fields")));
168 70 ▶ /* the application, for a backend that reads from it (the
Branch: 1 branch
BranchTrueFalseTotal
/* the application, for a backend that reads from it (the 70 0 70
169 70 ▶ * markdown one reads a mount's pages); the others ignore it */
Branch: 1 branch
BranchTrueFalseTotal
* markdown one reads a mount's pages); the others ignore it */ 70 0 70
170 if (items > 2 && SvROK(ST(2))) { /* # uncoverable condition right count:2 class:ignore_covered_err */
171 mPUSHs(newSVpvs("app"));
172 70 PUSHs(ST(2));
173 70 }
174 70 PUTBACK;
175 count = call_method("new", G_SCALAR);
176 70 SPAGAIN;
177 70 ▶ backend = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
backend = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
backend = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 19 51 70
178 PUTBACK; FREETMPS; LEAVE;
179 }
180 19  
181 57 ▶ self = newHV();
Branch: 1 branch
BranchTrueFalseTotal
self = newHV(); 38 19 57
182 38 (void)hv_stores(self, "backend", backend);
183 38 (void)hv_stores(self, "meta", newRV_inc((SV *)m));
184  
185 38 should = pm_get(aTHX_ m, "should_validate");
186 38 ▶ if (should && SvTRUE(should)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (should && SvTRUE(should)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 38 38
187 38 ▶ static const char *const which[] = { "cv_create", "cv_update" };
Branch: 1 branch
BranchTrueFalseTotal
static const char *const which[] = { "cv_create", "cv_update" }; 0 38 38
188 38 int partial;
189 38 (void)pk_require_once(aTHX_ "JSON::Schema::Fast", TRUE);
190 38 for (partial = 0; partial < 2; partial++) {
191 38 SV *schema = sv_2mortal(pm_object_schema(aTHX_ m, partial));
192 38 dSP;
193 38 ▶ int count;
Branch: 1 branch
BranchTrueFalseTotal
int count; 38 0 38
194 38 ENTER; SAVETMPS;
195 PUSHMARK(SP);
196 EXTEND(SP, 2);
197 38 ▶ mPUSHs(newSVpvs("JSON::Schema::Fast"));
Branch: 1 branch
BranchTrueFalseTotal
mPUSHs(newSVpvs("JSON::Schema::Fast")); 38 0 38
198 PUSHs(schema);
199 PUTBACK;
200 70 count = call_method("compile", G_SCALAR);
201 SPAGAIN;
202 # uncoverable branch false
203 # uncoverable condition right count:1 class:ignore_covered_err
204 if (count > 0)
205 (void)hv_store(self, which[partial],
206 (I32)strlen(which[partial]),
207 newSVsv(POPs), 0);
208 PUTBACK; FREETMPS; LEAVE;
209 }
210 }
211 RETVAL = sv_bless(newRV_noinc((SV *)self), gv_stashsv(class, GV_ADD));
212 }
213 OUTPUT:
214 RETVAL
215  
216 SV *
217 backend(self)
218 SV *self
219 ALIAS:
220 meta = 1
221 CODE:
222 {
223 68 ▶ SV *v = pm_slot(aTHX_ self, ix ? "meta" : "backend");
Branch: 1 branch
BranchTrueFalseTotal
SV *v = pm_slot(aTHX_ self, ix ? "meta" : "backend"); 4 64 68
224 68 ▶ RETVAL = v ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 68 0 68
225 }
226 OUTPUT:
227 RETVAL
228  
229 # get / search / delete / count: straight through to the backend, keeping
230 # the caller's context so a list-returning backend stays a list.
231 void
232 get(self, ...)
233 SV *self
234 ALIAS:
235 search = 1
236 delete = 2
237 count = 3
238 PPCODE:
239 {
240 static const char *const m[] = { "get", "search", "delete", "count" };
241 PERL_UNUSED_VAR(self);
242 855 pm_delegate(aTHX_ &ST(0), items, m[ix], GIMME_V);
243 728 return; /* whatever the backend left on the stack */
244 }
245  
246 # all: search with no filter and no options.
247 void
248 all(self)
249 SV *self
250 PPCODE:
251 {
252 23 SV *backend = pm_slot(aTHX_ self, "backend");
253 23 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 23 23
254 23 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 23 23
255 23 ▶ if (!backend) croak("Punk::Model: this model has no backend");
Branch: 1 branch
BranchTrueFalseTotal
if (!backend) croak("Punk::Model: this model has no backend"); 0 23 23
256 23 PUSHMARK(SP);
257 23 EXTEND(SP, 3);
258 23 PUSHs(backend);
259 23 mPUSHs(newRV_noinc((SV *)newHV()));
260 23 mPUSHs(newRV_noinc((SV *)newHV()));
261 21 PUTBACK;
262 21 call_method("search", GIMME_V);
263 SPAGAIN;
264 return;
265 }
266  
267 void
268 create(self, data)
269 SV *self
270 SV *data
271 PPCODE:
272 {
273 221 SV *cv_sv = pm_slot(aTHX_ self, "cv_create");
274 221 ▶ if (cv_sv && SvOK(cv_sv)) pm_validate(aTHX_ self, cv_sv, data); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cv_sv && SvOK(cv_sv)) pm_validate(aTHX_ self, cv_sv, data); /* # uncoverable condition right count:2 class:ignore_covered_err */ 31 190 221
if (cv_sv && SvOK(cv_sv)) pm_validate(aTHX_ self, cv_sv, data); /* # uncoverable condition right count:2 class:ignore_covered_err */ 31 0 31
275 217 pm_delegate(aTHX_ &ST(0), items, "create", GIMME_V);
276 198 return;
277 }
278  
279 # update validates the changes only: the primary key identifies the row
280 # rather than being part of what is being set, so a partial schema without
281 # it is what the payload has to satisfy.
282 void
283 update(self, data)
284 SV *self
285 SV *data
286 PPCODE:
287 {
288 38 SV *cv_sv = pm_slot(aTHX_ self, "cv_update");
289 38 ▶ if (cv_sv && SvOK(cv_sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cv_sv && SvOK(cv_sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 28 38
if (cv_sv && SvOK(cv_sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
290 10 SV *meta = pm_slot(aTHX_ self, "meta");
291 10 ▶ SV *pk = (meta && SvROK(meta))
Branch: 1 branch
BranchTrueFalseTotal
SV *pk = (meta && SvROK(meta)) 10 0 10
292 20 ▶ ? pm_get(aTHX_ (HV *)SvRV(meta), "primary") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? pm_get(aTHX_ (HV *)SvRV(meta), "primary") : NULL; 10 0 10
293 10 HV *changes = newHV();
294 10 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
295 10 # uncoverable condition right count:1 class:ignore_covered_err
296 # uncoverable condition right count:2 class:ignore_covered_err
297 10 if (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) {
298 26 ▶ HV *src = (HV *)SvRV(data);
Branch: 1 branch
BranchTrueFalseTotal
HV *src = (HV *)SvRV(data); 16 10 26
299 16 ▶ HE *he;
Branch: 2 branches
BranchTrueFalseTotal
HE *he; 16 0 16
HE *he; 0 16 16
300 16 ▶ hv_iterinit(src);
Branch: 3 branches
BranchTrueFalseTotal
hv_iterinit(src); 16 0 16
hv_iterinit(src); 16 0 16
hv_iterinit(src); 8 8 16
301 8 while ((he = hv_iternext(src))) {
302 SV *k = HeSVKEY_force(he);
303 # uncoverable condition right count:1 class:ignore_covered_err
304 10 # uncoverable condition right count:2 class:ignore_covered_err
305 if (pk && SvOK(pk) && sv_eq(k, pk)) continue;
306 (void)hv_store_ent(changes, k, newSVsv(HeVAL(he)), 0);
307 37 }
308 26 }
309 pm_validate(aTHX_ self, cv_sv,
310 sv_2mortal(newRV_noinc((SV *)changes)));
311 }
312 pm_delegate(aTHX_ &ST(0), items, "update", GIMME_V);
313 return;
314 }
« xs/auth.xs ↑ Summary include/punk/punk_validate.h »