@Test publicvoidtestSql(){ String sql = "select * from test where name = 'maple';"; List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL); SQLStatement sqlStatement = sqlStatements.get(0); TestVisitor testVisitor = new TestVisitor(); sqlStatement.accept(testVisitor); } /** * out: * SQLSelectStatement */
使用访问器修改查询语句
修改查询字段
这里以一个简单的逻辑删除字段为例,假设我们要为系统里所有的表指定一个字段为布尔值true。
这里有几个可能需要注意的地方:
在关联查询时,需要在on上追加条件,但在where条件中只追加左表的条件
举例来说,这里有一条语句:
1 2 3 4
SELECT * FROMtest a LEFTJOIN test1 b ON a.id = b.id WHERE a.name = 'maple';
假设处理之后的语句变为:
1 2 3 4 5 6 7 8 9
SELECT * FROMtest a LEFTJOIN test1 b ON a.id = b.id AND a.is_delete IStrue AND b.is_delete IStrue WHERE a.name = 'maple' AND a.is_delete IStrue AND b.is_delete IStrue;
@Test publicvoidselectVisitor(){ String sql = "select * from test left join test1 on 1 = 1 where name = 'maple';"; String sql5 = "select * from test a left join test1 b on a.id = b.id where a.name = 'maple';"; String sql2 = "select * from (select * from test a left join test1 b on 1 = 1 where name = 'maple') x where x" + ".sex = 'man';"; String sql3 = "select * from (select * from test a left join test1 b on 1 = 1 and a.is_delete is false where " + "name = 'maple') x where x.sex = 'man' and x.is_delete is false;"; String sql4 = "select * from (select * from test a left join test1 b on 1 = 1 and a.is_delete is false where " + "name = 'maple') x where x.sex = 'man' and x.is_delete is false " + "union all select * from (select * from test a left join test1 b on 1 = 1 and a.is_delete is false " + "where name = 'maple') x where x.sex = 'woman' and x.is_delete is false;"; SelectVisitor selectVisitor = new SelectVisitor(); System.out.println("--------测试无别名关联查询-------"); accept(selectVisitor, sql); System.out.println("--------测试有别名关联查询-------"); accept(selectVisitor, sql5); System.out.println("--------测试子查询-------"); accept(selectVisitor, sql2); System.out.println("--------测试已经自定义条件的情况下是否会覆盖条件-------"); accept(selectVisitor, sql3); System.out.println("--------测试union-------"); accept(selectVisitor, sql4); }
/** * out: * --------测试无别名关联查询------- * 原始语句: SELECT * * FROM test * LEFT JOIN test1 ON 1 = 1 * WHERE name = 'maple'; * 处理后语句: SELECT * * FROM test * LEFT JOIN test1 * ON 1 = 1 * AND test.is_delete IS true * AND test1.is_delete IS true * WHERE name = 'maple' * AND test.is_delete IS true; * --------测试有别名关联查询------- * 原始语句: SELECT * * FROM test a * LEFT JOIN test1 b ON a.id = b.id * WHERE a.name = 'maple'; * 处理后语句: SELECT * * FROM test a * LEFT JOIN test1 b * ON a.id = b.id * AND a.is_delete IS true * AND b.is_delete IS true * WHERE a.name = 'maple' * AND a.is_delete IS true; * --------测试子查询------- * 原始语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b ON 1 = 1 * WHERE name = 'maple' * ) x * WHERE x.sex = 'man'; * 处理后语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS true * AND b.is_delete IS true * WHERE name = 'maple' * AND a.is_delete IS true * ) x * WHERE x.sex = 'man' * AND x.is_delete IS true; * --------测试已经自定义条件的情况下是否会覆盖条件------- * 原始语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * WHERE name = 'maple' * ) x * WHERE x.sex = 'man' * AND x.is_delete IS false; * 处理后语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * AND b.is_delete IS true * WHERE name = 'maple' * AND a.is_delete IS true * ) x * WHERE x.sex = 'man' * AND x.is_delete IS false; * --------测试union------- * 原始语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * WHERE name = 'maple' * ) x * WHERE x.sex = 'man' * AND x.is_delete IS false * UNION ALL * SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * WHERE name = 'maple' * ) x * WHERE x.sex = 'woman' * AND x.is_delete IS false; * 处理后语句: SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * AND b.is_delete IS true * WHERE name = 'maple' * AND a.is_delete IS true * ) x * WHERE x.sex = 'man' * AND x.is_delete IS false * UNION ALL * SELECT * * FROM ( * SELECT * * FROM test a * LEFT JOIN test1 b * ON 1 = 1 * AND a.is_delete IS false * AND b.is_delete IS true * WHERE name = 'maple' * AND a.is_delete IS true * ) x * WHERE x.sex = 'woman' * AND x.is_delete IS false; */
@Test publicvoidinsertVisitor(){ String sql = "insert into test(id, name) values(1, 'maple');"; String sql2 = "insert into test(name) select name from test where name = 'maple';"; String sql3 = "insert into test(name,is_delete) select name,false from test where name = 'maple';"; InsertVisitor insertVisitor = new InsertVisitor(); System.out.println("--------测试普通插入语句-------"); accept(insertVisitor, sql); System.out.println("--------测试子查询插入语句-------"); accept(insertVisitor, sql2); System.out.println("--------测试已经存在对应字段的插入语句-------"); accept(insertVisitor, sql3); } /** * out: * --------测试普通插入语句------- * 原始语句: INSERT INTO test (id, name) * VALUES (1, 'maple'); * 处理后语句: INSERT INTO test (id, name, is_delete) * VALUES (1, 'maple', true); * --------测试子查询插入语句------- * 原始语句: INSERT INTO test (name) * SELECT name * FROM test * WHERE name = 'maple'; * 处理后语句: INSERT INTO test (name, is_delete) * SELECT name, true * FROM test * WHERE name = 'maple' * AND test.is_delete IS true; * --------测试已经存在对应字段时插入语句------- * 原始语句: INSERT INTO test (name, is_delete) * SELECT name, false * FROM test * WHERE name = 'maple'; * 处理后语句: INSERT INTO test (name, is_delete) * SELECT name, false * FROM test * WHERE name = 'maple' * AND test.is_delete IS true; */
@Test publicvoidupdateVisitor(){ String sql = "update test set name = 'maple' where name = '张三'"; String sql2 = "update test set name = 'maple',test.is_delete = true where name = '张三'"; String sql3 = "update test left join test t on test.username = t.username set test.is_delete = 1, test" + ".username = 'maple' where test.username = 'maple';"; UpdateVisitor updateVisitor = new UpdateVisitor(); System.out.println("--------测试普通更新语句-------"); accept(updateVisitor, sql); System.out.println("--------测试已经存在对应字段的更新语句-------"); accept(updateVisitor, sql2); System.out.println("--------测试联表的更新语句-------"); accept(updateVisitor, sql3); } /** * out: * --------测试普通更新语句------- * 原始语句: UPDATE test * SET name = 'maple' * WHERE name = '张三' * 处理后语句: UPDATE test * SET name = 'maple', test.is_delete = true * WHERE name = '张三' * --------测试已经存在对应字段的更新语句------- * 原始语句: UPDATE test * SET name = 'maple', test.is_delete = true * WHERE name = '张三' * 处理后语句: UPDATE test * SET name = 'maple', test.is_delete = true * WHERE name = '张三' * --------测试联表的更新语句------- * 原始语句: UPDATE test * LEFT JOIN test t ON test.username = t.username * SET test.is_delete = 1, test.username = 'maple' * WHERE test.username = 'maple'; * 处理后语句: UPDATE test * LEFT JOIN test t * ON test.username = t.username * AND test.is_delete IS true * AND t.is_delete IS true * SET test.is_delete = 1, test.username = 'maple', t.is_delete = true * WHERE test.username = 'maple'; */
@Test publicvoiddeleteVisitor(){ String sql = "delete from test where name = '张三'"; String sql2 = "delete test,test1\n" + "from test\n" + " left join test1 on test1.username = test.username and test1.is_delete is false\n" + "where test.username = '张三'"; DeleteVisitor deleteVisitor = new DeleteVisitor(); System.out.println("--------测试普通删除语句-------"); accept(deleteVisitor, sql); System.out.println("--------测试联表删除语句-------"); accept(deleteVisitor, sql2);
} /** * out: * --------测试普通删除语句------- * 原始语句: DELETE FROM test * WHERE name = '张三' * 处理后语句: DELETE FROM test * WHERE name = '张三' * AND test.is_delete IS true * --------测试联表删除语句------- * 原始语句: DELETE test, test1 * FROM test * LEFT JOIN test1 * ON test1.username = test.username * AND test1.is_delete IS false * WHERE test.username = '张三' * 处理后语句: DELETE test, test1 * FROM test * LEFT JOIN test1 * ON test1.username = test.username * AND test1.is_delete IS false * AND test.is_delete IS true * WHERE test.username = '张三' * AND test.is_delete IS true */