(associés, mais distincte de Syntax error with emulating "create user if not exists".)Comment créer génériquement/dynamique/utilisateurs drop
Est-il possible d'obtenir la fonctionnalité de génériquement/ajoutant dynamiquement un utilisateur (par exemple émulant la procédure système sp_adduser
inclus avec d'autres SGBD) en MySQL?
MySQL ne supporte pas la syntaxe suivante if [not] exists
, voir http://bugs.mysql.com/bug.php?id=15287:
create user if not exists 'foo'@'%' identified by password 'bar';
Il ne supporte pas:
drop procedure if exists create_user_if_not_exists;
delimiter ||
create procedure create_user_if_not_exists
(sUser varchar(60),
sHost varchar(16),
sPassword varchar(255))
begin
-- ensure user does not yet exist
if (select ifnull((select 1
from mysql.user
where User = sUser
and Host = sHost), 0) = 0) then
set @createUserText = concat('create user ''', sUser, '''@''', sHost, ''' identified by ''', sPassword, ''';');
prepare createUserStatement FROM @createUserText;
execute createUserStatement;
deallocate prepare createUserStatement;
end if;
end ||
delimiter ;
parce que si vous essayez d'appeler cette procédure:
call create_user_if_not_exists ('foo', '%', 'bar');
vous obtenez le beau message:
This command is not supported in the prepared statement protocol yet
Les travaux suivants, mais de toute évidence pas particulièrement réutilisable:
drop procedure if exists create_user_if_not_exists;
delimiter ||
create procedure create_user_if_not_exists
()
begin
if (select ifnull((select 1
from mysql.user
where User = 'foo'
and Host = '%'), 0) = 0) then
create user 'foo'@'%' identified by password 'bar';
end if;
end ||
delimiter ;
Oui, c'est ce que j'ai fini par faire. Ne se sent pas particulièrement propre si ... –