* Beware: the code below is a pure example. It has not been tested. * It could harm yourself or your computer or even hit you in the face.
If you want to avoid modifying your SQL queries, but still want to have clean code (which means that your code remains supported), you can develop a solution using wrappers. That is, using a small set of classes that wrap existing ones, you can achieve what you want easily for the rest of the application, which will still think that it works with real DataSource, Connection and Statement.
1 - implement the StatementWrapper or PreparedStatementWrapper class, depending on what your application is already using. These classes are wrappers around regular instances of Statement or PreparedStatement. They are implemented just like using an internal operator as a delegate that does all the work, except when it is a QUERY query (Statement.executeQuery () method). Only in this exact situation does the shell surround the query with the following two lines: "SELECT * FROM (" and ") WHERE ROWNUM <" + maxRowLimit. For a basic code wrapper code, see how it looks for a DataSourceWrapper below.
2 - write another wrapper: ConnectionWrapper, which terminates the connection, which returns StatementWrapper in createStatement () and PreparedStatementWrapper in prepareStatement (). These are previously coded classes that take ConnectionWrapper delegateConnection.createStatement () / prepareStatement () as constructive arguments.
3 - repeat the step using a DataSourceWrapper. Here is a simple code example.
public class DataSourceWrapper implements DataSource { private DataSource mDelegate; public DataSourceWrapper( DataSource delegate ) { if( delegate == null ) { throw new NullPointerException( "Delegate cannot be null" ); mDelegate = delegate; } public Connection getConnection(String username, String password) { return new ConnectionWrapper( mDelegate.getConnection( username, password ) ); } public Connection getConnection() { ... <same as getConnection(String, String)> ... } }
4 - Finally, use a DataSourceWrapper as your DataSource application. If you are using JNDI (NamingContext), this change should be trivial.
Encoding all of this is fast and very easy, especially if you use an intelligent IDE such as Eclipse or IntelliJ, which automatically implements delegation methods.