DROP PROCEDURE IF EXISTS mocha_get_instance_by_key;

CREATE PROCEDURE mocha_get_instance_by_key
(
    IN p_class_id INT,
    IN p_inst_id INT
)
BEGIN
    
    DECLARE p_tenant_id INT;
    SET p_tenant_id = mocha_get_current_tenant();

    SELECT * FROM mocha_instances
    WHERE (tenant_id = p_tenant_id OR tenant_id IN (SELECT target_tenant_id FROM mocha_tenant_references WHERE source_tenant_id = p_tenant_id))
    AND class_id = p_class_id
    AND inst_id = p_inst_id;

END;
