Search in sources :

Example 11 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperTupleTest method testIssue450.

@Test
public void testIssue450() throws Exception {
    JdbcMapper<Tuple2<Integer, List<Privilege>>> mapper = JdbcMapperFactory.newInstance().reflectionService(ReflectionService.newInstance().withSelfScoreFullName(true)).addKeys("resource_id", "id").newBuilder(new TypeReference<Tuple2<Integer, List<Privilege>>>() {
    }).addMapping("id", 1, 4).addMapping("name", 2, 12).addMapping("resource_id", 3, 4).mapper();
    Connection dbConnection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    if (dbConnection == null) {
        return;
    }
    Statement statement = dbConnection.createStatement();
    ResultSet rs = statement.executeQuery("with \n" + "\tresource_privileges(resource_id, privilege_id) AS (VALUES(1, 10), (2, 10), (3, 11)),\n" + "\tprivilege (id, name) as (values(10, 'write'), ('11', 'read'))\n" + "select privilege.id, privilege.name, resource_privileges.resource_id\n" + "from privilege join resource_privileges on resource_privileges.privilege_id = privilege.id\n" + "order by resource_privileges.resource_id");
    List<Tuple2<Integer, List<Privilege>>> list = mapper.forEach(rs, new ListCollector<Tuple2<Integer, List<Privilege>>>()).getList();
    assertEquals(2, list.size());
    assertEquals((Integer) 10, list.get(0).first());
    assertEquals((Integer) 11, list.get(1).first());
    assertEquals(2, list.get(0).second().size());
    assertEquals(1, list.get(1).second().size());
    dbConnection.close();
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) Tuple2(org.simpleflatmapper.tuple.Tuple2) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) List(java.util.List) Test(org.junit.Test)

Example 12 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class JoinSample method stackOverFlowJoin.

/**
 * Test for https://arnaudroger.github.io/blog/2017/02/24/jooq-one-to-many.html
 *
 * To avoid having to mock the result set metadata I used a static mapper here. In production code
 * you can just call
 *  newMapper(Location.class)
 * instead of newBuilder()...mapper()
 *
 * @throws SQLException
 */
@Test
public void stackOverFlowJoin() throws SQLException {
    JdbcMapper<Location> mapper = JdbcMapperFactory.newInstance().addKeys("player").newBuilder(Location.class).addMapping(new JdbcColumnKey("name", 1, Types.VARCHAR)).addMapping(new JdbcColumnKey("player", 2, Types.VARCHAR)).addMapping(new JdbcColumnKey("invited_players_player", 3, Types.VARCHAR)).mapper();
    UUID[] players = new UUID[] { UUID.randomUUID(), UUID.randomUUID() };
    UUID[] invitedPlayers = new UUID[] { UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID() };
    String[] name = new String[] { "location1", "location2" };
    ResultSet rs = mock(ResultSet.class);
    when(rs.next()).thenReturn(true, true, true, false);
    when(rs.getString(1)).thenReturn(name[0], name[1]);
    when(rs.getString(2)).thenReturn(players[0].toString(), players[1].toString());
    when(rs.getObject(2)).thenReturn(players[0].toString(), players[0].toString(), players[0].toString(), players[0].toString(), players[1].toString(), players[1].toString());
    when(rs.getString(3)).thenReturn(invitedPlayers[0].toString(), invitedPlayers[1].toString(), invitedPlayers[2].toString());
    List<Location> list = mapper.forEach(rs, new ListCollector<Location>()).getList();
    assertEquals(2, list.size());
    assertEquals("location1", list.get(0).getName());
    assertEquals(players[0], list.get(0).getPlayer());
    assertEquals(Arrays.asList(invitedPlayers[0], invitedPlayers[1]), list.get(0).getInvitedPlayers());
    assertEquals("location2", list.get(1).getName());
    assertEquals(players[1], list.get(1).getPlayer());
    assertEquals(Arrays.asList(invitedPlayers[2]), list.get(1).getInvitedPlayers());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ListCollector(org.simpleflatmapper.util.ListCollector) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class CsvParserTest method testNoUnescaping.

@Test
public void testNoUnescaping() throws IOException {
    StringReader stringReader = new StringReader("test,\" \"\"hello\"\" \"\n# this a comment, not data");
    List<String[]> data = CsvParser.dsl().disableUnescaping().forEach(stringReader, new ListCollector<String[]>()).getList();
    assertEquals(2, data.size());
    assertArrayEquals(new String[] { "test", "\" \"\"hello\"\" \"" }, data.get(0));
    assertArrayEquals(new String[] { "# this a comment", " not data" }, data.get(1));
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 14 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderTest method testMapDbObjectWithColumnIndex.

@Test
public void testMapDbObjectWithColumnIndex() throws Exception {
    CsvMapperBuilder<DbObject> builder = csvMapperFactory.newBuilder(DbObject.class);
    builder.addMapping("email", 2);
    CsvMapper<DbObject> mapper = builder.mapper();
    List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
    assertEquals(1, list.size());
    DbObject o = list.get(0);
    assertEquals(0, o.getId());
    assertNull(o.getName());
    assertEquals("name1@mail.com", o.getEmail());
    assertNull(o.getCreationTime());
    assertNull(o.getTypeName());
    assertNull(o.getTypeOrdinal());
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 15 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderTest method testMapDbObjectWithWrongColumnStillMapGoodOne.

@Test
public void testMapDbObjectWithWrongColumnStillMapGoodOne() throws Exception {
    CsvMapperBuilder<DbObject> builder = csvMapperFactory.mapperBuilderErrorHandler(MapperBuilderErrorHandler.NULL).newBuilder(DbObject.class);
    builder.addMapping("no_id");
    builder.addMapping("no_name");
    builder.addMapping("email");
    CsvMapper<DbObject> mapper = builder.mapper();
    List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
    assertEquals(1, list.size());
    DbObject o = list.get(0);
    assertEquals(0, o.getId());
    assertNull(o.getName());
    assertEquals("name1@mail.com", o.getEmail());
    assertNull(o.getCreationTime());
    assertNull(o.getTypeName());
    assertNull(o.getTypeOrdinal());
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Aggregations

ListCollector (org.simpleflatmapper.util.ListCollector)26 Test (org.junit.Test)22 StringReader (java.io.StringReader)10 ResultSet (java.sql.ResultSet)6 CsvMapperFactory (org.simpleflatmapper.csv.CsvMapperFactory)5 JoinTest (org.simpleflatmapper.test.jdbc.JoinTest)5 CsvMapperBuilder (org.simpleflatmapper.csv.CsvMapperBuilder)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ProfessorGS (org.simpleflatmapper.test.beans.ProfessorGS)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)2 Db1DeepPartialObject (org.simpleflatmapper.test.beans.Db1DeepPartialObject)2 ProfessorC (org.simpleflatmapper.test.beans.ProfessorC)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1