1 19 20 21 package org.apache.cayenne.dba.oracle; 22 23 import java.lang.reflect.Method ; 24 import java.sql.PreparedStatement ; 25 import java.sql.Statement ; 26 27 import org.apache.cayenne.access.QueryLogger; 28 import org.apache.cayenne.access.trans.SelectTranslator; 29 import org.apache.cayenne.query.QueryMetadata; 30 31 36 public class OracleSelectTranslator extends SelectTranslator { 37 38 private static boolean testedDriver; 39 private static boolean useOptimizations; 40 private static Method statementSetRowPrefetch; 41 42 private static final Object [] rowPrefetchArgs = new Object [] { 43 new Integer (100) 44 }; 45 46 public String createSqlString() throws Exception { 47 48 String sqlString = super.createSqlString(); 49 50 QueryMetadata info = getQuery().getMetaData(getEntityResolver()); 51 if (info.getFetchLimit() > 0) { 52 sqlString = "SELECT * FROM (" 53 + sqlString 54 + ") WHERE rownum <= " 55 + info.getFetchLimit(); 56 } 57 58 return sqlString; 59 } 60 61 65 private static final synchronized void testDriver(Statement st) { 66 if (testedDriver) { 67 return; 68 } 69 70 if (st == null) { 72 return; 73 } 74 75 testedDriver = true; 76 77 try { 78 80 Class [] args2 = new Class [] { 81 Integer.TYPE 82 }; 83 statementSetRowPrefetch = st.getClass().getMethod("setRowPrefetch", args2); 84 85 useOptimizations = true; 86 } 87 catch (Exception ex) { 88 useOptimizations = false; 89 statementSetRowPrefetch = null; 90 } 91 } 92 93 97 public PreparedStatement createStatement() throws Exception { 98 String sqlStr = createSqlString(); 99 QueryLogger.logQuery(sqlStr, values); 100 PreparedStatement stmt = connection.prepareStatement(sqlStr); 101 102 initStatement(stmt); 103 104 if (!testedDriver) { 105 testDriver(stmt); 106 } 107 108 if (useOptimizations) { 109 111 115 122 123 statementSetRowPrefetch.invoke(stmt, rowPrefetchArgs); 131 } 132 133 return stmt; 134 } 135 } 136 | Popular Tags |