Skip to content

Commit

Permalink
[persistence] fix @onDelete annotation processing (compilation error)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Mar 25, 2024
1 parent e97ec7d commit d95f2f7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public Output get() {
.collect(joining("\n", "", "\n")) +
" return instance;\n" +
" },\n" +
" (" + (onDeleteCb.isEmpty() ? "instance" : "entity") + ", statement) -> {\n" +
(!onDeleteCb.isEmpty() ? " entity." + onDeleteCb.get(0).getSimpleName().toString() + "();\n" : "") +
" (instance, statement) -> {\n" +
(!onDeleteCb.isEmpty() ? " instance." + onDeleteCb.get(0).getSimpleName().toString() + "();\n" : "") +
withIndex(ids.stream())
.map(it -> " " + jdbcSetter(
it.item(), it.index() + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,61 @@ public class SimpleFlatEntity$FusionPersistenceEntity extends io.yupiik.fusion.p
});
}


@Test
void persistenceOnDelete(@TempDir final Path work) throws IOException {
final var entity = "persistence.OnDeleteEntity";
final var compiler = new Compiler(work, entity);
compiler.compileAndAsserts((loader, container) -> assertEquals(
"""
package test.p.persistence;
@io.yupiik.fusion.framework.api.container.Generation(version = 1)
public class OnDeleteEntity$FusionPersistenceEntity extends io.yupiik.fusion.persistence.impl.BaseEntity<OnDeleteEntity, java.lang.String> {
public OnDeleteEntity$FusionPersistenceEntity(io.yupiik.fusion.persistence.impl.DatabaseConfiguration configuration) {
super(
configuration,
OnDeleteEntity.class,
"ON_DELETE",
java.util.List.of(
new io.yupiik.fusion.persistence.impl.ColumnMetadataImpl("id", java.lang.String.class, "id", 0, false)
),
false,
(instance, statement) -> {
if (instance.id() == null) { statement.setNull(1, java.sql.Types.VARCHAR); } else { statement.setString(1, instance.id()); }
return instance;
},
(instance, statement) -> {
if (instance.id() == null) { statement.setNull(1, java.sql.Types.VARCHAR); } else { statement.setString(1, instance.id()); }
return instance;
},
(instance, statement) -> {
instance.deleted();
if (instance.id() == null) { statement.setNull(1, java.sql.Types.VARCHAR); } else { statement.setString(1, instance.id()); }
},
(id, statement) -> {
if (id == null) { statement.setNull(1, java.sql.Types.VARCHAR); } else { statement.setString(1, id); }
},
(entity, statement) -> entity,
columns -> {
final var id = stringOf(columns.indexOf("id"));
return rset -> {
try {
return new test.p.persistence.OnDeleteEntity(id.apply(rset));
} catch (final java.sql.SQLException e) {
throw new io.yupiik.fusion.persistence.api.PersistenceException(e);
}
};
});
}
}
""",
compiler.readGeneratedSource(entity + "$FusionPersistenceEntity")));
}

@Test
void nestedRecordPersistence(@TempDir final Path work) throws IOException {
final var entity = "persistence.NestedEntity";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022 - present - Yupiik SAS - https://www.yupiik.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package test.p.persistence;

import io.yupiik.fusion.framework.build.api.persistence.Id;
import io.yupiik.fusion.framework.build.api.persistence.OnDelete;
import io.yupiik.fusion.framework.build.api.persistence.Table;

@Table("ON_DELETE")
public record OnDeleteEntity(@Id String id) {
@OnDelete
void deleted() {
// no-op
}
}

0 comments on commit d95f2f7

Please sign in to comment.