Skip to content

Commit 9a31600

Browse files
added test case for limit offset
1 parent d2e1e6d commit 9a31600

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

select_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,65 @@ func ExampleSelectBuilder_join() {
9191
// SELECT u.id, u.name, c.type, p.nickname FROM user u JOIN contract c ON u.id = c.user_id AND c.status IN (?, ?, ?) RIGHT OUTER JOIN person p ON u.id = p.user_id AND p.surname LIKE ? WHERE u.modified_at > u.created_at + ?
9292
// [1 2 5 %Du 86400]
9393
}
94+
95+
func ExampleSelectBuilder_limit_offset() {
96+
97+
sb := NewSelectBuilder()
98+
sb.Select("*")
99+
sb.From("user")
100+
101+
// first test case: limit and offset < 0
102+
// will not add LIMIT and OFFSET to either query for MySQL or PostgresSQL
103+
sb.Limit(-1)
104+
sb.Offset(-1)
105+
106+
pgSQL, _ := sb.BuildWithFlavor(PostgreSQL)
107+
fmt.Println(pgSQL)
108+
109+
mySQL, _ := sb.BuildWithFlavor(MySQL)
110+
fmt.Println(mySQL)
111+
112+
// second test case: limit <= 0 and offset >= 0
113+
// doesn't add offset for MySQL as limit <= 0
114+
// just adds offset to PostgresSQL with no limit as it is not specified
115+
sb.Limit(-1)
116+
sb.Offset(0)
117+
118+
pgSQL, _ = sb.BuildWithFlavor(PostgreSQL)
119+
fmt.Println(pgSQL)
120+
121+
mySQL, _ = sb.BuildWithFlavor(MySQL)
122+
fmt.Println(mySQL)
123+
124+
// third test case: limit >= 0 and offset >= 0
125+
// adds offset and limit for MySQL and PostgresSQL, the query will not return a row (hint: can be used to check if table exists)
126+
sb.Limit(0)
127+
sb.Offset(0)
128+
129+
pgSQL, _ = sb.BuildWithFlavor(PostgreSQL)
130+
fmt.Println(pgSQL)
131+
132+
mySQL, _ = sb.BuildWithFlavor(MySQL)
133+
fmt.Println(mySQL)
134+
135+
// forth test case: limit >= 0 and offset <= 0
136+
// adds limit for MySQL and PostgresSQL and omits offset
137+
sb.Limit(0)
138+
sb.Offset(-1)
139+
140+
pgSQL, _ = sb.BuildWithFlavor(PostgreSQL)
141+
fmt.Println(pgSQL)
142+
143+
mySQL, _ = sb.BuildWithFlavor(MySQL)
144+
fmt.Println(mySQL)
145+
146+
// Output:
147+
// SELECT * FROM user
148+
// SELECT * FROM user
149+
// SELECT * FROM user OFFSET 0
150+
// SELECT * FROM user
151+
// SELECT * FROM user LIMIT 0 OFFSET 0
152+
// SELECT * FROM user LIMIT 0 OFFSET 0
153+
// SELECT * FROM user LIMIT 0
154+
// SELECT * FROM user LIMIT 0
155+
}

0 commit comments

Comments
 (0)