Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Conversation

@HidetakaKojo
Copy link
Contributor

What this PR does ?

Add a Scalar SubQuery and Array SubQuery.

example

// user has many items
itemStmt := memeduck.Select("user_item", []string{"item_id", "count"}).
    Where(memeduck.Eq(memeduck.Ident("user_id"), "user-id")).
    AsStruct()
// user has one status
statusStmt := memeduck.Select("user_status", []string{"state"}).
    Where(memeduck.Eq(memeduck.Ident("user_id"), "user-id")).
query.Stmt, _ := memeduck.Select("user", []string{"name"}).
    SubQuery(
        memeduck.ArraySubQuery(itemStmt).As("user_item"),
        memeduck.ScalarSubQuery(statusStmt).As("state"),
    ).
    Where(memeduck.Eq(memeduck.Ident("user_id"), "user-id")).
    SQL()
// Output:
// SELECT
// name,
//	ARRAY(SELECT AS STRUCT item_id, count FROM user_item WHERE user_id = "user-id") AS user_item,
//	(SELECT state FROM user_status WHERE user_id = "user-id") AS state
// FROM user WHERE user_id = "user-id"

Copy link
Owner

@genkami genkami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I think it would be better to implement an abstract interface that accepts both ordinary columns and subqueries like Select("foo", ScalarSubQuery(...).As("bar")), but this requires a lot of breaking API changes. So this design makes sense for now.

Thank you for the PR!

@genkami genkami merged commit 8096172 into genkami:main Jul 30, 2021
@HidetakaKojo HidetakaKojo deleted the use-subquery branch September 18, 2021 02:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants