KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > modular > DatabaseSelectAction


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.acting.modular;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.service.ServiceException;
28
29 import org.apache.cocoon.util.JDBCTypeConversions;
30
31 /**
32  * Selects a record from a database. The action can select one or more
33  * tables, and can select from more than one row of a table at a
34  * time.
35  *
36  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
37  * @version CVS $Id: DatabaseSelectAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public class DatabaseSelectAction extends DatabaseAction {
40
41     /**
42      * determine which mode to use as default mode
43      * here: SELECT
44      * highly specific to operation INSERT / UPDATE / DELETE / SELECT
45      */

46     protected String JavaDoc selectMode ( boolean isAutoIncrement, Map JavaDoc modes ) {
47         
48         return (String JavaDoc) modes.get( MODE_OTHERS );
49     }
50
51
52     /**
53      * determine whether autoincrement columns should be honoured by
54      * this operation. This is usually snsible only for INSERTs.
55      */

56     protected boolean honourAutoIncrement() { return false; }
57
58
59     /**
60      * Get the String representation of the PreparedStatement. This is
61      * mapped to the Configuration object itself, so if it doesn't exist,
62      * it will be created.
63      *
64      * @param table the table's configuration object
65      * @return the insert query as a string
66      */

67     protected CacheHelper getQuery( Configuration table, Map JavaDoc modeTypes, Map JavaDoc defaultModeNames )
68         throws ConfigurationException, ServiceException {
69
70         LookUpKey lookUpKey = new LookUpKey( table, modeTypes );
71         CacheHelper queryData = null;
72         synchronized( this.cachedQueryData ) {
73             queryData = (CacheHelper) this.cachedQueryData.get( lookUpKey );
74             if (queryData == null) {
75                 Configuration[] keys = table.getChild("keys").getChildren("key");
76                 Configuration[] values = table.getChild("values").getChildren("value");
77
78                 queryData = new CacheHelper( keys.length, keys.length + values.length );
79                 fillModes( keys , true , defaultModeNames, modeTypes, queryData );
80                 fillModes( values, false, defaultModeNames, modeTypes, queryData );
81                 
82                 StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("SELECT ");
83
84                 //queryBuffer.append(table.getAttribute("name")).append(" WHERE ");
85
int count = 0;
86                 for (int i = 0; i < queryData.columns.length; i++) {
87                     if ( !queryData.columns[i].isKey ) {
88                         if ( count > 0 ) {
89                             queryBuffer.append(", ");
90                         }
91                         queryBuffer
92                             .append( queryData.columns[i].columnConf.getAttribute( "name" ) );
93                         count++;
94                     }
95                 }
96
97                 queryBuffer.append(" FROM ").append(table.getAttribute("name")).append(" WHERE ");
98                 count = 0;
99                 for (int i = 0; i < queryData.columns.length; i++) {
100                     if ( queryData.columns[i].isKey ) {
101                         if ( count > 0 ) {
102                             queryBuffer.append(" AND ");
103                         }
104                         queryBuffer
105                             .append( queryData.columns[i].columnConf.getAttribute( "name" ) )
106                             .append( "= ?");
107                         count++;
108                     }
109                 }
110
111                 queryData.queryString = queryBuffer.toString();
112
113                 this.cachedQueryData.put( lookUpKey, queryData );
114             }
115         }
116
117         return queryData;
118     }
119
120
121     /**
122      * Fetch all values for all key columns that are needed to do the
123      * database operation.
124      */

125     protected Object JavaDoc[][] getColumnValues( Configuration tableConf, CacheHelper queryData, Map JavaDoc objectModel )
126         throws ConfigurationException, ServiceException {
127
128         Object JavaDoc[][] columnValues = new Object JavaDoc[ queryData.columns.length ][];
129         for ( int i = 0; i < queryData.columns.length; i++ ){
130             if ( queryData.columns[i].isKey ) {
131                 columnValues[i] = this.getColumnValue( tableConf, queryData.columns[i], objectModel );
132             } else {
133                 // columnValues[i] = new Object[1]; // this should not be needed
134
}
135         }
136         return columnValues;
137     }
138
139
140     /**
141      * set all necessary ?s and execute the query
142      */

143     protected int processRow ( Map JavaDoc objectModel, Connection JavaDoc conn, PreparedStatement JavaDoc statement, String JavaDoc outputMode,
144                                Configuration table, CacheHelper queryData, Object JavaDoc[][] columnValues,
145                                int rowIndex, Map JavaDoc results )
146         throws SQLException JavaDoc, ConfigurationException, Exception JavaDoc {
147
148         int currentIndex = 1;
149
150         // ordering is different for SELECT just needs keys
151
for (int i = 0; i < queryData.columns.length; i++) {
152             Column col = queryData.columns[i];
153             if ( col.isKey ) {
154                 this.setColumn(objectModel, outputMode, results, table, col.columnConf, rowIndex,
155                                columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex );
156                 currentIndex++;
157             }
158         }
159         statement.execute();
160         // retrieve values
161
ResultSet JavaDoc resultset = statement.getResultSet();
162         rowIndex = 0;
163         while ( resultset.next() ){
164             for (int i = 0; i < queryData.columns.length; i++) {
165                 if ( !queryData.columns[i].isKey ) {
166                     Object JavaDoc value = JDBCTypeConversions.getColumn( resultset, queryData.columns[i].columnConf );
167                     this.setOutput(objectModel, outputMode, results, table, queryData.columns[i].columnConf, rowIndex, value);
168                 }
169             }
170             rowIndex++;
171         }
172         if (rowIndex == 0) { results = EMPTY_MAP;}
173         return rowIndex;
174     }
175
176
177 }
178
Popular Tags