KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > BaseSQLAction


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.jdbc;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.cayenne.CayenneException;
27 import org.apache.cayenne.access.OperationObserver;
28 import org.apache.cayenne.access.QueryLogger;
29 import org.apache.cayenne.dba.DbAdapter;
30 import org.apache.cayenne.map.EntityResolver;
31 import org.apache.cayenne.query.Query;
32 import org.apache.cayenne.query.SQLAction;
33
34 /**
35  * A convenience superclass for SQLAction implementations.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  */

40 public abstract class BaseSQLAction implements SQLAction {
41
42     protected DbAdapter adapter;
43     protected EntityResolver entityResolver;
44
45     public BaseSQLAction(DbAdapter adapter, EntityResolver entityResolver) {
46         this.adapter = adapter;
47         this.entityResolver = entityResolver;
48     }
49
50     public DbAdapter getAdapter() {
51         return adapter;
52     }
53
54     public EntityResolver getEntityResolver() {
55         return entityResolver;
56     }
57
58     /**
59      * Helper method to process a ResultSet.
60      */

61     protected void readResultSet(
62             ResultSet JavaDoc resultSet,
63             RowDescriptor descriptor,
64             Query query,
65             OperationObserver delegate) throws SQLException JavaDoc, Exception JavaDoc {
66
67         long t1 = System.currentTimeMillis();
68         JDBCResultIterator resultReader = new JDBCResultIterator(
69                 null,
70                 null,
71                 resultSet,
72                 descriptor,
73                 query.getMetaData(getEntityResolver()).getFetchLimit());
74
75         if (!delegate.isIteratedResult()) {
76             List JavaDoc resultRows = resultReader.dataRows(false);
77             QueryLogger
78                     .logSelectCount(resultRows.size(), System.currentTimeMillis() - t1);
79
80             delegate.nextDataRows(query, resultRows);
81         }
82         else {
83             try {
84                 resultReader.setClosingConnection(true);
85                 delegate.nextDataRows(query, resultReader);
86             }
87             catch (Exception JavaDoc ex) {
88
89                 try {
90                     resultReader.close();
91                 }
92                 catch (CayenneException cex) {
93                     // ignore...
94
}
95
96                 throw ex;
97             }
98         }
99     }
100 }
101
Popular Tags