Voila un exposé complet:
Les tables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
CREATE TABLE pub_forfait (
id_pub_forfait SERIAL PRIMARY KEY,
id_pub INTEGER NOT NULL REFERENCES pub ON DELETE CASCADE,
...
nb_pub_vu integer CHECK( nb_pub_vu >= 0 ) NOT NULL,
nb_pub integer CHECK( nb_pub >= 0 ) NOT NULL,
fini boolean NOT NULL
);
CREATE TABLE pub_vu_non_log (
id_pub_vu_non_log SERIAL PRIMARY KEY,
id_pub_forfait INTEGER NOT NULL REFERENCES pub_forfait ON DELETE CASCADE,
...
);
CREATE TABLE pub_vu (
id_pub_vu SERIAL PRIMARY KEY,
id_pub_forfait INTEGER NOT NULL REFERENCES pub_forfait ON DELETE CASCADE,
...
); |
exemple nouvelle pub vu non loggué:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
BEGIN;
INSERT INTO pub_vu_non_log (
id_pub_vu_non_log,
id_pub_forfait,...
)
VALUES(
nextval('pub_vu_non_log_id_pub_vu_non_log_seq'), 'XX' ,...);
UPDATE pub_forfait SET nb_pub_vu = (nb_pub_vu + 1) WHERE id_pub_forfait = 'XX';
-- je sais que je pourrais compter les enr dans les pub_vu à la place
SELECT id_pub_forfait FROM pub_forfait
WHERE id_pub_forfait = 'XX'
AND nb_pub_vu >= nb_pub;
-- if nb row == 1
UPDATE pub_forfait SET fini = true WHERE id_pub_forfait = 'XX';
--if pas d'erreur
COMMIT;
--else
ROLLBACK; |
J'imagine qu'il doit y avoir des manière plus élégante de le faire si vous avez des suggestions... cependant ma question n'est pas là.
mon problème au final:
1 2 3
| SELECT * FROM pub_vu_non_log WHERE id_pub_forfait = 'XX';
-- Additionné a
SELECT * FROM pub_vu WHERE id_pub_forfait = 'XX'; |
est différent de :
SELECT nb_pub_vu FROM pub_forfait WHERE id_pub_forfait = 'XX';
Normalement la transaction devrait garder la cohérence que ca commit ou pas?
Merci de votre aide.
Partager