benchmark/sql/method_api_depth.sql

67 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

USE qare67;
DELIMITER $$
DROP PROCEDURE IF EXISTS CalculateMethodApiDepth$$
CREATE PROCEDURE CalculateMethodApiDepth()
BEGIN
DECLARE current_depth INT DEFAULT 2;
DECLARE rows_affected INT DEFAULT 0;
DECLARE max_depth INT DEFAULT 20; -- 安全阀
/* 1. 清空结果表 */
TRUNCATE TABLE method_api_depth;
/* 2. 初始层:直接调用 API 的方法
C → api ==> depth = 2 */
INSERT INTO method_api_depth (method_id, api_id, depth, PATH)
SELECT
caller_method_id,
api_id,
2 AS depth,
CAST(caller_method_id AS CHAR) AS PATH
FROM api_call;
SELECT ROW_COUNT() INTO rows_affected;
/* 3. 逐层向上 BFS */
WHILE rows_affected > 0 AND current_depth < max_depth DO
SET current_depth = current_depth + 1;
INSERT IGNORE INTO method_api_depth (method_id, api_id, depth, PATH)
SELECT
mc.caller_method_id,
prev.api_id,
current_depth,
CONCAT(mc.caller_method_id, '->', prev.path)
FROM method_call mc
JOIN method_api_depth PREV
ON mc.callee_method_id = prev.method_id
WHERE prev.depth = current_depth - 1
-- 防环A->A / A->B->A
AND FIND_IN_SET(
mc.caller_method_id,
REPLACE(prev.path, '->', ',')
) = 0;
SELECT ROW_COUNT() INTO rows_affected;
END WHILE;
SELECT CONCAT('Calculation completed. Max depth = ', current_depth - 1) AS STATUS;
END$$
DELIMITER ;
CALL CalculateMethodApiDepth();