-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocedures.sql
418 lines (374 loc) · 10.7 KB
/
procedures.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use BookFace
go
----------------------------------------------------------------
--Procedure to import data from a csv file
create or alter procedure usp_insert_from_csv
@path varchar(100)
as
begin
declare @bulk_exec varchar(max)
set @bulk_exec =
'bulk insert "USER"
from ''' + @path + '''
with (
format = ''CSV'',
firstrow = 2,
fieldterminator = '','',
rowterminator = ''\n''
)'
exec(@bulk_exec)
end
GO
----------------------------------------------------------------
--Trigger to create friendship relations within the new user and
--all of users that have the same lastname
create or alter trigger ut_verify_lastname
on "USER"
after insert
as
declare @userid int
declare @lastname varchar(50)
declare @cont int
select @userid = USERID, @lastname = FIRSTLASTNAME from inserted
set transaction isolation level serializable;
begin tran
select @cont = COUNT(*) from "USER"
where FIRSTLASTNAME = @lastname
if (@cont = 2)
begin
insert into FRIENDSHIP values ((select USERID from "USER" where FIRSTLASTNAME = @lastname and USERID != @userid),@userid)
end
else if (@cont > 2)
begin
drop table if exists temp
select * into temp from "USER" where FIRSTLASTNAME = @lastname and USERID != @userid
declare @id int
declare c_create_friendship cursor for
select USERID from temp
open c_create_friendship
fetch c_create_friendship into @id
while (@@FETCH_STATUS = 0)
begin
insert into FRIENDSHIP values (@id,@userid)
FETCH NEXT FROM c_create_friendship into @id
end
drop table temp
close c_create_friendship
deallocate c_create_friendship
end
commit;
GO
----------------------------------------------------------------
--Trigger to verify if a friendship already exists,
--if it does the friendship is not inserted
create or alter trigger ft_verify_friendship
on FRIENDSHIP
instead of insert
as
declare @user1 int
declare @user2 int
declare @cont int
select @user1 = USERID, @user2 = FRIENDID from inserted
set transaction isolation level serializable;
begin tran
select @cont = COUNT(*) from FRIENDSHIP
where (USERID = @user1 and FRIENDID = @user2) or (USERID = @user2 and FRIENDID = @user1)
commit
if (@cont = 0)
begin
insert into FRIENDSHIP values (@user1,@user2)
end
else
begin
print 'El usuario ' + CAST(@user1 as varchar) + ' y el usuario ' +CAST(@user2 as varchar) + ' ya son amigos.'
end
GO
----------------------------------------------------------------
--Procedure to create a random number of posts
--for a random number of random users
create or alter procedure psp_gen_random_posts
as
declare @random1 int
declare @random2 int
declare @cont1 int
declare @cont2 int
declare @total_users int
declare @userid int
declare @deviceid int
begin
select @cont1 = 0
select @cont2 = 0
set transaction isolation level serializable;
begin tran
select @total_users = COUNT(*) from "USER"
commit
select @random1 = FLOOR(RAND()*(@total_users))+1;
select @random2 = FLOOR(RAND()*(20))+1;
print @random1
print @random2
print @total_users
while @cont1 != @random1
begin
begin tran
select top 1 @userid = USERID from "USER" order by NEWID()
while @cont2 != @random2
begin
select top 1 @deviceid = DEVICEID from DEVICE order by NEWID()
insert into POST values (@userid,1,@deviceid,'255.255.255.255',GETDATE(),'Post generado aleatoriamente :)')
select @cont2 = @cont2 + 1
if @@ERROR <> 0
begin
print 'No se pudo realizar la transacción.'
rollback
end
else
begin
commit
end
end
select @cont1 = @cont1 + 1
end
end
GO
----------------------------------------------------------------
--Procedure to delete a comment
create or alter procedure csp_delete_comment
@commentid int
as
begin
delete from COMMENT where COMMENTID = @commentid
end
GO
----------------------------------------------------------------------
--Trigger to update selected queued comments to active
create or alter trigger ct_queue_comments
on COMMENT
instead of delete
as
declare @newcomment int
declare @postid int
declare @commentid int
declare @total_comments int
select @commentid = COMMENTID from deleted
if (@commentid is not NULL)
begin
set transaction isolation level serializable;
begin tran
select @postid = POSTID from COMMENT where COMMENTID = @commentid
delete from COMMENT where COMMENTID = @commentid
select top 1 @newcomment = COMMENTID from COMMENT where (ACTIVESTATUS = 0) and POSTID = @postid order by COMMENTDATETIME desc
commit
if (@newcomment is not NULL)
begin
update COMMENT set ACTIVESTATUS = 1 where COMMENTID = @newcomment
end
end
else
begin
print 'El comentario ' + CAST(@commentid as varchar) + ' no existe.'
end
GO
----------------------------------------------------------------
--trigger para validar las inserciones de las interacciones
create or alter TRIGGER Interaction_tiInteractionValidation
on INTERACTION
instead of insert
AS
DECLARE @idPost int,
@idUser int,
@isLike bit,
@interactionRegister int,
@deviceId int,
@deviceIp varchar(15),
@dateTime datetime,
@idFriend int,
@qty int,
@cantidadM int,
@cantidadN int,
@tempInteraction bit,
@tempIdInteraction int
set transaction isolation level serializable
begin tran;
SELECT @idPost = POSTID, @idUser = USERID, @isLike = ISLIKE, @deviceId = DEVICEID ,@deviceIp = DEVICEIP, @dateTime = INTERACTIONDATETIME
FROM inserted
select @idFriend = USERID
from POST
where POSTID = @idPost
select @qty = COUNT(1)
from FRIENDSHIP
where (USERID = @idUser and FRIENDID = @idFriend) or (USERID = @idFriend and FRIENDID = @idUser)
select @interactionRegister = COUNT(1)
from INTERACTION
where POSTID = @idPost and USERID = @idUser
select @cantidadM = COUNT(1)
from INTERACTION
where ISLIKE = 1 and POSTID = @idPost
select @cantidadN = COUNT(1)
from INTERACTION
where ISLIKE = 0 and POSTID = @idPost
if (@interactionRegister = 0 and @qty = 1)
begin
if(@isLike = 0)
begin
if (@cantidadN <= @cantidadM)
begin
insert into INTERACTION values (@idUser,@idPost,@deviceId,@deviceIp,@dateTime,@isLike)
end
else
begin
print 'La cantidad de me gusta no es sufienta para insertar un no me gusta'
end
end
else
begin
insert into INTERACTION values (@idUser,@idPost,@deviceId,@deviceIp,@dateTime,@isLike)
end
end
else
begin
if(@qty < 1)
begin
print 'Los usuarios no son amigos'
end
else
begin
if(@interactionRegister = 1)
begin
select @tempInteraction = ISLIKE from INTERACTION where USERID = @idUser and POSTID = @idPost
if(@tempInteraction = @isLike)
begin
print 'Ya existe una interaccion'
end
else
begin
select @tempIdInteraction = INTERACTIONID
from INTERACTION
where POSTID = @idPost and USERID = @idUser
if(@isLike = 0)
begin
set @cantidadM = @cantidadM -1
set @cantidadN = @cantidadN + 1
if (@cantidadN <= @cantidadM)
begin
update INTERACTION
set ISLIKE = @isLike, INTERACTIONDATETIME = GETDATE()
where INTERACTIONID = @tempIdInteraction
end
else
begin
print 'No se puede actualizar ya que la cantidad de no me gusta es mayor a la de me gusta'
end
end
else
begin
update INTERACTION
set ISLIKE = @isLike, INTERACTIONDATETIME = GETDATE()
where INTERACTIONID = @tempIdInteraction
end
end
end
else
begin
print 'Ya existe una interaccion'
end
end
end
commit;
GO
----------------------------------------------------------------
--trigger para verificar la insercion de los comentarios
create or alter TRIGGER Comments_tiCommentsValidation
on COMMENT
instead of insert
AS
DECLARE @idPost int,
@idUser int,
@deviceIp varchar(15),
@dateTime datetime,
@idFriend int,
@qty int,
@content varchar(200),
@deviceId int
set transaction isolation level serializable
begin tran;
SELECT @idPost = USERID, @idUser = POSTID,@deviceId = DEVICEID ,@deviceIp = DEVICEIP, @dateTime = COMMENTDATETIME, @content = COMMENTCONTENT
FROM inserted
select @idFriend = USERID
from POST
where POSTID = @idPost
select @qty = COUNT(1)
from FRIENDSHIP
where (USERID = @idUser and FRIENDID = @idFriend) or (USERID = @idFriend and FRIENDID = @idUser)
if (@qty = 1)
begin
if((select COUNT(1) from "COMMENT" where POSTID = @idPost and ACTIVESTATUS = 1) >= 3)
begin
insert into COMMENT values (@idUser,@idPost,@deviceId,@deviceIp,@dateTime,@content,0)
end
else
begin
insert into COMMENT values (@idUser,@idPost,@deviceId,@deviceIp,@dateTime,@content,1)
end
end
else
begin
print 'They are not friends'
end
commit;
GO
----------------------------------------------------------------
--Procedure to add one to max_friends of users that have posts
--with 15 likes or more in a given day
create or alter procedure pincrement_max_friends
as
declare @userid int
declare @maxfriends int
drop table if exists temp2
set transaction isolation level serializable;
begin tran
select U.USERID, U.MAXFRIENDS into temp2 from "USER" U
inner join POST P on P.USERID = U.USERID
inner join INTERACTION I on I.POSTID = P.POSTID
where (DAY(P.POSTDATETIME) = DAY(GETDATE())) and (I.ISLIKE = 1)
group by U.USERID, U.MAXFRIENDS, P.POSTID
having COUNT(P.POSTID) >= 15
commit
declare c_inc_friends cursor for
select USERID from temp2
open c_inc_friends
fetch c_inc_friends into @userid
while (@@FETCH_STATUS = 0)
begin
begin tran
select @maxfriends = MAXFRIENDS from "USER" where USERID = @userid
update "USER" set MAXFRIENDS = @maxfriends + 1
where USERID = @userid
FETCH NEXT FROM c_inc_friends into @userid
commit
end
drop table temp2
close c_inc_friends
deallocate c_inc_friends
GO
-------------------------------------------------------------------------
-- procedimiento para las inserciones de interaciones(comentario o likes)
-- si like o dislike es nulo y el contenido del comentario no, lo es inserta el comentario si es al reves inseta un like o dislile
create or alter procedure Interacton_upsinsertion
@pUserId int,
@pPostId int,
@pDeviceId int,
@pDeviceIp varchar(15),
@pIsLike bit,
@pComment varchar(200)
as
begin
if(@pIsLike is not null and @pComment is null)
begin
insert into INTERACTION values (@pUserId,@pPostId,@pDeviceId,@pDeviceIp, GETDATE(), @pIsLike)
end
else
begin
INSERT INTO COMMENT VALUES(@pPostId,@pUserId,@pDeviceId,@pDeviceIp,GETDATE(),@pComment,0)
end
end
GO