01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.components.modules.database;
19:
20: import java.sql.Connection;
21: import java.sql.SQLException;
22: import java.sql.Statement;
23: import java.util.Map;
24:
25: import org.apache.avalon.framework.configuration.Configuration;
26: import org.apache.avalon.framework.configuration.ConfigurationException;
27: import org.apache.avalon.framework.thread.ThreadSafe;
28:
29: /**
30: * Abstraction layer to encapsulate different DBMS behaviour for autoincrement columns.
31: *
32: * Here: <a href="http://www.mckoi.org">McKoi</a> sequences. The default
33: * sequence name is constructed from the table name, a "_", the column name, and
34: * the suffix "_seq". To use a different sequence name, set an attribute
35: * "sequence" for the modeConf e.g. <mode name="auto" type="auto"
36: * sequence="my_sequence"/>.
37: *
38: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
39: * @version CVS $Id: McKoiSequenceModule.java 433543 2006-08-22 06:22:54Z crossley $
40: */
41: public class McKoiSequenceModule implements AutoIncrementModule,
42: ThreadSafe {
43:
44: public Object getPostValue(Configuration tableConf,
45: Configuration columnConf, Configuration modeConf,
46: Connection conn, Statement stmt, Map objectModel)
47: throws SQLException, ConfigurationException {
48: return null;
49: }
50:
51: public boolean includeInQuery() {
52: return true;
53: }
54:
55: public boolean includeAsValue() {
56: return false;
57: }
58:
59: public Object getPreValue(Configuration tableConf,
60: Configuration columnConf, Configuration modeConf,
61: Connection conn, Map objectModel) throws SQLException,
62: ConfigurationException {
63:
64: return null;
65: }
66:
67: public String getSubquery(Configuration tableConf,
68: Configuration columnConf, Configuration modeConf)
69: throws ConfigurationException {
70:
71: String sequence = modeConf.getAttribute("sequence", null);
72: StringBuffer queryBuffer = new StringBuffer();
73: queryBuffer.append(" NEXTVAL('");
74: if (sequence != null) {
75: queryBuffer.append(sequence);
76: } else {
77: queryBuffer.append(tableConf.getAttribute("name", ""));
78: queryBuffer.append('_');
79: queryBuffer.append(columnConf.getAttribute("name"));
80: queryBuffer.append("_seq");
81: }
82: queryBuffer.append("')");
83:
84: return queryBuffer.toString();
85: }
86: }
|