KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > sequence > SequenceManagerMSSQLGuidImpl


1 package org.apache.ojb.broker.util.sequence;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.commons.lang.SystemUtils;
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.broker.PersistenceBrokerException;
21 import org.apache.ojb.broker.metadata.JdbcTypesHelper;
22 import org.apache.ojb.broker.util.logging.LoggerFactory;
23 import org.apache.ojb.broker.accesslayer.ResultSetAndStatement;
24 import org.apache.ojb.broker.metadata.FieldDescriptor;
25 import org.apache.ojb.broker.metadata.JdbcType;
26 import org.apache.ojb.broker.query.Query;
27
28 import java.sql.SQLException JavaDoc;
29
30
31 /**
32  * An Implementation Class that will retrieve a valid new value
33  * for a PK field that is of type 'uniqueidentifier'. Since values
34  * for these types are generated through a 'newid()' call to
35  * MSSQL Server, this class is only valid for MSSQL Server 7.0 and up.
36  * <br/>
37  * This SequenceManager can be used for any classes that have their PK
38  * defined as a 'uniqueidetifier'
39  *
40  * @author <a HREF="mailto:aclute825@hotmail.com">Andrew Clute</a>
41  * @version $Id: SequenceManagerMSSQLGuidImpl.java,v 1.4.2.2 2005/12/21 22:28:41 tomdz Exp $
42  */

43 public class SequenceManagerMSSQLGuidImpl extends AbstractSequenceManager
44 {
45     private static final JdbcType JDBC_TYPE_VARCHAR = JdbcTypesHelper.getJdbcTypeByName("varchar");
46     /**
47      * Constructor used by
48      * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}
49      *
50      * @param broker PB instance to perform the
51      * id generation.
52      */

53     public SequenceManagerMSSQLGuidImpl(PersistenceBroker broker)
54     {
55         super(broker);
56     }
57
58     public Object JavaDoc getUniqueValue(FieldDescriptor field) throws SequenceManagerException
59     {
60         // only works for VARCHAR fields
61
if(!field.getJdbcType().equals(JDBC_TYPE_VARCHAR))
62         {
63             throw new SequenceManagerException("This implementation only works with fields defined" +
64                     " as VARCHAR, but given field was " + (field != null ? field.getJdbcType() : null));
65         }
66         Object JavaDoc result = getUniqueString(field);
67         // perform a sql to java conversion here, so that clients do
68
// not see any db specific values
69
result = field.getFieldConversion().sqlToJava(result);
70         return result;
71     }
72
73     /**
74      * returns a unique String for given field.
75      * the returned uid is unique accross all tables.
76      */

77     protected String JavaDoc getUniqueString(FieldDescriptor field) throws SequenceManagerException
78     {
79         ResultSetAndStatement rsStmt = null;
80         String JavaDoc returnValue = null;
81         try
82         {
83             rsStmt = getBrokerForClass().serviceJdbcAccess().executeSQL(
84                     "select newid()", field.getClassDescriptor(), Query.NOT_SCROLLABLE);
85             if (rsStmt.m_rs.next())
86             {
87                 returnValue = rsStmt.m_rs.getString(1);
88             }
89             else
90             {
91                 LoggerFactory.getDefaultLogger().error(this.getClass()
92                         + ": Can't lookup new oid for field " + field);
93             }
94         }
95         catch (PersistenceBrokerException e)
96         {
97             throw new SequenceManagerException(e);
98         }
99         catch (SQLException JavaDoc e)
100         {
101             throw new SequenceManagerException(e);
102         }
103
104         finally
105         {
106             // close the used resources
107
if (rsStmt != null) rsStmt.close();
108         }
109         return returnValue;
110     }
111
112     /**
113      * Returns a new unique int for the given Class and fieldname.
114      */

115     protected int getUniqueId(FieldDescriptor field) throws SequenceManagerException
116     {
117         throw new SequenceManagerException(
118                 SystemUtils.LINE_SEPARATOR +
119                 "Failure attempting to retrieve a Guid for a field that is an int -- field should be returned as a VARCHAR");
120     }
121
122     /**
123      * Returns a new unique int for the given Class and fieldname.
124      */

125     protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException
126     {
127         throw new SequenceManagerException(
128                 SystemUtils.LINE_SEPARATOR +
129                 "Failure attempting to retrieve a Guid for a field that is a long -- field should be returned as a VARCHAR");
130     }
131
132     // TODO: never used? remove?
133
// /**
134
// * returns a unique Object for class clazz and field fieldName.
135
// * the returned Object is unique accross all tables in the extent of clazz.
136
// */
137
// protected Object getUniqueObject(FieldDescriptor field) throws SequenceManagerException
138
// {
139
// return getUniqueString(field);
140
// }
141
}
142
143
Popular Tags