new grid adapt file, still not tested

parent a92a1cb4
DELIMITER $$
DROP PROCEDURE IF EXISTS newGridSystem $$
CREATE PROCEDURE newGridSystem()
BEGIN
DECLARE _id_stu_picto INT;
DECLARE _id_stu INT;
DECLARE _attributes JSON;
DECLARE _free_category_coord_x INT;
DECLARE _free_category_coord_y INT;
DECLARE _id_cat INT;
DECLARE _grid_name VARCHAR(100);
DECLARE _grid_instances INT;
DECLARE _grid_id INT;
DECLARE _id_pic INT;
DECLARE _id_parent_picto INT;
DECLARE LID INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_stu_picto CURSOR FOR SELECT id, id_stu, attributes FROM pictodb.stu_picto;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_stu_picto;
read_loop: LOOP
FETCH cursor_stu_picto INTO _id_stu_picto, _id_stu, _attributes;
IF done THEN
LEAVE read_loop;
END IF;
SELECT _attributes->>"$.free_category_coord_x" INTO _free_category_coord_x;
SELECT _attributes->>"$.free_category_coord_y" INTO _free_category_coord_y;
-- Esto hay que hacerlo porque el null procediente del JSON para id_cat lo reconoce como un literal...
IF _attributes->>"$.id_cat" = 'null' THEN
SET _id_cat = NULL;
ELSE
SELECT _attributes->>"$.id_cat" INTO _id_cat;
END IF;
-- Pictogramas que estan en escenas sin categorias
IF (_free_category_coord_x IS NOT NULL) OR (_free_category_coord_y IS NOT NULL) THEN
-- Pasa las coordenadas libres a coord_x y coord_y
UPDATE stu_picto
SET
`attributes` = JSON_REPLACE(`attributes`, '$.coord_x', _free_category_coord_x),
`attributes` = JSON_REPLACE(`attributes`, '$.coord_y', _free_category_coord_y)
WHERE id = _id_stu_picto;
-- Pictogramas que estan en escenas con categorias
ELSE
-- Pictogramas que ya existen en la escena principal y ademas tienen bien las coordenadas, no se tocan
-- Es decir, las free_category_coord y id_cat son NULL
-- Pictogramas que residen en escenas hijas
IF (_id_cat IS NOT NULL) THEN
-- Obtengo el nombre del pictograma asociado (que sera el nombre de la nueva escena)
SELECT `text` FROM picto_exp WHERE id_pic = _id_cat INTO _grid_name;
-- Verifico si existe una escena con dicho nombre para el alumno actual
SELECT COUNT(*) FROM grid WHERE name = _grid_name AND id_stu = _id_stu INTO _grid_instances;
-- La escena ya existe, asocio el pictograma a dicha escena
IF (_grid_instances = 1) THEN
SELECT id_grid FROM grid WHERE name = _grid_name AND id_stu = _id_stu LIMIT 1 INTO _grid_id;
UPDATE stu_picto
SET id_grid = _grid_id
WHERE id = _id_stu_picto;
END IF;
-- La escena no existe, la creo, la asocio con el picto padre y asocio el pictograma actual a dicha escena
IF (_grid_instances = 0) THEN
-- Inserto nueva escena
INSERT INTO grid (name,id_stu) VALUES (_grid_name, _id_stu);
SET LID = LAST_INSERT_ID();
-- Esta nueva escena es siempre hija de algun pictograma, lo busco para asociarlo a dicha escena
SELECT id_pic FROM picto_exp WHERE `text` = _grid_name INTO _id_pic;
SELECT id FROM stu_picto WHERE id_pic = _id_pic AND id_stu = _id_stu AND id_child_grid IS NULL INTO _id_parent_picto;
-- Actualizo id_child_grid para el pictograma padre
UPDATE stu_picto
SET id_child_grid = LID
WHERE id = _id_parent_picto;
-- Actualizo el id_grid al que pertenece el picto
UPDATE stu_picto
SET id_grid = LID
WHERE id = _id_stu_picto;
END IF;
-- Se pone id_cat a NULL, puesto que dicho valor no lo volveremos a necesitar
UPDATE stu_picto
SET
`attributes` = JSON_REPLACE(`attributes`, '$.id_cat', NULL)
WHERE id = _id_stu_picto;
END IF;
END IF;
END LOOP;
CLOSE cursor_stu_picto;
END $$
DELIMITER ;
CALL newGridSystem();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment