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: import org.apache.torque.adapter.DB;
025: import org.apache.torque.util.SQLBuilder;
026: import com.workingdogs.village.QueryDataSet;
027: import com.workingdogs.village.Record;
028: import com.workingdogs.village.Value;
029:
030: /**
031: * This generator works with databases that have an sql syntax that
032: * allows the retrieval of the last id used to insert a row for a
033: * Connection.
034: *
035: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
036: * @version $Id: AutoIncrementIdGenerator.java 473821 2006-11-11 22:37:25Z tv $
037: */
038: public class AutoIncrementIdGenerator implements IdGenerator {
039: /** the adapter that knows the correct sql syntax */
040: private DB dbAdapter;
041:
042: /** The internal name of the Database that this Generator is connected to */
043: private String name = null;
044:
045: /**
046: * Creates an IdGenerator which will work with the specified database.
047: *
048: * @param dbAdapter the adapter that knows the correct sql syntax.
049: * @param name The name of the datasource to find the correct schema
050: */
051: public AutoIncrementIdGenerator(final DB dbAdapter,
052: final String name) {
053: this .dbAdapter = dbAdapter;
054: this .name = name;
055: }
056:
057: /**
058: * Returns the last ID used by this connection.
059: *
060: * @param connection A Connection.
061: * @param keyInfo an Object that contains additional info.
062: * @return An int with the value for the id.
063: * @exception Exception Database error.
064: */
065: public int getIdAsInt(Connection connection, Object keyInfo)
066: throws Exception {
067: return getIdAsVillageValue(connection, keyInfo).asInt();
068: }
069:
070: /**
071: * Returns the last ID used by this connection.
072: *
073: * @param connection A Connection.
074: * @param keyInfo an Object that contains additional info.
075: * @return A long with the value for the id.
076: * @exception Exception Database error.
077: */
078: public long getIdAsLong(Connection connection, Object keyInfo)
079: throws Exception {
080: return getIdAsVillageValue(connection, keyInfo).asLong();
081: }
082:
083: /**
084: * Returns the last ID used by this connection.
085: *
086: * @param connection A Connection.
087: * @param keyInfo an Object that contains additional info.
088: * @return A BigDecimal with the last value auto-incremented as a
089: * result of an insert.
090: * @exception Exception Database error.
091: */
092: public BigDecimal getIdAsBigDecimal(Connection connection,
093: Object keyInfo) throws Exception {
094: return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
095: }
096:
097: /**
098: * Returns the last ID used by this connection.
099: *
100: * @param connection A Connection.
101: * @param keyInfo an Object that contains additional info.
102: * @return A String with the last value auto-incremented as a
103: * result of an insert.
104: * @exception Exception Database error.
105: */
106: public String getIdAsString(Connection connection, Object keyInfo)
107: throws Exception {
108: return getIdAsVillageValue(connection, keyInfo).asString();
109: }
110:
111: /**
112: * A flag to determine the timing of the id generation
113: *
114: * @return a <code>boolean</code> value
115: */
116: public boolean isPriorToInsert() {
117: return false;
118: }
119:
120: /**
121: * A flag to determine the timing of the id generation
122: *
123: * @return a <code>boolean</code> value
124: */
125: public boolean isPostInsert() {
126: return true;
127: }
128:
129: /**
130: * A flag to determine whether a Connection is required to
131: * generate an id.
132: *
133: * @return a <code>boolean</code> value
134: */
135: public final boolean isConnectionRequired() {
136: return true;
137: }
138:
139: /**
140: * Returns the last ID used by this connection.
141: *
142: * @param connection A Connection.
143: * @param keyInfo an Object that contains additional info.
144: * @return A Village Value with the last value auto-incremented as a
145: * result of an insert.
146: * @exception Exception Database error.
147: */
148: private Value getIdAsVillageValue(Connection connection,
149: Object keyInfo) throws Exception {
150: String tableName = SQLBuilder.getFullTableName(String
151: .valueOf(keyInfo), name);
152: String idSQL = dbAdapter.getIDMethodSQL(tableName);
153: Value id = null;
154: QueryDataSet qds = null;
155: try {
156: qds = new QueryDataSet(connection, idSQL);
157: qds.fetchRecords(1);
158: Record rec = qds.getRecord(0);
159: id = rec.getValue(1);
160: } finally {
161: if (qds != null) {
162: qds.close();
163: }
164: }
165: return id;
166: }
167: }
|