From 72cc79e0d15503705263f9efaf0cbeec15230bf3 Mon Sep 17 00:00:00 2001 From: "Adam D. Bradley" Date: Wed, 8 Oct 2008 18:14:30 +0000 Subject: Add "setPrimaryKeyCheckDisable" property to JDBCRepositoryBuilder. --- .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 7 ++-- .../carbonado/repo/jdbc/JDBCRepositoryBuilder.java | 20 +++++++++++- .../repo/jdbc/JDBCStorableIntrospector.java | 38 ++++++++++++++-------- 3 files changed, 49 insertions(+), 16 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo') 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 @@ -173,6 +174,7 @@ class JDBCRepository extends AbstractRepository 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 mAutoVersioningMap; @@ -226,7 +228,7 @@ class JDBCRepository extends AbstractRepository Integer fetchSize, Map autoVersioningMap, Map suppressReloadMap, - String sequenceSelectStatement, boolean forceStoredSequence, + String sequenceSelectStatement, boolean forceStoredSequence, boolean primaryKeyCheckDisabled, SchemaResolver resolver) throws RepositoryException { @@ -242,6 +244,7 @@ class JDBCRepository extends AbstractRepository mCatalog = catalog; mSchema = schema; mFetchSize = fetchSize; + mPrimaryKeyCheckDisabled = primaryKeyCheckDisabled; mAutoVersioningMap = autoVersioningMap; mSuppressReloadMap = suppressReloadMap; @@ -351,7 +354,7 @@ class JDBCRepository extends AbstractRepository { 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 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 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 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 JDBCStorableInfo examine - (Class type, DataSource ds, String catalog, String schema, SchemaResolver resolver) + (Class 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 JDBCStorableInfo examine (StorableInfo 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 mainProperty : mainProperties.values()) { if (mainProperty.isDerived() || mainProperty.isJoin() || tableName == null) { - jProperties.put(mainProperty.getName(), new JProperty(mainProperty)); + jProperties.put(mainProperty.getName(), new JProperty(mainProperty, primaryKeyCheckDisabled)); continue; } @@ -378,7 +379,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector { } jProperty = new JProperty(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(mainProperty)); + jProperties.put(mainProperty.getName(), new JProperty(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[] mInternal; private JDBCStorableProperty[] mExternal; @@ -1189,6 +1198,7 @@ public class JDBCStorableIntrospector extends StorableIntrospector { JProperty(StorableProperty 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 mainProperty) { + JProperty(StorableProperty 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 info = examine(getEnclosingType(), ds, catalog, schema, resolver); + JDBCStorableInfo info = examine(getEnclosingType(), ds, catalog, schema, resolver, mPrimaryKeyCheckDisabled); JDBCStorableProperty[] 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; ) { -- cgit v1.2.3