Skip to content

Commit 058918d

Browse files
authored
fix: add Timestamp predicate builder support (#287)
* fix: add Timestamp predicate builder support * fix: add Timestamp unit tests
1 parent 62222e0 commit 058918d

File tree

4 files changed

+307
-12
lines changed

4 files changed

+307
-12
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java

+48
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Constructor;
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
23+
import java.sql.Timestamp;
2324
import java.time.Instant;
2425
import java.time.LocalDate;
2526
import java.time.LocalDateTime;
@@ -348,6 +349,50 @@ protected Predicate getDatePredicate(Path<? extends Date> root, PredicateFilter
348349
return null;
349350
}
350351

352+
protected Predicate getTimestampPredicate(Path<? extends Timestamp> root, PredicateFilter filter) {
353+
if (filter.getValue() != null && filter.getValue() instanceof Timestamp) {
354+
if (filter.getCriterias().contains(PredicateFilter.Criteria.LT)) {
355+
return cb.lessThan(root, (Timestamp) filter.getValue());
356+
}
357+
if (filter.getCriterias().contains(PredicateFilter.Criteria.GT)) {
358+
return cb.greaterThan(root, (Timestamp) filter.getValue());
359+
}
360+
if (filter.getCriterias().contains(PredicateFilter.Criteria.GE)) {
361+
return cb.greaterThanOrEqualTo(root, (Timestamp) filter.getValue());
362+
}
363+
if (filter.getCriterias().contains(PredicateFilter.Criteria.EQ)) {
364+
return cb.equal(root, filter.getValue());
365+
}
366+
if (filter.getCriterias().contains(PredicateFilter.Criteria.NE)) {
367+
return cb.notEqual(root, filter.getValue());
368+
}
369+
// LE or default
370+
return cb.lessThanOrEqualTo(root, (Timestamp) filter.getValue());
371+
} else if (filter.getValue().getClass().isArray() || filter.getValue() instanceof Collection) {
372+
if (!filter.getCriterias().contains(PredicateFilter.Criteria.NE)
373+
&& (filter.getCriterias().contains(Criteria.BETWEEN) || filter.getCriterias().contains(Criteria.NOT_BETWEEN))) {
374+
375+
Object[] values;
376+
if (filter.getValue().getClass().isArray()) {
377+
values = (Object[]) filter.getValue();
378+
} else {
379+
values = ((Collection<?>) filter.getValue()).toArray();
380+
}
381+
382+
if (values.length == 2) {
383+
Expression<Timestamp> name = (Expression<Timestamp>) root;
384+
Expression<Timestamp> fromDate = cb.literal((Timestamp) values[0]);
385+
Expression<Timestamp> toDate = cb.literal((Timestamp) values[1]);
386+
Predicate between = cb.between(name, fromDate, toDate);
387+
if (filter.getCriterias().contains(Criteria.BETWEEN))
388+
return between;
389+
return cb.not(between);
390+
}
391+
}
392+
}
393+
return null;
394+
}
395+
351396
protected Predicate getLocalDatePredicate(Path<? extends LocalDate> root, PredicateFilter filter) {
352397
if (filter.getValue() != null && filter.getValue() instanceof LocalDate) {
353398
if (filter.getCriterias().contains(PredicateFilter.Criteria.LT)) {
@@ -720,6 +765,9 @@ else if(type.equals(ZonedDateTime.class)){
720765
else if(type.equals(OffsetDateTime.class)){
721766
return getOffsetDateTimePredicate((Path<OffsetDateTime>) field, predicateFilter);
722767
}
768+
else if (type.equals(Timestamp.class)) {
769+
return getTimestampPredicate((Path<Timestamp>) field, predicateFilter);
770+
}
723771
else if (type.equals(Boolean.class)) {
724772
return getBooleanPredicate(field, predicateFilter);
725773
}

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/localdatetime/GraphQLLocalDateTimeTest.java

+233
Original file line numberDiff line numberDiff line change
@@ -1202,4 +1202,237 @@ public void queryInstantWithEqualTest() {
12021202
// then
12031203
assertThat(result.toString()).isEqualTo(expected);
12041204
}
1205+
1206+
@Test
1207+
public void queryTimestampWithGreaterThanTest() {
1208+
//given
1209+
String query = "query{" +
1210+
" localDates" +
1211+
" (where:{" +
1212+
" timestamp:{" +
1213+
" GT:\"2019-08-07T03:58:08Z\"" +
1214+
" }" +
1215+
" })" +
1216+
"{" +
1217+
" select{" +
1218+
" id" +
1219+
" timestamp" +
1220+
" description" +
1221+
" }" +
1222+
" }" +
1223+
"}";
1224+
1225+
String expected = "{localDates={select=[{id=8, timestamp=2019-08-08T03:58:08Z, description=Add test for LocalDate.}]}}";
1226+
1227+
//when
1228+
Object result = executor.execute(query).getData();
1229+
1230+
// then
1231+
assertThat(result.toString()).isEqualTo(expected);
1232+
}
1233+
1234+
@Test
1235+
public void queryTimestampWithGreaterThanOrEqualTest() {
1236+
//given
1237+
String query = "query{" +
1238+
" localDates" +
1239+
" (where:{" +
1240+
" timestamp:{" +
1241+
" GE:\"2019-08-08T03:58:08Z\"" +
1242+
" }" +
1243+
" })" +
1244+
"{" +
1245+
" select{" +
1246+
" id" +
1247+
" timestamp" +
1248+
" description" +
1249+
" }" +
1250+
" }" +
1251+
"}";
1252+
1253+
String expected = "{localDates={select=[{id=8, timestamp=2019-08-08T03:58:08Z, description=Add test for LocalDate.}]}}";
1254+
1255+
//when
1256+
Object result = executor.execute(query).getData();
1257+
1258+
// then
1259+
assertThat(result.toString()).isEqualTo(expected);
1260+
}
1261+
1262+
@Test
1263+
public void queryTimestampWithLessThanOrEqualTest() {
1264+
//given
1265+
String query = "query{" +
1266+
" localDates" +
1267+
" (where:{" +
1268+
" timestamp:{" +
1269+
" LE:\"2019-08-01T03:58:08Z\"" +
1270+
" }" +
1271+
" })" +
1272+
"{" +
1273+
" select{" +
1274+
" id" +
1275+
" timestamp" +
1276+
" description" +
1277+
" }" +
1278+
" }" +
1279+
"}";
1280+
1281+
String expected = "{localDates={select=[{id=1, timestamp=2019-08-01T03:58:08Z, description=Add test for LocalDate.}]}}";
1282+
1283+
//when
1284+
Object result = executor.execute(query).getData();
1285+
1286+
// then
1287+
assertThat(result.toString()).isEqualTo(expected);
1288+
}
1289+
1290+
@Test
1291+
public void queryTimestampWithLessThanTest() {
1292+
//given
1293+
String query = "query{" +
1294+
" localDates" +
1295+
" (where:{" +
1296+
" timestamp:{" +
1297+
" LT:\"2019-08-02T03:58:08Z\"" +
1298+
" }" +
1299+
" })" +
1300+
"{" +
1301+
" select{" +
1302+
" id" +
1303+
" timestamp" +
1304+
" description" +
1305+
" }" +
1306+
" }" +
1307+
"}";
1308+
1309+
String expected = "{localDates={select=[{id=1, timestamp=2019-08-01T03:58:08Z, description=Add test for LocalDate.}]}}";
1310+
1311+
//when
1312+
Object result = executor.execute(query).getData();
1313+
1314+
// then
1315+
assertThat(result.toString()).isEqualTo(expected);
1316+
}
1317+
1318+
@Test
1319+
public void queryTimestampWithBetweenTest() {
1320+
//given
1321+
String query = "query{" +
1322+
" localDates" +
1323+
" (where:{" +
1324+
" timestamp:{" +
1325+
" BETWEEN:[\"2019-08-06T03:58:08Z\",\"2019-08-06T03:58:08Z\"]" +
1326+
" }" +
1327+
" })" +
1328+
"{" +
1329+
" select{" +
1330+
" id" +
1331+
" timestamp" +
1332+
" description" +
1333+
" }" +
1334+
" }" +
1335+
"}";
1336+
1337+
String expected = "{localDates={select=[{id=6, timestamp=2019-08-06T03:58:08Z, description=Add test for LocalDate.}]}}";
1338+
1339+
//when
1340+
Object result = executor.execute(query).getData();
1341+
1342+
// then
1343+
assertThat(result.toString()).isEqualTo(expected);
1344+
}
1345+
1346+
@Test
1347+
public void queryTimestampWithNotBetweenTest() {
1348+
//given
1349+
String query = "query{" +
1350+
" localDates" +
1351+
" (where:{" +
1352+
" timestamp:{" +
1353+
" NOT_BETWEEN:[\"2019-08-02T03:58:08Z\",\"2019-08-08T03:58:08Z\"]" +
1354+
" }" +
1355+
" })" +
1356+
"{" +
1357+
" select{" +
1358+
" id" +
1359+
" timestamp" +
1360+
" description" +
1361+
" }" +
1362+
" }" +
1363+
"}";
1364+
1365+
String expected = "{localDates={select=[{id=1, timestamp=2019-08-01T03:58:08Z, description=Add test for LocalDate.}]}}";
1366+
1367+
//when
1368+
Object result = executor.execute(query).getData();
1369+
1370+
// then
1371+
assertThat(result.toString()).isEqualTo(expected);
1372+
}
1373+
1374+
@Test
1375+
public void queryTimestampWithNotEqualTest() {
1376+
//given
1377+
String query = "query{" +
1378+
" localDates" +
1379+
" (where:{" +
1380+
" timestamp:{" +
1381+
" NE:\"2019-08-08T03:58:08Z\"" +
1382+
" }" +
1383+
" })" +
1384+
"{" +
1385+
" select{" +
1386+
" id" +
1387+
" timestamp" +
1388+
" description" +
1389+
" }" +
1390+
" }" +
1391+
"}";
1392+
1393+
String expected = "{localDates={select=[" +
1394+
"{id=1, timestamp=2019-08-01T03:58:08Z, description=Add test for LocalDate.}, " +
1395+
"{id=2, timestamp=2019-08-02T03:58:08Z, description=Add test for LocalDate.}, " +
1396+
"{id=3, timestamp=2019-08-03T03:58:08Z, description=Add test for LocalDate.}, " +
1397+
"{id=4, timestamp=2019-08-04T03:58:08Z, description=Add test for LocalDate.}, " +
1398+
"{id=5, timestamp=2019-08-05T03:58:08Z, description=Add test for LocalDate.}, " +
1399+
"{id=6, timestamp=2019-08-06T03:58:08Z, description=Add test for LocalDate.}, " +
1400+
"{id=7, timestamp=2019-08-07T03:58:08Z, description=Add test for LocalDate.}]}}";
1401+
1402+
//when
1403+
Object result = executor.execute(query).getData();
1404+
1405+
// then
1406+
assertThat(result.toString()).isEqualTo(expected);
1407+
}
1408+
1409+
@Test
1410+
public void queryTimestampWithEqualTest() {
1411+
//given
1412+
String query = "query{" +
1413+
" localDates" +
1414+
" (where:{" +
1415+
" timestamp:{" +
1416+
" EQ:\"2019-08-06T03:58:08Z\"" +
1417+
" }" +
1418+
" })" +
1419+
"{" +
1420+
" select{" +
1421+
" id" +
1422+
" timestamp" +
1423+
" description" +
1424+
" }" +
1425+
" }" +
1426+
"}";
1427+
1428+
String expected = "{localDates={select=[{id=6, timestamp=2019-08-06T03:58:08Z, description=Add test for LocalDate.}]}}";
1429+
1430+
//when
1431+
Object result = executor.execute(query).getData();
1432+
1433+
// then
1434+
assertThat(result.toString()).isEqualTo(expected);
1435+
}
1436+
1437+
12051438
}

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/localdatetime/model/LocalDateEntity.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package com.introproventures.graphql.jpa.query.localdatetime.model;
22

3-
import lombok.Getter;
3+
import java.sql.Timestamp;
4+
import java.time.Instant;
5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
import java.time.OffsetDateTime;
8+
import java.time.ZonedDateTime;
9+
10+
import javax.persistence.Column;
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.GenerationType;
14+
import javax.persistence.Id;
15+
import javax.persistence.Table;
416

5-
import javax.persistence.*;
6-
import java.time.*;
17+
import lombok.Getter;
718

819
@Table(name = "LOCAL_DATE")
920
@Entity(name = "localDate")
@@ -28,6 +39,9 @@ public class LocalDateEntity {
2839
@Column(name = "INSTANT")
2940
Instant instant;
3041

42+
@Column(name = "TIMESTAMP")
43+
Timestamp timestamp;
44+
3145
@Column(name = "description")
3246
String description;
3347
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
insert into LOCAL_DATE(id, LOCALDATETIME, OFFSETDATETIME, ZONEDDATETIME, INSTANT, LOCALDATE, description) values
2-
('1', '2019-08-01 10:58:08', '2019-08-01 10:58:07+07:00','2019-08-01 10:58:08+07:00[Asia/Bangkok]', '2019-08-01 03:58:08Z', '2019-08-01', 'Add test for LocalDate.'),
3-
('2', '2019-08-02 10:58:08', '2019-08-02 10:58:07+07:00','2019-08-02 10:58:08+07:00[Asia/Bangkok]', '2019-08-02 03:58:08Z', '2019-08-02', 'Add test for LocalDate.'),
4-
('3', '2019-08-03 10:58:08', '2019-08-03 10:58:07+07:00','2019-08-03 10:58:08+07:00[Asia/Bangkok]', '2019-08-03 03:58:08Z', '2019-08-03', 'Add test for LocalDate.'),
5-
('4', '2019-08-04 10:58:08', '2019-08-04 10:58:07+07:00','2019-08-04 10:58:08+07:00[Asia/Bangkok]', '2019-08-04 03:58:08Z', '2019-08-04', 'Add test for LocalDate.'),
6-
('5', '2019-08-05 10:58:08', '2019-08-05 10:58:07+07:00','2019-08-05 10:58:08+07:00[Asia/Bangkok]', '2019-08-05 03:58:08Z', '2019-08-05', 'Add test for LocalDate.'),
7-
('6', '2019-08-06 10:58:08', '2019-08-06 10:58:07+07:00','2019-08-06 10:58:08+07:00[Asia/Bangkok]', '2019-08-06 03:58:08Z', '2019-08-06', 'Add test for LocalDate.'),
8-
('7', '2019-08-07 10:58:08', '2019-08-07 10:58:07+07:00','2019-08-07 10:58:08+07:00[Asia/Bangkok]', '2019-08-07 03:58:08Z', '2019-08-07', 'Add test for LocalDate.'),
9-
('8', '2019-08-08 10:58:08', '2019-08-08 10:58:07+07:00','2019-08-08 10:58:08+07:00[Asia/Bangkok]', '2019-08-08 03:58:08Z', '2019-08-08', 'Add test for LocalDate.');
1+
insert into LOCAL_DATE(id, LOCALDATETIME, OFFSETDATETIME, ZONEDDATETIME, INSTANT, LOCALDATE, TIMESTAMP, description) values
2+
('1', '2019-08-01 10:58:08', '2019-08-01 10:58:07+07:00','2019-08-01 10:58:08+07:00[Asia/Bangkok]', '2019-08-01 03:58:08Z', '2019-08-01', '2019-08-01 03:58:08Z', 'Add test for LocalDate.'),
3+
('2', '2019-08-02 10:58:08', '2019-08-02 10:58:07+07:00','2019-08-02 10:58:08+07:00[Asia/Bangkok]', '2019-08-02 03:58:08Z', '2019-08-02', '2019-08-02 03:58:08Z', 'Add test for LocalDate.'),
4+
('3', '2019-08-03 10:58:08', '2019-08-03 10:58:07+07:00','2019-08-03 10:58:08+07:00[Asia/Bangkok]', '2019-08-03 03:58:08Z', '2019-08-03', '2019-08-03 03:58:08Z', 'Add test for LocalDate.'),
5+
('4', '2019-08-04 10:58:08', '2019-08-04 10:58:07+07:00','2019-08-04 10:58:08+07:00[Asia/Bangkok]', '2019-08-04 03:58:08Z', '2019-08-04', '2019-08-04 03:58:08Z', 'Add test for LocalDate.'),
6+
('5', '2019-08-05 10:58:08', '2019-08-05 10:58:07+07:00','2019-08-05 10:58:08+07:00[Asia/Bangkok]', '2019-08-05 03:58:08Z', '2019-08-05', '2019-08-05 03:58:08Z', 'Add test for LocalDate.'),
7+
('6', '2019-08-06 10:58:08', '2019-08-06 10:58:07+07:00','2019-08-06 10:58:08+07:00[Asia/Bangkok]', '2019-08-06 03:58:08Z', '2019-08-06', '2019-08-06 03:58:08Z', 'Add test for LocalDate.'),
8+
('7', '2019-08-07 10:58:08', '2019-08-07 10:58:07+07:00','2019-08-07 10:58:08+07:00[Asia/Bangkok]', '2019-08-07 03:58:08Z', '2019-08-07', '2019-08-07 03:58:08Z', 'Add test for LocalDate.'),
9+
('8', '2019-08-08 10:58:08', '2019-08-08 10:58:07+07:00','2019-08-08 10:58:08+07:00[Asia/Bangkok]', '2019-08-08 03:58:08Z', '2019-08-08', '2019-08-08 03:58:08Z', 'Add test for LocalDate.');

0 commit comments

Comments
 (0)