-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
165 lines (149 loc) · 3.76 KB
/
queries.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Calcualtes average rating for an image with imageId @id.
delimiter //
create function averageForImageId(id integer unsigned)
returns double
begin
declare r double default 0;
select avg(rating) into r
from PictureRating
where imageId = id;
return r;
end //
delimiter ;
# Get path from imageId @id
delimiter //
create function imagePathForId(id integer unsigned)
returns varchar(256)
begin
declare path varchar(256) default NULL;
select imagePath into path
from Picture
where ImageId = id;
return path;
end //
delimiter ;
# Trigger to add a notification to user when someone tags him in some picture.
delimiter //
create trigger notifyUser
after insert on InPicture
for each row
begin
declare path varchar(256);
declare usr varchar(80);
select imagePath, username into path, usr
from InPicture join Picture
on Picture.imageId = InPicture.imageId
join User on Picture.userId = User.userId
where InPicture.imageId = new.imageId;
insert into Notification (userId, message, notificationDate)
values (new.userId, concat(usr, " has tagged you in: ", path), now());
end //
delimiter ;
# Gets all Pictures for a specific event with eventId @id.
delimiter //
create procedure picturesForEventId(id integer unsigned)
begin
select imageId
from Picture
where eventId is not null and eventId = id;
end //
delimiter ;
# Gets all comments for an image.
delimiter //
create procedure commentsForImageId(id integer unsigned)
begin
select commentId, commentText
from PictureComment
where imageId = id;
end //
delimiter ;
# Gets all pictures of a userid @id.
delimiter //
create procedure picturesOfUserWithId(id integer unsigned)
begin
select imagePath
from Picture
where userId = id;
end //
delimiter ;
# Gets all pictures with tag @tag.
delimiter //
create procedure picturesWithTagName(tag varchar(35))
begin
select imagePath
from Picture natural join PictureTag
where tagName = tag;
end //
delimiter ;
# Gets all pictures with location @geo
delimiter //
create procedure picturesWithLocation(geo varchar(256))
begin
select imagePath
from Picture natural join Location
where geolocalization = geo;
end //
delimiter ;
# Gets photographers that have submitted pictures for some event with eventId @id.
delimiter //
create procedure photographersForEventId(id integer unsigned)
begin
select userId
from Picture
where eventId = id;
end //
delimiter ;
# Gets all pictures where a user with userId is tagged in
delimiter //
create procedure picturesWhereUserWithIdIsTaggedIn(id integer unsigned)
begin
select imagePath
from Picture natural join InPicture
where InPicture.userId = id;
end //
delimiter ;
# Gets all people tagged in some picture.
delimiter //
create procedure peopleTaggedInPictureWith(id integer unsigned)
begin
select username, firstName, lastName
from InPicture natural join User
where InPicture.imageId = id;
end //
delimiter ;
# Gets all pictures taken with device @dev.
delimiter //
create procedure picturesTakenWithDevice(dev varchar(80))
begin
select imageId
from Picture
where device = dev;
end //
delimiter ;
# Get all pictures with seen bigger than or equal to @minSeen.
delimiter //
create procedure picturesWithNumberOfSeenBiggerThanOrEquall(minSeen integer unsigned)
begin
select imageId
from Picture
Where numberSeen >= minSeen;
end //
delimiter ;
# Get all picture with size less than @sz.
delimiter //
create procedure picturesWithSizeLessThanOrEquallTo(sz double)
begin
select imageId
from Picture
where size <= sz;
end //
delimiter ;
# Get all pictures in time interval [@earliest, @latest].
delimiter //
create procedure picturesInTimeInterval(earliest datetime, latest datetime)
begin
select imageId
from Picture
where createTime between earliest and latest;
end //
delimiter ;