1 16 17 package org.apache.cocoon.acting.modular; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.SQLException ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.service.ServiceException; 27 28 35 public class DatabaseUpdateAction extends DatabaseAction { 36 37 42 protected String selectMode ( boolean isAutoIncrement, Map modes ) { 43 44 return (String ) modes.get( MODE_OTHERS ); 45 } 46 47 48 52 protected boolean honourAutoIncrement() { return false; } 53 54 55 59 protected Object [][] getColumnValues( Configuration tableConf, CacheHelper queryData, Map objectModel ) 60 throws ConfigurationException, ServiceException { 61 62 Object [][] columnValues = new Object [ queryData.columns.length ][]; 63 for ( int i = 0; i < queryData.columns.length; i++ ){ 64 columnValues[i] = this.getColumnValue( tableConf, queryData.columns[i], objectModel ); 65 } 66 return columnValues; 67 } 68 69 70 78 protected CacheHelper getQuery( Configuration table, Map modeTypes, Map defaultModeNames ) 79 throws ConfigurationException, ServiceException { 80 81 LookUpKey lookUpKey = new LookUpKey( table, modeTypes ); 82 CacheHelper queryData = null; 83 synchronized( this.cachedQueryData ) { 84 queryData = (CacheHelper) this.cachedQueryData.get( lookUpKey ); 85 if (queryData == null) { 86 Configuration[] keys = table.getChild("keys").getChildren("key"); 87 Configuration[] values = table.getChild("values").getChildren("value"); 88 89 queryData = new CacheHelper( keys.length, keys.length + values.length ); 90 fillModes( keys, true, defaultModeNames, modeTypes, queryData ); 91 fillModes( values, false, defaultModeNames, modeTypes, queryData ); 92 93 StringBuffer queryBuffer = new StringBuffer ("UPDATE "); 94 queryBuffer.append(table.getAttribute("name")); 95 96 if (values.length > 0){ 97 queryBuffer.append(" SET "); 98 int cols = 0; 99 for (int i = 0; i < queryData.columns.length; i++) { 100 if ( !queryData.columns[i].isKey ) { 101 if ( cols > 0 ) { 102 queryBuffer.append(", "); 103 } 104 cols++; 105 queryBuffer 106 .append( queryData.columns[i].columnConf.getAttribute( "name" ) ) 107 .append( "= ?" ); 108 } 109 } 110 } 111 112 queryBuffer.append(" WHERE "); 113 for (int i = 0; i < queryData.columns.length; i++) { 114 if ( queryData.columns[i].isKey ) { 115 if ( i > 0 ) { 116 queryBuffer.append(" AND "); 117 } 118 queryBuffer 119 .append( queryData.columns[i].columnConf.getAttribute( "name" ) ) 120 .append( "= ?" ); 121 } 122 } 123 124 queryData.queryString = queryBuffer.toString(); 125 126 this.cachedQueryData.put( lookUpKey, queryData ); 127 } 128 } 129 130 return queryData; 131 } 132 133 134 137 protected int processRow ( Map objectModel, Connection conn, PreparedStatement statement, String outputMode, 138 Configuration table, CacheHelper queryData, Object [][] columnValues, 139 int rowIndex, Map results ) 140 throws SQLException , ConfigurationException, Exception { 141 142 143 int currentIndex = 1; 144 145 for (int i = 0; i < queryData.columns.length; i++) { 147 Column col = queryData.columns[i]; 148 if ( !col.isKey ) { 149 this.setColumn( objectModel, outputMode, results, table, col.columnConf, rowIndex, 150 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex ); 151 currentIndex++; 152 } 153 } 154 for (int i = 0; i < queryData.columns.length; i++) { 155 Column col = queryData.columns[i]; 156 if ( col.isKey ) { 157 this.setColumn( objectModel, outputMode, results, table, col.columnConf, rowIndex, 158 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex ); 159 currentIndex++; 160 } 161 } 162 int rowCount = statement.executeUpdate(); 163 return rowCount; 164 } 165 166 } 167 | Popular Tags |