Skip to content

Commit

Permalink
Lateral - initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
go-jet committed Apr 30, 2021
1 parent 1146afe commit 0cba1f6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/jet/select_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type selectTableImpl struct {
}

// NewSelectTable func
func NewSelectTable(selectStmt SerializerStatement, alias string) SelectTable {
selectTable := &selectTableImpl{selectStmt: selectStmt, alias: alias}
func NewSelectTable(selectStmt SerializerStatement, alias string) selectTableImpl {
selectTable := selectTableImpl{selectStmt: selectStmt, alias: alias}
return selectTable
}

Expand All @@ -38,3 +38,21 @@ func (s selectTableImpl) serialize(statement StatementType, out *SQLBuilder, opt
out.WriteString("AS")
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 changes: 13 additions & 0 deletions postgres/lateral.go
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions postgres/lateral_test.go
Original file line number Diff line number Diff line change
@@ -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`)
}
31 changes: 31 additions & 0 deletions tests/postgres/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,3 +1893,34 @@ WHERE ($1 AND (customer.customer_id = $2)) AND (customer.activebool = $3);
require.Len(t, dest, 1)
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)
}

0 comments on commit 0cba1f6

Please sign in to comment.