lwgeom_triggers.c 3.7 KB
Newer Older
1 2 3
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
4
 * http://postgis.net
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * PostGIS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PostGIS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19 20 21
 **********************************************************************
 *
 * Copyright (C) 2004 Refractions Research Inc.
22 23 24
 *
 **********************************************************************/

25

26 27 28
#include "postgres.h"
#include "executor/spi.h"       /* this is what you need to work with SPI */
#include "commands/trigger.h"   /* ... and triggers */
29 30

#include "../postgis_config.h"
31
#include "lwgeom_pg.h"
32
#include "utils/rel.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

Datum cache_bbox(PG_FUNCTION_ARGS);

/** @file
 * 	The intended use for this trigger function is making
 * 	a geometry field cache it's bbox. Use like this:
 *
 * 	CREATE TRIGGER <name> BEFORE INSERT OR UPDATE
 *		ON <table> FOR EACH ROW EXECUTE PROCEDURE
 *		cache_bbox(<field>);
 *
 */
PG_FUNCTION_INFO_V1(cache_bbox);
Datum cache_bbox(PG_FUNCTION_ARGS)
{
	TriggerData *trigdata = (TriggerData *) fcinfo->context;
	Trigger *trigger;
	TupleDesc   tupdesc;
	HeapTuple   rettuple;
	bool        isnull;
	Datum in, out;
	int attno, ret;

	/* make sure it's called as a trigger at all */
	if (!CALLED_AS_TRIGGER(fcinfo))
		elog(ERROR, "cache_bbox: not called by trigger manager");

	/*
	 * make sure it's called with at least one argument
	 * (the geometry fields)
	 */
	if ( trigdata->tg_trigger->tgnargs != 1 )
		elog(ERROR, "trigger 'cache_bbox' must be called with one argument");

	trigger = trigdata->tg_trigger;

	/* tuple to return to executor */
	if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
		rettuple = trigdata->tg_newtuple;
	else
		rettuple = trigdata->tg_trigtuple;

	/* Do nothing when fired by delete, after or for statement */
	if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
	{
		elog(NOTICE, "Useless cache_box trigger fired by DELETE");
		return PointerGetDatum(rettuple);
	}
	if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
	{
		elog(NOTICE, "Useless cache_box trigger fired AFTER");
		return PointerGetDatum(rettuple);
	}
	if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
	{
		elog(NOTICE, "Useless cache_box trigger fired for STATEMENT");
		return PointerGetDatum(rettuple);
	}

	tupdesc = trigdata->tg_relation->rd_att;

	/* Connect to SPI manager */
	if ((ret = SPI_connect()) < 0)
		elog(ERROR, "cache_bbox: SPI_connect returned %d", ret);

	/* Find number of requested argument */
	attno = SPI_fnumber(tupdesc, trigger->tgargs[0]);
	if ( attno == SPI_ERROR_NOATTRIBUTE )
		elog(ERROR, "trigger %s can't find attribute %s",
		     trigger->tgname, trigger->tgargs[0]);

	/* Find number of requested argument */
	if ( strcmp(SPI_gettype(tupdesc, attno), "geometry") )
		elog(ERROR, "trigger %s requested to apply to a non-geometry field (%s)", trigger->tgname, trigger->tgargs[0]);

	/* Get input lwgeom */
	in = SPI_getbinval(rettuple, tupdesc, attno, &isnull);

	if ( ! isnull )
	{
		out = PointerGetDatum(DirectFunctionCall1(LWGEOM_addBBOX, in));

		rettuple = SPI_modifytuple(trigdata->tg_relation, rettuple,
		                           1, &attno, &out, NULL);
	}

	/* Disconnect from SPI */
	SPI_finish();

	return PointerGetDatum(rettuple);
}