/**
 * Checks whether a passed extension number has valid sintax
 *
 * @param v_extension The extension to check format
 * @return %TRUE if it's well formated
 **/
USE pbx;
DROP PROCEDURE IF EXISTS extension_is_valid;
DELIMITER $$
CREATE PROCEDURE extension_is_valid (v_extension VARCHAR(255))
BEGIN
	DECLARE v_is_valid BOOLEAN;

	SET v_is_valid =  v_extension IS NULL
		OR (v_extension REGEXP '^[0-9]{4}$'
		AND MOD(v_extension, 100) != 0);

	IF NOT v_is_valid
	THEN
		SIGNAL SQLSTATE '45000'
			SET MESSAGE_TEXT = 'EXTENSION_INVALID_FORMAT';
	END IF;
END$$
DELIMITER ;
