Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DynamicTableNameInnerInterceptor.java #3965

Merged
merged 1 commit into from
Dec 24, 2021
Merged

Conversation

baymaxxjf
Copy link
Contributor

修改描述

添加动态表名的钩子函数,以方便回调用ThreadLocal#remove方法防止内存泄漏

测试用例

/**
 * 动态表名内部拦截器测试
 *
 * @author miemie, hcl
 * @since 2020-07-16
 */
class DynamicTableNameInnerInterceptorTest {

    public static ThreadLocal<String> myTableName = new ThreadLocal<>();

    public static void remove(){
        myTableName.remove();
    }

    /**
     * 测试 SQL 中的动态表名替换
     */
    @Test
    @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
    void doItWithHook() {
        DynamicTableNameInnerInterceptor interceptor = new DynamicTableNameInnerInterceptor();
        interceptor.setHook(() ->{
            remove();
        });
        interceptor.setTableNameHandler((sql, tableName) -> "t_user_r");
        // 表名相互包含
        @Language("SQL")
        String origin = "SELECT * FROM t_user, t_user_role", replaced = "SELECT * FROM t_user_r, t_user_role";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 表名在末尾
        origin = "SELECT * FROM t_user";
        replaced = "SELECT * FROM t_user_r";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 表名前后有注释
        origin = "SELECT * FROM /**/t_user/* t_user */";
        replaced = "SELECT * FROM /**/t_user_r/* t_user */";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 值中带有表名
        origin = "SELECT * FROM t_user WHERE name = 't_user'";
        replaced = "SELECT * FROM t_user_r WHERE name = 't_user'";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 别名被声明要替换
        origin = "SELECT t_user.* FROM t_user_real t_user";
        assertEquals(origin, interceptor.changeTable(origin));
    }

    @Test
    @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
    void doItWithoutHook() {
        DynamicTableNameInnerInterceptor interceptor = new DynamicTableNameInnerInterceptor();
        interceptor.setTableNameHandler((sql, tableName) -> "t_user_r");
        // 表名相互包含
        @Language("SQL")
        String origin = "SELECT * FROM t_user, t_user_role", replaced = "SELECT * FROM t_user_r, t_user_role";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 表名在末尾
        origin = "SELECT * FROM t_user";
        replaced = "SELECT * FROM t_user_r";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 表名前后有注释
        origin = "SELECT * FROM /**/t_user/* t_user */";
        replaced = "SELECT * FROM /**/t_user_r/* t_user */";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 值中带有表名
        origin = "SELECT * FROM t_user WHERE name = 't_user'";
        replaced = "SELECT * FROM t_user_r WHERE name = 't_user'";
        assertEquals(replaced, interceptor.changeTable(origin));
        // 别名被声明要替换
        origin = "SELECT t_user.* FROM t_user_real t_user";
        assertEquals(origin, interceptor.changeTable(origin));
    }
}

添加动态表名的钩子函数,以方便回调用ThreadLocal#remove方法防止内存泄漏
@qmdx qmdx merged commit 31ff5b5 into baomidou:3.0 Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants