Skip to content

Commit

Permalink
Support sessions of type Uni<Mutiny.Session>
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage authored and gavinking committed Mar 1, 2024
1 parent 3ca494b commit 36c9ce9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,29 @@ boolean isUsingStatelessSession() {
}

boolean isReactive() {
return MUTINY_SESSION.equals(sessionType);
return MUTINY_SESSION.equals(sessionType)
|| UNI_MUTINY_SESSION.equals(sessionType);
}

boolean isReactiveSession() {
return UNI_MUTINY_SESSION.equals(sessionType);
}

String localSessionName() {
return isReactiveSession() ? "resolvedSession" : sessionName;
}

void chainSession(StringBuilder declaration) {
// Reactive calls always have a return type
if ( isReactiveSession() ) {
declaration.append("\treturn ").append(sessionName).append(".chain(").append(localSessionName()).append(" -> {\n\t");
}
}

void chainSessionEnd(boolean isUpdate, StringBuilder declaration) {
if ( isReactiveSession() ) {
declaration.append("\n\t});");
}
}

void setPage(StringBuilder declaration, String paramName, String paramType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private static boolean isSessionGetter(ExecutableElement method) {
if ( method.getParameters().isEmpty() ) {
final TypeMirror type = method.getReturnType();
if ( type.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) type;
final DeclaredType declaredType = ununi( (DeclaredType) type );
final Element element = declaredType.asElement();
if ( element.getKind() == ElementKind.INTERFACE ) {
final Name name = ((TypeElement) element).getQualifiedName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public String getAttributeDeclarationString() {
comment( declaration );
modifiers( declaration );
preamble( declaration, returnType(), paramTypes );
chainSession( declaration );
nullChecks( paramTypes, declaration );
createCriteriaQuery( declaration );
where( declaration, paramTypes );
// orderBy( paramTypes, declaration );
executeQuery( declaration, paramTypes );
convertExceptions( declaration );
chainSessionEnd( false, declaration );
closingBrace( declaration );
return declaration.toString();
}
Expand Down Expand Up @@ -102,7 +104,7 @@ private boolean specialNeeds(StringBuilder declaration) {
@Override
void createQuery(StringBuilder declaration) {
declaration
.append(sessionName)
.append(localSessionName())
.append(".createQuery(_query)\n");
}

Expand All @@ -119,7 +121,7 @@ private void execute(StringBuilder declaration, List<String> paramTypes, boolean
private void createCriteriaQuery(StringBuilder declaration) {
declaration
.append("\tvar _builder = ")
.append(sessionName)
.append(localSessionName())
.append(isUsingEntityManager()
? ".getEntityManagerFactory()"
: ".getFactory()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public String getAttributeDeclarationString() {
modifiers( paramTypes, declaration );
preamble( declaration, returnType, paramTypes );
collectOrdering( declaration, paramTypes );
chainSession( declaration );
tryReturn( declaration, paramTypes, containerType );
castResult( declaration, returnType );
createQuery( declaration );
Expand All @@ -94,6 +95,7 @@ public String getAttributeDeclarationString() {
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
execute( declaration, unwrapped );
convertExceptions( declaration );
chainSessionEnd( isUpdate, declaration );
closingBrace( declaration );
return declaration.toString();
}
Expand All @@ -107,7 +109,7 @@ private boolean specialNeeds(StringBuilder declaration) {
@Override
void createQuery(StringBuilder declaration) {
declaration
.append(sessionName)
.append(localSessionName())
.append(isNative ? ".createNativeQuery" : ".createQuery")
.append("(")
.append(getConstantName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public final class Constants {
public static final String TUPLE = "jakarta.persistence.Tuple";

public static final String UNI = "io.smallrye.mutiny.Uni";
public static final String UNI_MUTINY_SESSION = UNI+"<org.hibernate.reactive.mutiny.Mutiny.Session>";

public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute";
public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";
Expand Down Expand Up @@ -129,7 +130,8 @@ public final class Constants {
Constants.ENTITY_MANAGER,
Constants.HIB_SESSION,
Constants.HIB_STATELESS_SESSION,
Constants.MUTINY_SESSION
Constants.MUTINY_SESSION,
Constants.UNI_MUTINY_SESSION
);

//TODO: this is not even an exhaustive list of built-in basic types
Expand Down

0 comments on commit 36c9ce9

Please sign in to comment.