Skip to content

Commit

Permalink
add patch function
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Aug 7, 2023
1 parent 8a7700c commit 5e58459
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Add support for ordering by multiple fields
- Add basic enum support
- Add pagination support
- Add `patch()` function on db classes as a convenience for creating patch objects

## [1.0.1] - 2023-07-26

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var id = exampleDb.insert(new Example()
.setTimestamp(Instant.now()), conn);

// update a single field on an existing entity
exampleDb.update(id, new ExambleDb.Patch().setValue("updated"), conn);
exampleDb.update(id, ExambleDb.patch().setValue("updated"), conn);

// select all entities, ordered by a specific column
var examples = exampleDb.select(select -> select.sort(orderBy -> orderBy.valueAsc()), conn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public JavaFile generate(EntitySpec entitySpec) {
builder.addType(genPatchClass(entityType, patchType, idType, entitySpec))
.addType(genFilterBuilderClass(entityType, filterBuilderType, entitySpec))
.addType(genSortBuilderClass(entityType, sortBuilderType, entitySpec))
.addMethod(genPatchFunction(patchType))
.addMethod(genSelect(entityType, idType, entitySpec))
.addMethod(genSelect(entityType, filterBuilderType, sortBuilderType, entitySpec))
.addMethod(genSelectAll(entityType, entitySpec))
Expand Down Expand Up @@ -273,6 +274,17 @@ private TypeSpec genSortBuilderClass(TypeName entityType, TypeName sortBuilderTy
return builder.build();
}

private MethodSpec genPatchFunction(TypeName patchType) {
return MethodSpec.methodBuilder("patch")
.addJavadoc("Creates a new patch object to use for partially updating an entity.\n")
.addJavadoc("\n")
.addJavadoc("@return new patch")
.addModifiers(PUBLIC, STATIC)
.returns(patchType)
.addStatement("return new $T()", patchType)
.build();
}

private MethodSpec genSelect(TypeName entityType, TypeName idType, EntitySpec entitySpec) {
var query = "SELECT " + columnNames(entitySpec)
+ " FROM " + entitySpec.getTableName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public Patch setExampleEnum(ExampleEnum exampleEnum) {
}
}

/**
* Creates a new patch object to use for partially updating an entity.
*
* @return new patch
*/
public static Patch patch() {
return new Patch();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ protected GetterSetterAllTypesEntity updateEntity(GetterSetterAllTypesEntity ent

@Override
protected GetterSetterAllTypesEntityDb.Patch patchAll(GetterSetterAllTypesEntity entity) {
return new GetterSetterAllTypesEntityDb.Patch()
return GetterSetterAllTypesEntityDb.patch()
.setLongPrim(entity.getLongPrim())
.setIntObj(entity.getIntObj())
.setIntPrim(entity.getIntPrim())
Expand Down Expand Up @@ -1028,7 +1028,7 @@ protected GetterSetterAllTypesEntity nullEntity(GetterSetterAllTypesEntity entit

@Override
protected GetterSetterAllTypesEntityDb.Patch nullPatchAll() {
return new GetterSetterAllTypesEntityDb.Patch()
return GetterSetterAllTypesEntityDb.patch()
.setIntObj(null)
.setShortObj(null)
.setDoubleObj(null)
Expand Down Expand Up @@ -1056,7 +1056,7 @@ protected Pair<GetterSetterAllTypesEntity, GetterSetterAllTypesEntityDb.Patch> p

return ImmutablePair.of(
updated,
new GetterSetterAllTypesEntityDb.Patch()
GetterSetterAllTypesEntityDb.patch()
.setString(updated.getString())
.setUuid(updated.getUuid())
.setInstant(updated.getInstant())
Expand Down

0 comments on commit 5e58459

Please sign in to comment.