-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathRowMapper.java
More file actions
51 lines (46 loc) · 1.41 KB
/
RowMapper.java
File metadata and controls
51 lines (46 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.sqlclient.templates;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Row;
/**
* Map a {@link Row} to an arbitrary {@code T} object.
*/
@VertxGen
@FunctionalInterface
public interface RowMapper<T> {
/**
* Create a mapper that converts a {@link Row} to an instance of the given {@code type}.
*
* <p>This feature relies on {@link io.vertx.core.json.JsonObject#mapTo} feature. This likely requires
* to use Jackson databind in the project.
*
* @param type the target class
* @return the mapper
*/
static <T> RowMapper<T> mapper(Class<T> type) {
return row -> {
JsonObject json = new JsonObject();
for (int i = 0; i < row.size(); i++) {
json.getMap().put(row.getColumnName(i), row.getValue(i));
}
return json.mapTo(type);
};
}
/**
* Build a {@code T} representation of the given {@code row}
*
* @param row the row
* @return the object
*/
T map(Row row);
}