KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > oracle > OracleSelectTranslator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.dba.oracle;
22
23 import java.lang.reflect.Method JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.Statement JavaDoc;
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 /**
32  * Select translator that implements Oracle-specific optimizations.
33  *
34  * @author Andrus Adamchik
35  */

36 public class OracleSelectTranslator extends SelectTranslator {
37
38     private static boolean testedDriver;
39     private static boolean useOptimizations;
40     private static Method JavaDoc statementSetRowPrefetch;
41
42     private static final Object JavaDoc[] rowPrefetchArgs = new Object JavaDoc[] {
43         new Integer JavaDoc(100)
44     };
45
46     public String JavaDoc createSqlString() throws Exception JavaDoc {
47
48         String JavaDoc 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     /**
62      * Determines if we can use Oracle optimizations. If yes, configure this object to use
63      * them via reflection.
64      */

65     private static final synchronized void testDriver(Statement JavaDoc st) {
66         if (testedDriver) {
67             return;
68         }
69
70         // invalid call.. give it another chance later
71
if (st == null) {
72             return;
73         }
74
75         testedDriver = true;
76
77         try {
78             // search for matching methods in class and its superclasses
79

80             Class JavaDoc[] args2 = new Class JavaDoc[] {
81                 Integer.TYPE
82             };
83             statementSetRowPrefetch = st.getClass().getMethod("setRowPrefetch", args2);
84
85             useOptimizations = true;
86         }
87         catch (Exception JavaDoc ex) {
88             useOptimizations = false;
89             statementSetRowPrefetch = null;
90         }
91     }
92
93     /**
94      * Translates internal query into PreparedStatement, applying Oracle optimizations if
95      * possible.
96      */

97     public PreparedStatement JavaDoc createStatement() throws Exception JavaDoc {
98         String JavaDoc sqlStr = createSqlString();
99         QueryLogger.logQuery(sqlStr, values);
100         PreparedStatement JavaDoc stmt = connection.prepareStatement(sqlStr);
101
102         initStatement(stmt);
103
104         if (!testedDriver) {
105             testDriver(stmt);
106         }
107
108         if (useOptimizations) {
109             // apply Oracle optimization of the statement
110

111             // Performance tests conducted by Arndt (bug #699966) show
112
// that using explicit "defineColumnType" slows things down,
113
// so this is disabled now
114

115             // 1. name result columns
116
/*
117              * List columns = getColumns(); int len = columns.size(); Object[] args = new
118              * Object[2]; for (int i = 0; i < len; i++) { DbAttribute attr = (DbAttribute)
119              * columns.get(i); args[0] = new Integer(i + 1); args[1] = new
120              * Integer(attr.getType()); statementDefineColumnType.invoke(stmt, args); }
121              */

122
123             // 2. prefetch bigger batches of rows
124
// [This optimization didn't give any measurable performance
125
// increase. Keeping it for the future research]
126
// Note that this is done by statement,
127
// instead of Connection, since we do not want to mess
128
// with Connection that is potentially used by
129
// other people.
130
statementSetRowPrefetch.invoke(stmt, rowPrefetchArgs);
131         }
132
133         return stmt;
134     }
135 }
136
Popular Tags