-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sql
79 lines (59 loc) · 1.83 KB
/
init.sql
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
DELIMITER //
CREATE DATABASE IF NOT EXISTS images;
USE images;
DROP PROCEDURE IF EXISTS dragInFrontOfOtherImage;
DROP TABLE IF EXISTS image;
DROP TABLE IF EXISTS tag;
DROP TABLE IF EXISTS tagmap;
CREATE TABLE `image` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(20),
`description` varchar(100),
`uuid` char(36) NOT NULL,
`type` varchar(10) NOT NULL,
`time_created` datetime,
PRIMARY KEY (`id`)
);
CREATE TABLE `tag` (
`tag_id` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(20) NOT NULL,
PRIMARY KEY (`tag_id`)
);
CREATE TABLE `tagmap` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
`pos` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE PROCEDURE dragInFrontOfOtherImage(
IN idShip INT,
IN idDestination INT,
IN tagname VARCHAR(100)
)
BEGIN
SET @posShip=0;
SET @posDestination=0;
Set @tagId=0;
SELECT @tagId:=t.tag_id FROM tag t WHERE t.tname=tagname;
SELECT @posShip:=tm.pos FROM tagmap tm WHERE tm.tag_id=@tagId AND tm.image_id=idShip;
SELECT @posDestination:=tm.pos FROM tagmap tm WHERE tm.tag_id=@tagId AND tm.image_id=idDestination;
IF @posShip < @posDestination #if image in front of destination image
THEN
UPDATE tagmap #make space by moving images in between up
SET pos=pos-1
WHERE id>0 AND tag_id=@tagId AND pos>@posShip AND pos<@posDestination;
UPDATE tagmap
SET pos=(@posDestination-1)
WHERE id>0 AND tag_id=@tagId AND image_id=idShip;
ELSEIF @posShip > @posDestination #if image behind destination image
THEN
UPDATE tagmap #make space by moving images in between down
SET pos=pos+1
WHERE id>0 AND tag_id=@tagId AND pos>=@posDestination AND pos<@posShip;
UPDATE tagmap
SET pos=@posDestination
WHERE id>0 AND tag_id=@tagId AND image_id=idShip;
END IF;
END//
DELIMITER ;