summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado
diff options
context:
space:
mode:
authorAdam D. Bradley <artdodge@users.sourceforge.net>2008-10-08 18:14:30 +0000
committerAdam D. Bradley <artdodge@users.sourceforge.net>2008-10-08 18:14:30 +0000
commit72cc79e0d15503705263f9efaf0cbeec15230bf3 (patch)
tree992bd50e4fe7273a23e603703ad2cd79a70a9255 /src/main/java/com/amazon/carbonado
parent4bfd18e608a6e18ffbdc2c1efad5c90ad95d3092 (diff)
Add "setPrimaryKeyCheckDisable" property to JDBCRepositoryBuilder.
Diffstat (limited to 'src/main/java/com/amazon/carbonado')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java7
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java20
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java38
3 files changed, 49 insertions, 16 deletions
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java
index 03383c6..16f44cc 100644
--- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java
+++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java
@@ -69,6 +69,7 @@ import com.amazon.carbonado.util.ThrowUnchecked;
*
* @author Brian S O'Neill
* @author bcastill
+ * @author Adam D Bradley
* @see JDBCRepositoryBuilder
*/
class JDBCRepository extends AbstractRepository<JDBCTransaction>
@@ -173,6 +174,7 @@ class JDBCRepository extends AbstractRepository<JDBCTransaction>
private final String mCatalog;
private final String mSchema;
private final Integer mFetchSize;
+ private final boolean mPrimaryKeyCheckDisabled;
// Maps Storable types which should have automatic version management.
private Map<String, Boolean> mAutoVersioningMap;
@@ -226,7 +228,7 @@ class JDBCRepository extends AbstractRepository<JDBCTransaction>
Integer fetchSize,
Map<String, Boolean> autoVersioningMap,
Map<String, Boolean> suppressReloadMap,
- String sequenceSelectStatement, boolean forceStoredSequence,
+ String sequenceSelectStatement, boolean forceStoredSequence, boolean primaryKeyCheckDisabled,
SchemaResolver resolver)
throws RepositoryException
{
@@ -242,6 +244,7 @@ class JDBCRepository extends AbstractRepository<JDBCTransaction>
mCatalog = catalog;
mSchema = schema;
mFetchSize = fetchSize;
+ mPrimaryKeyCheckDisabled = primaryKeyCheckDisabled;
mAutoVersioningMap = autoVersioningMap;
mSuppressReloadMap = suppressReloadMap;
@@ -351,7 +354,7 @@ class JDBCRepository extends AbstractRepository<JDBCTransaction>
{
try {
return JDBCStorableIntrospector
- .examine(type, mDataSource, mCatalog, mSchema, mResolver);
+ .examine(type, mDataSource, mCatalog, mSchema, mResolver, mPrimaryKeyCheckDisabled);
} catch (SQLException e) {
throw toRepositoryException(e);
}
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
index 57629b6..e18ba67 100644
--- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
+++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
@@ -57,6 +57,7 @@ import com.amazon.carbonado.spi.AbstractRepositoryBuilder;
*
* @author Brian S O'Neill
* @author bcastill
+ * @author Adam D Bradley
*/
public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
private String mName;
@@ -75,6 +76,7 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
private Map<String, Boolean> mSuppressReloadMap;
private String mSequenceSelectStatement;
private boolean mForceStoredSequence;
+ private boolean mPrimaryKeyCheckDisabled;
private SchemaResolver mResolver;
@@ -90,7 +92,7 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
mFetchSize,
getAutoVersioningMap(),
getSuppressReloadMap(),
- mSequenceSelectStatement, mForceStoredSequence,
+ mSequenceSelectStatement, mForceStoredSequence, mPrimaryKeyCheckDisabled,
mResolver);
rootRef.set(repo);
return repo;
@@ -395,6 +397,22 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
mForceStoredSequence = forceStoredSequence;
}
+ /**
+ * By default, JDBCRepository makes sure that every declared primary key
+ * in the database table for a Storable lines up with a declared
+ * PrimaryKey or AlternateKey. This is not always the desired behavior;
+ * for example, you may have a table which uses a bigint for its actual
+ * primary key but uses another column with a unique index as the
+ * "primary" key from the application's point of view. Setting this
+ * value to true allows this check to fail gracefully instead of
+ * throwing a {@link com.amazon.carbonado.MismatchException}.
+ *
+ * @since 1.2
+ */
+ public void setPrimaryKeyCheckDisabled(boolean primaryKeyCheckDisabled) {
+ mPrimaryKeyCheckDisabled = primaryKeyCheckDisabled;
+ }
+
@Override
public void errorCheck(Collection<String> messages) throws ConfigurationException {
super.errorCheck(messages);
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java
index f845e3b..9809e69 100644
--- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java
+++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java
@@ -72,6 +72,7 @@ import com.amazon.carbonado.info.StorablePropertyConstraint;
* unless the examination failed.
*
* @author Brian S O'Neill
+ * @author Adam D Bradley
*/
public class JDBCStorableIntrospector extends StorableIntrospector {
// Maps compound keys to softly referenced JDBCStorableInfo objects.
@@ -97,11 +98,11 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
(Class<S> type, DataSource ds, String catalog, String schema)
throws SQLException, SupportException
{
- return examine(type, ds, catalog, schema, null);
+ return examine(type, ds, catalog, schema, null, false);
}
static <S extends Storable> JDBCStorableInfo<S> examine
- (Class<S> type, DataSource ds, String catalog, String schema, SchemaResolver resolver)
+ (Class<S> type, DataSource ds, String catalog, String schema, SchemaResolver resolver, boolean primaryKeyCheckDisabled)
throws SQLException, SupportException
{
Object key = KeyFactory.createKey(new Object[] {type, ds, catalog, schema});
@@ -117,15 +118,15 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
Connection con = ds.getConnection();
try {
try {
- jInfo = examine(mainInfo, con, catalog, schema, resolver);
+ jInfo = examine(mainInfo, con, catalog, schema, resolver, primaryKeyCheckDisabled);
if (!jInfo.isSupported() && resolver != null &&
resolver.resolve(mainInfo, con, catalog, schema))
{
- jInfo = examine(mainInfo, con, catalog, schema, resolver);
+ jInfo = examine(mainInfo, con, catalog, schema, resolver, primaryKeyCheckDisabled);
}
} catch (SupportException e) {
if (resolver != null && resolver.resolve(mainInfo, con, catalog, schema)) {
- jInfo = examine(mainInfo, con, catalog, schema, resolver);
+ jInfo = examine(mainInfo, con, catalog, schema, resolver, primaryKeyCheckDisabled);
} else {
throw e;
}
@@ -161,7 +162,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
private static <S extends Storable> JDBCStorableInfo<S> examine
(StorableInfo<S> mainInfo, Connection con,
final String searchCatalog, final String searchSchema,
- SchemaResolver resolver)
+ SchemaResolver resolver, boolean primaryKeyCheckDisabled)
throws SQLException, SupportException
{
final DatabaseMetaData meta = con.getMetaData();
@@ -301,7 +302,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
for (StorableProperty<S> mainProperty : mainProperties.values()) {
if (mainProperty.isDerived() || mainProperty.isJoin() || tableName == null) {
- jProperties.put(mainProperty.getName(), new JProperty<S>(mainProperty));
+ jProperties.put(mainProperty.getName(), new JProperty<S>(mainProperty, primaryKeyCheckDisabled));
continue;
}
@@ -378,7 +379,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
}
jProperty = new JProperty<S>(mainProperty, columnInfo,
- autoIncrement,
+ autoIncrement, primaryKeyCheckDisabled,
accessInfo.mResultSetGet,
accessInfo.mPreparedStatementSet,
accessInfo.getAdapter());
@@ -392,7 +393,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
columnToProperty.put(jProperty.getColumnName(), jProperty.getName());
} else {
if (mainProperty.isIndependent()) {
- jProperties.put(mainProperty.getName(), new JProperty<S>(mainProperty));
+ jProperties.put(mainProperty.getName(), new JProperty<S>(mainProperty, primaryKeyCheckDisabled));
} else if (!addedError) {
StringBuilder buf = new StringBuilder();
buf.append("Unable to find matching database column for property \"");
@@ -490,7 +491,14 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
}
if (errorMessages.size() > 0) {
- throw new MismatchException(mainInfo.getStorableType(), errorMessages);
+ if (primaryKeyCheckDisabled) {
+ for (String errorMessage:errorMessages) {
+ getLog().warn("Suppressed error: " + errorMessage);
+ }
+ errorMessages.clear();
+ } else {
+ throw new MismatchException(mainInfo.getStorableType(), errorMessages);
+ }
}
// IndexInfo is empty, as querying for it tends to cause a table analyze to run.
@@ -1179,6 +1187,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
private final Integer mCharOctetLength;
private final Integer mOrdinalPosition;
private final boolean mAutoIncrement;
+ private final boolean mPrimaryKeyCheckDisabled;
private JDBCStorableProperty<S>[] mInternal;
private JDBCStorableProperty<?>[] mExternal;
@@ -1189,6 +1198,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
JProperty(StorableProperty<S> mainProperty,
ColumnInfo columnInfo,
boolean autoIncrement,
+ boolean primaryKeyCheckDisabled,
Method resultSetGet,
Method preparedStatementSet,
StorablePropertyAdapter adapter)
@@ -1206,9 +1216,10 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
mCharOctetLength = columnInfo.charOctetLength;
mOrdinalPosition = columnInfo.ordinalPosition;
mAutoIncrement = autoIncrement;
+ mPrimaryKeyCheckDisabled = primaryKeyCheckDisabled;
}
- JProperty(StorableProperty<S> mainProperty) {
+ JProperty(StorableProperty<S> mainProperty, boolean primaryKeyCheckDisabled) {
mMainProperty = mainProperty;
mColumnName = null;
mDataType = null;
@@ -1222,6 +1233,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
mCharOctetLength = null;
mOrdinalPosition = null;
mAutoIncrement = false;
+ mPrimaryKeyCheckDisabled = primaryKeyCheckDisabled;
}
public String getName() {
@@ -1458,7 +1470,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
return;
}
- JDBCStorableInfo<S> info = examine(getEnclosingType(), ds, catalog, schema, resolver);
+ JDBCStorableInfo<S> info = examine(getEnclosingType(), ds, catalog, schema, resolver, mPrimaryKeyCheckDisabled);
JDBCStorableProperty<S>[] internal = new JDBCStorableProperty[mainInternal.length];
for (int i=mainInternal.length; --i>=0; ) {
@@ -1477,7 +1489,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector {
return;
}
- JDBCStorableInfo<?> info = examine(getJoinedType(), ds, catalog, schema, resolver);
+ JDBCStorableInfo<?> info = examine(getJoinedType(), ds, catalog, schema, resolver, mPrimaryKeyCheckDisabled);
JDBCStorableProperty<?>[] external = new JDBCStorableProperty[mainExternal.length];
for (int i=mainExternal.length; --i>=0; ) {