Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from jesterpm/fix-automatic-and-alt-key
Browse files Browse the repository at this point in the history
Fix load by alternate key with Automatic primary key
  • Loading branch information
jesterpm committed Jul 3, 2018
2 parents 88d28b6 + be46104 commit eee29b3
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/main/java/com/amazon/carbonado/gen/StorableGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,17 @@ public final class StorableGenerator<S extends Storable> {

/**
* Name of protected method in generated storable that returns false if any
* primary keys are uninitialized.
* required primary keys are uninitialized.
*/
public static final String IS_REQUIRED_PK_INITIALIZED_METHOD_NAME = "isRequiredPkInitialized$";

/**
* Name of protected method in generated storable that returns false if any
* required or optional (e.g. Automatic) primary keys are uninitialized.
*/
public static final String IS_PK_INITIALIZED_METHOD_NAME = "isPkInitialized$";

/**
/**
* Name of protected method in generated storable that returns false if any
* partition keys are uninitialized.
*/
Expand Down Expand Up @@ -1748,6 +1754,10 @@ private void generateClass() {
}

{
// Define protected isRequiredPkInitialized method.
addIsRequiredInitializedMethod
(IS_REQUIRED_PK_INITIALIZED_METHOD_NAME, mInfo.getPrimaryKeyProperties());

// Define protected isPkInitialized method.
addIsInitializedMethod
(IS_PK_INITIALIZED_METHOD_NAME, mInfo.getPrimaryKeyProperties());
Expand All @@ -1761,9 +1771,9 @@ private void generateClass() {
partitionProperties.put(property.getName(), property);
}
}

// Add methods to check that the partition key is defined
addIsInitializedMethod(IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, partitionProperties);
addIsRequiredInitializedMethod(IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, partitionProperties);
}

// Define protected methods to check if alternate key is initialized.
Expand Down Expand Up @@ -1802,7 +1812,7 @@ private void generateClass() {
}
}

addIsInitializedMethod
addIsRequiredInitializedMethod
(IS_REQUIRED_DATA_INITIALIZED_METHOD_NAME, requiredProperties);
}

Expand Down Expand Up @@ -2447,14 +2457,27 @@ private void branchIfDirty(CodeBuilder b, boolean includePk, Label label) {
}
}

private void addIsRequiredInitializedMethod
(String name, Map<String, ? extends StorableProperty<S>> properties)
{
addIsInitializedMethod(name, properties, true);
}

private void addIsInitializedMethod
(String name, Map<String, ? extends StorableProperty<S>> properties)
{
addIsInitializedMethod(name, properties, false);
}

private void addIsInitializedMethod
(String name, Map<String, ? extends StorableProperty<S>> properties, boolean ignoreAutomatic)
{
// Don't check Automatic, Independent, or Version properties.
{
boolean cloned = false;
for (StorableProperty<S> prop : properties.values()) {
if (prop.isAutomatic() || prop.isIndependent() || prop.isVersion()) {
if ((ignoreAutomatic && prop.isAutomatic()) ||
prop.isIndependent() || prop.isVersion()) {
if (!cloned) {
properties = new LinkedHashMap<String, StorableProperty<S>>(properties);
cloned = true;
Expand Down Expand Up @@ -2559,7 +2582,7 @@ private void requirePkInitialized(CodeBuilder b, String methodName) {
// Now define new method, discarding original builder object.
b = new CodeBuilder(mClassFile.addMethod(Modifiers.PROTECTED, methodName, null, null));
b.loadThis();
b.invokeVirtual(IS_PK_INITIALIZED_METHOD_NAME, TypeDesc.BOOLEAN, null);
b.invokeVirtual(IS_REQUIRED_PK_INITIALIZED_METHOD_NAME, TypeDesc.BOOLEAN, null);
Label pkInitialized = b.createLabel();
b.ifZeroComparisonBranch(pkInitialized, "!=");
CodeBuilderUtil.throwException
Expand Down

0 comments on commit eee29b3

Please sign in to comment.