CREATE DEFINER=`developer`@`localhost` PROCEDURE `permission_group_set`(in i_ds_access_key char(32), in i_group_access_key char(32), in i_can_read boolean, in i_can_add boolean, in i_can_edit boolean, in i_can_delete boolean)
BEGIN
	declare record_found boolean;

	select false into record_found;

	select 
		if(count(*)  > 0, true, false) into record_found 
	from 
		dataset_permissions as dp
	where 
		dp.dataset_access_key = i_ds_access_key and
		dp.group_access_key = i_group_access_key;
        
    if record_found then
        update
            dataset_permissions as dp
        set
            dp.can_read = i_can_read,
            dp.can_add = i_can_add,
            dp.can_edit = i_can_edit,
            dp.can_delete = i_can_delete
        where
            dp.dataset_access_key = i_ds_access_key and
            dp.group_access_key = i_group_access_key;
    else
        insert into
            dataset_permissions
        values
            (i_ds_access_key, i_group_access_key, null, i_can_read, i_can_add, i_can_edit, i_can_delete);
    end if;
END