001: package org.apache.ojb.broker.util.sequence;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.SystemUtils;
019: import org.apache.ojb.broker.PersistenceBroker;
020: import org.apache.ojb.broker.PersistenceBrokerException;
021: import org.apache.ojb.broker.metadata.JdbcTypesHelper;
022: import org.apache.ojb.broker.util.logging.LoggerFactory;
023: import org.apache.ojb.broker.accesslayer.ResultSetAndStatement;
024: import org.apache.ojb.broker.metadata.FieldDescriptor;
025: import org.apache.ojb.broker.metadata.JdbcType;
026: import org.apache.ojb.broker.query.Query;
027:
028: import java.sql.SQLException;
029:
030: /**
031: * An Implementation Class that will retrieve a valid new value
032: * for a PK field that is of type 'uniqueidentifier'. Since values
033: * for these types are generated through a 'newid()' call to
034: * MSSQL Server, this class is only valid for MSSQL Server 7.0 and up.
035: * <br/>
036: * This SequenceManager can be used for any classes that have their PK
037: * defined as a 'uniqueidetifier'
038: *
039: * @author <a href="mailto:aclute825@hotmail.com">Andrew Clute</a>
040: * @version $Id: SequenceManagerMSSQLGuidImpl.java,v 1.4.2.2 2005/12/21 22:28:41 tomdz Exp $
041: */
042: public class SequenceManagerMSSQLGuidImpl extends
043: AbstractSequenceManager {
044: private static final JdbcType JDBC_TYPE_VARCHAR = JdbcTypesHelper
045: .getJdbcTypeByName("varchar");
046:
047: /**
048: * Constructor used by
049: * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}
050: *
051: * @param broker PB instance to perform the
052: * id generation.
053: */
054: public SequenceManagerMSSQLGuidImpl(PersistenceBroker broker) {
055: super (broker);
056: }
057:
058: public Object getUniqueValue(FieldDescriptor field)
059: throws SequenceManagerException {
060: // only works for VARCHAR fields
061: if (!field.getJdbcType().equals(JDBC_TYPE_VARCHAR)) {
062: throw new SequenceManagerException(
063: "This implementation only works with fields defined"
064: + " as VARCHAR, but given field was "
065: + (field != null ? field.getJdbcType()
066: : null));
067: }
068: Object result = getUniqueString(field);
069: // perform a sql to java conversion here, so that clients do
070: // not see any db specific values
071: result = field.getFieldConversion().sqlToJava(result);
072: return result;
073: }
074:
075: /**
076: * returns a unique String for given field.
077: * the returned uid is unique accross all tables.
078: */
079: protected String getUniqueString(FieldDescriptor field)
080: throws SequenceManagerException {
081: ResultSetAndStatement rsStmt = null;
082: String returnValue = null;
083: try {
084: rsStmt = getBrokerForClass().serviceJdbcAccess()
085: .executeSQL("select newid()",
086: field.getClassDescriptor(),
087: Query.NOT_SCROLLABLE);
088: if (rsStmt.m_rs.next()) {
089: returnValue = rsStmt.m_rs.getString(1);
090: } else {
091: LoggerFactory.getDefaultLogger().error(
092: this .getClass()
093: + ": Can't lookup new oid for field "
094: + field);
095: }
096: } catch (PersistenceBrokerException e) {
097: throw new SequenceManagerException(e);
098: } catch (SQLException e) {
099: throw new SequenceManagerException(e);
100: }
101:
102: finally {
103: // close the used resources
104: if (rsStmt != null)
105: rsStmt.close();
106: }
107: return returnValue;
108: }
109:
110: /**
111: * Returns a new unique int for the given Class and fieldname.
112: */
113: protected int getUniqueId(FieldDescriptor field)
114: throws SequenceManagerException {
115: throw new SequenceManagerException(
116: SystemUtils.LINE_SEPARATOR
117: + "Failure attempting to retrieve a Guid for a field that is an int -- field should be returned as a VARCHAR");
118: }
119:
120: /**
121: * Returns a new unique int for the given Class and fieldname.
122: */
123: protected long getUniqueLong(FieldDescriptor field)
124: throws SequenceManagerException {
125: throw new SequenceManagerException(
126: SystemUtils.LINE_SEPARATOR
127: + "Failure attempting to retrieve a Guid for a field that is a long -- field should be returned as a VARCHAR");
128: }
129:
130: // TODO: never used? remove?
131: // /**
132: // * returns a unique Object for class clazz and field fieldName.
133: // * the returned Object is unique accross all tables in the extent of clazz.
134: // */
135: // protected Object getUniqueObject(FieldDescriptor field) throws SequenceManagerException
136: // {
137: // return getUniqueString(field);
138: // }
139: }
|