KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > database > ManualAutoIncrementModule


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.components.modules.database;
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.sql.Statement JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
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 /**
32  * Abstraction layer to encapsulate different DBMS behaviour for
33  * autoincrement columns.
34  *
35  * Here: manual mode The new value is determined by doing a "select
36  * max(column)+1 from table" query. With transactions and correct
37  * isolation levels, this should to the trick almost everywhere.
38  *
39  * Note however, that the above query does not prevent a parallel
40  * transaction to try to insert a row with the same ID since it
41  * requires only shared locks. C.f. "Phantom Problem"
42  *
43  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
44  * @version CVS $Id: ManualAutoIncrementModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
45  */

46 public class ManualAutoIncrementModule extends AbstractAutoIncrementModule implements ThreadSafe {
47
48     private Map JavaDoc selectStatements = new HashMap JavaDoc();
49
50
51     public Object JavaDoc getPostValue( Configuration tableConf, Configuration columnConf, Configuration modenConf,
52                                 Connection JavaDoc conn, Statement JavaDoc stmt, Map JavaDoc objectModel )
53         throws SQLException JavaDoc, 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 JavaDoc getPreValue( Configuration tableConf, Configuration columnConf, Configuration modeConf,
65                                Connection JavaDoc conn, Map JavaDoc objectModel )
66         throws SQLException JavaDoc, ConfigurationException {
67
68         /** Set the key value using SELECT MAX(keyname)+1 **/
69         String JavaDoc tableName = tableConf.getAttribute("name","");
70         String JavaDoc selectQuery = this.getSelectQuery(tableName, columnConf);
71         PreparedStatement JavaDoc select_statement = conn.prepareStatement(selectQuery);
72         ResultSet JavaDoc 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 JavaDoc(maxid + 1);
80     }
81
82
83     public String JavaDoc getSubquery( Configuration tableConf, Configuration columnConf, Configuration modeConf )
84         throws ConfigurationException {
85
86         return null;
87     }
88
89
90     /**
91      * Set the String representation of the MaxID lookup statement. This is
92      * mapped to the Configuration object itself, so if it doesn't exist,
93      * it will be created.
94      */

95     protected final synchronized void setSelectQuery( String JavaDoc tableName, Configuration entry ) throws ConfigurationException {
96
97         StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("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 JavaDoc getSelectQuery( String JavaDoc tableName, Configuration entry ) throws ConfigurationException {
107
108         String JavaDoc result = (String JavaDoc) this.selectStatements.get(entry);
109         if (result == null) {
110             setSelectQuery(tableName, entry);
111             result = (String JavaDoc) this.selectStatements.get(entry);
112         }
113         return result;
114     }
115
116 }
117
Popular Tags