Skip to content

Commit 2effe5a

Browse files
committed
Map any java class using its class and consequently mapping MySQL duration and PosgreSQL specific data types
1 parent c7c8cd3 commit 2effe5a

File tree

14 files changed

+476
-188
lines changed

14 files changed

+476
-188
lines changed

vertx-sql-client-template/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
<version>4.0.0-SNAPSHOT</version>
7979
<scope>test</scope>
8080
</dependency>
81+
<dependency>
82+
<groupId>io.vertx</groupId>
83+
<artifactId>vertx-mysql-client</artifactId>
84+
<version>4.0.0-SNAPSHOT</version>
85+
<scope>test</scope>
86+
</dependency>
8187
<dependency>
8288
<groupId>io.vertx</groupId>
8389
<artifactId>vertx-pg-client</artifactId>

vertx-sql-client-template/src/main/java/io/vertx/sqlclient/template/generator/RowMapperGen.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,17 @@ private static String rowType(TypeInfo type) {
168168
}
169169

170170
private static Function<String, String> getter_(TypeInfo type, boolean isArray) {
171-
if (type.getKind() == ClassKind.ENUM) {
171+
String getter = getter(type);
172+
if (getter != null) {
173+
return col -> "row." + getter + (isArray ? "Array" : "") + "(\"" + col + "\")";
174+
}
175+
if (type.getKind() == ClassKind.ENUM || type instanceof ClassTypeInfo) {
172176
if (isArray) {
173177
return col -> "row.get(" + type.getName() + "[].class, \"" + col + "\")";
174178
} else {
175179
return col -> "row.get(" + type.getName() + ".class, \"" + col + "\")";
176180
}
177181
}
178-
String getter = getter(type);
179-
if (getter != null) {
180-
return col -> "row." + getter + (isArray ? "Array" : "") + "(\"" + col + "\")";
181-
}
182182
return null;
183183
}
184184

@@ -197,6 +197,10 @@ private static String getter(TypeInfo type) {
197197
return "getJsonArray";
198198
}
199199
if (type instanceof ClassTypeInfo) {
200+
DataObjectInfo dataObject = type.getDataObject();
201+
if (dataObject != null) {
202+
return getter(dataObject.getJsonType());
203+
}
200204
ClassTypeInfo ct = (ClassTypeInfo) type;
201205
switch (ct.getName()) {
202206
case "java.time.LocalDateTime":
@@ -216,10 +220,6 @@ private static String getter(TypeInfo type) {
216220
case "io.vertx.core.buffer.Buffer":
217221
return "getBuffer";
218222
}
219-
DataObjectInfo dataObject = type.getDataObject();
220-
if (dataObject != null) {
221-
return getter(dataObject.getJsonType());
222-
}
223223
}
224224
return null;
225225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.vertx.sqlclient.template;
2+
3+
/**
4+
* Mapper for {@link MySQLDataObject}.
5+
* NOTE: This class has been automatically generated from the {@link MySQLDataObject} original class using Vert.x codegen.
6+
*/
7+
public class MySQLDataObjectParamMapper implements java.util.function.Function<MySQLDataObject, java.util.Map<String, Object>> {
8+
9+
public static final java.util.function.Function<MySQLDataObject, java.util.Map<String, Object>> INSTANCE = new MySQLDataObjectParamMapper();
10+
11+
public java.util.Map<String, Object> apply(MySQLDataObject obj) {
12+
java.util.Map<String, Object> params = new java.util.HashMap<>();
13+
params.put("duration", obj.getDuration());
14+
return params;
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.vertx.sqlclient.template;
2+
3+
/**
4+
* Mapper for {@link MySQLDataObject}.
5+
* NOTE: This class has been automatically generated from the {@link MySQLDataObject} original class using Vert.x codegen.
6+
*/
7+
public class MySQLDataObjectRowMapper implements java.util.function.Function<io.vertx.sqlclient.Row, MySQLDataObject> {
8+
9+
public static final java.util.function.Function<io.vertx.sqlclient.Row, MySQLDataObject> INSTANCE = new MySQLDataObjectRowMapper();
10+
11+
public static final java.util.stream.Collector<io.vertx.sqlclient.Row, ?, java.util.List<MySQLDataObject>> COLLECTOR = java.util.stream.Collectors.mapping(INSTANCE, java.util.stream.Collectors.toList());
12+
13+
public MySQLDataObject apply(io.vertx.sqlclient.Row row) {
14+
MySQLDataObject obj = new MySQLDataObject();
15+
Object val;
16+
val = row.get(java.time.Duration.class, "duration");
17+
if (val != null) {
18+
obj.setDuration((java.time.Duration)val);
19+
}
20+
return obj;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.vertx.sqlclient.template;
2+
3+
/**
4+
* Mapper for {@link PostgreSQLDataObject}.
5+
* NOTE: This class has been automatically generated from the {@link PostgreSQLDataObject} original class using Vert.x codegen.
6+
*/
7+
public class PostgreSQLDataObjectParamMapper implements java.util.function.Function<PostgreSQLDataObject, java.util.Map<String, Object>> {
8+
9+
public static final java.util.function.Function<PostgreSQLDataObject, java.util.Map<String, Object>> INSTANCE = new PostgreSQLDataObjectParamMapper();
10+
11+
public java.util.Map<String, Object> apply(PostgreSQLDataObject obj) {
12+
java.util.Map<String, Object> params = new java.util.HashMap<>();
13+
params.put("box", obj.getBox());
14+
params.put("circle", obj.getCircle());
15+
params.put("interval", obj.getInterval());
16+
params.put("line", obj.getLine());
17+
params.put("lineSegment", obj.getLineSegment());
18+
params.put("path", obj.getPath());
19+
params.put("point", obj.getPoint());
20+
params.put("polygon", obj.getPolygon());
21+
return params;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.vertx.sqlclient.template;
2+
3+
/**
4+
* Mapper for {@link PostgreSQLDataObject}.
5+
* NOTE: This class has been automatically generated from the {@link PostgreSQLDataObject} original class using Vert.x codegen.
6+
*/
7+
public class PostgreSQLDataObjectRowMapper implements java.util.function.Function<io.vertx.sqlclient.Row, PostgreSQLDataObject> {
8+
9+
public static final java.util.function.Function<io.vertx.sqlclient.Row, PostgreSQLDataObject> INSTANCE = new PostgreSQLDataObjectRowMapper();
10+
11+
public static final java.util.stream.Collector<io.vertx.sqlclient.Row, ?, java.util.List<PostgreSQLDataObject>> COLLECTOR = java.util.stream.Collectors.mapping(INSTANCE, java.util.stream.Collectors.toList());
12+
13+
public PostgreSQLDataObject apply(io.vertx.sqlclient.Row row) {
14+
PostgreSQLDataObject obj = new PostgreSQLDataObject();
15+
Object val;
16+
val = row.get(io.vertx.pgclient.data.Box.class, "box");
17+
if (val != null) {
18+
obj.setBox((io.vertx.pgclient.data.Box)val);
19+
}
20+
val = row.get(io.vertx.pgclient.data.Circle.class, "circle");
21+
if (val != null) {
22+
obj.setCircle((io.vertx.pgclient.data.Circle)val);
23+
}
24+
val = row.get(io.vertx.pgclient.data.Interval.class, "interval");
25+
if (val != null) {
26+
obj.setInterval((io.vertx.pgclient.data.Interval)val);
27+
}
28+
val = row.get(io.vertx.pgclient.data.Line.class, "line");
29+
if (val != null) {
30+
obj.setLine((io.vertx.pgclient.data.Line)val);
31+
}
32+
val = row.get(io.vertx.pgclient.data.LineSegment.class, "lineSegment");
33+
if (val != null) {
34+
obj.setLineSegment((io.vertx.pgclient.data.LineSegment)val);
35+
}
36+
val = row.get(io.vertx.pgclient.data.Path.class, "path");
37+
if (val != null) {
38+
obj.setPath((io.vertx.pgclient.data.Path)val);
39+
}
40+
val = row.get(io.vertx.pgclient.data.Point.class, "point");
41+
if (val != null) {
42+
obj.setPoint((io.vertx.pgclient.data.Point)val);
43+
}
44+
val = row.get(io.vertx.pgclient.data.Polygon.class, "polygon");
45+
if (val != null) {
46+
obj.setPolygon((io.vertx.pgclient.data.Polygon)val);
47+
}
48+
return obj;
49+
}
50+
}

vertx-sql-client-template/src/test/java/io/vertx/sqlclient/template/DataObjectParamsTest.java

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717

1818
package io.vertx.sqlclient.template;
1919

20-
import io.vertx.core.Vertx;
2120
import io.vertx.core.buffer.Buffer;
2221
import io.vertx.core.json.JsonArray;
2322
import io.vertx.core.json.JsonObject;
24-
import io.vertx.ext.unit.Async;
2523
import io.vertx.ext.unit.TestContext;
26-
import io.vertx.pgclient.PgConnection;
2724
import io.vertx.sqlclient.Row;
28-
import io.vertx.sqlclient.RowSet;
29-
import org.junit.After;
30-
import org.junit.Before;
3125
import org.junit.Test;
3226

3327
import java.time.LocalDate;
@@ -36,17 +30,14 @@
3630
import java.time.OffsetDateTime;
3731
import java.time.OffsetTime;
3832
import java.time.ZoneOffset;
39-
import java.util.Map;
4033
import java.util.UUID;
4134
import java.util.concurrent.TimeUnit;
42-
import java.util.function.Consumer;
43-
44-
import static org.junit.Assert.assertEquals;
35+
import java.util.function.Function;
4536

4637
/**
4738
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
4839
*/
49-
public class DataObjectParamsTest extends TemplateTestBase {
40+
public class DataObjectParamsTest extends PgTemplateTestBase {
5041

5142
private final LocalTime localTime = LocalTime.parse("19:35:58.237666");
5243
private final OffsetTime offsetTime = OffsetTime.of(localTime, ZoneOffset.UTC);
@@ -58,60 +49,31 @@ public class DataObjectParamsTest extends TemplateTestBase {
5849
private final Buffer buffer = Buffer.buffer(string);
5950
private final JsonObject jsonObject = new JsonObject().put("string", "str-value").put("number", 1234);
6051
private final JsonArray jsonArray = new JsonArray().add(1).add(2).add(3);
61-
protected Vertx vertx;
62-
protected PgConnection connection;
63-
64-
@Before
65-
public void setup(TestContext ctx) throws Exception {
66-
vertx = Vertx.vertx();
67-
Async async = ctx.async();
68-
PgConnection.connect(vertx, connectOptions(), ctx.asyncAssertSuccess(conn -> {
69-
connection = conn;
70-
async.complete();
71-
}));
72-
async.await(10000);
73-
}
74-
75-
@After
76-
public void teardown(TestContext ctx) {
77-
if (connection != null) {
78-
connection.close();
79-
}
80-
vertx.close(ctx.asyncAssertSuccess());
81-
}
8252

8353
@Test
8454
public void testString(TestContext ctx) {
8555
TestDataObject obj = new TestDataObject();
8656
obj.setString("the_string");;
87-
testGet(ctx, "VARCHAR", "the_string", obj, row -> {
88-
assertEquals("the_string", row.getValue("value"));
89-
});
57+
testGet(ctx, "VARCHAR", "the_string", obj, "the_string");
9058
}
9159

9260
@Test
9361
public void testEnum(TestContext ctx) {
9462
TestDataObject obj = new TestDataObject();
9563
obj.setTimeUnit(TimeUnit.MICROSECONDS);;
96-
testGet(ctx, "VARCHAR", "timeUnit", obj, row -> {
97-
assertEquals("MICROSECONDS", row.getValue("value"));
98-
});
64+
testGet(ctx, "VARCHAR", "timeUnit", obj, "MICROSECONDS");
9965
}
10066

101-
private void testGet(TestContext ctx, String sqlType, String paramName, TestDataObject obj, Consumer<Row> checker) {
102-
Async async = ctx.async();
103-
SqlTemplate<Map<String, Object>, RowSet<Row>> template = SqlTemplate.forQuery(connection, "SELECT ${" + paramName + "} :: " + sqlType + " \"value\"");
104-
template.execute(TestDataObjectParamMapper.INSTANCE.apply(obj), ctx.asyncAssertSuccess(result -> {
105-
ctx.assertEquals(1, result.size());
106-
Row row = result.iterator().next();
107-
try {
108-
checker.accept(row);
109-
} catch (Throwable t) {
110-
ctx.fail(t);
111-
return;
112-
}
113-
async.complete();
114-
}));
115-
async.await(10000);
67+
private void testGet(TestContext ctx, String sqlType, String paramName, TestDataObject obj, Object expected) {
68+
super.<TestDataObject, Row, Object>testGet(
69+
ctx,
70+
sqlType,
71+
Function.identity(),
72+
TestDataObjectParamMapper.INSTANCE,
73+
paramName,
74+
obj,
75+
expected,
76+
row -> row.getValue("value"),
77+
"value");
11678
}
11779
}

vertx-sql-client-template/src/test/java/io/vertx/sqlclient/template/DataObjectTypesTest.java

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
package io.vertx.sqlclient.template;
1919

20-
import io.vertx.core.Vertx;
2120
import io.vertx.core.buffer.Buffer;
2221
import io.vertx.core.json.JsonArray;
2322
import io.vertx.core.json.JsonObject;
2423
import io.vertx.ext.unit.Async;
2524
import io.vertx.ext.unit.TestContext;
26-
import io.vertx.pgclient.PgConnectOptions;
27-
import io.vertx.pgclient.PgConnection;
2825
import io.vertx.sqlclient.RowSet;
2926
import io.vertx.sqlclient.template.wrappers.BooleanWrapper;
3027
import io.vertx.sqlclient.template.wrappers.DoubleWrapper;
@@ -35,8 +32,6 @@
3532
import io.vertx.sqlclient.template.wrappers.LongWrapper;
3633
import io.vertx.sqlclient.template.wrappers.ShortWrapper;
3734
import io.vertx.sqlclient.template.wrappers.StringWrapper;
38-
import org.junit.After;
39-
import org.junit.Before;
4035
import org.junit.Test;
4136

4237
import java.time.LocalDate;
@@ -56,7 +51,7 @@
5651
/**
5752
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
5853
*/
59-
public class DataObjectTypesTest extends TemplateTestBase {
54+
public class DataObjectTypesTest extends PgTemplateTestBase {
6055

6156
private final LocalTime localTime = LocalTime.parse("19:35:58.237666");
6257
private final OffsetTime offsetTime = OffsetTime.of(localTime, ZoneOffset.UTC);
@@ -68,28 +63,6 @@ public class DataObjectTypesTest extends TemplateTestBase {
6863
private final Buffer buffer = Buffer.buffer(string);
6964
private final JsonObject jsonObject = new JsonObject().put("string", "str-value").put("number", 1234);
7065
private final JsonArray jsonArray = new JsonArray().add(1).add(2).add(3);
71-
protected Vertx vertx;
72-
protected PgConnection connection;
73-
74-
@Before
75-
public void setup(TestContext ctx) {
76-
PgConnectOptions options = connectOptions();
77-
vertx = Vertx.vertx();
78-
Async async = ctx.async();
79-
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
80-
connection = conn;
81-
async.complete();
82-
}));
83-
async.await(10000);
84-
}
85-
86-
@After
87-
public void teardown(TestContext ctx) {
88-
if (connection != null) {
89-
connection.close();
90-
}
91-
vertx.close(ctx.asyncAssertSuccess());
92-
}
9366

9467
@Test
9568
public void testBoolean(TestContext ctx) {
@@ -381,16 +354,16 @@ public void testJsonArrayMethodMappedDataObject(TestContext ctx) {
381354
testGet(ctx, "JSON[]", new Object[] {jsonArray}, Collections.singletonList(new JsonArrayWrapper(jsonArray)), "addedJsonArrayMethodMappedDataObjects", TestDataObject::getAddedJsonArrayMethodMappedDataObjects);
382355
}
383356

384-
private <I, O> void testGet(TestContext ctx, String sqlType, I value, O expected, String column, Function<TestDataObject, O> getter) {
385-
Async async = ctx.async();
386-
SqlTemplate<Map<String, Object>, RowSet<TestDataObject>> template = SqlTemplate
387-
.forQuery(connection, "SELECT ${value} :: " + sqlType + " \"" + column + "\"")
388-
.mapTo(TestDataObjectRowMapper.INSTANCE);
389-
template.execute(Collections.singletonMap("value", value), ctx.asyncAssertSuccess(result -> {
390-
ctx.assertEquals(1, result.size());
391-
ctx.assertEquals(expected, getter.apply(result.iterator().next()));
392-
async.complete();
393-
}));
394-
async.await(10000);
357+
private <P, V> void testGet(TestContext ctx, String sqlType, P param, V value, String column, Function<TestDataObject, V> getter) {
358+
super.testGet(
359+
ctx,
360+
sqlType,
361+
TestDataObjectRowMapper.INSTANCE,
362+
Function.identity(),
363+
"value",
364+
Collections.singletonMap("value", param),
365+
value,
366+
getter,
367+
column);
395368
}
396369
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.vertx.sqlclient.template;
2+
3+
import io.vertx.codegen.annotations.DataObject;
4+
import io.vertx.sqlclient.template.annotations.ParamsMapped;
5+
import io.vertx.sqlclient.template.annotations.RowMapped;
6+
7+
import java.time.Duration;
8+
9+
@DataObject
10+
@RowMapped
11+
@ParamsMapped
12+
public class MySQLDataObject {
13+
14+
private Duration duration;
15+
16+
public Duration getDuration() {
17+
return duration;
18+
}
19+
20+
public MySQLDataObject setDuration(Duration duration) {
21+
this.duration = duration;
22+
return this;
23+
}
24+
}

0 commit comments

Comments
 (0)