entc/integration: add examples for sub-queries + minor fixed for selector builder (#2509)

* dialect/sql: move FROM to inside switch

* dialect/sql: adds simple example of subquery usage

* dialect/sql: adds a more complex example with join

* Apply suggestions from code review

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* dialect/sql: fix missing type and change modify to where

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
Pedro Henrique
2022-05-05 16:46:13 -03:00
committed by GitHub
parent 0c75ffc007
commit 4d1c0af20d
3 changed files with 36 additions and 1 deletions

View File

@@ -2657,12 +2657,13 @@ func (s *Selector) Query() (string, []interface{}) {
} else {
b.WriteString("*")
}
b.WriteString(" FROM ")
switch t := s.from.(type) {
case *SelectTable:
b.WriteString(" FROM ")
t.SetDialect(s.dialect)
b.WriteString(t.ref())
case *Selector:
b.WriteString(" FROM ")
t.SetDialect(s.dialect)
b.Nested(func(b *Builder) {
b.Join(t)
@@ -2670,6 +2671,7 @@ func (s *Selector) Query() (string, []interface{}) {
b.WriteString(" AS ")
b.Ident(t.as)
case *WithBuilder:
b.WriteString(" FROM ")
t.SetDialect(s.dialect)
b.Ident(t.Name())
}

View File

@@ -1288,6 +1288,14 @@ func TestBuilder(t *testing.T) {
wantQuery: "WITH `groups` AS (SELECT * FROM `groups` WHERE `name` = ?) SELECT `age` FROM `groups`",
wantArgs: []interface{}{"bar"},
},
{
input: SelectExpr(Raw("1")),
wantQuery: "SELECT 1",
},
{
input: Select("*").From(SelectExpr(Raw("1")).As("s")),
wantQuery: "SELECT * FROM (SELECT 1) AS `s`",
},
{
input: func() Querier {
builder := Dialect(dialect.Postgres)