Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This repository includes the following examples:
* [example-sql-annotation](example-sql-annotation) - Uses SQL annotations to store SQL templates.
* [example-criteria](example-criteria) - Uses the Criteria API.
* [example-jpms](example-jpms) - Uses the Java Platform Module System (JPMS).
* [example-geometric-type](example-geometric-type) - Uses Geometric types of PostgreSQL.

ER diagram
---------------------
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ tasks {
mapOf(
"doma.domain.converters" to "example.common.domain.DomainConverterProvider",
)
// If you are not using Eclipse, you can simply write the above code as follows without using aptOptions;
// options.compilerArgs.add("-Adoma.domain.converters=example.common.domain.DomainConverterProvider")
}
}
13 changes: 13 additions & 0 deletions example-geometric-type/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.classpath
.factorypath
.project
.settings
/.apt_generated
/bin
/build
.gradle
.idea
out
/.apt_generated_tests/
.vscode
.sdkmanrc
21 changes: 21 additions & 0 deletions example-geometric-type/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id("java")
}

dependencies {
implementation(libs.jdbc.postgresql)
testImplementation(platform(libs.testcontainers.bom))
testImplementation(libs.testcontainers.postgresql)
}

tasks {
compileJava {
val aptOptions = extensions.getByType<com.diffplug.gradle.eclipse.apt.AptPlugin.AptOptions>()
aptOptions.processorArgs =
mapOf(
"doma.domain.converters" to "example.geometric.type.domain.DomainConverterProvider",
)
// If you are not using Eclipse, you can simply write the above code as follows without using aptOptions;
// options.compilerArgs.add("-Adoma.domain.converters=example.geometric.type.domain.DomainConverterProvider")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.geometric.type.dao;

import example.geometric.type.entity.CircleZone;
import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.Sql;

@Dao
public interface CircleZoneDao {

@Sql("select /*%expand */* from circle_zone where id = /* id */0")
@Select
CircleZone selectById(Integer id);

@Insert
int insert(CircleZone circleZone);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.geometric.type.dao;

import example.geometric.type.entity.Landmark;
import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.Sql;

@Dao
public interface LandmarkDao {

@Sql("select /*%expand */* from landmark where id = /* id */0")
@Select
Landmark selectById(Integer id);

@Insert
int insert(Landmark landmark);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.geometric.type.dao;

import org.seasar.doma.Dao;
import org.seasar.doma.Script;

@Dao
public interface ScriptDao {

@Script
void create();

@Script
void drop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package example.geometric.type.domain;

public record Circle(Point center, double radius) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package example.geometric.type.domain;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.postgresql.geometric.PGcircle;
import org.postgresql.geometric.PGpoint;
import org.seasar.doma.ExternalDomain;
import org.seasar.doma.jdbc.domain.JdbcTypeProvider;
import org.seasar.doma.jdbc.type.AbstractJdbcType;
import org.seasar.doma.jdbc.type.JdbcType;

@ExternalDomain
public class CircleTypeProvider extends JdbcTypeProvider<Circle> {

private static final CircleType circleType = new CircleType();

@Override
public JdbcType<Circle> getJdbcType() {
return circleType;
}
}

class CircleType extends AbstractJdbcType<Circle> {

protected CircleType() {
super(Types.OTHER);
}

@Override
protected Circle doGetValue(ResultSet resultSet, int index) throws SQLException {
PGcircle c = resultSet.getObject(index, PGcircle.class);
return toCircle(c);
}

@Override
protected void doSetValue(PreparedStatement preparedStatement, int index, Circle value)
throws SQLException {
PGpoint p = new PGpoint(value.center().x(), value.center().y());
PGcircle c = new PGcircle(p, value.radius());
preparedStatement.setObject(index, c);
}

@Override
protected Circle doGetValue(CallableStatement callableStatement, int index) throws SQLException {
PGcircle c = callableStatement.getObject(index, PGcircle.class);
return toCircle(c);
}

@Override
protected String doConvertToLogFormat(Circle value) {
return value.toString();
}

static Circle toCircle(PGcircle c) {
if (c == null) {
return null;
}
Point center = PointType.toPoint(c.center);
return new Circle(center, c.radius);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package example.geometric.type.domain;

import org.seasar.doma.DomainConverters;

@DomainConverters({CircleTypeProvider.class, PointTypeProvider.class})
public class DomainConverterProvider {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package example.geometric.type.domain;

public record Point(double x, double y) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package example.geometric.type.domain;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.postgresql.geometric.PGpoint;
import org.seasar.doma.ExternalDomain;
import org.seasar.doma.jdbc.domain.JdbcTypeProvider;
import org.seasar.doma.jdbc.type.AbstractJdbcType;
import org.seasar.doma.jdbc.type.JdbcType;

@ExternalDomain
public class PointTypeProvider extends JdbcTypeProvider<Point> {

private static final PointType pointType = new PointType();

@Override
public JdbcType<Point> getJdbcType() {
return pointType;
}
}

class PointType extends AbstractJdbcType<Point> {

protected PointType() {
super(Types.OTHER);
}

@Override
protected Point doGetValue(ResultSet resultSet, int index) throws SQLException {
PGpoint p = resultSet.getObject(index, PGpoint.class);
return toPoint(p);
}

@Override
protected void doSetValue(PreparedStatement preparedStatement, int index, Point value)
throws SQLException {
PGpoint p = new PGpoint(value.x(), value.y());
preparedStatement.setObject(index, p);
}

@Override
protected Point doGetValue(CallableStatement callableStatement, int index) throws SQLException {
PGpoint p = callableStatement.getObject(index, PGpoint.class);
return toPoint(p);
}

@Override
protected String doConvertToLogFormat(Point value) {
return value.toString();
}

static Point toPoint(PGpoint p) {
if (p == null) {
return null;
}
return new Point(p.x, p.y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.geometric.type.entity;

import example.geometric.type.domain.Circle;
import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Metamodel;

@Entity(metamodel = @Metamodel)
public class CircleZone {
@Id public Integer id;
public String name;
public Circle boundary;

@Override
public String toString() {
return "CircleZone [id=" + id + ", name=" + name + ", boundary=" + boundary + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.geometric.type.entity;

import example.geometric.type.domain.Point;
import org.seasar.doma.Entity;
import org.seasar.doma.Id;
import org.seasar.doma.Metamodel;

@Entity(metamodel = @Metamodel)
public class Landmark {
@Id public Integer id;
public String name;
public Point location;

@Override
public String toString() {
return "Landmark [id=" + id + ", name=" + name + ", location=" + location + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE landmark (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
location POINT NOT NULL
);

CREATE TABLE circle_zone (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
boundary CIRCLE NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE landmark;
DROP TABLE circle_zone;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package example.geometric.type;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import example.geometric.type.dao.CircleZoneDao;
import example.geometric.type.dao.CircleZoneDaoImpl;
import example.geometric.type.domain.Circle;
import example.geometric.type.domain.Point;
import example.geometric.type.entity.CircleZone;
import example.geometric.type.entity.CircleZone_;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.seasar.doma.jdbc.Config;
import org.seasar.doma.jdbc.criteria.QueryDsl;

@ExtendWith(TestEnvironment.class)
public class CircleZoneTest {

@Test
void testCircle_withSqlAnnotation(Config config) {
CircleZoneDao dao = new CircleZoneDaoImpl(config);

CircleZone circleZone = new CircleZone();
circleZone.id = 1;
circleZone.name = "Round Plaza";
circleZone.boundary = new Circle(new Point(35.6804, 139.7690), 0.005);

int count = dao.insert(circleZone);
assertEquals(1, count);

CircleZone entity = dao.selectById(1);
assertNotNull(entity);
assertEquals(circleZone.id, entity.id);
assertEquals(circleZone.name, entity.name);
assertEquals(circleZone.boundary, entity.boundary);
}

@Test
void testCircle_withCriteria(Config config) {
QueryDsl dsl = new QueryDsl(config);
CircleZone_ c = new CircleZone_();

CircleZone circleZone = new CircleZone();
circleZone.id = 1;
circleZone.name = "Round Plaza";
circleZone.boundary = new Circle(new Point(35.6804, 139.7690), 0.005);

var result = dsl.insert(c).single(circleZone).execute();
assertEquals(1, result.getCount());

CircleZone entity = dsl.from(c).where(wh -> wh.eq(c.id, 1)).fetchOne();
assertNotNull(entity);
assertEquals(circleZone.id, entity.id);
assertEquals(circleZone.name, entity.name);
assertEquals(circleZone.boundary, entity.boundary);
}
}
Loading