DROP PROCEDURE IF EXISTS RegisterPerson;
DELIMITER //
CREATE PROCEDURE RegisterPerson
(
  IN p_pseudo TEXT,
  IN p_password TEXT,
  OUT outputCode INT
)
MODIFIES SQL DATA
BEGIN
SET outputCode :=(
	SELECT
	(
		CASE 
			WHEN LENGTH(p_pseudo) > 255 THEN 2002
			WHEN CAST(p_pseudo AS BINARY) NOT REGEXP BINARY '^[-a-zA-Z0-9\_]+$' THEN 2003
			WHEN(
				SELECT
					COUNT(*)
				FROM
					membres
				WHERE
					pseudo = p_pseudo
			) > 0 THEN 2004
			ELSE 42
		END
	)
);
IF outputCode = 42 THEN INSERT INTO membres(pseudo,hashedPassword,dateCreation) VALUES (CAST(p_pseudo AS CHAR(255)),MD5(p_password),NOW()); END IF;

END//
DELIMITER ;

