| 2572 | } |
| 2573 | |
| 2574 | static Datum |
| 2575 | tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column) |
| 2576 | { |
| 2577 | TriggerData *trigdata; |
| 2578 | Trigger *trigger; |
| 2579 | Relation rel; |
| 2580 | HeapTuple rettuple = NULL; |
| 2581 | int tsvector_attr_num, |
| 2582 | i; |
| 2583 | ParsedText prs; |
| 2584 | Datum datum; |
| 2585 | bool isnull; |
| 2586 | text *txt; |
| 2587 | Oid cfgId; |
| 2588 | bool update_needed; |
| 2589 | |
| 2590 | /* Check call context */ |
| 2591 | if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */ |
| 2592 | elog(ERROR, "tsvector_update_trigger: not fired by trigger manager"); |
| 2593 | |
| 2594 | trigdata = (TriggerData *) fcinfo->context; |
| 2595 | if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) |
| 2596 | elog(ERROR, "tsvector_update_trigger: must be fired for row"); |
| 2597 | if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event)) |
| 2598 | elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event"); |
| 2599 | |
| 2600 | if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) |
| 2601 | { |
| 2602 | rettuple = trigdata->tg_trigtuple; |
| 2603 | update_needed = true; |
| 2604 | } |
| 2605 | else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) |
| 2606 | { |
| 2607 | rettuple = trigdata->tg_newtuple; |
| 2608 | update_needed = false; /* computed below */ |
| 2609 | } |
| 2610 | else |
| 2611 | elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE"); |
| 2612 | |
| 2613 | trigger = trigdata->tg_trigger; |
| 2614 | rel = trigdata->tg_relation; |
| 2615 | |
| 2616 | if (trigger->tgnargs < 3) |
| 2617 | elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)"); |
| 2618 | |
| 2619 | /* Find the target tsvector column */ |
| 2620 | tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]); |
| 2621 | if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE) |
| 2622 | ereport(ERROR, |
| 2623 | (errcode(ERRCODE_UNDEFINED_COLUMN), |
| 2624 | errmsg("tsvector column \"%s\" does not exist", |
| 2625 | trigger->tgargs[0]))); |
| 2626 | /* This will effectively reject system columns, so no separate test: */ |
| 2627 | if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num), |
| 2628 | TSVECTOROID)) |
| 2629 | ereport(ERROR, |
| 2630 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
| 2631 | errmsg("column \"%s\" is not of tsvector type", |
no test coverage detected