Skip to content

Commit

Permalink
Merge pull request agrosner#311 from Raizlabs/develop
Browse files Browse the repository at this point in the history
Version 2.2.1
  • Loading branch information
agrosner committed Jul 4, 2015
2 parents 9c0d36d + 9d369e4 commit 3195859
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ public void writeSaveDefinition(JavaWriter javaWriter, boolean isModelContainerA
javaWriter.emitSingleLineComment("Model Container Definition");
}

ColumnAccessModel accessModel = new ColumnAccessModel(manager, this, isModelContainerAdapter);
String modelDefinition = isModelContainerAdapter ? (ModelUtils.getVariable(true) + columnFieldName)
: ModelUtils.getModelStatement(columnFieldName);
: ModelUtils.getModelStatement(accessModel.getReferencedColumnFieldName());
if (isModelContainerAdapter) {
javaWriter.emitStatement("ModelContainer %1s = %1s.getInstance(%1s.getValue(\"%1s\"), %1s.class)",
modelDefinition,
ModelUtils.getVariable(true), ModelUtils.getVariable(true),
columnFieldName,
containerKeyName,
foreignKeyTableClassName);
} else {
javaWriter.beginControlFlow("if (%1s != null)", modelDefinition);
Expand Down Expand Up @@ -382,6 +383,8 @@ public void writeLoadFromCursorDefinition(BaseTableDefinition tableDefinition, J
// special case for model objects within class
if (!fieldIsModelContainer && !isModelContainerAdapter && isModel) {

ColumnAccessModel columnAccessModel = new ColumnAccessModel(manager, this, isModelContainerAdapter);

for (ForeignKeyReference foreignKeyReference : foreignKeyReferences) {
javaWriter.emitStatement(ModelUtils.getColumnIndex(foreignKeyReference.columnName()));
}
Expand All @@ -396,9 +399,14 @@ public void writeLoadFromCursorDefinition(BaseTableDefinition tableDefinition, J

AdapterQueryBuilder adapterQueryBuilder = new AdapterQueryBuilder().appendVariable(false);
adapterQueryBuilder.append(".")
.append(columnFieldName)
.appendSpaceSeparated("=");
.append(columnAccessModel.getSetterReferenceColumnFieldName());
if (!columnAccessModel.isPrivate()) {
adapterQueryBuilder.appendSpaceSeparated("=");
}
adapterQueryBuilder.append(rawConditionStatement);
if (columnAccessModel.isPrivate()) {
adapterQueryBuilder.append(")");
}
javaWriter.emitStatement(adapterQueryBuilder.getQuery());

javaWriter.endControlFlow();
Expand Down Expand Up @@ -448,13 +456,13 @@ public void writeLoadFromCursorDefinition(BaseTableDefinition tableDefinition, J

}

if (isModelContainerAdapter && isModel && fieldIsModelContainer) {
if (isModelContainerAdapter && isModel) {
javaWriter.emitStatement("%1s.put(\"%1s\",%1s.getData())", ModelUtils.getVariable(true),
columnFieldName, modelContainerName);
containerKeyName, modelContainerName);
javaWriter.nextControlFlow("else");

javaWriter.emitStatement("%1s.put(\"%1s\", null)", ModelUtils.getVariable(true),
columnFieldName);
containerKeyName);
}

javaWriter.endControlFlow();
Expand All @@ -468,12 +476,13 @@ public void writeLoadFromCursorDefinition(BaseTableDefinition tableDefinition, J
ColumnAccessModel columnAccessModel = new ColumnAccessModel(manager, this, isModelContainerAdapter);
LoadFromCursorModel loadFromCursorModel = new LoadFromCursorModel(columnAccessModel);
loadFromCursorModel.setModelContainerName(columnName);
loadFromCursorModel.setIsModelContainerAdapter(isModelContainerAdapter);
loadFromCursorModel.setIsNullable(isNullable());
loadFromCursorModel.writeSingleField(javaWriter);
}
}

public void writeToModelDefinition(JavaWriter javaWriter) throws IOException {
public void writeToModelDefinition(JavaWriter javaWriter, boolean isModelContainerAdapter) throws IOException {

if (!isModel) {
AdapterQueryBuilder adapterQueryBuilder = new AdapterQueryBuilder("Object value");
Expand All @@ -487,11 +496,16 @@ public void writeToModelDefinition(JavaWriter javaWriter) throws IOException {
}


ColumnAccessModel columnAccessModel = new ColumnAccessModel(manager, this, isModelContainerAdapter);

AdapterQueryBuilder queryBuilder = new AdapterQueryBuilder();
queryBuilder.appendVariable(false)
.append(".")
.append(columnFieldName);
queryBuilder.appendSpaceSeparated("=");
.append(columnAccessModel.getSetterReferenceColumnFieldName());

if (!columnAccessModel.isPrivate()) {
queryBuilder.appendSpaceSeparated("=");
}

String getType = columnFieldType;
// Type converters can never be primitive except boolean
Expand All @@ -518,9 +532,20 @@ public void writeToModelDefinition(JavaWriter javaWriter) throws IOException {
.append(
".toModel())");
} else {
if (columnAccessModel.isRequiresTypeConverter() && !columnAccessModel.isEnum() && !columnAccessModel.isBoolean()) {
queryBuilder.appendTypeConverter(null, getType, true);
}
queryBuilder.append(String.format("value%1s)", columnFieldName));
}

if (columnAccessModel.isRequiresTypeConverter() && !columnAccessModel.isEnum() && !columnAccessModel.isBoolean()) {
queryBuilder.append(")");
}

if (columnAccessModel.isPrivate()) {
queryBuilder.append(")");
}

javaWriter.emitStatement(queryBuilder.getQuery());

if (!isModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ public class ColumnAccessModel implements Query {

boolean isPrivate;

boolean isBoolean;

boolean isEnum;

String setterName;

String getterName;

ColumnDefinition parentColumnDefinition;

public ColumnAccessModel(ProcessorManager manager, ColumnDefinition columnDefinition,
boolean isModelContainerAdapter) {
parentColumnDefinition = columnDefinition;
this.fieldIsAModelContainer = columnDefinition.fieldIsModelContainer;
columnName = columnDefinition.columnName;
columnFieldName = columnDefinition.columnFieldName;
columnFieldType = columnDefinition.columnFieldType;
columnFieldActualType = columnDefinition.columnFieldActualType;
referencedColumnFieldName = columnDefinition.columnFieldName;
isBoolean = columnDefinition.isBoolean;
foreignKeyLocalColumnName = columnName;
containerKeyName = columnDefinition.containerKeyName;
isPrivate = columnDefinition.isPrivate;
Expand Down Expand Up @@ -133,10 +139,12 @@ public ColumnAccessModel(ProcessorManager manager, ColumnDefinition columnDefini
}

public ColumnAccessModel(ColumnDefinition columnDefinition, ForeignKeyReference foreignKeyReference) {
parentColumnDefinition = columnDefinition;
this.fieldIsAModelContainer = columnDefinition.fieldIsModelContainer;
columnName = columnDefinition.columnName;
setterName = columnDefinition.setterName;
getterName = columnDefinition.getterName;
isBoolean = columnDefinition.isBoolean;
columnFieldActualType = columnDefinition.columnFieldActualType;
columnFieldName = columnDefinition.columnFieldName;
columnFieldType = columnDefinition.columnFieldType;
Expand All @@ -156,6 +164,22 @@ public ColumnAccessModel(ColumnDefinition columnDefinition, ForeignKeyReference
columnFieldBoxedType = columnFieldActualType;
}

public boolean isPrivate() {
return isPrivate;
}

public boolean isEnum() {
return isEnum;
}

public boolean isBoolean() {
return isBoolean;
}

public boolean isRequiresTypeConverter() {
return requiresTypeConverter;
}

public String getQueryNoCast() {
return getQuery(false);
}
Expand Down Expand Up @@ -185,7 +209,8 @@ public String getQuery(boolean useCastIfApplicable) {
.appendGetValue(referencedColumnFieldName);
} else {
if (isForeignKeyField) {
contentValue.append(columnName)
ColumnAccessModel columnAccessModel = new ColumnAccessModel(parentColumnDefinition.getManager(), parentColumnDefinition, isModelContainerAdapter);
contentValue.append(columnAccessModel.getReferencedColumnFieldName())
.append(".");
}
contentValue.append(getReferencedColumnFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void write(JavaWriter javaWriter) throws IOException {
AdapterQueryBuilder adapterQueryBuilder = new AdapterQueryBuilder();

AdapterQueryBuilder ifBuilder = new AdapterQueryBuilder()
.append(modelContainerName).append(".").appendGetValue(accessModel.referencedColumnFieldName);
.append(modelContainerName).append(".").appendGetValue(accessModel.getReferencedColumnFieldName());
javaWriter.beginControlFlow("if (%1s != null) ", ifBuilder.getQuery());
if (!isContentValues()) {
adapterQueryBuilder.appendBindSQLiteStatement(getIndex(), accessModel.castedClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,42 @@ public void writeSingleField(JavaWriter javaWriter) throws IOException {

@Override
public void write(JavaWriter javaWriter) throws IOException {
if (isModelContainerAdapter) {
AdapterQueryBuilder adapterQueryBuilder = new AdapterQueryBuilder();
adapterQueryBuilder.append(modelContainerName)
.appendPut(accessModel.getReferencedColumnFieldName())
.append(ModelUtils.getCursorStatement(
accessModel.castedClass, accessModel.foreignKeyLocalColumnName))
.append(")");
javaWriter.emitStatement(adapterQueryBuilder.getQuery());
} else {
String cursorStatementClass = accessModel.castedClass;
if (accessModel.isEnum) {
cursorStatementClass = String.class.getName();
}
String cursorStatment = ModelUtils.getCursorStatement(cursorStatementClass,
accessModel.foreignKeyLocalColumnName);
emitColumnAssignment(javaWriter, cursorStatment);
String cursorStatementClass = accessModel.castedClass;
if (accessModel.isEnum) {
cursorStatementClass = String.class.getName();
}
String cursorStatment = ModelUtils.getCursorStatement(cursorStatementClass,
accessModel.foreignKeyLocalColumnName);
emitColumnAssignment(javaWriter, cursorStatment);
}

private void emitColumnAssignment(JavaWriter javaWriter,
String valueStatement) throws IOException {
boolean isContainerFieldDefinition = accessModel.isModelContainerAdapter;
boolean isWritingForContainers = accessModel.fieldIsAModelContainer;
AdapterQueryBuilder queryBuilder = new AdapterQueryBuilder().appendVariable(isContainerFieldDefinition);
if (isWritingForContainers) {
queryBuilder.append(".")
.append(accessModel.columnName);
boolean fieldIsAModelContainer = accessModel.fieldIsAModelContainer;
boolean isNull = valueStatement.equals("null");
AdapterQueryBuilder queryBuilder = new AdapterQueryBuilder();

if (isModelContainerAdapter) {
if (accessModel.isForeignKeyField) {
queryBuilder.append(modelContainerName);
} else {
queryBuilder.append(ModelUtils.getVariable(true));
}
} else {
queryBuilder.appendVariable(isContainerFieldDefinition);
}
if (fieldIsAModelContainer && isModelContainerAdapter) {
queryBuilder.appendPut(accessModel.getReferencedColumnFieldName());
} else if (fieldIsAModelContainer) {
queryBuilder.append(".").append(accessModel.columnFieldName)
.appendPut(accessModel.getReferencedColumnFieldName());
} else if (isModelContainerAdapter && accessModel.isForeignKeyField) {
queryBuilder.appendPut(accessModel.getReferencedColumnFieldName());
}
if (isContainerFieldDefinition) {
queryBuilder.appendPut(accessModel.containerKeyName);
} else if (isWritingForContainers) {
queryBuilder.appendPut(accessModel.getReferencedColumnFieldName());
} else {
} else if (!fieldIsAModelContainer && !isModelContainerAdapter) {
queryBuilder.append(".")
.append(accessModel.getSetterReferenceColumnFieldName());
if (!accessModel.isPrivate) {
Expand All @@ -97,30 +100,35 @@ private void emitColumnAssignment(JavaWriter javaWriter,
}
if (accessModel.isEnum) {
// don't attempt to use valueOf on a null, will throw a NullPointerException
if (!valueStatement.equals("null")) {
if (!isNull) {
queryBuilder.append(accessModel.castedClass)
.append(".valueOf(");
}
} else {
if (accessModel.requiresTypeConverter && !isContainerFieldDefinition) {
if (accessModel.requiresTypeConverter && !isNull) {
queryBuilder.appendTypeConverter(accessModel.columnFieldBoxedType, accessModel.columnFieldBoxedType,
true);
true);
} else if (accessModel.isABlob) {
queryBuilder.append(String.format("new %1s(", Blob.class.getName()));
}
}

queryBuilder.append(valueStatement);

if (accessModel.requiresTypeConverter && !isContainerFieldDefinition && !accessModel.isEnum ||
(accessModel.isEnum && isContainerFieldDefinition && !valueStatement.equals("null"))) {
if (accessModel.requiresTypeConverter && !isNull && !accessModel.isEnum ||
(accessModel.isEnum && isContainerFieldDefinition && !isNull)) {
queryBuilder.append("))");
} else if (isContainerFieldDefinition || isWritingForContainers || accessModel.isABlob ||
(accessModel.isEnum && !valueStatement.equals("null"))) {
} else if (isModelContainerAdapter || isContainerFieldDefinition || fieldIsAModelContainer || accessModel.isABlob ||
(accessModel.isEnum && !isNull)) {
queryBuilder.append(")");
}

if (accessModel.isPrivate && !isContainerFieldDefinition) {
queryBuilder.append(")");
}

if (accessModel.isPrivate) {
if (accessModel.requiresTypeConverter && !accessModel.isEnum()
&& isModelContainerAdapter && !isNull) {
queryBuilder.append(")");
}

Expand Down
Loading

0 comments on commit 3195859

Please sign in to comment.