Skip to content

Commit

Permalink
Let @key repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Nov 23, 2024
1 parent 3919d8c commit e40ad01
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.babyfish.jimmer.meta.ImmutablePropCategory;
import org.babyfish.jimmer.runtime.Internal;
import org.babyfish.jimmer.sql.Key;
import org.babyfish.jimmer.sql.Keys;
import org.babyfish.jimmer.sql.ManyToOne;
import org.babyfish.jimmer.sql.OneToOne;

Expand Down Expand Up @@ -169,6 +170,7 @@ private void addType() {
category = ImmutablePropCategory.SCALAR;
}
String slotName = type.isMappedSuperClass() ? "-1" : prop.getSlotName();
boolean isKey = prop.getAnnotation(Keys.class) != null || prop.getAnnotation(Key.class) != null;
if (prop == type.getIdProp()) {
builder.add(
".id($L, $S, $T.class)\n",
Expand All @@ -190,15 +192,15 @@ private void addType() {
prop.getRawElementTypeName(),
prop.isNullable()
);
} else if (prop.getAnnotation(Key.class) != null && !prop.isAssociation(false)) {
} else if (isKey && !prop.isAssociation(false)) {
builder.add(
".key($L, $S, $T.class, $L)\n",
slotName,
prop.getName(),
prop.getRawElementTypeName(),
prop.isNullable()
);
} else if (prop.getAnnotation(Key.class) != null && prop.isAssociation(false)) {
} else if (isKey && prop.isAssociation(false)) {
builder.add(
".keyReference($L, $S, $T.class, $T.class, $L)\n",
slotName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,10 +1197,23 @@ public ImmutableType build() {
}
for (String keyPropName : keyPropNames) {
ImmutableProp prop = type.declaredProps.get(keyPropName);
String group = prop.getAnnotation(Key.class).group();
keyGroupMap
.computeIfAbsent(group, it -> new LinkedHashSet<>())
.add(prop);
Keys keys = prop.getAnnotation(Keys.class);
if (keys != null) {
for (Key key : keys.value()) {
String group = key.group();
keyGroupMap
.computeIfAbsent(group, it -> new LinkedHashSet<>())
.add(prop);
}
} else {
Key key = prop.getAnnotation(Key.class);
if (key != null) {
String group = key.group();
keyGroupMap
.computeIfAbsent(group, it -> new LinkedHashSet<>())
.add(prop);
}
}
}
type.setKeyGroups(keyGroupMap);
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
@Retention(RetentionPolicy.RUNTIME)
@kotlin.annotation.Target(allowedTargets = AnnotationTarget.PROPERTY)
@Target(ElementType.METHOD)
@Repeatable(Keys.class)
public @interface Key {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.babyfish.jimmer.sql;

import kotlin.annotation.AnnotationTarget;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@kotlin.annotation.Target(allowedTargets = AnnotationTarget.PROPERTY)
@Target(ElementType.METHOD)
public @interface Keys {
Key[] value();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.babyfish.jimmer.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.babyfish.jimmer.Immutable;

import javax.validation.constraints.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.babyfish.jimmer.sql.kt.model.flat
import org.babyfish.jimmer.sql.DatabaseValidationIgnore
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.Key
import org.babyfish.jimmer.sql.ManyToOne
import org.babyfish.jimmer.sql.Table

Expand All @@ -14,10 +15,14 @@ interface Company {
@Id
val id: Long

@Key
val companyName: String

@Key
@Key(group = "2")
@ManyToOne
val street: Street

@Key(group = "2")
val value: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.babyfish.jimmer.sql.kt.util

import org.babyfish.jimmer.meta.ImmutableType
import org.babyfish.jimmer.sql.kt.common.assertContent
import org.babyfish.jimmer.sql.kt.model.flat.Company
import kotlin.test.Test

class ComplexKeyPropsTest {

@Test
fun test() {
assertContent(
"""{
|=[
|--->org.babyfish.jimmer.sql.kt.model.flat.Company.companyName,
|--->org.babyfish.jimmer.sql.kt.model.flat.Company.street
|],
|2=[
|--->org.babyfish.jimmer.sql.kt.model.flat.Company.street,
|--->org.babyfish.jimmer.sql.kt.model.flat.Company.value
|]}""".trimMargin(),
ImmutableType.get(Company::class.java).keyMatcher.toMap()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.babyfish.jimmer.sql.model.exclude;

import org.babyfish.jimmer.sql.DatabaseValidationIgnore;
import org.babyfish.jimmer.sql.Entity;
import org.babyfish.jimmer.sql.ExcludeFromAllScalars;
import org.babyfish.jimmer.sql.Id;
import org.babyfish.jimmer.sql.*;

@Entity
@DatabaseValidationIgnore
Expand All @@ -12,10 +9,14 @@ public interface User {
@Id
long id();

@Key
String name();

@Key
@Key(group = "2")
String nickName();

@Key(group = "2")
@ExcludeFromAllScalars
String password();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.babyfish.jimmer.sql.util;

import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.sql.common.Tests;
import org.babyfish.jimmer.sql.model.exclude.User;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ComplexKeyTest extends Tests {

@Test
public void test() {
ImmutableType type = ImmutableType.get(User.class);
assertContentEquals(
"{" +
"=[" +
"--->org.babyfish.jimmer.sql.model.exclude.User.name, " +
"--->org.babyfish.jimmer.sql.model.exclude.User.nickName" +
"], " +
"2=[" +
"--->org.babyfish.jimmer.sql.model.exclude.User.nickName, " +
"--->org.babyfish.jimmer.sql.model.exclude.User.password" +
"]}",
type.getKeyMatcher().toMap()
);
}
}

0 comments on commit e40ad01

Please sign in to comment.