Skip to content

Commit 6012e39

Browse files
committed
Add test cases for CTE clause support.
1 parent 192d386 commit 6012e39

21 files changed

+347
-102
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ class SqlFormatterTest : BasePlatformTestCase() {
9898
formatSqlFile("InsertConflictUpdateWithOutTable.sql", "InsertConflictUpdateWithOutTable$formatDataPrefix.sql")
9999
}
100100

101+
fun testWithSelect() {
102+
formatSqlFile("WithSelect.sql", "WithSelect$formatDataPrefix.sql")
103+
}
104+
105+
fun testWithMultiQuery() {
106+
formatSqlFile("WithMultiQuery.sql", "WithMultiQuery$formatDataPrefix.sql")
107+
}
108+
109+
fun testWithRecursive() {
110+
formatSqlFile("WithRecursive.sql", "WithRecursive$formatDataPrefix.sql")
111+
}
112+
113+
fun testWithUnionAll() {
114+
formatSqlFile("WithUnionAll.sql", "WithUnionAll$formatDataPrefix.sql")
115+
}
116+
117+
fun testWithInsert() {
118+
formatSqlFile("WithInsert.sql", "WithInsert$formatDataPrefix.sql")
119+
}
120+
121+
fun testWithUpdate() {
122+
formatSqlFile("WithUpdate.sql", "WithUpdate$formatDataPrefix.sql")
123+
}
124+
125+
fun testWithDelete() {
126+
formatSqlFile("WithDelete.sql", "WithDelete$formatDataPrefix.sql")
127+
}
128+
101129
private fun formatSqlFile(
102130
beforeFile: String,
103131
afterFile: String,
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
INSERT INTO employee(id, name)
2-
VALUES ( /* employees.id */0
3-
, /* employees.name */'name')
1+
insert into /*# tableName */
2+
(x1 , x2
3+
/*%for entity : entities */
4+
, /*# entity.itemIdentifier */
5+
/*%end*/
6+
, x3 , x4) values ( /* reportId */1
7+
, /* reportId */1
8+
/*%for entity : entities */
9+
, /* entity.value */'abc'
10+
/*%end*/
11+
, /* @userId() */1
12+
, x5
13+
, /* @userId() */1
14+
, x6 , 1
15+
, /* @maxDateTime() */'9999-12-31')
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
INSERT INTO employee
2-
(id
3-
, name)
4-
VALUES ( /* employees.id */0
5-
, /* employees.name */'name')
1+
INSERT INTO /*# tableName */
2+
(x1
3+
, x2
4+
/*%for entity : entities */
5+
, /*# entity.itemIdentifier */
6+
/*%end*/
7+
, x3
8+
, x4)
9+
VALUES ( /* reportId */1
10+
, /* reportId */1
11+
/*%for entity : entities */
12+
, /* entity.value */'abc'
13+
/*%end*/
14+
, /* @userId() */1
15+
, x5
16+
, /* @userId() */1
17+
, x6
18+
, 1
19+
, /* @maxDateTime() */'9999-12-31')
Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,54 @@
11
/** TopBlock */
2-
SELECT COUNT( DISTINCT (x)),o.*
3-
, ISNULL(nbor.nearest
4-
, 999) AS nearest -- column Line comment
2+
SELECT COUNT(DISTINCT x) AS count_x
3+
, o.*
4+
, COALESCE(nbor.nearest, 999)
5+
AS nearest
56
/** From */
67
FROM ( -- SubGroupLine
7-
SELECT p.objid
8-
, p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 AS rpm
9-
, p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) AS gi
10-
, ISNULL(s.plate
11-
, 0) AS plate
12-
, ISNULL(s.mjd
13-
, 0) AS mjd
14-
, ISNULL(s.fiberid
15-
, 0) AS fiberid
16-
8+
SELECT p.objid
9+
, p.psfmag_g - p.extinction_g + 5 * LOG(10, u.propermotion / 100.0) + 5 AS rpm
10+
, p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) AS gi
11+
, COALESCE(s.plate, 0) AS plate
12+
, COALESCE(s.mjd
13+
, 0) AS mjd
14+
, COALESCE(s.fiberid, 0) AS fiberid
1715
FROM phototag p
18-
-- Line1
16+
-- Line1
1917
JOIN usno u
20-
-- Line2
18+
-- Line2
2119
ON p.objid = u.objid
2220
/** Join */
2321
LEFT OUTER JOIN specobj s
2422
/** ON */
25-
ON p.objid = s.bestobjid AND p.plate = s.plate
23+
ON p.objid = s.bestobjid
24+
AND p.plate = s.plate
2625
/** Where */
27-
WHERE p.TYPE = DBO.FPHOTOTYPE('Star')
28-
-- Line3
29-
OR (p.flags & DBO.FPHOTOFLAGS('EDGE') = 0
30-
31-
AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
32-
/*%if status == 2 */
33-
-- Line4
34-
AND u.propermotion > 2.
26+
WHERE p.TYPE = 'Star'
27+
-- Line3
28+
OR ( p.flags & FPHOTOFLAGS('EDGE') = 0 AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
29+
/*%if status == 2 */
30+
-- Line4
31+
and u.propermotion > 2.0
3532
/** And Group */
36-
AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i))
37-
38-
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0. AND p.extinction_g - u.propermotion > 0)
39-
40-
/*%end*/) AS o
41-
42-
LEFT OUTER JOIN ( SELECT n.objid
43-
, MIN(n.distance) AS nearest
44-
45-
FROM neighbors n
46-
JOIN phototag x
47-
ON n.neighborobjid = x.objid
48-
AND n.neighbormode = DBO.FPHOTOMODE('Primary')
49-
OR n.MODE = /*# "active" */'mode'
50-
AND n.status =2
51-
AND n.flag = true
52-
WHERE n.TYPE = DBO.FPHOTOTYPE('Star')
53-
54-
AND n.MODE = DBO.FPHOTOMODE('Primary')
55-
OR n.neighbormode = DBO.FPHOTOMODE('Primary')
56-
AND (x.TYPE = DBO.FPHOTOTYPE('Star')
57-
58-
AND x.TYPE = DBO.FPHOTOTYPE('Galaxy'))
59-
OR x.modelmag_g BETWEEN 10 AND 21
60-
61-
GROUP BY n.objid ) AS nbor
62-
ON o.objid = nbor.objid
63-
WHERE p.params IN /* params */(1
64-
,2
65-
,3)
33+
and (p.psfmag_g - p.extinction_g + 5 * LOG(10, u.propermotion / 100.0) + 5 > 16.136 + 2.727
34+
or (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i)) < 0.0
35+
and (p.extinction_g - u.propermotion) > 0)
36+
/*%end*/ ) as o
37+
left outer join ( SELECT n.objid
38+
, MIN(n.distance) AS nearest
39+
40+
FROM neighbors n
41+
JOIN phototag x
42+
ON n.neighborobjid = x.objid
43+
AND (n.neighbormode = 'Primary'
44+
OR n.mode = 'mode')
45+
AND n.status = 2
46+
AND n.flag = TRUE
47+
48+
WHERE n.TYPE = 'Star'
49+
OR (n.MODE = 'Primary'
50+
OR n.neighbormode = 'Primary')
51+
AND ( (x.TYPE = 'Star'
52+
AND x.TYPE = 'Galaxy') OR x.modelmag_g BETWEEN 10 AND 21)
53+
GROUP BY n.objid ) AS nbor ON o.objid = nbor.objid
54+
WHERE o.objid IN /* params */(1, 2, 3)
Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
/** TopBlock */
2-
SELECT COUNT(DISTINCT (x))
2+
SELECT COUNT(DISTINCT x) AS count_x
33
, o.*
4-
, ISNULL(nbor.nearest
5-
, 999) AS nearest -- column Line comment
4+
, COALESCE(nbor.nearest
5+
, 999) AS nearest
66
/** From */
77
FROM ( -- SubGroupLine
8-
SELECT p.objid
9-
, p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 AS rpm
10-
, p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) AS gi
11-
, ISNULL(s.plate
12-
, 0) AS plate
13-
, ISNULL(s.mjd
14-
, 0) AS mjd
15-
, ISNULL(s.fiberid
16-
, 0) AS fiberid
8+
SELECT p.objid
9+
, p.psfmag_g - p.extinction_g + 5 * LOG(10
10+
, u.propermotion / 100.0) + 5 AS rpm
11+
, p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) AS gi
12+
, COALESCE(s.plate
13+
, 0) AS plate
14+
, COALESCE(s.mjd
15+
, 0) AS mjd
16+
, COALESCE(s.fiberid
17+
, 0) AS fiberid
1718
FROM phototag p
1819
-- Line1
1920
JOIN usno u
@@ -25,33 +26,34 @@ SELECT COUNT(DISTINCT (x))
2526
ON p.objid = s.bestobjid
2627
AND p.plate = s.plate
2728
/** Where */
28-
WHERE p.TYPE = DBO.FPHOTOTYPE('Star')
29+
WHERE p.TYPE = 'Star'
2930
-- Line3
30-
OR (p.flags & DBO.FPHOTOFLAGS('EDGE') = 0
31+
OR (p.flags & FPHOTOFLAGS('EDGE') = 0
3132
AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
32-
/*%if status == 2 */
33-
-- Line4
34-
AND u.propermotion > 2.
35-
/** And Group */
36-
AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i))
37-
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.
38-
AND p.extinction_g - u.propermotion > 0)
39-
/*%end*/ ) AS o
40-
LEFT OUTER JOIN ( SELECT n.objid
41-
, MIN(n.distance) AS nearest
42-
FROM neighbors n
43-
JOIN phototag x
44-
ON n.neighborobjid = x.objid
45-
AND n.neighbormode = DBO.FPHOTOMODE('Primary')
46-
OR n.MODE = /*# "active" */'mode'
47-
AND n.status = 2
48-
AND n.flag = true
49-
WHERE n.TYPE = DBO.FPHOTOTYPE('Star')
50-
AND n.MODE = DBO.FPHOTOMODE('Primary')
51-
OR n.neighbormode = DBO.FPHOTOMODE('Primary')
52-
AND (x.TYPE = DBO.FPHOTOTYPE('Star')
53-
AND x.TYPE = DBO.FPHOTOTYPE('Galaxy'))
54-
OR x.modelmag_g BETWEEN 10 AND 21
55-
GROUP BY n.objid ) AS nbor
56-
ON o.objid = nbor.objid
57-
WHERE p.params IN /* params */(1, 2, 3)
33+
/*%if status == 2 */
34+
-- Line4
35+
AND u.propermotion > 2.0
36+
/** And Group */
37+
AND (p.psfmag_g - p.extinction_g + 5 * LOG(10
38+
, u.propermotion / 100.0) + 5 > 16.136 + 2.727
39+
OR (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i)) < 0.0
40+
AND (p.extinction_g - u.propermotion) > 0)
41+
/*%end*/ ) AS o
42+
LEFT OUTER JOIN ( SELECT n.objid
43+
, MIN(n.distance) AS nearest
44+
FROM neighbors n
45+
JOIN phototag x
46+
ON n.neighborobjid = x.objid
47+
AND (n.neighbormode = 'Primary'
48+
OR n.mode = 'mode')
49+
AND n.status = 2
50+
AND n.flag = TRUE
51+
WHERE n.TYPE = 'Star'
52+
OR (n.MODE = 'Primary'
53+
OR n.neighbormode = 'Primary')
54+
AND ((x.TYPE = 'Star'
55+
AND x.TYPE = 'Galaxy')
56+
OR x.modelmag_g BETWEEN 10 AND 21)
57+
GROUP BY n.objid ) AS nbor
58+
ON o.objid = nbor.objid
59+
WHERE o.objid IN /* params */(1, 2, 3)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
WITH deleted_posts AS (
2+
delete FROM posts
3+
where deleted = TRUE AND deleted_at < CURRENT_DATE - INTERVAL '30 days'
4+
returning id
5+
)
6+
SELECT * FROM deleted_posts
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
WITH deleted_posts AS (
2+
DELETE FROM posts
3+
WHERE deleted = TRUE
4+
AND deleted_at < CURRENT_DATE - INTERVAL '30 days'
5+
RETURNING id
6+
)
7+
SELECT *
8+
FROM deleted_posts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
WITH new_user AS ( INSERT INTO users (name, email)
2+
VALUES ('Alice Example', '[email protected]') RETURNING id)
3+
INSERT INTO user_profiles (user_id, bio)
4+
SELECT id , 'This is Alice' , profile
5+
FROM new_user
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
WITH new_user AS (
2+
INSERT INTO users
3+
(name
4+
, email)
5+
VALUES ('Alice Example'
6+
7+
RETURNING id
8+
)
9+
INSERT INTO user_profiles
10+
(user_id
11+
, bio)
12+
SELECT id
13+
, 'This is Alice'
14+
, profile
15+
FROM new_user
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with users AS (
2+
SELECT user_id, name FROM users WHERE name = 'premium'
3+
),
4+
active_users AS (
5+
SELECT user_id, name FROM users WHERE last_login > CURRENT_DATE - INTERVAL '30 days'
6+
)
7+
SELECT * FROM premium_users
8+
INTERSECT
9+
SELECT * FROM active_users
10+
EXCEPT
11+
SELECT user_id, name FROM users WHERE suspended = TRUE
12+
ORDER BY name ASC
13+
LIMIT 100 OFFSET 10

0 commit comments

Comments
 (0)