* If the variable has an armed "promise", compute the promised value * and assign it to the variable. * The assignment automatically disarms the promise. */
| 1343 | * The assignment automatically disarms the promise. |
| 1344 | */ |
| 1345 | static void |
| 1346 | plpgsql_fulfill_promise(PLpgSQL_execstate *estate, |
| 1347 | PLpgSQL_var *var) |
| 1348 | { |
| 1349 | MemoryContext oldcontext; |
| 1350 | |
| 1351 | if (var->promise == PLPGSQL_PROMISE_NONE) |
| 1352 | return; /* nothing to do */ |
| 1353 | |
| 1354 | /* |
| 1355 | * This will typically be invoked in a short-lived context such as the |
| 1356 | * mcontext. We must create variable values in the estate's datum |
| 1357 | * context. This quick-and-dirty solution risks leaking some additional |
| 1358 | * cruft there, but since any one promise is honored at most once per |
| 1359 | * function call, it's probably not worth being more careful. |
| 1360 | */ |
| 1361 | oldcontext = MemoryContextSwitchTo(estate->datum_context); |
| 1362 | |
| 1363 | switch (var->promise) |
| 1364 | { |
| 1365 | case PLPGSQL_PROMISE_TG_NAME: |
| 1366 | if (estate->trigdata == NULL) |
| 1367 | elog(ERROR, "trigger promise is not in a trigger function"); |
| 1368 | assign_simple_var(estate, var, |
| 1369 | DirectFunctionCall1(namein, |
| 1370 | CStringGetDatum(estate->trigdata->tg_trigger->tgname)), |
| 1371 | false, true); |
| 1372 | break; |
| 1373 | |
| 1374 | case PLPGSQL_PROMISE_TG_WHEN: |
| 1375 | if (estate->trigdata == NULL) |
| 1376 | elog(ERROR, "trigger promise is not in a trigger function"); |
| 1377 | if (TRIGGER_FIRED_BEFORE(estate->trigdata->tg_event)) |
| 1378 | assign_text_var(estate, var, "BEFORE"); |
| 1379 | else if (TRIGGER_FIRED_AFTER(estate->trigdata->tg_event)) |
| 1380 | assign_text_var(estate, var, "AFTER"); |
| 1381 | else if (TRIGGER_FIRED_INSTEAD(estate->trigdata->tg_event)) |
| 1382 | assign_text_var(estate, var, "INSTEAD OF"); |
| 1383 | else |
| 1384 | elog(ERROR, "unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF"); |
| 1385 | break; |
| 1386 | |
| 1387 | case PLPGSQL_PROMISE_TG_LEVEL: |
| 1388 | if (estate->trigdata == NULL) |
| 1389 | elog(ERROR, "trigger promise is not in a trigger function"); |
| 1390 | if (TRIGGER_FIRED_FOR_ROW(estate->trigdata->tg_event)) |
| 1391 | assign_text_var(estate, var, "ROW"); |
| 1392 | else if (TRIGGER_FIRED_FOR_STATEMENT(estate->trigdata->tg_event)) |
| 1393 | assign_text_var(estate, var, "STATEMENT"); |
| 1394 | else |
| 1395 | elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT"); |
| 1396 | break; |
| 1397 | |
| 1398 | case PLPGSQL_PROMISE_TG_OP: |
| 1399 | if (estate->trigdata == NULL) |
| 1400 | elog(ERROR, "trigger promise is not in a trigger function"); |
| 1401 | if (TRIGGER_FIRED_BY_INSERT(estate->trigdata->tg_event)) |
| 1402 | assign_text_var(estate, var, "INSERT"); |
no test coverage detected