1 16 17 package org.apache.cocoon.acting.modular; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.util.Map ; 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 39 public class DatabaseSelectAction extends DatabaseAction { 40 41 46 protected String selectMode ( boolean isAutoIncrement, Map modes ) { 47 48 return (String ) modes.get( MODE_OTHERS ); 49 } 50 51 52 56 protected boolean honourAutoIncrement() { return false; } 57 58 59 67 protected CacheHelper getQuery( Configuration table, Map modeTypes, Map 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 queryBuffer = new StringBuffer ("SELECT "); 83 84 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 125 protected Object [][] getColumnValues( Configuration tableConf, CacheHelper queryData, Map objectModel ) 126 throws ConfigurationException, ServiceException { 127 128 Object [][] columnValues = new Object [ 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 } 135 } 136 return columnValues; 137 } 138 139 140 143 protected int processRow ( Map objectModel, Connection conn, PreparedStatement statement, String outputMode, 144 Configuration table, CacheHelper queryData, Object [][] columnValues, 145 int rowIndex, Map results ) 146 throws SQLException , ConfigurationException, Exception { 147 148 int currentIndex = 1; 149 150 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 ResultSet 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 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 |