Skip to content

Commit 47936c8

Browse files
authored
Merge pull request #319 from domaframework/feat/geometric-type
Add an example for PostgreSQL geometric types
2 parents 1fb2541 + 725d7b7 commit 47936c8

File tree

25 files changed

+529
-5
lines changed

25 files changed

+529
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This repository includes the following examples:
99
* [example-sql-annotation](example-sql-annotation) - Uses SQL annotations to store SQL templates.
1010
* [example-criteria](example-criteria) - Uses the Criteria API.
1111
* [example-jpms](example-jpms) - Uses the Java Platform Module System (JPMS).
12+
* [example-geometric-type](example-geometric-type) - Uses Geometric types of PostgreSQL.
1213

1314
ER diagram
1415
---------------------

common/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ tasks {
99
mapOf(
1010
"doma.domain.converters" to "example.common.domain.DomainConverterProvider",
1111
)
12+
// If you are not using Eclipse, you can simply write the above code as follows without using aptOptions;
13+
// options.compilerArgs.add("-Adoma.domain.converters=example.common.domain.DomainConverterProvider")
1214
}
1315
}

example-geometric-type/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.classpath
2+
.factorypath
3+
.project
4+
.settings
5+
/.apt_generated
6+
/bin
7+
/build
8+
.gradle
9+
.idea
10+
out
11+
/.apt_generated_tests/
12+
.vscode
13+
.sdkmanrc
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
dependencies {
6+
implementation(libs.jdbc.postgresql)
7+
testImplementation(platform(libs.testcontainers.bom))
8+
testImplementation(libs.testcontainers.postgresql)
9+
}
10+
11+
tasks {
12+
compileJava {
13+
val aptOptions = extensions.getByType<com.diffplug.gradle.eclipse.apt.AptPlugin.AptOptions>()
14+
aptOptions.processorArgs =
15+
mapOf(
16+
"doma.domain.converters" to "example.geometric.type.domain.DomainConverterProvider",
17+
)
18+
// If you are not using Eclipse, you can simply write the above code as follows without using aptOptions;
19+
// options.compilerArgs.add("-Adoma.domain.converters=example.geometric.type.domain.DomainConverterProvider")
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package example.geometric.type.dao;
2+
3+
import example.geometric.type.entity.CircleZone;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Insert;
6+
import org.seasar.doma.Select;
7+
import org.seasar.doma.Sql;
8+
9+
@Dao
10+
public interface CircleZoneDao {
11+
12+
@Sql("select /*%expand */* from circle_zone where id = /* id */0")
13+
@Select
14+
CircleZone selectById(Integer id);
15+
16+
@Insert
17+
int insert(CircleZone circleZone);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package example.geometric.type.dao;
2+
3+
import example.geometric.type.entity.Landmark;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Insert;
6+
import org.seasar.doma.Select;
7+
import org.seasar.doma.Sql;
8+
9+
@Dao
10+
public interface LandmarkDao {
11+
12+
@Sql("select /*%expand */* from landmark where id = /* id */0")
13+
@Select
14+
Landmark selectById(Integer id);
15+
16+
@Insert
17+
int insert(Landmark landmark);
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example.geometric.type.dao;
2+
3+
import org.seasar.doma.Dao;
4+
import org.seasar.doma.Script;
5+
6+
@Dao
7+
public interface ScriptDao {
8+
9+
@Script
10+
void create();
11+
12+
@Script
13+
void drop();
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example.geometric.type.domain;
2+
3+
public record Circle(Point center, double radius) {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package example.geometric.type.domain;
2+
3+
import java.sql.CallableStatement;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Types;
8+
import org.postgresql.geometric.PGcircle;
9+
import org.postgresql.geometric.PGpoint;
10+
import org.seasar.doma.ExternalDomain;
11+
import org.seasar.doma.jdbc.domain.JdbcTypeProvider;
12+
import org.seasar.doma.jdbc.type.AbstractJdbcType;
13+
import org.seasar.doma.jdbc.type.JdbcType;
14+
15+
@ExternalDomain
16+
public class CircleTypeProvider extends JdbcTypeProvider<Circle> {
17+
18+
private static final CircleType circleType = new CircleType();
19+
20+
@Override
21+
public JdbcType<Circle> getJdbcType() {
22+
return circleType;
23+
}
24+
}
25+
26+
class CircleType extends AbstractJdbcType<Circle> {
27+
28+
protected CircleType() {
29+
super(Types.OTHER);
30+
}
31+
32+
@Override
33+
protected Circle doGetValue(ResultSet resultSet, int index) throws SQLException {
34+
PGcircle c = resultSet.getObject(index, PGcircle.class);
35+
return toCircle(c);
36+
}
37+
38+
@Override
39+
protected void doSetValue(PreparedStatement preparedStatement, int index, Circle value)
40+
throws SQLException {
41+
PGpoint p = new PGpoint(value.center().x(), value.center().y());
42+
PGcircle c = new PGcircle(p, value.radius());
43+
preparedStatement.setObject(index, c);
44+
}
45+
46+
@Override
47+
protected Circle doGetValue(CallableStatement callableStatement, int index) throws SQLException {
48+
PGcircle c = callableStatement.getObject(index, PGcircle.class);
49+
return toCircle(c);
50+
}
51+
52+
@Override
53+
protected String doConvertToLogFormat(Circle value) {
54+
return value.toString();
55+
}
56+
57+
static Circle toCircle(PGcircle c) {
58+
if (c == null) {
59+
return null;
60+
}
61+
Point center = PointType.toPoint(c.center);
62+
return new Circle(center, c.radius);
63+
}
64+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package example.geometric.type.domain;
2+
3+
import org.seasar.doma.DomainConverters;
4+
5+
@DomainConverters({CircleTypeProvider.class, PointTypeProvider.class})
6+
public class DomainConverterProvider {}

0 commit comments

Comments
 (0)