KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > JdbcEsqlQuery


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

16
17 package org.apache.cocoon.components.language.markup.xsp;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.Connection JavaDoc;
24
25 /**
26  * This EsqlQuery only uses the standard JDBC API approaches.
27  * Please note that whether this is good, ok or bad depends
28  * on the driver implementation of your database vendor.
29  * It should work with all JDBC compliant databases.
30  * Unfortunately it seems NOT to work with mssql
31  *
32  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
33  * @version CVS $Id: JdbcEsqlQuery.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 final public class JdbcEsqlQuery extends AbstractEsqlQuery {
36
37     public JdbcEsqlQuery(Connection JavaDoc connection, String JavaDoc query) {
38         super(connection, query);
39     }
40
41     /**
42      * Only newInstance may use this contructor
43      * @param resultSet
44      */

45     private JdbcEsqlQuery(final ResultSet JavaDoc resultSet) {
46         super(resultSet);
47     }
48
49     /**
50      * Create a EsqlQuery of the same type
51      * @param resultSet
52      */

53     public AbstractEsqlQuery newInstance(final ResultSet JavaDoc resultSet) {
54         return(new JdbcEsqlQuery(resultSet));
55     }
56
57     public PreparedStatement JavaDoc prepareStatement() throws SQLException JavaDoc {
58         return (
59                 setPreparedStatement(
60                         getConnection().prepareStatement(
61                                 getQueryString(),
62                                 ResultSet.TYPE_SCROLL_INSENSITIVE,
63                                 ResultSet.CONCUR_READ_ONLY)
64                 ));
65     }
66
67     public CallableStatement JavaDoc prepareCall() throws SQLException JavaDoc {
68         return (
69                 (CallableStatement JavaDoc) setPreparedStatement(
70                         getConnection().prepareCall(
71                                 getQueryString(),
72                                 ResultSet.TYPE_SCROLL_INSENSITIVE,
73                                 ResultSet.CONCUR_READ_ONLY
74                         )
75                 )
76                 );
77     }
78
79     /**
80      * AFAIK this is the proposed JDBC API way to get the number
81      * of results. Unfortunately -at least some- driver implementation
82      * are transfering the complete resultset when moving to the end.
83      * Which is totally stupid for limit/paging purposes. So we probably
84      * better stick with an additional count query from the AbstractEsqlQuery
85      */

86
87     public int getRowCount() throws SQLException JavaDoc {
88         ResultSet JavaDoc rs = getResultSet();
89         synchronized (rs) {
90             int currentRow = rs.getRow();
91             rs.last();
92             int count = rs.getRow();
93             if (currentRow > 0) {
94                 rs.absolute(currentRow);
95             }
96             else {
97                 rs.first();
98                 rs.relative(-1);
99             }
100             return (count);
101         }
102     }
103
104     public void getResultRows() throws SQLException JavaDoc {
105         final int skip = getSkipRows();
106         if (skip > 0) {
107             getResultSet().absolute(skip);
108         }
109         setPosition(skip);
110     }
111
112 }
113
Popular Tags