| 1 | |
package org.apache.ojb.broker.util.sequence; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class SequenceManagerMSSQLGuidImpl extends AbstractSequenceManager |
| 44 | |
{ |
| 45 | |
private static final JdbcType JDBC_TYPE_VARCHAR = JdbcTypesHelper.getJdbcTypeByName("varchar"); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public SequenceManagerMSSQLGuidImpl(PersistenceBroker broker) |
| 54 | |
{ |
| 55 | |
super(broker); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public Object getUniqueValue(FieldDescriptor field) throws SequenceManagerException |
| 59 | |
{ |
| 60 | |
|
| 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 result = getUniqueString(field); |
| 67 | |
|
| 68 | |
|
| 69 | |
result = field.getFieldConversion().sqlToJava(result); |
| 70 | |
return result; |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
protected String getUniqueString(FieldDescriptor field) throws SequenceManagerException |
| 78 | |
{ |
| 79 | |
ResultSetAndStatement rsStmt = null; |
| 80 | |
String 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 e) |
| 100 | |
{ |
| 101 | |
throw new SequenceManagerException(e); |
| 102 | |
} |
| 103 | |
|
| 104 | |
finally |
| 105 | |
{ |
| 106 | |
|
| 107 | |
if (rsStmt != null) rsStmt.close(); |
| 108 | |
} |
| 109 | |
return returnValue; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 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 | |
|
| 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 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
} |
| 142 | |
|