Skip to content

Commit

Permalink
feat: uni 相关的异步实现,目前只做了 create
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Jul 31, 2024
1 parent 30390bf commit f69d91d
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 29 deletions.
2 changes: 1 addition & 1 deletion http/test.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POST http://localhost:8080/module/create
POST http://localhost:8080/api/module/create
Content-Type: application/json

{
Expand Down
15 changes: 15 additions & 0 deletions my-ability-uni/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id 'java'
id 'java-library'
id 'checkstyle'
id 'configure-jandex'
}


dependencies {
api enforcedPlatform(libs.quarkus.platform.bom)

api project(':my-ability')
api project(':my-database-uni')

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.ximatai.muyun.ability.uni;

public interface ICURDUniAbility extends ICreateAbilityUni {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.ximatai.muyun.ability.uni;

import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.ability.IDatabaseUniAbility;
import net.ximatai.muyun.ability.IMetadataAbility;

import java.util.Map;

public interface ICreateAbilityUni extends IDatabaseUniAbility, IMetadataAbility {

@POST
@Path("/create")
default Uni<String> create(Map body) {
return getDatabase().insert(getInsertSql(body), body);
}

}
12 changes: 12 additions & 0 deletions my-ability/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'java'
id 'java-library'
id 'checkstyle'
id 'configure-jandex'
}


dependencies {
api enforcedPlatform(libs.quarkus.platform.bom)
api project(':my-core')
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.ximatai.muyun.core.ability.curd;
package net.ximatai.muyun.ability;

public interface ICURDAbility extends ICreateAbility, IUpdateAbility, IDeleteAbility, ISelectAbility {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package net.ximatai.muyun.core.ability.curd;
package net.ximatai.muyun.ability;

import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.core.ability.IDatabaseAbility;
import net.ximatai.muyun.core.ability.IMetadataAbility;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.ximatai.muyun.core.ability.curd;
package net.ximatai.muyun.ability;

import jakarta.transaction.Transactional;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import net.ximatai.muyun.core.ability.IDatabaseAbility;
import net.ximatai.muyun.core.ability.IMetadataAbility;
import net.ximatai.muyun.database.exception.MyDatabaseException;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package net.ximatai.muyun.core.ability.curd;
package net.ximatai.muyun.ability;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import net.ximatai.muyun.core.ability.IDatabaseAbility;
import net.ximatai.muyun.core.ability.IMetadataAbility;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.ximatai.muyun.core.ability.curd;
package net.ximatai.muyun.ability;

import jakarta.transaction.Transactional;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import net.ximatai.muyun.core.ability.IDatabaseAbility;
import net.ximatai.muyun.core.ability.IMetadataAbility;
import net.ximatai.muyun.database.exception.MyDatabaseException;

import java.util.Map;
Expand Down
4 changes: 4 additions & 0 deletions my-boot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ quarkus.datasource.username=postgres
quarkus.datasource.password=muyun2024
quarkus.datasource.jdbc.url= jdbc:postgresql://localhost:54324/muyun

quarkus.datasource.reactive.url = vertx-reactive:postgresql://localhost:54324/muyun

quarkus.datasource.jdbc=false

quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.ximatai.muyun.core.ability;
package net.ximatai.muyun.ability;

import net.ximatai.muyun.database.IDatabaseAccess;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ximatai.muyun.ability;

import net.ximatai.muyun.database.IDatabaseAccessUni;

public interface IDatabaseUniAbility {

IDatabaseAccessUni getDatabase();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.ximatai.muyun.core.ability;
package net.ximatai.muyun.ability;

import java.util.Map;
import java.util.StringJoiner;
Expand Down
6 changes: 5 additions & 1 deletion my-database-uni/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
plugins {
id 'java'
id 'java-library'
id 'checkstyle'
id 'configure-jandex'
}


dependencies {
api enforcedPlatform(libs.quarkus.platform.bom)
implementation project(':my-database')
// implementation project(':my-core-uni')

api "io.quarkus:quarkus-hibernate-reactive-panache"
api "io.quarkus:quarkus-reactive-pg-client"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.ximatai.muyun.database.uni;

import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.NoResultException;
import jakarta.persistence.Tuple;
import net.ximatai.muyun.database.IDatabaseAccessUni;
import net.ximatai.muyun.database.tool.TupleTool;
import org.hibernate.reactive.mutiny.Mutiny;

import java.util.List;
import java.util.Map;

@ApplicationScoped
public class UniDataAccess implements IDatabaseAccessUni {

@Inject
Mutiny.SessionFactory sessionFactory;

@Override
public Uni<String> insert(String sql, Map<String, Object> params) {
return this.row(sql, params).map(row -> row.get("id").toString());
}

@Override
public Uni<Map<String, Object>> row(String sql, Map<String, Object> params) {
return sessionFactory.withSession(session -> {

Mutiny.SelectionQuery<Tuple> query = session.createNativeQuery(sql, Tuple.class);

if (params != null) {
params.forEach(query::setParameter);
}

return query
.getSingleResult()
.map(TupleTool::toMap)
.onFailure(NoResultException.class)
.recoverWithItem(() -> null);
});
}

@Override
public Uni<Map<String, Object>> row(String sql) {
return null;
}

@Override
public Uni<List<Map<String, Object>>> query(String sql, Map<String, Object> params) {
return null;
}

@Override
public Uni<List<Map<String, Object>>> query(String sql) {
return null;
}

@Override
public Uni<Integer> update(String sql, Map<String, Object> params) {
return null;
}

@Override
public Uni<Integer> delete(String sql, Map<String, Object> params) {
return null;
}


}
2 changes: 2 additions & 0 deletions my-database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ dependencies {

api 'io.quarkus:quarkus-arc'
api "io.quarkus:quarkus-hibernate-orm"

compileOnlyApi "io.quarkus:quarkus-hibernate-reactive-panache"
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.Map;

public interface IDatabaseAccess {
public interface IDatabaseAccess extends IDatabaseAccessBase {

String insert(String sql, Map<String, Object> params);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.ximatai.muyun.database;

import java.util.Map;

public interface IDatabaseAccessBase {

Object insert(String sql, Map<String, Object> params);

Object row(String sql, Map<String, Object> params);

Object row(String sql);

Object query(String sql, Map<String, Object> params);

Object query(String sql);

Object update(String sql, Map<String, Object> params);

Object delete(String sql, Map<String, Object> params);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.ximatai.muyun.database;

import io.smallrye.mutiny.Uni;

import java.util.List;
import java.util.Map;

public interface IDatabaseAccessUni {

Uni<String> insert(String sql, Map<String, Object> params);

Uni<Map<String, Object>> row(String sql, Map<String, Object> params);

Uni<Map<String, Object>> row(String sql);

Uni<List<Map<String, Object>>> query(String sql, Map<String, Object> params);

Uni<List<Map<String, Object>>> query(String sql);

Uni<Integer> update(String sql, Map<String, Object> params);

Uni<Integer> delete(String sql, Map<String, Object> params);

}
3 changes: 2 additions & 1 deletion my-platform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {

dependencies {
implementation project(':my-core')
implementation project(':my-database-standard')
implementation project(':my-ability-uni')
implementation project(':my-database-uni')
// implementation project(':my-core-uni')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.core.ability.curd.ICURDAbility;
import net.ximatai.muyun.database.IDatabaseAccess;
import net.ximatai.muyun.database.standard.DataAccess;
import net.ximatai.muyun.ability.uni.ICURDUniAbility;
import net.ximatai.muyun.database.IDatabaseAccessUni;

@Path("/module")
public class ModuleController implements ICURDAbility {
public class ModuleController implements ICURDUniAbility {

@Inject
DataAccess dataAccess;
IDatabaseAccessUni databaseAccess;

@Override
public IDatabaseAccess getDatabase() {
return dataAccess;
}

@Override
public String getMainTable() {
return "app_module";
}

@Override
public IDatabaseAccessUni getDatabase() {
return databaseAccess;
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ assert JavaVersion.current() >= JavaVersion.VERSION_21:
"You must use at least Java 21 to build the project, you're currently using ${System.getProperty("java.version")}"

include 'my-core'
//include 'my-core-uni'
include 'my-ability'
include 'my-ability-uni'
include 'my-platform'
include 'my-database'
include 'my-database-standard'
Expand Down

0 comments on commit f69d91d

Please sign in to comment.