Lateral - initial commit.

This commit is contained in:
go-jet 2020-10-16 13:26:53 +02:00
parent 1146afe343
commit 0cba1f6401
4 changed files with 73 additions and 2 deletions

View file

@ -13,8 +13,8 @@ type selectTableImpl struct {
} }
// NewSelectTable func // NewSelectTable func
func NewSelectTable(selectStmt SerializerStatement, alias string) SelectTable { func NewSelectTable(selectStmt SerializerStatement, alias string) selectTableImpl {
selectTable := &selectTableImpl{selectStmt: selectStmt, alias: alias} selectTable := selectTableImpl{selectStmt: selectStmt, alias: alias}
return selectTable return selectTable
} }
@ -38,3 +38,21 @@ func (s selectTableImpl) serialize(statement StatementType, out *SQLBuilder, opt
out.WriteString("AS") out.WriteString("AS")
out.WriteIdentifier(s.alias) out.WriteIdentifier(s.alias)
} }
// --------------------------------------
type lateralImpl struct {
selectTableImpl
}
func NewLateral(selectStmt SerializerStatement, alias string) SelectTable {
return lateralImpl{selectTableImpl: NewSelectTable(selectStmt, alias)}
}
func (s lateralImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
out.WriteString("LATERAL")
s.selectStmt.serialize(statement, out)
out.WriteString("AS")
out.WriteIdentifier(s.alias)
}

13
postgres/lateral.go Normal file
View file

@ -0,0 +1,13 @@
package postgres
import "github.com/go-jet/jet/v2/internal/jet"
func LATERAL(selectStmt SelectStatement, alias string) SelectTable {
subQuery := &selectTableImpl{
SelectTable: jet.NewLateral(selectStmt, alias),
}
subQuery.readableTableInterfaceImpl.parent = subQuery
return subQuery
}

9
postgres/lateral_test.go Normal file
View file

@ -0,0 +1,9 @@
package postgres
import "testing"
func TestLATERAL(t *testing.T) {
assertSerialize(t, LATERAL(SELECT(Int(1)), "lat1"), `LATERAL (
SELECT $1
) AS lat1`)
}

View file

@ -1893,3 +1893,34 @@ WHERE ($1 AND (customer.customer_id = $2)) AND (customer.activebool = $3);
require.Len(t, dest, 1) require.Len(t, dest, 1)
testutils.AssertDeepEqual(t, dest[0], customer0) testutils.AssertDeepEqual(t, dest[0], customer0)
} }
func TestLateral(t *testing.T) {
languages := LATERAL(
SELECT(
Language.AllColumns,
).FROM(
Language,
).WHERE(
Language.Name.NOT_IN(String("spanish")).
AND(Film.LanguageID.EQ(Language.LanguageID)),
),
"films")
stmt := SELECT(
Film.AllColumns,
languages.AllColumns(),
).FROM(
Film.CROSS_JOIN(languages),
).WHERE(
Film.FilmID.EQ(Int(1)),
)
var dest []struct {
model.Film
model.Language
}
err := stmt.Query(db, &dest)
require.NoError(t, err)
}