|
| 1 | +/* |
| 2 | + * Copyright 2003 Doug Mealing LLC dba Meta Objects. All Rights Reserved. |
| 3 | + * |
| 4 | + * This software is the proprietary information of Doug Mealing LLC dba Meta Objects. |
| 5 | + * Use is subject to license terms. |
| 6 | + * |
| 7 | + * Task 1.4 (remediation) — exercise the FULL getSelectStatementWhere assembly |
| 8 | + * path (not getRangeString in isolation) for OFFSET/FETCH dialects (MSSQL, |
| 9 | + * Oracle). An UNORDERED ranged query must emit the deterministic fallback |
| 10 | + * ORDER BY immediately followed by the OFFSET/FETCH range clause, in the correct |
| 11 | + * clause order. The assembled SQL is captured at prepareStatement() time via a |
| 12 | + * stub Connection, so no live database is required. |
| 13 | + */ |
| 14 | +package com.metaobjects.manager.db.driver; |
| 15 | + |
| 16 | +import com.metaobjects.field.MetaField; |
| 17 | +import com.metaobjects.loader.MetaDataLoader; |
| 18 | +import com.metaobjects.manager.QueryOptions; |
| 19 | +import com.metaobjects.manager.db.ObjectManagerDB; |
| 20 | +import com.metaobjects.manager.db.ObjectMapping; |
| 21 | +import com.metaobjects.manager.db.ObjectMappingDB; |
| 22 | +import com.metaobjects.manager.db.validator.MetaClassDBValidatorService; |
| 23 | +import com.metaobjects.manager.exp.Range; |
| 24 | +import com.metaobjects.object.MetaObject; |
| 25 | +import com.metaobjects.registry.MetaDataLoaderRegistry; |
| 26 | +import com.metaobjects.registry.ServiceRegistryFactory; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import javax.sql.DataSource; |
| 32 | +import java.io.PrintWriter; |
| 33 | +import java.lang.reflect.InvocationHandler; |
| 34 | +import java.lang.reflect.Method; |
| 35 | +import java.lang.reflect.Proxy; |
| 36 | +import java.sql.Connection; |
| 37 | +import java.sql.DriverManager; |
| 38 | +import java.sql.PreparedStatement; |
| 39 | +import java.sql.SQLException; |
| 40 | +import java.sql.SQLNonTransientConnectionException; |
| 41 | +import java.util.Collection; |
| 42 | +import java.util.concurrent.atomic.AtomicReference; |
| 43 | +import java.util.logging.Logger; |
| 44 | + |
| 45 | +import static org.junit.Assert.*; |
| 46 | + |
| 47 | +public class SelectAssemblyRangeTest { |
| 48 | + |
| 49 | + private static ObjectManagerDB omdb; |
| 50 | + private static String dbFile; |
| 51 | + private static MetaDataLoader loader; |
| 52 | + private static MetaDataLoaderRegistry registry; |
| 53 | + |
| 54 | + /** Exposes the protected getReadMapping so the test can fetch the read mapping. */ |
| 55 | + static class MappingExposingOMDB extends ObjectManagerDB { |
| 56 | + ObjectMapping readMappingFor(MetaObject mc) { return getReadMapping(mc); } |
| 57 | + } |
| 58 | + |
| 59 | + @BeforeClass |
| 60 | + public static void setupDB() throws Exception { |
| 61 | + registry = new MetaDataLoaderRegistry(ServiceRegistryFactory.getDefault()); |
| 62 | + // Reuse the codec fixture (codectest::Sample) — any concrete table-backed |
| 63 | + // entity works; we only need a valid read mapping to assemble a SELECT. |
| 64 | + loader = MetaDataLoader.fromResources("test-select-assembly", java.util.List.of("meta.codec.json")); |
| 65 | + registry.registerLoader(loader); |
| 66 | + |
| 67 | + dbFile = "omb-select-assembly-" + System.currentTimeMillis(); |
| 68 | + Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); |
| 69 | + derbyConnection().close(); |
| 70 | + |
| 71 | + DataSource ds = new DataSource() { |
| 72 | + @Override public Connection getConnection() throws SQLException { return derbyConnection(); } |
| 73 | + @Override public Connection getConnection(String u, String p) throws SQLException { return getConnection(); } |
| 74 | + @Override public PrintWriter getLogWriter() { return new PrintWriter(System.out); } |
| 75 | + @Override public void setLogWriter(PrintWriter out) {} |
| 76 | + @Override public void setLoginTimeout(int s) {} |
| 77 | + @Override public int getLoginTimeout() { return 100; } |
| 78 | + @Override public Logger getParentLogger() { throw new UnsupportedOperationException(); } |
| 79 | + @Override public <T> T unwrap(Class<T> iface) { throw new UnsupportedOperationException(); } |
| 80 | + @Override public boolean isWrapperFor(Class<?> iface) { return false; } |
| 81 | + }; |
| 82 | + |
| 83 | + MappingExposingOMDB m = new MappingExposingOMDB(); |
| 84 | + // Derby driver just to bootstrap the schema / mapping. The actual assembly |
| 85 | + // under test uses MSSQL / Oracle drivers against a capturing stub Connection. |
| 86 | + m.setDatabaseDriver(new DerbyDriver()); |
| 87 | + m.setDataSource(ds); |
| 88 | + m.init(); |
| 89 | + omdb = m; |
| 90 | + |
| 91 | + MetaClassDBValidatorService vs = new MetaClassDBValidatorService(); |
| 92 | + vs.setObjectManager(omdb); |
| 93 | + vs.setAutoCreate(true); |
| 94 | + vs.setMetaDataLoaderRegistry(registry); |
| 95 | + vs.init(); |
| 96 | + } |
| 97 | + |
| 98 | + private static Connection derbyConnection() throws SQLException { |
| 99 | + return DriverManager.getConnection("jdbc:derby:memory:" + dbFile + ";create=true"); |
| 100 | + } |
| 101 | + |
| 102 | + @AfterClass |
| 103 | + public static void teardown() throws Exception { |
| 104 | + if (dbFile != null) { |
| 105 | + try { DriverManager.getConnection("jdbc:derby:memory:" + dbFile + ";drop=true"); } |
| 106 | + catch (SQLNonTransientConnectionException ignored) {} |
| 107 | + } |
| 108 | + if (loader != null) loader.destroy(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A Connection whose only job is to capture the SQL passed to |
| 113 | + * prepareStatement(String) and return a no-op PreparedStatement proxy. No |
| 114 | + * statement is ever executed, so dialect-specific SQL never needs a real DB. |
| 115 | + */ |
| 116 | + private static Connection capturingConnection(AtomicReference<String> sink) { |
| 117 | + InvocationHandler stmtHandler = (proxy, method, args) -> { |
| 118 | + switch (method.getName()) { |
| 119 | + case "close": return null; |
| 120 | + case "hashCode": return System.identityHashCode(proxy); |
| 121 | + case "equals": return proxy == args[0]; |
| 122 | + case "toString": return "stub-PreparedStatement"; |
| 123 | + default: return defaultReturn(method.getReturnType()); |
| 124 | + } |
| 125 | + }; |
| 126 | + PreparedStatement stubPs = (PreparedStatement) Proxy.newProxyInstance( |
| 127 | + SelectAssemblyRangeTest.class.getClassLoader(), |
| 128 | + new Class<?>[]{PreparedStatement.class}, stmtHandler); |
| 129 | + |
| 130 | + InvocationHandler connHandler = (proxy, method, args) -> { |
| 131 | + if ("prepareStatement".equals(method.getName()) && args != null && args.length >= 1) { |
| 132 | + sink.set((String) args[0]); |
| 133 | + return stubPs; |
| 134 | + } |
| 135 | + switch (method.getName()) { |
| 136 | + case "close": return null; |
| 137 | + case "hashCode": return System.identityHashCode(proxy); |
| 138 | + case "equals": return proxy == args[0]; |
| 139 | + case "toString": return "stub-Connection"; |
| 140 | + default: return defaultReturn(method.getReturnType()); |
| 141 | + } |
| 142 | + }; |
| 143 | + return (Connection) Proxy.newProxyInstance( |
| 144 | + SelectAssemblyRangeTest.class.getClassLoader(), |
| 145 | + new Class<?>[]{Connection.class}, connHandler); |
| 146 | + } |
| 147 | + |
| 148 | + private static Object defaultReturn(Class<?> rt) { |
| 149 | + if (rt == boolean.class) return false; |
| 150 | + if (rt == int.class) return 0; |
| 151 | + if (rt == long.class) return 0L; |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + /** Drives the full getSelectStatementWhere assembly for an unordered ranged query. */ |
| 156 | + private String assembleUnorderedRangedSelect(GenericSQLDriver driver) throws Exception { |
| 157 | + driver.setManager(omdb); |
| 158 | + MetaObject mc = registry.findMetaObjectByName("codectest::Sample"); |
| 159 | + ObjectMappingDB mapping = (ObjectMappingDB) ((MappingExposingOMDB) omdb).readMappingFor(mc); |
| 160 | + Collection<MetaField> fields = mapping.getMetaFields(); |
| 161 | + |
| 162 | + QueryOptions qo = new QueryOptions(); // NO sort order |
| 163 | + qo.setRange(11, 20); // offset 10, fetch 10 |
| 164 | + |
| 165 | + AtomicReference<String> sql = new AtomicReference<>(); |
| 166 | + driver.getSelectStatementWhere(capturingConnection(sql), mc, mapping, fields, qo); |
| 167 | + return sql.get(); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void mssqlUnorderedRangeUsesFallbackOrderByThenOffsetFetch() throws Exception { |
| 172 | + String sql = assembleUnorderedRangedSelect(new MSSQLDriver()); |
| 173 | + assertNotNull("SQL must have been assembled", sql); |
| 174 | + |
| 175 | + String fallback = "ORDER BY (SELECT NULL)"; |
| 176 | + String range = "OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY"; |
| 177 | + assertTrue("must contain the MSSQL fallback ORDER BY: " + sql, sql.contains(fallback)); |
| 178 | + assertTrue("must contain the OFFSET/FETCH range: " + sql, sql.contains(range)); |
| 179 | + // The range clause must immediately follow the fallback ORDER BY. |
| 180 | + assertTrue("fallback ORDER BY must be immediately followed by OFFSET/FETCH: " + sql, |
| 181 | + sql.contains(fallback + " " + range)); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void oracleUnorderedRangeUsesFallbackOrderByThenOffsetFetch() throws Exception { |
| 186 | + String sql = assembleUnorderedRangedSelect(new OracleDriver()); |
| 187 | + assertNotNull("SQL must have been assembled", sql); |
| 188 | + |
| 189 | + String fallback = "ORDER BY NULL"; |
| 190 | + String range = "OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY"; |
| 191 | + assertTrue("must contain the Oracle fallback ORDER BY: " + sql, sql.contains(fallback)); |
| 192 | + assertTrue("must contain the OFFSET/FETCH range: " + sql, sql.contains(range)); |
| 193 | + assertTrue("fallback ORDER BY must be immediately followed by OFFSET/FETCH: " + sql, |
| 194 | + sql.contains(fallback + " " + range)); |
| 195 | + } |
| 196 | +} |
0 commit comments