Files
upstream-sqlc/internal/compiler/catalog_core.go
upstream-mirror 0f4df23290
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 02:17:34 +05:00

63 lines
1.9 KiB
Go

package compiler
import (
"strings"
"github.com/sqlc-dev/sqlc/internal/core"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
)
// coreResultCatalog dumps the core catalog into the legacy catalog shape a
// Result carries, so codegen sees the same table models either way a query
// set was analyzed. Only relations make the trip: codegen reads tables and
// their columns to build models, and none of the types, functions or
// operators the core catalog also holds.
func coreResultCatalog(c *core.Catalog) (*catalog.Catalog, error) {
cat := catalog.New("public")
namespaces, err := c.Namespaces()
if err != nil {
return nil, err
}
for _, ns := range namespaces {
schema := &catalog.Schema{Name: ns.Name}
tables, err := c.TablesInNamespace(ns.OID)
if err != nil {
return nil, err
}
for _, table := range tables {
cols, err := c.ClassCodegenColumns(table.OID)
if err != nil {
return nil, err
}
t := &catalog.Table{Rel: &ast.TableName{Schema: ns.Name, Name: table.Name}}
for _, col := range cols {
// Codegen reads a data type and an array flag, and renders
// a "[]" per dimension, so an array of arrays of integers
// is the type integer with two dimensions.
expr, err := c.TypeExprOf(col.TypeOID)
if err != nil {
return nil, err
}
inner := expr.Innermost()
column := &catalog.Column{
Name: col.Name,
Type: ast.TypeName{Name: strings.TrimSuffix(inner.Name, " unsigned")},
IsNotNull: col.NotNull,
IsArray: expr.IsArray(),
ArrayDims: expr.ArrayDims(),
IsUnsigned: strings.HasSuffix(inner.Name, " unsigned"),
}
if len(inner.Args) > 0 && inner.Args[0].Int != nil {
l := int(*inner.Args[0].Int)
column.Length = &l
}
t.Columns = append(t.Columns, column)
}
schema.Tables = append(schema.Tables, t)
}
cat.Schemas = append(cat.Schemas, schema)
}
return cat, nil
}