1 16 17 package org.apache.cocoon.components.modules.database; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 31 46 public class ManualAutoIncrementModule extends AbstractAutoIncrementModule implements ThreadSafe { 47 48 private Map selectStatements = new HashMap (); 49 50 51 public Object getPostValue( Configuration tableConf, Configuration columnConf, Configuration modenConf, 52 Connection conn, Statement stmt, Map objectModel ) 53 throws SQLException , ConfigurationException { 54 55 return null; 56 } 57 58 public boolean includeInQuery( ) { return true; } 59 60 61 public boolean includeAsValue( ) { return true; } 62 63 64 public Object getPreValue( Configuration tableConf, Configuration columnConf, Configuration modeConf, 65 Connection conn, Map objectModel ) 66 throws SQLException , ConfigurationException { 67 68 69 String tableName = tableConf.getAttribute("name",""); 70 String selectQuery = this.getSelectQuery(tableName, columnConf); 71 PreparedStatement select_statement = conn.prepareStatement(selectQuery); 72 ResultSet set = select_statement.executeQuery(); 73 set.next(); 74 int maxid = set.getInt("maxid"); 75 set.close(); 76 select_statement.close(); 77 if (getLogger().isDebugEnabled()) 78 getLogger().debug("autoincrementValue " + (maxid+1)); 79 return new Integer (maxid + 1); 80 } 81 82 83 public String getSubquery( Configuration tableConf, Configuration columnConf, Configuration modeConf ) 84 throws ConfigurationException { 85 86 return null; 87 } 88 89 90 95 protected final synchronized void setSelectQuery( String tableName, Configuration entry ) throws ConfigurationException { 96 97 StringBuffer queryBuffer = new StringBuffer ("SELECT max("); 98 queryBuffer.append(entry.getAttribute("name")); 99 queryBuffer.append(") AS maxid FROM "); 100 queryBuffer.append(tableName); 101 102 this.selectStatements.put(entry, queryBuffer.toString()); 103 } 104 105 106 protected final synchronized String getSelectQuery( String tableName, Configuration entry ) throws ConfigurationException { 107 108 String result = (String ) this.selectStatements.get(entry); 109 if (result == null) { 110 setSelectQuery(tableName, entry); 111 result = (String ) this.selectStatements.get(entry); 112 } 113 return result; 114 } 115 116 } 117 | Popular Tags |