mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
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:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user