CREATE DEFINER=`jhise`@`localhost` PROCEDURE `project_set_user_permission`(in i_project_access_key char(32), in i_user_account_key char(32), in i_can_read int, in i_can_edit int)
BEGIN
	declare l_record_found bool;
    declare l_project_id int;

    select id into l_project_id from project where access_key = i_project_access_key;

    select
		if(count(*) > 0, true, false) into l_record_found
    from
		permissions_project pp
	where
		pp.user_account_key = i_user_account_key and
        pp.project_id = l_project_id;

	if ( l_record_found = true )then
		update
			permissions_project
		set
			can_read = i_can_read,
            can_edit = i_can_edit
		where
			user_account_key = i_user_account_key and
            project_id = l_project_id;
    else
		insert into permissions_project
			(project_id, user_account_key, group_account_key, can_read, can_edit)
		values
			(l_project_id, i_user_account_key, null, i_can_read, i_can_edit);
    end if;
END
