Coverage report

  %line %branch
org.apache.torque.oid.SequenceIdGenerator
0% 
0% 

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.