0a770263c14bdd0b6d4fff1378a4958155de194d
[xonotic/xonstatdb.git] / functions / merge_players.sql
1 ´╗┐create or replace function merge_players(p_winner_player_id players.player_id%TYPE, p_loser_player_id players.player_id%TYPE) RETURNS void as
2 $$
3 declare
4    rowcount integer;   
5 begin
6    -- start with weapon stats
7    update player_weapon_stats
8    set player_id = p_winner_player_id
9    where player_id = p_loser_player_id;
10
11    get diagnostics rowcount = ROW_COUNT;
12    raise notice '% weapon stat rows updated.', rowcount;
13
14    -- then game stats
15    update player_game_stats
16    set player_id = p_winner_player_id
17    where player_id = p_loser_player_id;
18
19    get diagnostics rowcount = ROW_COUNT;
20    raise notice '% game stat rows updated.', rowcount;
21
22    -- then hashkeys (winner takes the loser's hashkey)
23    update hashkeys
24    set player_id = p_winner_player_id
25    where player_id = p_loser_player_id;
26    
27    get diagnostics rowcount = ROW_COUNT;
28    raise notice '% hashkeys copied.', rowcount;
29
30    -- and finally deactivate the old player record
31    update players set active_ind = false where player_id = p_loser_player_id;
32 end;
33 $$
34 language plpgsql;