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.ResultSetMetaData ; 23 import java.sql.SQLException ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.service.ServiceException; 29 30 54 public class DatabaseQueryAction extends DatabaseAction { 55 56 61 protected String selectMode ( boolean isAutoIncrement, Map modes ) { 62 63 return (String ) modes.get( MODE_OTHERS ); 64 } 65 66 70 protected boolean honourAutoIncrement() { return false; } 71 72 73 81 protected CacheHelper getQuery( Configuration table, Map modeTypes, Map defaultModeNames ) 82 throws ConfigurationException, ServiceException { 83 84 LookUpKey lookUpKey = new LookUpKey( table, modeTypes ); 85 CacheHelper queryData = null; 86 synchronized( this.cachedQueryData ) { 87 queryData = (CacheHelper) this.cachedQueryData.get( lookUpKey ); 88 if (queryData == null) { 89 Configuration[] queries = table.getChild("queries").getChildren("query"); 90 Configuration[] keys = table.getChild("keys").getChildren("key"); 91 Configuration[] values = table.getChild("values").getChildren("value"); 92 93 boolean found = false; 94 String queryModeName = ""; 95 String query = ""; 96 boolean useValues = true; 97 for (int i=0; i<queries.length; i++) { 98 queryModeName = queries[i].getAttribute("mode",null); 99 if ( queryModeName.equals(modeTypes.get(MODE_OTHERS)) || "all".equals(queryModeName)) { 100 query = queries[i].getValue(); 101 useValues = queries[i].getAttributeAsBoolean("use-values", useValues); 102 found = true; 103 break; 104 } 105 } 106 107 if (!found) { 108 throw new ConfigurationException("Could not find query mode " + 109 modeTypes.get(MODE_OTHERS) + 110 " for table " + table.getAttribute("name",null)); 111 } 112 113 114 115 queryData = new CacheHelper( keys.length, keys.length + (useValues ? values.length : 0)); 116 queryData.queryString = query; 117 fillModes( keys , true , defaultModeNames, modeTypes, queryData ); 118 if (useValues) fillModes( values, false, defaultModeNames, modeTypes, queryData ); 119 120 this.cachedQueryData.put( lookUpKey, queryData ); 121 } 122 } 123 124 return queryData; 125 } 126 127 128 131 protected Object [][] getColumnValues( Configuration tableConf, CacheHelper queryData, Map objectModel ) 132 throws ConfigurationException, ServiceException { 133 134 Object [][] columnValues = new Object [ queryData.columns.length ][]; 135 for ( int i = 0; i < queryData.columns.length; i++ ){ 136 columnValues[i] = this.getColumnValue( tableConf, queryData.columns[i], objectModel ); 137 } 138 return columnValues; 139 } 140 141 142 145 protected int processRow ( Map objectModel, Connection conn, PreparedStatement statement, String outputMode, 146 Configuration table, CacheHelper queryData, Object [][] columnValues, 147 int rowIndex, Map results ) 148 throws SQLException , ConfigurationException, Exception { 149 150 int currentIndex = 1; 151 152 for (int i = 0; i < queryData.columns.length; i++) { 154 Column col = queryData.columns[i]; 155 if ( col.isKey ) { 156 this.setColumn(objectModel, outputMode, results, table, col.columnConf, rowIndex, 157 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex ); 158 currentIndex++; 159 } 160 } 161 boolean hasResult = statement.execute(); 162 if (!hasResult) { 163 return statement.getUpdateCount(); 164 } else { 165 ResultSet resultset = statement.getResultSet(); 167 ResultSetMetaData metadata = resultset.getMetaData(); 168 rowIndex = 0; 169 while ( resultset.next() ){ 170 rowIndex++; 172 String tableName = ""; 174 String columnName = ""; 175 for (int i = 1; i <= metadata.getColumnCount(); i++) { 176 Object value = resultset.getObject(i); 177 tableName = metadata.getTableName(i); 178 columnName = metadata.getColumnLabel(i) + "[" + rowIndex + "]"; 179 if (!tableName.equals("")) { 180 columnName = tableName + "." + columnName; 181 } 182 if (this.getLogger().isDebugEnabled()) { 183 this.getLogger().debug("retrieving " + columnName + " as " + value); 184 } 185 results.put(metadata.getTableName(i)+"."+metadata.getColumnLabel(i),value); 186 } 187 } 188 return rowIndex; 189 } 190 } 191 192 193 } 194 | Popular Tags |