KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.Integer JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29
30 /**
31  * Abstraction layer to encapsulate different DBMS behaviour for autoincrement columns.
32  *
33  * Here: <a HREF="http://www.postgres.org">PostgreSQL</a>
34  * sequences. The default sequence name is constructed from the table
35  * name, a "_", the column name, and the suffix "_seq". To use a
36  * different sequence name, set an attribute "sequence" for the
37  * modeConf e.g. &lt;mode name="auto" type="auto" sequence="my_sequence"/&gt;.
38  *
39  * @author <a HREF="mailto:pmhahn@titan.lahn.de">Philipp Hahn</a>
40  * @version CVS $Id: PgsqlAutoIncrementModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class PgsqlAutoIncrementModule implements AutoIncrementModule, ThreadSafe {
43
44     public Object JavaDoc getPostValue( Configuration tableConf, Configuration columnConf, Configuration modeConf,
45                                 Connection JavaDoc conn, Statement JavaDoc stmt, Map JavaDoc objectModel ) throws SQLException JavaDoc, ConfigurationException {
46
47         Integer JavaDoc id = null;
48         /*
49           // Postgres does support callable statements ... i'm not sure what what would go here, maybe:
50
51           CallableStatement callStmt = conn.prepareCall("? = {CALL LAST_INSERT_ID()}");
52           callStmt.registerOutParameter(1, Types.INTEGER);
53           ResultSet resultSet = callStmt.executeQuery();
54         */

55
56         String JavaDoc sequence = modeConf.getAttribute("sequence",null);
57
58         StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("SELECT currval('");
59         if (sequence != null) {
60             queryBuffer.append(sequence);
61         } else {
62             queryBuffer.append(tableConf.getAttribute("name",""));
63             queryBuffer.append('_');
64             queryBuffer.append(columnConf.getAttribute("name"));
65             queryBuffer.append("_seq");
66         }
67         queryBuffer.append("')");
68         
69         PreparedStatement JavaDoc pstmt = conn.prepareStatement(queryBuffer.toString());
70         ResultSet JavaDoc resultSet = pstmt.executeQuery();
71         while ( resultSet.next() ) {
72             id = new Integer JavaDoc(resultSet.getInt(1));
73         }
74         resultSet.close();
75
76         return id;
77     }
78
79
80     public boolean includeInQuery() { return false; }
81
82
83     public boolean includeAsValue() { return false; }
84
85
86     public Object JavaDoc getPreValue( Configuration tableConf, Configuration columnConf, Configuration modeConf,
87                                Connection JavaDoc conn, Map JavaDoc objectModel ) throws SQLException JavaDoc, ConfigurationException {
88
89         return null;
90     }
91
92     public String JavaDoc getSubquery( Configuration tableConf, Configuration columnConf, Configuration modeConf )
93         throws ConfigurationException {
94
95         return null;
96     }
97 }
98
Popular Tags