你好几个小时都对这个问题感到困惑,我相信它很容易在MSSQL中完成,但MySQL出于某种原因不接受if语句中的插入。
我的伪代码
这就是我的代码变成了:
SET @maxtries = 3;
SET @maxBlockTime = 15;
SET @username = 'test';
SET @ipAddress = '0.0.0.0';
SET @salt = 'salt';
SET @passwordHash = 'pswhsh';
SET @numOfUsernameFails = 0;
SET @relationNumber = NULL;
SET @emailConfirmed = NULL;
SET @foundStatus = 'Failed';
SELECT @relationNumber := `RelationNumber`, @emailConfirmed := `EmailConfirmed`
FROM accounts
WHERE
Username=@username AND
PasswordHash=@passwordHash AND
Salt=@salt;
SET @message := IF(ISNULL(@relationNumber), 'UnkownUsername', IF(@emailConfirmed = 0, 'NotConfirmed', 'Success'));
SET @foundStatus := IF(@message != 'Success', 'Failed', 'Succeeded');
INSERT INTO logins (`Username`, `Date`, `Status`, `IPAddress`) VALUES (@username, UTC_TIMESTAMP(), @foundStatus, @ipAddress);
-- if status is failed, check the number of times it has failed
-- if this is > @maxtries add to blocked table (sp_add_blocked)
-- if this is not, return table with message(s)
-- if status = success return table with username & relationnumber
SET @t := IF(@foundStatus = 'Failed'
, IF((SELECT COUNT(*) FROM (SELECT * FROM `logins` WHERE `Username`= @username order by `Date` DESC LIMIT 0, 3) `l` WHERE `l`.`Status` = 'Failed' AND (TIME_TO_SEC(TIMEDIFF(UTC_TIMESTAMP(), DATE)) / 60) < 15) >= 3,
'Blocked',
@message)
, @relationNumber);
SELECT IF(@t = 'Blocked',
'insert into blocked stored procedure and return blocked as message',
'return message unkown user');