Skip to content

Commit

Permalink
#3326 Add test as example of Postgres DML with RETURNING clause
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Feb 11, 2024
1 parent 261b13e commit a883537
Showing 1 changed file with 104 additions and 4 deletions.
108 changes: 104 additions & 4 deletions ebean-test/src/test/java/org/tests/insert/TestInsertOnConflict.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.tests.insert;

import io.ebean.DB;
import io.ebean.Database;
import io.ebean.InsertOptions;
import io.ebean.Transaction;
import io.ebean.*;
import io.ebean.annotation.Platform;
import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import io.ebean.xtest.ForPlatform;
import org.junit.jupiter.api.Test;
import org.tests.update.EPersonOnline;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static io.ebean.InsertOptions.ON_CONFLICT_NOTHING;
Expand Down Expand Up @@ -288,6 +289,105 @@ void insertOnConflictNothing_when_noIdValue() {
assertThat(list.get(0).getWhenUpdated()).isEqualTo(bean.getWhenUpdated());
}

@ForPlatform({Platform.POSTGRES, Platform.YUGABYTE})
@Test
void updateQueryReturning() throws SQLException {
Database db = DB.getDefault();
db.truncate(EPersonOnline.class);

var bean1 = newBean("a1@bee.com");
var bean2 = newBean("a2@cee.com");
var bean3 = newBean("a3@bee.com");
db.saveAll(bean1, bean2, bean3);

String sql = "update e_person_online set email = concat('x',email) where email like ? returning id, email, online_status";

try (Transaction txn = db.createTransaction()) {
Connection connection = txn.connection();
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, "%bee.com");
try (ResultSet resultSet = pstmt.executeQuery()) {
while (resultSet.next()) {
long id = resultSet.getLong(1);
String email = resultSet.getString(2);
boolean status = resultSet.getBoolean(3);
// do something with the id, email and status
assertThat(id).isGreaterThan(0);
assertThat(email).startsWith("x");
assertThat(status).isTrue();
}
}
}
txn.commit();
}

List<SqlRow> sqlRowList = DB.sqlQuery(sql)
.setParameter("%bee.com")
.findList();

assertThat(sqlRowList).hasSize(2);
assertThat(sqlRowList.get(0).getString("email")).startsWith("xx");

List<ReturnDto> dtoList = db.findDto(ReturnDto.class, sql)
.setParameter("%bee.com")
.findList();

assertThat(dtoList).hasSize(2);
assertThat(dtoList.get(0).email).startsWith("xxx");

List<ReturnDto2> dtoList2 = db.findDto(ReturnDto2.class, sql)
.setParameter("%bee.com")
.findList();

assertThat(dtoList2).hasSize(2);
assertThat(dtoList2.get(0).email).startsWith("xxxx");
assertThat(dtoList2.get(0).id).isGreaterThan(0);
}

public static class ReturnDto {

private final long id;
private final String email;
private final boolean onlineStatus;

public ReturnDto(long id, String email, boolean onlineStatus) {
this.id = id;
this.email = email;
this.onlineStatus = onlineStatus;
}
}

public static class ReturnDto2 {

private long id;
private String email;
private boolean onlineStatus;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public boolean isOnlineStatus() {
return onlineStatus;
}

public void setOnlineStatus(boolean onlineStatus) {
this.onlineStatus = onlineStatus;
}
}

private static EPersonOnline newBean(String email) {
EPersonOnline bean = new EPersonOnline();
bean.setEmail(email);
Expand Down

0 comments on commit a883537

Please sign in to comment.