File tree Expand file tree Collapse file tree 3 files changed +50
-9
lines changed
Expand file tree Collapse file tree 3 files changed +50
-9
lines changed Original file line number Diff line number Diff line change 1+ -- Use the database
2+ USE smartcooking;
3+
4+ DELIMITER //
5+
6+ CREATE OR REPLACE FUNCTION person_exists (
7+ IN p_person_id INT ,
8+ IN p_email VARCHAR (100 )
9+ ) RETURNS BOOLEAN
10+ BEGIN
11+ DECLARE v_exists BOOLEAN ;
12+
13+ SELECT EXISTS (
14+ SELECT 1
15+ FROM person
16+ WHERE (person_id = p_person_id OR email = p_email)
17+ ) INTO v_exists;
18+
19+ RETURN v_exists;
20+ END //
21+
22+ DELIMITER ;
Original file line number Diff line number Diff line change @@ -44,22 +44,41 @@ BEGIN
4444END //
4545
4646CREATE OR REPLACE PROCEDURE login_person(
47+ IN p_person_id INT ,
4748 IN p_email VARCHAR (100 )
4849)
4950BEGIN
51+ DECLARE v_person_id INT ;
52+ DECLARE v_hashed_password VARCHAR (100 );
53+ DECLARE v_salt BINARY(16 );
54+
5055 -- Check if the person exists
51- IF NOT EXISTS (
52- SELECT 1
53- FROM person
54- WHERE email = p_email
55- ) THEN
56+ IF NOT person_exists(p_person_id, p_email) THEN
5657 SIGNAL SQLSTATE ' 45000' SET MESSAGE_TEXT = ' User not found' ;
5758 END IF;
5859
59- -- Retrieve the hashed password and salt as a result set
60+ -- Retrieve the person_id, hashed password, and salt
6061 SELECT person_id, hashed_password, salt
62+ INTO v_person_id, v_hashed_password, v_salt
6163 FROM person
62- WHERE email = p_email;
64+ WHERE (person_id = p_person_id OR email = p_email);
65+
66+ -- Return the result set
67+ SELECT v_person_id AS person_id, v_hashed_password AS hashed_password, v_salt AS salt;
68+ END //
69+
70+ CREATE OR REPLACE PROCEDURE login_person_by_id(
71+ IN p_person_id INT
72+ )
73+ BEGIN
74+ CALL login_person(p_person_id, NULL );
75+ END //
76+
77+ CREATE OR REPLACE PROCEDURE login_person_by_email(
78+ IN p_email VARCHAR (100 )
79+ )
80+ BEGIN
81+ CALL login_person(NULL , p_email);
6382END //
6483
6584DELIMITER ;
Original file line number Diff line number Diff line change @@ -33,11 +33,11 @@ BEGIN
3333 FROM person;
3434
3535 IF name_exists > 0 THEN
36- SIGNAL SQLSTATE ' 45000' SET MESSAGE_TEXT = ' User name already exists for another record ' ;
36+ SIGNAL SQLSTATE ' 45000' SET MESSAGE_TEXT = ' User name already exists' ;
3737 END IF;
3838
3939 IF email_exists > 0 THEN
40- SIGNAL SQLSTATE ' 45000' SET MESSAGE_TEXT = ' Email already exists for another record ' ;
40+ SIGNAL SQLSTATE ' 45000' SET MESSAGE_TEXT = ' Email already exists' ;
4141 END IF;
4242
4343 -- Retrieve the language ID if provided
You can’t perform that action at this time.
0 commit comments