简体   繁体   English

如何返回 Uni<int[]> 在quarkus中执行多个查询时?</int[]>

[英]How return Uni<int[]> while executing multiple queries in quarkus?

In my application I am executing two queries in one transaction, first one is select and second one is Insert but after insert I want to return ids which are inserted how can we do it?在我的应用程序中,我在一个事务中执行两个查询,第一个是 select,第二个是 Insert 但插入后我想返回插入的 id,我们该怎么做?

public Uni<int[]> addRUnRe(int id, String ids,String owner) {
    try{
        
        String sql="SELECT COUNT(*) as c FROM table1 WHERE id=$1 AND owner=$2";

        String sql2= "Insert into table2 (event_id, request_id)"
        + "  values ($1, $2) RETURNING id)";
        Uni<int[]> insertTwo = null;
        return SqlClientHelper.inTransactionUni(client, tx -> {
            Uni<Integer> insertOne = tx.preparedQuery(sql)
                    .execute(Tuple.of(id, owner)).onItem().transform(pg -> pg.iterator())
                    .onItem().transform(r -> r.hasNext() ? r.next().getInteger("c"): 0);
            

  
            int isUserAllowed = insertOne.await().indefinitely();
            
            if (isUserAllowed == 1) {
                if(StringUtils.isNotBlank(ids)){
                    String[] rids=ids.split(",");
                List<Tuple> batch = new ArrayList<>();
                for (int i=0;i<rids.length;i++) {
                    Object[] array = {insertOne, rids[i]};
                    batch.add(Tuple.of(Arrays.asList(array)));
                }


                Uni<int[]> insertTwo = tx.preparedQuery(sql2)
                    .executeBatch(batch).onItem().transform(pgRowSet -> IntStream.range(0, pgRowSet.size()).toArray());
                    return insertTwo;
                }
            }

            return insertOne.and(insertTwo)
                    // Ignore the results (the two ids)
                    .onItem().ignore().andContinueWithNull();
        });
        
    }catch(Exception e){
        return null;
    }
}

Please let me know what is wrong am I doing and how to do it properly?请让我知道我做错了什么以及如何正确地做?

As an option, we can return the map, although in Java it is desirable to choose immutable keys for the map interface.作为一个选项,我们可以返回 map,尽管在 Java 中,希望为 map 接口选择不可变密钥。

 Map<String[], Uni<int[]>>

And then you can get the values by using然后你可以通过使用

map.entrySet().forEach((key, value) -> ... )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM