Search code examples
javasql-serveraspectjpointcuts

Can I amend the executed SQL before execution using an AspectJ pointcut


I'm trying to add a specific piece of SQL to all SQL executed in a system using AspectJ.

I've not used AspectJ before but I believe what I need to do is create a pointcut on

call(PreparedStatement Connection.prepareStatement(String))

and provide the advice

before(Connection con, String sql): call(PreparedStatement Connection.prepareStatement(String)) && target(con) && args(sql) { sql = "exec myStordProc();" + sql; }

after which the Connection.prepareStatement() method will continue with the altered String?

Or should I be intercepting calls to prepareStatement and executeQuery and creating a piece of advice that changes this to addBatch() adding my stored procdure call as the first batch sql then the original sql finally executing with executeBatch()?

Any pointers would be much appreciated.

Thanks


Solution

  • If you want to modify a parameters value you would need to use around advice so that you can make the call with your modified value:

    around(...): ... {
        proceed("exec myStordProc();" + sql);
    }