Skip to content

Commit 6109434

Browse files
authored
Merge pull request #4 from domaframework/refactor/condition
Add condition-based search functionality for employees
2 parents 89ffa0f + 41775ce commit 6109434

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

build.gradle.kts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ tasks {
8181
writeEmployeeDaoCode(employeeDaoFile, i)
8282
val employeeAggregateStrategyFile = File(sourceDir, "Employee${i}AggregateStrategy.java")
8383
writeEmployeeAggregateStrategyCode(employeeAggregateStrategyFile, i)
84+
val employeeConditionFile = File(sourceDir, "Employee${i}Condition.java")
85+
writeEmployeeConditionCode(employeeConditionFile, i)
8486
}
8587
println("Generated DAO files in src/main/java/com/example/dao")
8688
}
@@ -119,8 +121,10 @@ tasks {
119121
(1..generationSize).forEach { i ->
120122
val dir = file("$sqlFileDirPath/Employee${i}Dao")
121123
dir.mkdirs()
122-
val sqlFile = File(dir, "selectById.sql")
123-
writeSelectByIdSqlFile(sqlFile, i)
124+
val selectByIdSqlFile = File(dir, "selectById.sql")
125+
writeSelectByIdSqlFile(selectByIdSqlFile, i)
126+
val selectByConditionSqlFile = File(dir, "selectByCondition.sql")
127+
writeSelectByConditionSqlFile(selectByConditionSqlFile, i)
124128
val createScriptFile = File(dir, "create.script")
125129
writeCreateScriptFile(createScriptFile, i)
126130
val dropScriptFile = File(dir, "drop.script")
@@ -143,6 +147,8 @@ fun writeEmployeeDaoCode(
143147
"""
144148
package com.example.dao;
145149
150+
import java.util.List;
151+
146152
import org.seasar.doma.Dao;
147153
import org.seasar.doma.Insert;
148154
import org.seasar.doma.Update;
@@ -166,6 +172,9 @@ fun writeEmployeeDaoCode(
166172
@Select(aggregateStrategy = Employee${i}AggregateStrategy.class)
167173
Employee$i selectById(Long id);
168174
175+
@Select
176+
List<Employee$i> selectByCondition(Employee${i}Condition condition);
177+
169178
@Script
170179
void create();
171180
@@ -209,6 +218,25 @@ fun writeEmployeeAggregateStrategyCode(
209218
)
210219
}
211220

221+
fun writeEmployeeConditionCode(
222+
file: File,
223+
i: Int,
224+
) {
225+
file.writeText(
226+
"""
227+
package com.example.dao;
228+
229+
import java.util.Objects;
230+
231+
public record Employee${i}Condition(String namePattern, int age) {
232+
public Employee${i}Condition {
233+
Objects.requireNonNull(namePattern);
234+
}
235+
}
236+
""".trimIndent(),
237+
)
238+
}
239+
212240
fun writeEmployeeCode(
213241
file: File,
214242
i: Int,
@@ -312,6 +340,20 @@ fun writeSelectByIdSqlFile(
312340
)
313341
}
314342

343+
fun writeSelectByConditionSqlFile(
344+
file: File,
345+
i: Int,
346+
) {
347+
file.writeText(
348+
"""
349+
SELECT /*%expand*/*
350+
FROM employee$i e
351+
WHERE e.age > /* condition.age */0
352+
AND e.name LIKE /* condition.namePattern */'test'
353+
""".trimIndent(),
354+
)
355+
}
356+
315357
fun writeCreateScriptFile(
316358
file: File,
317359
i: Int,
@@ -463,6 +505,13 @@ fun writeEmployeeDaoTestCode(
463505
assertEquals("Engineering", employee2.department.name.value());
464506
assertEquals(employee.id, employee2.manager.id);
465507
}
508+
509+
@Test
510+
void selectByCondition() {
511+
var condition = new Employee${i}Condition("%J%", 20);
512+
var employees = employeeDao.selectByCondition(condition);
513+
assertEquals(2, employees.size());
514+
}
466515
}
467516
""".trimIndent(),
468517
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example;
2+
3+
import java.util.Objects;
4+
5+
public record EmployeeCondition(String namePattern, int age) {
6+
public EmployeeCondition {
7+
Objects.requireNonNull(namePattern);
8+
}
9+
}

src/main/java/com/example/EmployeeDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example;
22

3+
import java.util.List;
34
import org.seasar.doma.Dao;
45
import org.seasar.doma.Delete;
56
import org.seasar.doma.Insert;
@@ -21,6 +22,9 @@ public interface EmployeeDao {
2122
@Select(aggregateStrategy = EmployeeAggregateStrategy.class)
2223
Employee selectById(Long id);
2324

25+
@Select
26+
List<Employee> selectByCondition(EmployeeCondition condition);
27+
2428
@Script
2529
void create();
2630

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT /*%expand*/*
2+
FROM employee e
3+
WHERE e.age > /* condition.age */0
4+
AND e.name LIKE /* condition.namePattern */'test'

src/test/java/com/example/EmployeeDaoTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ void selectById() {
6969
assertEquals("Engineering", employee2.department.name.value());
7070
assertEquals(employee.id, employee2.manager.id);
7171
}
72+
73+
@Test
74+
void selectByCondition() {
75+
var condition = new EmployeeCondition("%J%", 20);
76+
var employees = employeeDao.selectByCondition(condition);
77+
assertEquals(2, employees.size());
78+
}
7279
}

0 commit comments

Comments
 (0)