View Javadoc

1   package org.apache.torque.oid;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.math.BigDecimal;
20  import java.sql.Connection;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.apache.torque.adapter.DB;
26  
27  import com.workingdogs.village.QueryDataSet;
28  import com.workingdogs.village.Record;
29  import com.workingdogs.village.Value;
30  
31  /***
32   * This generator works with databases that have an sql syntax for
33   * getting an id prior to inserting a row into the database.
34   *
35   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
36   * @version $Id: SequenceIdGenerator.java,v 1.10.2.2 2004/05/20 04:36:07 seade Exp $
37   */
38  public class SequenceIdGenerator implements IdGenerator
39  {
40      /*** The log. */
41      private static Log log = LogFactory.getLog(SequenceIdGenerator.class);
42  
43      /*** the adapter that knows the correct sql syntax */
44      private DB dbAdapter;
45  
46      /***
47       * Creates an IdGenerator which will work with the specified database.
48       *
49       * @param adapter the adapter that knows the correct sql syntax.
50       */
51      public SequenceIdGenerator(DB adapter)
52      {
53          dbAdapter = adapter;
54      }
55  
56      /***
57       * Retrieves an id as an int.
58       *
59       * @param connection A Connection.
60       * @param keyInfo an Object that contains additional info.
61       * @return An int with the value for the id.
62       * @exception Exception Database error.
63       */
64      public int getIdAsInt(Connection connection, Object keyInfo)
65          throws Exception
66      {
67          return getIdAsVillageValue(connection, keyInfo).asInt();
68      }
69  
70      /***
71       * Retrieves an id as an long.
72       *
73       * @param connection A Connection.
74       * @param keyInfo an Object that contains additional info.
75       * @return A long with the value for the id.
76       * @exception Exception Database error.
77       */
78      public long getIdAsLong(Connection connection, Object keyInfo)
79          throws Exception
80      {
81          return getIdAsVillageValue(connection, keyInfo).asLong();
82      }
83  
84      /***
85       * Retrieves an id as a BigDecimal.
86       *
87       * @param connection A Connection.
88       * @param keyInfo an Object that contains additional info.
89       * @return A BigDecimal id
90       * @exception Exception Database error.
91       */
92      public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
93          throws Exception
94      {
95          return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
96      }
97  
98      /***
99       * Retrieves an id as an String.
100      *
101      * @param connection A Connection.
102      * @param keyInfo an Object that contains additional info.
103      * @return A String id
104      * @exception Exception Database error.
105      */
106     public String getIdAsString(Connection connection, Object keyInfo)
107         throws Exception
108     {
109         return getIdAsVillageValue(connection, keyInfo).asString();
110     }
111 
112     /***
113      * A flag to determine the timing of the id generation
114      *
115      * @return a <code>boolean</code> value
116      */
117     public boolean isPriorToInsert()
118     {
119         return true;
120     }
121 
122     /***
123      * A flag to determine the timing of the id generation
124      *
125      * @return a <code>boolean</code> value
126      */
127     public boolean isPostInsert()
128     {
129         return false;
130     }
131 
132     /***
133      * A flag to determine whether a Connection is required to
134      * generate an id.
135      *
136      * @return a <code>boolean</code> value
137      */
138     public boolean isConnectionRequired()
139     {
140         return true;
141     }
142 
143     /***
144      * Retrieves an id as a Village Value.
145      *
146      * @param connection A Connection.
147      * @param keyInfo an Object that contains additional info.
148      * @return A Village Value id.
149      * @exception Exception Database error.
150      */
151     private Value getIdAsVillageValue(Connection connection, Object keyInfo)
152         throws Exception
153     {
154         String idSql = dbAdapter.getIDMethodSQL(keyInfo);
155         if (log.isDebugEnabled())
156         {
157             log.debug(idSql);
158         }
159 
160         // Execute the query.
161         QueryDataSet qds = new QueryDataSet(connection, idSql);
162         Record rec;
163         try
164         {
165             qds.fetchRecords(1);
166             rec = qds.getRecord(0);  // Records are 0 based.
167         }
168         finally
169         {
170             if (qds != null)
171             {
172                 qds.close();
173             }
174         }
175         return rec.getValue(1); // Values are 1 based.
176     }
177 }