From 41775ce9f6ec35c2d9ae3371aa8286aefb5c2451 Mon Sep 17 00:00:00 2001 From: Toshihiro Nakamura Date: Sun, 15 Jun 2025 11:52:39 +0900 Subject: [PATCH] Add condition-based search functionality for employees MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add EmployeeCondition record for search criteria (name pattern and age) - Add selectByCondition method to EmployeeDao - Add SQL query for condition-based search - Update build script to generate condition classes for all DAOs - Add test for selectByCondition functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- build.gradle.kts | 53 ++++++++++++++++++- .../java/com/example/EmployeeCondition.java | 9 ++++ src/main/java/com/example/EmployeeDao.java | 4 ++ .../example/EmployeeDao/selectByCondition.sql | 4 ++ .../java/com/example/EmployeeDaoTest.java | 7 +++ 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/EmployeeCondition.java create mode 100644 src/main/resources/META-INF/com/example/EmployeeDao/selectByCondition.sql diff --git a/build.gradle.kts b/build.gradle.kts index 342acaf..0d3eb39 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -81,6 +81,8 @@ tasks { writeEmployeeDaoCode(employeeDaoFile, i) val employeeAggregateStrategyFile = File(sourceDir, "Employee${i}AggregateStrategy.java") writeEmployeeAggregateStrategyCode(employeeAggregateStrategyFile, i) + val employeeConditionFile = File(sourceDir, "Employee${i}Condition.java") + writeEmployeeConditionCode(employeeConditionFile, i) } println("Generated DAO files in src/main/java/com/example/dao") } @@ -119,8 +121,10 @@ tasks { (1..generationSize).forEach { i -> val dir = file("$sqlFileDirPath/Employee${i}Dao") dir.mkdirs() - val sqlFile = File(dir, "selectById.sql") - writeSelectByIdSqlFile(sqlFile, i) + val selectByIdSqlFile = File(dir, "selectById.sql") + writeSelectByIdSqlFile(selectByIdSqlFile, i) + val selectByConditionSqlFile = File(dir, "selectByCondition.sql") + writeSelectByConditionSqlFile(selectByConditionSqlFile, i) val createScriptFile = File(dir, "create.script") writeCreateScriptFile(createScriptFile, i) val dropScriptFile = File(dir, "drop.script") @@ -143,6 +147,8 @@ fun writeEmployeeDaoCode( """ package com.example.dao; + import java.util.List; + import org.seasar.doma.Dao; import org.seasar.doma.Insert; import org.seasar.doma.Update; @@ -166,6 +172,9 @@ fun writeEmployeeDaoCode( @Select(aggregateStrategy = Employee${i}AggregateStrategy.class) Employee$i selectById(Long id); + @Select + List selectByCondition(Employee${i}Condition condition); + @Script void create(); @@ -209,6 +218,25 @@ fun writeEmployeeAggregateStrategyCode( ) } +fun writeEmployeeConditionCode( + file: File, + i: Int, +) { + file.writeText( + """ + package com.example.dao; + + import java.util.Objects; + + public record Employee${i}Condition(String namePattern, int age) { + public Employee${i}Condition { + Objects.requireNonNull(namePattern); + } + } + """.trimIndent(), + ) +} + fun writeEmployeeCode( file: File, i: Int, @@ -312,6 +340,20 @@ fun writeSelectByIdSqlFile( ) } +fun writeSelectByConditionSqlFile( + file: File, + i: Int, +) { + file.writeText( + """ + SELECT /*%expand*/* + FROM employee$i e + WHERE e.age > /* condition.age */0 + AND e.name LIKE /* condition.namePattern */'test' + """.trimIndent(), + ) +} + fun writeCreateScriptFile( file: File, i: Int, @@ -463,6 +505,13 @@ fun writeEmployeeDaoTestCode( assertEquals("Engineering", employee2.department.name.value()); assertEquals(employee.id, employee2.manager.id); } + + @Test + void selectByCondition() { + var condition = new Employee${i}Condition("%J%", 20); + var employees = employeeDao.selectByCondition(condition); + assertEquals(2, employees.size()); + } } """.trimIndent(), ) diff --git a/src/main/java/com/example/EmployeeCondition.java b/src/main/java/com/example/EmployeeCondition.java new file mode 100644 index 0000000..09726cc --- /dev/null +++ b/src/main/java/com/example/EmployeeCondition.java @@ -0,0 +1,9 @@ +package com.example; + +import java.util.Objects; + +public record EmployeeCondition(String namePattern, int age) { + public EmployeeCondition { + Objects.requireNonNull(namePattern); + } +} diff --git a/src/main/java/com/example/EmployeeDao.java b/src/main/java/com/example/EmployeeDao.java index 3332c2f..5af12e1 100644 --- a/src/main/java/com/example/EmployeeDao.java +++ b/src/main/java/com/example/EmployeeDao.java @@ -1,5 +1,6 @@ package com.example; +import java.util.List; import org.seasar.doma.Dao; import org.seasar.doma.Delete; import org.seasar.doma.Insert; @@ -21,6 +22,9 @@ public interface EmployeeDao { @Select(aggregateStrategy = EmployeeAggregateStrategy.class) Employee selectById(Long id); + @Select + List selectByCondition(EmployeeCondition condition); + @Script void create(); diff --git a/src/main/resources/META-INF/com/example/EmployeeDao/selectByCondition.sql b/src/main/resources/META-INF/com/example/EmployeeDao/selectByCondition.sql new file mode 100644 index 0000000..983f75b --- /dev/null +++ b/src/main/resources/META-INF/com/example/EmployeeDao/selectByCondition.sql @@ -0,0 +1,4 @@ +SELECT /*%expand*/* + FROM employee e + WHERE e.age > /* condition.age */0 + AND e.name LIKE /* condition.namePattern */'test' diff --git a/src/test/java/com/example/EmployeeDaoTest.java b/src/test/java/com/example/EmployeeDaoTest.java index f5f9028..dbd3fc9 100644 --- a/src/test/java/com/example/EmployeeDaoTest.java +++ b/src/test/java/com/example/EmployeeDaoTest.java @@ -69,4 +69,11 @@ void selectById() { assertEquals("Engineering", employee2.department.name.value()); assertEquals(employee.id, employee2.manager.id); } + + @Test + void selectByCondition() { + var condition = new EmployeeCondition("%J%", 20); + var employees = employeeDao.selectByCondition(condition); + assertEquals(2, employees.size()); + } }