-
Notifications
You must be signed in to change notification settings - Fork 0
/
sWeakActorZoneMove.sql
105 lines (90 loc) · 2.69 KB
/
sWeakActorZoneMove.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
-- SetupConfig: {}
create procedure CK.sWeakActorZoneMove
(
@ActorId int,
@WeakActorId int,
@NewZoneId int,
@Option int, -- not null enum { "None": 0, "Intersect": 1 }
@NewWeakActorName nvarchar(255) = null
)
as
begin
if @ActorId <= 0 throw 50000, 'Security.AnonymousNotAllowed', 1;
if @NewZoneId < 0 throw 50000, 'Zone.InvalidId', 1;
--[beginsp]
declare @CurrentZoneId int;
select @CurrentZoneId = w.ZoneId
from CK.tWeakActor w
where w.WeakActorId = @WeakActorId;
if (@CurrentZoneId is null)
begin
;throw 50000, 'WeakActor.InvalidWeakActor', 1;
end
--<PreWeakActorCheck revert />
if @Option = 0 -- None
begin
--<AnyGroupConflicting>
if exists
(
select GroupId
from CK.tActorProfile ap
where ap.ActorId = @WeakActorId
and ap.GroupId != @WeakActorId
and ap.GroupId != @CurrentZoneId
)
--</AnyGroupConflicting>
begin
;throw 50000, 'WeakActor.IsInAGroup', 1
end
end
else if @Option = 1 -- Intersect
begin
declare @GroupId int;
declare @GroupMatcher cursor;
set @GroupMatcher = cursor local fast_forward for
--<GroupsToRemove>
select GroupId
from CK.tActorProfile ap
where ap.ActorId = @WeakActorId
and ap.GroupId != @WeakActorId
and ap.GroupId != @CurrentZoneId;
--</GroupsToRemove>
open @GroupMatcher;
fetch from @GroupMatcher into @GroupId;
while @@FETCH_STATUS = 0
begin
exec CK.sGroupUserRemove @ActorId, @GroupId, @WeakActorId;
fetch next from @GroupMatcher into @GroupId;
end
deallocate @GroupMatcher;
end
else
begin
;throw 50000, 'ArgumentNotSupported', 1;
end
--<PostWeakActorCheck />
--<PreWeakActorMove revert />
-- With only Zone, no HZone, we can rely on the table constraint.
-- Here it will throw if WeakActorName is not unique within the Zone.
if (@NewWeakActorName is null)
begin
--<PreWeakActorUpdateWithoutNewWeakActorName />
update CK.tWeakActor
set ZoneId = @NewZoneId
where WeakActorId = @WeakActorId;
end
else
begin
--<PreWeakActorUpdateWithNewWeakActorName />
update CK.tWeakActor
set ZoneId = @NewZoneId,
WeakActorName = @NewWeakActorName
where WeakActorId = @WeakActorId;
end
update CK.tActorProfile
set GroupId = @NewZoneId
where ActorId = @WeakActorId
and GroupId = @CurrentZoneId;
--<PostWeakActorMove />
--[endsp]
end