is there anyway to intercept the sql query before it is sent to the database and rewrite it using castle active record ?
Set up an interceptor and use OnPrepareStatement to modify the generated SQL. See this example.
OnPrepareStatement