Src |
CREATE FUNCTION "URX_PUBLIC"."_add_raster_constraint_out_db"(rastschema name, rasttable name, rastcolumn name)
RETURNS boolean AS
$BODY$
DECLARE
fqtn text;
cn name;
sql text;
attr boolean[];
max int;
BEGIN
fqtn := '';
IF length($1) > 0 THEN
fqtn := quote_ident($1) || '.';
END IF;
fqtn := fqtn || quote_ident($2);
cn := 'enforce_out_db_' || $3;
sql := 'SELECT "URX_PUBLIC"._raster_constraint_out_db(' || quote_ident($3)
|| ') FROM ' || fqtn
|| ' LIMIT 1';
BEGIN
EXECUTE sql INTO attr;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Unable to get the out-of-database bands of a sample raster: % (%)',
SQLERRM, SQLSTATE;
RETURN FALSE;
END;
max := array_length(attr, 1);
IF max < 1 OR max IS NULL THEN
RAISE NOTICE 'Unable to get the out-of-database bands of a sample raster (max < 1 or null)';
RETURN FALSE;
END IF;
sql := 'ALTER TABLE ' || fqtn
|| ' ADD CONSTRAINT ' || quote_ident(cn)
|| ' CHECK ( "URX_PUBLIC"._raster_constraint_out_db(' || quote_ident($3)
|| ') = ''{';
FOR x in 1..max LOOP
IF attr[x] IS FALSE THEN
sql := sql || 'FALSE';
ELSE
sql := sql || 'TRUE';
END IF;
IF x < max THEN
sql := sql || ',';
END IF;
END LOOP;
sql := sql || '}''::boolean[])';
RETURN "URX_PUBLIC"._add_raster_constraint(cn, sql);
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILESTRICT ;
|