Implemented missing cache functions for Postgis module.

This commit is contained in:
Ryszard Trojnacki
2023-04-21 08:58:44 +02:00
parent e04f33aaf0
commit 40a6693a4c
9 changed files with 185 additions and 6 deletions
@@ -98,12 +98,7 @@ abstract class ScalarTypePgisBase<T extends Geometry> implements ScalarType<T> {
@Override
public String formatValue(T value) {
return null;
}
@Override
public T parse(String value) {
return null;
return value.toString();
}
@Override
@@ -3,10 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.LineString;
import java.sql.SQLException;
public class ScalarTypePgisLineString extends ScalarTypePgisBase<LineString> {
public ScalarTypePgisLineString() {
super(ExtraDbTypes.LINESTRING, LineString.class);
}
@Override
public LineString parse(String value) {
try {
return new LineString(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -3,10 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.MultiLineString;
import java.sql.SQLException;
public class ScalarTypePgisMultiLineString extends ScalarTypePgisBase<MultiLineString> {
public ScalarTypePgisMultiLineString() {
super(ExtraDbTypes.MULTILINESTRING, MultiLineString.class);
}
@Override
public MultiLineString parse(String value) {
try {
return new MultiLineString(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -3,10 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.MultiPoint;
import java.sql.SQLException;
public class ScalarTypePgisMultiPoint extends ScalarTypePgisBase<MultiPoint> {
public ScalarTypePgisMultiPoint() {
super(ExtraDbTypes.MULTIPOINT, MultiPoint.class);
}
@Override
public MultiPoint parse(String value) {
try {
return new MultiPoint(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -3,10 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.MultiPolygon;
import java.sql.SQLException;
public class ScalarTypePgisMultiPolygon extends ScalarTypePgisBase<MultiPolygon> {
public ScalarTypePgisMultiPolygon() {
super(ExtraDbTypes.MULTIPOLYGON, MultiPolygon.class);
}
@Override
public MultiPolygon parse(String value) {
try {
return new MultiPolygon(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -3,10 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.Point;
import java.sql.SQLException;
public class ScalarTypePgisPoint extends ScalarTypePgisBase<Point> {
public ScalarTypePgisPoint() {
super(ExtraDbTypes.POINT, Point.class);
}
@Override
public Point parse(String value) {
try {
return new Point(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -3,9 +3,20 @@ package io.ebean.postgis;
import io.ebean.config.dbplatform.ExtraDbTypes;
import org.postgis.Polygon;
import java.sql.SQLException;
public class ScalarTypePgisPolygon extends ScalarTypePgisBase<Polygon> {
public ScalarTypePgisPolygon() {
super(ExtraDbTypes.POLYGON, Polygon.class);
}
@Override
public Polygon parse(String value) {
try {
return new Polygon(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,43 @@
package org.example.domain;
import io.ebean.annotation.Cache;
import org.postgis.Point;
import org.postgis.Polygon;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="mybean_cached")
@Cache
public class CachedBean extends BaseEntity {
String name;
Point point;
Polygon polygon;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public Polygon getPolygon() {
return polygon;
}
public void setPolygon(Polygon polygon) {
this.polygon = polygon;
}
}
@@ -0,0 +1,80 @@
package org.example.domain;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import org.postgis.Point;
import org.postgis.Polygon;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.*;
public class TestCacheSerialization {
@Test
public void testCache() throws SQLException {
Point p1=new Point(1.0, 2.0);
p1.setSrid(4674);
Polygon pol=new Polygon("SRID=4674;POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))");
CachedBean tb=new CachedBean();
tb.setId(2080L);
tb.setName("Serialize Test");
tb.setPoint(p1);
tb.setPolygon(pol);
DB.insert(tb);
CachedBean r1=DB.find(CachedBean.class, tb.getId());
assertNotNull(r1);
assertEquals(p1, r1.getPoint());
assertEquals(pol, r1.getPolygon());
CachedBean r2=DB.find(CachedBean.class, tb.getId()); // this is returned from cache
assertNotNull(r2);
assertEquals(p1, r2.getPoint()); // and did fail https://github.com/ebean-orm/ebean/issues/3026
assertEquals(pol, r2.getPolygon());
}
@Test
public void testNullCache() throws SQLException {
Point p1=new Point(1.0, 2.0);
p1.setSrid(4674);
Polygon pol=new Polygon("SRID=4674;POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))");
CachedBean tb=new CachedBean();
tb.setId(2081L);
tb.setName("Serialize Test");
tb.setPoint(p1);
tb.setPolygon(null);
DB.insert(tb);
CachedBean r1=DB.find(CachedBean.class, tb.getId());
assertNotNull(r1);
assertEquals(p1, r1.getPoint());
assertNull(r1.getPolygon());
CachedBean r2=DB.find(CachedBean.class, tb.getId()); // this is returned from cache
assertNotNull(r2);
assertEquals(p1, r2.getPoint()); // and did fail https://github.com/ebean-orm/ebean/issues/3026
assertNull(r1.getPolygon());
tb.setPoint(null);
tb.setPolygon(pol);
DB.save(tb);
r1=DB.find(CachedBean.class, tb.getId());
assertNotNull(r1);
assertEquals(pol, r1.getPolygon());
assertNull(r1.getPoint());
r2=DB.find(CachedBean.class, tb.getId()); // this is returned from cache
assertNotNull(r2);
assertEquals(pol, r2.getPolygon()); // and did fail https://github.com/ebean-orm/ebean/issues/3026
assertNull(r1.getPoint());
}
}