mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Fix for #142 - ENH: Add support for using RawSql with user supplied ResultSet
This commit is contained in:
@@ -35,13 +35,15 @@ public class TestRawSqlPositionedParams extends BaseTestCase {
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_unparsed() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select r.id, r.name from o_customer r where r.id >= ? and r.name like ?")
|
||||
.columnMapping("r.id", "id").columnMapping("r.name", "name").create();
|
||||
.columnMapping("r.id", "id")
|
||||
.columnMapping("r.name", "name").create();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.avaje.tests.rawsql;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.RawSql;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestRawSqlWithResultSet extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws SQLException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// Transaction supplies our jdbc Connection
|
||||
Transaction txn = Ebean.beginTransaction();
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
try {
|
||||
pstmt = txn.getConnection().prepareStatement("select id, name, billing_address_id from o_customer");
|
||||
|
||||
// ResultSet will be closed by Ebean
|
||||
ResultSet resultSet = pstmt.executeQuery();
|
||||
|
||||
RawSql rawSql = new RawSql(resultSet, "id", "name", "billingAddress.id");
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.setRawSql(rawSql)
|
||||
// also test a secondary query join
|
||||
.fetch("billingAddress", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
for (Customer customer : list) {
|
||||
System.out.println("id:"+customer.getId()+" name:"+customer.getName()+" billingAddress:"+customer.getBillingAddress());
|
||||
}
|
||||
|
||||
} finally {
|
||||
close(pstmt);
|
||||
txn.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void close(Statement stmt) {
|
||||
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user