View Javadoc
1 package org.apache.torque.oid; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.math.BigDecimal; 58 import java.sql.Connection; 59 60 import org.apache.commons.logging.Log; 61 import org.apache.commons.logging.LogFactory; 62 63 import org.apache.torque.adapter.DB; 64 65 import com.workingdogs.village.QueryDataSet; 66 import com.workingdogs.village.Record; 67 import com.workingdogs.village.Value; 68 69 /*** 70 * This generator works with databases that have an sql syntax for 71 * getting an id prior to inserting a row into the database. 72 * 73 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> 74 * @version $Id: SequenceIdGenerator.java,v 1.10 2003/08/25 16:33:22 henning Exp $ 75 */ 76 public class SequenceIdGenerator implements IdGenerator 77 { 78 /*** The log. */ 79 private static Log log = LogFactory.getLog(SequenceIdGenerator.class); 80 81 /*** the adapter that knows the correct sql syntax */ 82 private DB dbAdapter; 83 84 /*** 85 * Creates an IdGenerator which will work with the specified database. 86 * 87 * @param adapter the adapter that knows the correct sql syntax. 88 */ 89 public SequenceIdGenerator(DB adapter) 90 { 91 dbAdapter = adapter; 92 } 93 94 /*** 95 * Retrieves an id as an int. 96 * 97 * @param connection A Connection. 98 * @param keyInfo an Object that contains additional info. 99 * @return An int with the value for the id. 100 * @exception Exception Database error. 101 */ 102 public int getIdAsInt(Connection connection, Object keyInfo) 103 throws Exception 104 { 105 return getIdAsVillageValue(connection, keyInfo).asInt(); 106 } 107 108 /*** 109 * Retrieves an id as an long. 110 * 111 * @param connection A Connection. 112 * @param keyInfo an Object that contains additional info. 113 * @return A long with the value for the id. 114 * @exception Exception Database error. 115 */ 116 public long getIdAsLong(Connection connection, Object keyInfo) 117 throws Exception 118 { 119 return getIdAsVillageValue(connection, keyInfo).asLong(); 120 } 121 122 /*** 123 * Retrieves an id as a BigDecimal. 124 * 125 * @param connection A Connection. 126 * @param keyInfo an Object that contains additional info. 127 * @return A BigDecimal id 128 * @exception Exception Database error. 129 */ 130 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo) 131 throws Exception 132 { 133 return getIdAsVillageValue(connection, keyInfo).asBigDecimal(); 134 } 135 136 /*** 137 * Retrieves an id as an String. 138 * 139 * @param connection A Connection. 140 * @param keyInfo an Object that contains additional info. 141 * @return A String id 142 * @exception Exception Database error. 143 */ 144 public String getIdAsString(Connection connection, Object keyInfo) 145 throws Exception 146 { 147 return getIdAsVillageValue(connection, keyInfo).asString(); 148 } 149 150 /*** 151 * A flag to determine the timing of the id generation 152 * 153 * @return a <code>boolean</code> value 154 */ 155 public boolean isPriorToInsert() 156 { 157 return true; 158 } 159 160 /*** 161 * A flag to determine the timing of the id generation 162 * 163 * @return a <code>boolean</code> value 164 */ 165 public boolean isPostInsert() 166 { 167 return false; 168 } 169 170 /*** 171 * A flag to determine whether a Connection is required to 172 * generate an id. 173 * 174 * @return a <code>boolean</code> value 175 */ 176 public boolean isConnectionRequired() 177 { 178 return true; 179 } 180 181 /*** 182 * Retrieves an id as a Village Value. 183 * 184 * @param connection A Connection. 185 * @param keyInfo an Object that contains additional info. 186 * @return A Village Value id. 187 * @exception Exception Database error. 188 */ 189 private Value getIdAsVillageValue(Connection connection, Object keyInfo) 190 throws Exception 191 { 192 String idSql = dbAdapter.getIDMethodSQL(keyInfo); 193 if (log.isDebugEnabled()) 194 { 195 log.debug(idSql); 196 } 197 198 // Execute the query. 199 QueryDataSet qds = new QueryDataSet(connection, idSql); 200 Record rec; 201 try 202 { 203 qds.fetchRecords(1); 204 rec = qds.getRecord(0); // Records are 0 based. 205 } 206 finally 207 { 208 if (qds != null) 209 { 210 qds.close(); 211 } 212 } 213 return rec.getValue(1); // Values are 1 based. 214 } 215 }

This page was automatically generated by Maven