KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > trans > QueryAssembler


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 package org.apache.cayenne.access.trans;
21
22 import java.sql.PreparedStatement JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.cayenne.access.QueryLogger;
27 import org.apache.cayenne.access.QueryTranslator;
28 import org.apache.cayenne.map.DbAttribute;
29 import org.apache.cayenne.map.DbEntity;
30 import org.apache.cayenne.map.DbRelationship;
31
32 /**
33  * Abstract superclass of Query translators.
34  *
35  * @author Andrus Adamchik
36  */

37 public abstract class QueryAssembler extends QueryTranslator {
38
39     /** PreparedStatement values. */
40     protected List JavaDoc values = new ArrayList JavaDoc();
41
42     /**
43      * PreparedStatement attributes matching entries in <code>values</code> list.
44      */

45     protected List JavaDoc attributes = new ArrayList JavaDoc();
46
47     /** Processes a join being added. */
48     public abstract void dbRelationshipAdded(DbRelationship dbRel);
49
50     /**
51      * Translates query into sql string. This is a workhorse method of QueryAssembler. It
52      * is called internally from <code>createStatement</code>. Usually there is no need
53      * to invoke it explicitly.
54      */

55     public abstract String JavaDoc createSqlString() throws Exception JavaDoc;
56
57     public String JavaDoc aliasForTable(DbEntity ent, DbRelationship rel) {
58         return aliasForTable(ent); // Default implementation
59
}
60
61     /**
62      * Returns a name that can be used as column alias. This can be one of the following:
63      * <ul>
64      * <li>an alias for this table, if it uses aliases</li>
65      * <li>a fully qualified table name, if not.</li>
66      * </ul>
67      * CayenneRuntimeException is thrown if a table alias can not be created.
68      */

69     public abstract String JavaDoc aliasForTable(DbEntity dbEnt);
70
71     /**
72      * Returns <code>true</code> if table aliases are supported. Default implementation
73      * returns false.
74      */

75     public boolean supportsTableAliases() {
76         return false;
77     }
78
79     /**
80      * Registers <code>anObject</code> as a PreparedStatement paramter.
81      *
82      * @param anObject object that represents a value of DbAttribute
83      * @param dbAttr DbAttribute being processed.
84      */

85     public void addToParamList(DbAttribute dbAttr, Object JavaDoc anObject) {
86         attributes.add(dbAttr);
87         values.add(anObject);
88     }
89
90     /**
91      * Translates internal query into PreparedStatement.
92      */

93     public PreparedStatement JavaDoc createStatement() throws Exception JavaDoc {
94         long t1 = System.currentTimeMillis();
95         String JavaDoc sqlStr = createSqlString();
96         QueryLogger.logQuery(sqlStr, values, System.currentTimeMillis() - t1);
97         PreparedStatement JavaDoc stmt = connection.prepareStatement(sqlStr);
98         initStatement(stmt);
99         return stmt;
100     }
101
102     /**
103      * Initializes prepared statements with collected parameters. Called internally from
104      * "createStatement". Cayenne users shouldn't normally call it directly.
105      */

106     protected void initStatement(PreparedStatement JavaDoc stmt) throws Exception JavaDoc {
107         if (values != null && values.size() > 0) {
108             int len = values.size();
109             for (int i = 0; i < len; i++) {
110                 Object JavaDoc val = values.get(i);
111
112                 DbAttribute attr = (DbAttribute) attributes.get(i);
113
114                 // null DbAttributes are a result of inferior qualifier processing
115
// (qualifier can't map parameters to DbAttributes and therefore
116
// only supports standard java types now)
117
// hence, a special moronic case here:
118
if (attr == null) {
119                     stmt.setObject(i + 1, val);
120                 }
121                 else {
122                     adapter.bindParameter(stmt, val, i + 1, attr.getType(), attr
123                             .getScale());
124                 }
125             }
126         }
127     }
128 }
129
Popular Tags