1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| drop database if exists messagerie;
create database messagerie default character set utf8 collate utf8_general_ci;
use messagerie;
create table message(
id int(4) not null auto_increment,
expediteur_id int(4),
destinataire_id int(4),
parent_id int(4),
sujet varchar(255) not null,
message text not null,
`date` datetime not null,
vu bool not null default false,
efface bool not null default false,
constraint pk_message primary key(id),
key(expediteur_id,destinataire_id,parent_id)
)engine=myisam auto_increment=1;
insert into message(expediteur_id,destinataire_id,parent_id,sujet,message,`date`)values
(1,2,null,"coucou","ça va?","2011-04-18 10:00:00"),
(2,1,1,"re:coucou","oui et toi","2011-04-18 12:00:00"),
(3,1,2,"re:re:coucou","moi aussi","2011-04-18 14:00:00"),
(3,2,null,"rdv","on peut se voir quand?","2011-04-18 10:00:00");
delimiter |
create procedure last_response(in id_message int(4),out l_id int(4))
begin
declare test bool;
declare id_m int(4);
select count(id)>0,id into test,id_m from message where parent_id=id_message;
if test then
call last_response(id_m,l_id);
else
set l_id=id_message;
end if;
end|
create procedure list_messages()
begin
declare d datetime;
declare done bool default false;
declare i,j int(4);
declare s varchar(255);
declare lit cursor for select id,sujet,`date` from message where isnull(parent_id);
declare CONTINUE HANDLER for SQLSTATE '02000' set done = 1;
set @@session.max_sp_recursion_depth=255;
open lit;
repeat
fetch lit into i,s,d;
if not done then
call last_response(i,j);
if i=j then
select i as "id",s as "sujet",d as "date";
else
select i as "id",s as "sujet",d as "date",j as "rep id",m.sujet as "rep sujet",m.`date` as "rep date"
from message m where m.id=j;
end if;
end if;
until done end repeat;
close lit;
end|
delimiter ;
call list_messages(); |
Partager