001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.modules.database;
019:
020: import java.lang.Integer;
021: import java.sql.PreparedStatement;
022: import java.sql.Connection;
023: import java.sql.ResultSet;
024: import java.sql.Statement;
025: import java.sql.SQLException;
026: import java.util.Map;
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.avalon.framework.thread.ThreadSafe;
030:
031: /**
032: * Abstraction layer to encapsulate different DBMS behaviour for autoincrement columns.
033: *
034: * Here: <a href="http://www.postgres.org">PostgreSQL</a>
035: * sequences. The default sequence name is constructed from the table
036: * name, a "_", the column name, and the suffix "_seq". To use a
037: * different sequence name, set an attribute "sequence" for the
038: * modeConf e.g. <mode name="auto" type="auto" sequence="my_sequence"/>.
039: *
040: * @author <a href="mailto:pmhahn@titan.lahn.de">Philipp Hahn</a>
041: * @version CVS $Id: PgsqlAutoIncrementModule.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class PgsqlAutoIncrementModule implements AutoIncrementModule,
044: ThreadSafe {
045:
046: public Object getPostValue(Configuration tableConf,
047: Configuration columnConf, Configuration modeConf,
048: Connection conn, Statement stmt, Map objectModel)
049: throws SQLException, ConfigurationException {
050:
051: Integer id = null;
052: /*
053: // Postgres does support callable statements ... i'm not sure what what would go here, maybe:
054:
055: CallableStatement callStmt = conn.prepareCall("? = {CALL LAST_INSERT_ID()}");
056: callStmt.registerOutParameter(1, Types.INTEGER);
057: ResultSet resultSet = callStmt.executeQuery();
058: */
059:
060: String sequence = modeConf.getAttribute("sequence", null);
061:
062: StringBuffer queryBuffer = new StringBuffer("SELECT currval('");
063: if (sequence != null) {
064: queryBuffer.append(sequence);
065: } else {
066: queryBuffer.append(tableConf.getAttribute("name", ""));
067: queryBuffer.append('_');
068: queryBuffer.append(columnConf.getAttribute("name"));
069: queryBuffer.append("_seq");
070: }
071: queryBuffer.append("')");
072:
073: PreparedStatement pstmt = conn.prepareStatement(queryBuffer
074: .toString());
075: ResultSet resultSet = pstmt.executeQuery();
076: while (resultSet.next()) {
077: id = new Integer(resultSet.getInt(1));
078: }
079: resultSet.close();
080:
081: return id;
082: }
083:
084: public boolean includeInQuery() {
085: return false;
086: }
087:
088: public boolean includeAsValue() {
089: return false;
090: }
091:
092: public Object getPreValue(Configuration tableConf,
093: Configuration columnConf, Configuration modeConf,
094: Connection conn, Map objectModel) throws SQLException,
095: ConfigurationException {
096:
097: return null;
098: }
099:
100: public String getSubquery(Configuration tableConf,
101: Configuration columnConf, Configuration modeConf)
102: throws ConfigurationException {
103:
104: return null;
105: }
106: }
|