Skip to content

Commit c9e6fb1

Browse files
authored
Merge pull request #462 from go-jet/columnlist-from
Add support for exporting a ColumnList from a subquery.
2 parents 13ad686 + 85ea908 commit c9e6fb1

File tree

3 files changed

+124
-7
lines changed

3 files changed

+124
-7
lines changed

internal/jet/column_list.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ func (cl ColumnList) As(tableAlias string) ProjectionList {
5151
return ret
5252
}
5353

54+
// From creates a new ColumnList that references the specified subquery.
55+
// This method is typically used to project columns from a subquery into the surrounding query.
56+
func (cl ColumnList) From(subQuery SelectTable) ColumnList {
57+
var ret ColumnList
58+
59+
for _, column := range cl {
60+
ret = append(ret, column.fromImpl(subQuery).(ColumnExpression))
61+
}
62+
63+
return ret
64+
}
65+
5466
func (cl ColumnList) fromImpl(subQuery SelectTable) Projection {
5567
newProjectionList := ProjectionList{}
5668

tests/mysql/select_json_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,7 @@ func TestSelectJson_GroupBy(t *testing.T) {
332332
).AsTable("customers_info")
333333

334334
stmt := SELECT_JSON_ARR(
335-
subQuery.AllColumns().Except( // TODO: remove when ColumnList.From() is implemented
336-
FloatColumn("sum"),
337-
FloatColumn("avg"),
338-
FloatColumn("max"),
339-
FloatColumn("min"),
340-
FloatColumn("count"),
341-
),
335+
Customer.AllColumns.From(subQuery),
342336

343337
SELECT_JSON_OBJ(
344338
FloatColumn("sum").From(subQuery),

tests/postgres/northwind_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,114 @@ func testNorthwindJoinEverythingJson(t require.TestingT) {
204204
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/northwind-all2.json")
205205
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/northwind-all.json")
206206
}
207+
208+
func TestColumnListFrom(t *testing.T) {
209+
210+
toOrderBy := map[string]bool{
211+
"city": true,
212+
"region": true,
213+
"postal_code": true,
214+
}
215+
216+
subQuery := SELECT(
217+
Customers.AllColumns,
218+
).FROM(
219+
Customers,
220+
).AsTable("subQuery")
221+
222+
var orderBy []OrderByClause
223+
224+
for _, column := range Customers.AllColumns.From(subQuery) {
225+
if toOrderBy[column.Name()] {
226+
orderBy = append(orderBy, column.ASC())
227+
}
228+
}
229+
230+
stmt := SELECT(
231+
subQuery.AllColumns(),
232+
).FROM(
233+
subQuery,
234+
).ORDER_BY(
235+
orderBy...,
236+
).LIMIT(3)
237+
238+
testutils.AssertDebugStatementSql(t, stmt, `
239+
SELECT "subQuery"."customers.customer_id" AS "customers.customer_id",
240+
"subQuery"."customers.company_name" AS "customers.company_name",
241+
"subQuery"."customers.contact_name" AS "customers.contact_name",
242+
"subQuery"."customers.contact_title" AS "customers.contact_title",
243+
"subQuery"."customers.address" AS "customers.address",
244+
"subQuery"."customers.city" AS "customers.city",
245+
"subQuery"."customers.region" AS "customers.region",
246+
"subQuery"."customers.postal_code" AS "customers.postal_code",
247+
"subQuery"."customers.country" AS "customers.country",
248+
"subQuery"."customers.phone" AS "customers.phone",
249+
"subQuery"."customers.fax" AS "customers.fax"
250+
FROM (
251+
SELECT customers.customer_id AS "customers.customer_id",
252+
customers.company_name AS "customers.company_name",
253+
customers.contact_name AS "customers.contact_name",
254+
customers.contact_title AS "customers.contact_title",
255+
customers.address AS "customers.address",
256+
customers.city AS "customers.city",
257+
customers.region AS "customers.region",
258+
customers.postal_code AS "customers.postal_code",
259+
customers.country AS "customers.country",
260+
customers.phone AS "customers.phone",
261+
customers.fax AS "customers.fax"
262+
FROM northwind.customers
263+
) AS "subQuery"
264+
ORDER BY "subQuery"."customers.city" ASC, "subQuery"."customers.region" ASC, "subQuery"."customers.postal_code" ASC
265+
LIMIT 3;
266+
`)
267+
268+
var dest []model.Customers
269+
270+
err := stmt.Query(db, &dest)
271+
require.NoError(t, err)
272+
273+
testutils.AssertJSON(t, dest, `
274+
[
275+
{
276+
"CustomerID": "DRACD",
277+
"CompanyName": "Drachenblut Delikatessen",
278+
"ContactName": "Sven Ottlieb",
279+
"ContactTitle": "Order Administrator",
280+
"Address": "Walserweg 21",
281+
"City": "Aachen",
282+
"Region": null,
283+
"PostalCode": "52066",
284+
"Country": "Germany",
285+
"Phone": "0241-039123",
286+
"Fax": "0241-059428"
287+
},
288+
{
289+
"CustomerID": "RATTC",
290+
"CompanyName": "Rattlesnake Canyon Grocery",
291+
"ContactName": "Paula Wilson",
292+
"ContactTitle": "Assistant Sales Representative",
293+
"Address": "2817 Milton Dr.",
294+
"City": "Albuquerque",
295+
"Region": "NM",
296+
"PostalCode": "87110",
297+
"Country": "USA",
298+
"Phone": "(505) 555-5939",
299+
"Fax": "(505) 555-3620"
300+
},
301+
{
302+
"CustomerID": "OLDWO",
303+
"CompanyName": "Old World Delicatessen",
304+
"ContactName": "Rene Phillips",
305+
"ContactTitle": "Sales Representative",
306+
"Address": "2743 Bering St.",
307+
"City": "Anchorage",
308+
"Region": "AK",
309+
"PostalCode": "99508",
310+
"Country": "USA",
311+
"Phone": "(907) 555-7584",
312+
"Fax": "(907) 555-2880"
313+
}
314+
]
315+
`)
316+
317+
}

0 commit comments

Comments
 (0)