Skip to content

Commit c11b647

Browse files
committed
Added support for embeding custom types
1 parent e6fff7a commit c11b647

File tree

7 files changed

+76
-72
lines changed

7 files changed

+76
-72
lines changed

internal/compiler/output_columns.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,13 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
545545
Name: fn.ReturnType.Name,
546546
})
547547

548-
// Failed to find table, check for type with name.
549548
if err != nil {
550-
table, err = qc.GetCompositeType(fn.ReturnType)
549+
// No table, check to see if there a type
550+
tyTable, tyErr := qc.GetTableForType(fn.ReturnType)
551+
if tyErr == nil {
552+
table = tyTable
553+
err = nil
554+
}
551555
}
552556
}
553557
if table == nil || err != nil {

internal/compiler/query_catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (qc QueryCatalog) GetFunc(rel *ast.FuncName) (*Function, error) {
108108
}, nil
109109
}
110110

111-
func (qc QueryCatalog) GetCompositeType(rel *ast.TypeName) (*Table, error) {
111+
func (qc QueryCatalog) GetTableForType(rel *ast.TypeName) (*Table, error) {
112112
ty, err := qc.catalog.GetCompostiteType(rel)
113113
if err != nil {
114114
return &Table{}, err

internal/compiler/resolve.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
9494
continue
9595
}
9696

97+
tyName := &ast.TypeName{
98+
Catalog: embed.Table.Catalog,
99+
Schema: embed.Table.Schema,
100+
Name: embed.Table.Name,
101+
}
102+
ty, err := c.GetCompostiteType(tyName)
103+
if err == nil {
104+
embed.Table = &ast.TableName{Name: ty.Name}
105+
continue
106+
}
107+
97108
return nil, fmt.Errorf("unable to resolve table with %q: %w", embed.Orig(), err)
98109
}
99110

internal/endtoend/testdata/select_fn/postgresql/pgx/v5/go/models.go

Lines changed: 5 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_fn/postgresql/pgx/v5/go/query.sql.go

Lines changed: 44 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
-- name: SelectBars :many
2-
SELECT * FROM bar_fn();
3-
41
-- name: SelectFoos :many
52
SELECT * FROM foo_fn();
3+
4+
-- name: SelectFooEmbed :many
5+
SELECT sqlc.embed(foo), 1 AS one FROM foo_fn() AS foo;
6+
7+
-- name: SelectSingleColumn :many
8+
SELECT baz FROM foo_fn();
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
CREATE TYPE foo AS ENUM ('bar', 'baz');
1+
CREATE TYPE foo AS (foo TEXT, baz INTEGER);
22

3-
CREATE TYPE bar AS (foo foo, baz INTEGER);
4-
5-
CREATE FUNCTION bar_fn()
6-
RETURNS SETOF bar LANGUAGE SQL STABLE AS $$
7-
SELECT * FROM VALUES ('bar', 1);
8-
$$;
3+
CREATE TABLE bar (foo TEXT, baz INTEGER);
94

105
CREATE FUNCTION foo_fn()
116
RETURNS SETOF foo LANGUAGE SQL STABLE AS $$
12-
SELECT * FROM VALUES ('bar');
7+
SELECT foo, baz FROM bar;
138
$$;

0 commit comments

Comments
 (0)