001: package org.apache.torque.oid;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.math.BigDecimal;
023: import java.sql.Connection;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.apache.torque.adapter.DB;
029: import org.apache.torque.util.SQLBuilder;
030:
031: import com.workingdogs.village.QueryDataSet;
032: import com.workingdogs.village.Record;
033: import com.workingdogs.village.Value;
034:
035: /**
036: * This generator works with databases that have an sql syntax for
037: * getting an id prior to inserting a row into the database.
038: *
039: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
040: * @version $Id: SequenceIdGenerator.java 473821 2006-11-11 22:37:25Z tv $
041: */
042: public class SequenceIdGenerator implements IdGenerator {
043: /** The log. */
044: private static Log log = LogFactory
045: .getLog(SequenceIdGenerator.class);
046:
047: /** the adapter that knows the correct sql syntax */
048: private DB dbAdapter;
049:
050: /** The internal name of the Database that this Generator is connected to */
051: private String name = null;
052:
053: /**
054: * Creates an IdGenerator which will work with the specified database.
055: *
056: * @param dbAdapter the adapter that knows the correct sql syntax.
057: * @param name The name of the datasource to find the correct schema
058: */
059: public SequenceIdGenerator(final DB dbAdapter, final String name) {
060: this .dbAdapter = dbAdapter;
061: this .name = name;
062: }
063:
064: /**
065: * Retrieves an id as an int.
066: *
067: * @param connection A Connection.
068: * @param keyInfo an Object that contains additional info.
069: * @return An int with the value for the id.
070: * @exception Exception Database error.
071: */
072: public int getIdAsInt(Connection connection, Object keyInfo)
073: throws Exception {
074: return getIdAsVillageValue(connection, keyInfo).asInt();
075: }
076:
077: /**
078: * Retrieves an id as an long.
079: *
080: * @param connection A Connection.
081: * @param keyInfo an Object that contains additional info.
082: * @return A long with the value for the id.
083: * @exception Exception Database error.
084: */
085: public long getIdAsLong(Connection connection, Object keyInfo)
086: throws Exception {
087: return getIdAsVillageValue(connection, keyInfo).asLong();
088: }
089:
090: /**
091: * Retrieves an id as a BigDecimal.
092: *
093: * @param connection A Connection.
094: * @param keyInfo an Object that contains additional info.
095: * @return A BigDecimal id
096: * @exception Exception Database error.
097: */
098: public BigDecimal getIdAsBigDecimal(Connection connection,
099: Object keyInfo) throws Exception {
100: return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
101: }
102:
103: /**
104: * Retrieves an id as an String.
105: *
106: * @param connection A Connection.
107: * @param keyInfo an Object that contains additional info.
108: * @return A String id
109: * @exception Exception Database error.
110: */
111: public String getIdAsString(Connection connection, Object keyInfo)
112: throws Exception {
113: return getIdAsVillageValue(connection, keyInfo).asString();
114: }
115:
116: /**
117: * A flag to determine the timing of the id generation
118: *
119: * @return a <code>boolean</code> value
120: */
121: public boolean isPriorToInsert() {
122: return true;
123: }
124:
125: /**
126: * A flag to determine the timing of the id generation
127: *
128: * @return a <code>boolean</code> value
129: */
130: public boolean isPostInsert() {
131: return false;
132: }
133:
134: /**
135: * A flag to determine whether a Connection is required to
136: * generate an id.
137: *
138: * @return a <code>boolean</code> value
139: */
140: public boolean isConnectionRequired() {
141: return true;
142: }
143:
144: /**
145: * Retrieves an id as a Village Value.
146: *
147: * @param connection A Connection.
148: * @param keyInfo an Object that contains additional info.
149: * @return A Village Value id.
150: * @exception Exception Database error.
151: */
152: private Value getIdAsVillageValue(Connection connection,
153: Object keyInfo) throws Exception {
154: String sequenceName = SQLBuilder.getFullTableName(String
155: .valueOf(keyInfo), name);
156: String idSql = dbAdapter.getIDMethodSQL(sequenceName);
157: if (log.isDebugEnabled()) {
158: log.debug(idSql);
159: }
160:
161: // Execute the query.
162: QueryDataSet qds = new QueryDataSet(connection, idSql);
163: Record rec;
164: try {
165: qds.fetchRecords(1);
166: rec = qds.getRecord(0); // Records are 0 based.
167: } finally {
168: qds.close();
169: }
170: return rec.getValue(1); // Values are 1 based.
171: }
172: }
|