URX_PUBLIC._add_raster_constraint_extent(rastschema name, rasttable name, rastcolumn name) - Procedure
Home|Tables|Schemas|Diagrams|Foreign Keys|Indexes

NameURX_PUBLIC._add_raster_constraint_extent(rastschema name, rasttable name, rastcolumn name)
Comment
Src
CREATE FUNCTION "URX_PUBLIC"."_add_raster_constraint_extent"(rastschema name, rasttable name, rastcolumn name) 
 RETURNS boolean AS 
$BODY$
	DECLARE
		fqtn text;
		cn name;
		sql text;
		attr text; srid integer;
	BEGIN
		fqtn := '';
		IF length($1) > 0 THEN
			fqtn := quote_ident($1) || '.';
		END IF;
		fqtn := fqtn || quote_ident($2);

		sql := 'SELECT "URX_PUBLIC".ST_SRID('
            || quote_ident($3)
      || ') FROM '
            || fqtn
            || ' LIMIT 1;';
    EXECUTE sql INTO srid;

		cn := 'enforce_max_extent_' || $3;

		sql := 'SELECT "URX_PUBLIC".st_ashexewkb( "URX_PUBLIC".st_setsrid( "URX_PUBLIC".st_extent( "URX_PUBLIC".st_envelope('
			|| quote_ident($3)
			|| ')), ' || srid || ')) FROM '
			|| fqtn;
		EXECUTE sql INTO attr;

		-- NOTE: I put NOT VALID to prevent the costly step of validating the constraint
		sql := 'ALTER TABLE ' || fqtn
			|| ' ADD CONSTRAINT ' || quote_ident(cn)
			|| ' CHECK ( "URX_PUBLIC".st_envelope('
			|| quote_ident($3)
			|| ') @ ''' || attr || '''::geometry) NOT VALID';
		RETURN  "URX_PUBLIC"._add_raster_constraint(cn, sql);
	END;
	$BODY$
 LANGUAGE 'plpgsql' VOLATILESTRICT ;