Skip to content

Commit 63aa0c4

Browse files
committed
Add SQL files for conditional clauses and format them for improved readability
1 parent 0472fb9 commit 63aa0c4

13 files changed

+298
-6
lines changed

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,6 @@ class SqlFormatterTest : BasePlatformTestCase() {
246246
formatSqlFile("ConditionalUnion.sql", "ConditionalUnion$formatDataPrefix.sql")
247247
}
248248

249-
fun testConditionalSubqueryFormatter() {
250-
formatSqlFile("ConditionalSubquery.sql", "ConditionalSubquery$formatDataPrefix.sql")
251-
}
252-
253249
private fun formatSqlFile(
254250
beforeFile: String,
255251
afterFile: String,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SELECT id, name, department_id
2+
FROM employees
3+
WHERE
4+
/*%if departmentIds != null */
5+
department_id IN /* departmentIds */(1, 2, 3)
6+
/*%end*/
7+
/*%if statusList != null */
8+
AND status IN /* statusList */('active', 'pending', 'inactive')
9+
/*%end*/
10+
/*%if categoryIds != null && categoryIds.size() > 0 */
11+
AND category_id IN /* categoryIds */(100, 200, 300, 400, 500)
12+
/*%end*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT id
2+
, name
3+
, department_id
4+
FROM employees
5+
WHERE
6+
/*%if departmentIds != null */
7+
department_id IN /* departmentIds */(1, 2, 3)
8+
/*%end*/
9+
/*%if statusList != null */
10+
AND status IN /* statusList */('active', 'pending', 'inactive')
11+
/*%end*/
12+
/*%if categoryIds != null && categoryIds.size() > 0 */
13+
AND category_id IN /* categoryIds */(100, 200, 300, 400, 500)
14+
/*%end*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SELECT e.*, d.department_name, p.project_name
2+
FROM employees e
3+
/*%if includeDepartments */
4+
INNER JOIN departments d ON e.department_id = d.id
5+
/*%end*/
6+
/*%if includeProjects */
7+
LEFT JOIN projects p ON e.project_id = p.id
8+
/*%elseif includeActiveProjects */
9+
INNER JOIN projects p ON e.project_id = p.id AND p.status = 'active'
10+
/*%end*/
11+
/*%if includeSkills */
12+
LEFT JOIN employee_skills es ON e.id = es.employee_id
13+
INNER JOIN skills s ON es.skill_id = s.id
14+
/*%end*/
15+
WHERE e.active = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SELECT e.*
2+
, d.department_name
3+
, p.project_name
4+
FROM employees e
5+
/*%if includeDepartments */
6+
INNER JOIN departments d
7+
ON e.department_id = d.id
8+
/*%end*/
9+
/*%if includeProjects */
10+
LEFT JOIN projects p
11+
ON e.project_id = p.id
12+
/*%elseif includeActiveProjects */
13+
INNER JOIN projects p
14+
ON e.project_id = p.id
15+
AND p.status = 'active'
16+
/*%end*/
17+
/*%if includeSkills */
18+
LEFT JOIN employee_skills es
19+
ON e.id = es.employee_id
20+
INNER JOIN skills s
21+
ON es.skill_id = s.id
22+
/*%end*/
23+
WHERE e.active = true
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*%if includeEmployees */
2+
SELECT 'Employee' as type, e.id, e.name, e.email
3+
FROM employees e
4+
WHERE e.is_active = true
5+
/*%if departmentId != null */
6+
AND e.department_id = /* departmentId */1
7+
/*%end*/
8+
/*%end*/
9+
/*%if includeEmployees && includeContractors */
10+
UNION ALL
11+
/*%end*/
12+
/*%if includeContractors */
13+
SELECT 'Contractor' as type, c.id, c.name, c.email
14+
FROM contractors c
15+
WHERE c.contract_end_date >= CURRENT_DATE
16+
/*%if projectId != null */
17+
AND c.project_id = /* projectId */10
18+
/*%end*/
19+
/*%end*/
20+
/*%if (includeEmployees || includeContractors) && includeVendors */
21+
UNION ALL
22+
/*%end*/
23+
/*%if includeVendors */
24+
SELECT 'Vendor' as type, v.id, v.company_name as name, v.contact_email as email
25+
FROM vendors v
26+
WHERE v.status = 'active'
27+
/*%if vendorType != null */
28+
AND v.vendor_type IN /* vendorType */('supplier', 'service')
29+
/*%end*/
30+
/*%end*/
31+
ORDER BY type, name
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*%if includeEmployees */
2+
SELECT 'Employee' AS type
3+
, e.id
4+
, e.name
5+
, e.email
6+
FROM employees e
7+
WHERE e.is_active = true
8+
/*%if departmentId != null */
9+
AND e.department_id = /* departmentId */1
10+
/*%end*/
11+
/*%end*/
12+
/*%if includeEmployees && includeContractors */
13+
UNION ALL
14+
/*%end*/
15+
/*%if includeContractors */
16+
SELECT 'Contractor' AS type
17+
, c.id
18+
, c.name
19+
, c.email
20+
FROM contractors c
21+
WHERE c.contract_end_date >= CURRENT_DATE
22+
/*%if projectId != null */
23+
AND c.project_id = /* projectId */10
24+
/*%end*/
25+
/*%end*/
26+
/*%if (includeEmployees || includeContractors) && includeVendors */
27+
UNION ALL
28+
/*%end*/
29+
/*%if includeVendors */
30+
SELECT 'Vendor' AS type
31+
, v.id
32+
, v.company_name AS name
33+
, v.contact_email AS email
34+
FROM vendors v
35+
WHERE v.status = 'active'
36+
/*%if vendorType != null */
37+
AND v.vendor_type IN /* vendorType */('supplier', 'service')
38+
/*%end*/
39+
/*%end*/
40+
ORDER BY type
41+
, name
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SELECT * FROM products p
2+
WHERE 1 = 1
3+
/*%if productName != null */
4+
AND p.name LIKE /* productName */'%laptop%'
5+
/*%end*/
6+
/*%if minPrice != null */
7+
AND p.price >= /* minPrice */1000
8+
/*%end*/
9+
/*%if maxPrice != null */
10+
AND p.price <= /* maxPrice */5000
11+
/*%end*/
12+
/*%if categoryIds != null && categoryIds.size() > 0 */
13+
AND p.category_id IN /* categoryIds */(1, 2, 3)
14+
/*%end*/
15+
/*%if status != null */
16+
/*%if status == "available" */
17+
AND p.stock_quantity > IN /* quantitys */(0, 1, 2) AND p.is_active = true
18+
/*%elseif status == "outofstock" */
19+
AND p.stock_quantity = IN /* quantitys */(0, 1, 2)
20+
/*%else*/
21+
AND p.is_active = false
22+
/*%end*/
23+
/*%end*/
24+
ORDER BY p.created_at DESC
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SELECT *
2+
FROM products p
3+
WHERE 1 = 1
4+
/*%if productName != null */
5+
AND p.name LIKE /* productName */'%laptop%'
6+
/*%end*/
7+
/*%if minPrice != null */
8+
AND p.price >= /* minPrice */1000
9+
/*%end*/
10+
/*%if maxPrice != null */
11+
AND p.price <= /* maxPrice */5000
12+
/*%end*/
13+
/*%if categoryIds != null && categoryIds.size() > 0 */
14+
AND p.category_id IN /* categoryIds */(1, 2, 3)
15+
/*%end*/
16+
/*%if status != null */
17+
/*%if status == "available" */
18+
AND p.stock_quantity > IN /* quantitys */(0, 1, 2)
19+
AND p.is_active = true
20+
/*%elseif status == "outofstock" */
21+
AND p.stock_quantitys IN /* quantitys */(0, 1, 2)
22+
/*%else*/
23+
AND p.is_active = false
24+
/*%end*/
25+
/*%end*/
26+
ORDER BY p.created_at DESC
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
SELECT e.*, d.department_name, s.salary_amount
2+
FROM employees e
3+
/*%if departmentFilter != null */
4+
INNER JOIN departments d ON e.department_id = d.id
5+
/*%end*/
6+
/*%if includeSalary */
7+
LEFT JOIN salaries s ON e.id = s.employee_id
8+
/*%if salaryFilter != null */
9+
AND s.effective_date = (
10+
SELECT MAX(s2.effective_date)
11+
FROM salaries s2
12+
WHERE s2.employee_id = e.id
13+
/*%if salaryFilter.maxDate != null */
14+
AND s2.effective_date <= /* salaryFilter.maxDate */'2024-01-01'
15+
/*%end*/
16+
)
17+
/*%end*/
18+
/*%end*/
19+
WHERE
20+
/*%if departmentFilter != null */
21+
/*%if departmentFilter.includeActive */
22+
d.is_active = true
23+
/*%if departmentFilter.locations != null && departmentFilter.locations.size() > 0 */
24+
AND d.location IN /* departmentFilter.locations */('Tokyo', 'Osaka')
25+
/*%end*/
26+
/*%else*/
27+
d.is_active = false
28+
/*%end*/
29+
/*%if employeeFilter != null */
30+
AND
31+
/*%if employeeFilter.minExperience != null */
32+
e.experience_years >= /* employeeFilter.minExperience */5
33+
/*%if employeeFilter.maxExperience != null */
34+
AND e.experience_years <= /* employeeFilter.maxExperience */10
35+
/*%end*/
36+
/*%end*/
37+
/*%if employeeFilter.skills != null && employeeFilter.skills.size() > 0 */
38+
AND EXISTS (
39+
SELECT 1 FROM employee_skills es
40+
WHERE es.employee_id = e.id
41+
/*%if employeeFilter.skillMatchType == "all" */
42+
AND es.skill_id IN /* employeeFilter.skills */(1, 2, 3)
43+
GROUP BY es.employee_id
44+
HAVING COUNT(DISTINCT es.skill_id) = /* employeeFilter.skills.size() */3
45+
/*%else*/
46+
AND es.skill_id IN /* employeeFilter.skills */(1, 2, 3)
47+
/*%end*/
48+
)
49+
/*%end*/
50+
/*%end*/
51+
/*%else*/
52+
e.department_id IS NULL
53+
/*%end*/
54+
ORDER BY e.created_at DESC

0 commit comments

Comments
 (0)