
CREATE OR REPLACE FUNCTION  getgrounds(INTEGER,INTEGER,INTEGER,INTEGER,INTEGER) RETURNS RECORD AS '
DECLARE cases RECORD;
DECLARE casesAndUsers RECORD;
DECLARE guilde RECORD

BEGIN
	SELECT INTO cases ground_pos[0] AS x, ground_pos[1] AS y, user_id, type_id, ground_build, deco_id FROM grounds WHERE ground_pos[0]>=$1 AND ground_pos[0]<$2 AND ground_pos[1]>=$3 AND ground_pos[1]<$4;

	SELECT INTO casesAndUsers x, y, user_id, user_guild_emblem, user_guild_master_id, type_id, deco_id, ground_build FROM cases JOIN users u ON u.user_id=cases.user_id

	FOR ligne IN casesAndUsers 

	LOOP
		IF (ligne.user_guild_master_id = ligne.user_id) THEN
		ELSE
			SELECT INTO guilde user_guild_emblem, user_guild_master_id FROM users WHERE users.user_id=ligne.user_guild_master_id;
			ligne.user_guild_emblem = guilde.user_guild_emblem;
		END IF;
	END LOOP;

	RETURN casesAndUsers;
END ;'
LANGUAGE 'plpgsql' ;







DECLARE
rec record;
curs refcursor;

BEGIN

OPEN curs for SELECT ground_pos[0], ground_pos[1] FROM grounds WHERE ground_pos[0]>=$1 AND ground_pos[0]<$2 AND ground_pos[1]>=$3 AND ground_pos[1]<$4;
loop
FETCH curs INTO rec;
EXIT WHEN NOT FOUND;

END LOOP;
CLOSE curs;

RETURN rec;

END;





getgrounds 	integer, integer, integer, integer, integer 	setof grounds 	plpgsql
	

DECLARE terrains grounds;

BEGIN
	SELECT INTO terrains * FROM grounds WHERE ground_pos[0]>=$1 AND ground_pos[0]<$2 AND ground_pos[1]>=$3 AND ground_pos[1]<$4;

	RETURN terrains;
END;



SELECT * FROM getgrounds(-4,4,-4,4,1)