MCPcopy Create free account
hub / github.com/bytebase/bytebase / TestQuery_ConditionalComposition

Function TestQuery_ConditionalComposition

backend/common/qb/qb_test.go:196–245  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

194}
195
196func TestQuery_ConditionalComposition(t *testing.T) {
197 getName := true
198 getID := false
199 filterAdult := true
200 ageCheck := true
201 filterChild := false
202
203 // Build SELECT clause
204 sel := Q().Space("SELECT")
205 if getName {
206 sel.Join(", ", "name")
207 }
208 if getID {
209 sel.Join(", ", "id")
210 }
211 if !getName && !getID {
212 sel.Join(", ", "*")
213 }
214
215 from := Q().Space("FROM my_table")
216
217 // Build WHERE clause
218 where := Q()
219 hasConditions := false
220
221 if filterAdult {
222 adultCond := Q().Space("name = ?", "adult")
223 if ageCheck {
224 adultCond.And("age > ?", 20)
225 }
226 where.Space("WHERE (?)", adultCond)
227 hasConditions = true
228 }
229
230 if filterChild {
231 if hasConditions {
232 where.Or("(name = ? AND age < ?)", "youth", 21)
233 } else {
234 where.Space("WHERE (name = ? AND age < ?)", "youth", 21)
235 }
236 }
237
238 // Compose final query
239 q := Q().Space("? ? ?", sel, from, where).Space("LIMIT ?", 10)
240
241 sql, args, err := q.ToSQL()
242 require.NoError(t, err)
243 require.Equal(t, "SELECT, name FROM my_table WHERE (name = $1 AND age > $2) LIMIT $3", sql)
244 require.Equal(t, []any{"adult", 20, 10}, args)
245}
246
247func TestQuery_NestedComposition(t *testing.T) {
248 // Build a subquery

Callers

nothing calls this directly

Calls 7

QFunction · 0.85
SpaceMethod · 0.80
JoinMethod · 0.80
AndMethod · 0.80
OrMethod · 0.80
ToSQLMethod · 0.80
EqualMethod · 0.65

Tested by

no test coverage detected