@@ -2,6 +2,7 @@ package schema
22
33import (
44 "fmt"
5+ "strings"
56
67 "github.com/sqlc-dev/sqlc/internal/core"
78 "github.com/sqlc-dev/sqlc/internal/core/analyzer"
@@ -27,6 +28,12 @@ func Apply(cat *core.Catalog, n ast.Node) error {
2728 return applyDropTable (cat , v )
2829 case * ast.CreateEnumStmt :
2930 return applyCreateEnum (cat , v )
31+ case * ast.CreateDomainStmt :
32+ return applyCreateDomain (cat , v )
33+ case * ast.CompositeTypeStmt :
34+ return applyCompositeType (cat , v )
35+ case * ast.CreateRangeStmt :
36+ return applyCreateRange (cat , v )
3037 case * ast.CreateExtensionStmt :
3138 if v .Extname == nil {
3239 return nil
@@ -166,7 +173,7 @@ func applyCreateTable(cat *core.Catalog, stmt *ast.CreateTableStmt) error {
166173 Name : col .Colname ,
167174 TypeOID : typeOID ,
168175 Num : i + 1 ,
169- NotNull : col .IsNotNull || col .PrimaryKey ,
176+ NotNull : col .IsNotNull || col .PrimaryKey || typeNotNull ( cat , typeOID ) ,
170177 IsPrimaryKey : col .PrimaryKey ,
171178 DeclType : declType (col .TypeName ),
172179 Hidden : col .IsHidden ,
@@ -238,7 +245,7 @@ func applyAlterTable(cat *core.Catalog, stmt *ast.AlterTableStmt) error {
238245 Name : cmd .Def .Colname ,
239246 TypeOID : typeOID ,
240247 Num : num ,
241- NotNull : cmd .Def .IsNotNull || cmd .Def .PrimaryKey ,
248+ NotNull : cmd .Def .IsNotNull || cmd .Def .PrimaryKey || typeNotNull ( cat , typeOID ) ,
242249 IsPrimaryKey : cmd .Def .PrimaryKey ,
243250 DeclType : declType (cmd .Def .TypeName ),
244251 }); err != nil {
@@ -350,21 +357,134 @@ func listItems(l *ast.List) []ast.Node {
350357 return l .Items
351358}
352359
360+ // applyCreateEnum records an enum as a type whose arguments are its labels,
361+ // in order, the way pg_enum keeps them.
353362func applyCreateEnum (cat * core.Catalog , stmt * ast.CreateEnumStmt ) error {
354363 if stmt .TypeName == nil {
355364 return fmt .Errorf ("create type with nil name" )
356365 }
357- t := core . TypeExprOfTypeName (stmt .TypeName )
358- if t == nil {
366+ name := declaredTypeName (stmt .TypeName )
367+ if name == "" {
359368 return fmt .Errorf ("create type with empty name" )
360369 }
361- if _ , err := cat .TypeOID (t .Name ); err == nil {
370+ if _ , err := cat .TypeOID (name ); err == nil {
371+ return nil
372+ }
373+ var labels []core.TypeArg
374+ for _ , label := range listStrings (stmt .Vals ) {
375+ l := label
376+ labels = append (labels , core.TypeArg {String : & l })
377+ }
378+ _ , err := cat .CreateTypeWithArgs (core.TypeSpec {Name : name , Typtype : "e" , Category : "E" }, labels )
379+ return err
380+ }
381+
382+ // applyCreateDomain records a domain: a type of its own that stands on its
383+ // base, which is what it resolves through, and that may forbid NULL.
384+ func applyCreateDomain (cat * core.Catalog , stmt * ast.CreateDomainStmt ) error {
385+ name := strings .ToLower (strings .Join (listStrings (stmt .Domainname ), "." ))
386+ if name == "" || stmt .TypeName == nil {
387+ return fmt .Errorf ("create domain: missing name or type" )
388+ }
389+ if _ , err := cat .TypeOID (name ); err == nil {
362390 return nil
363391 }
364- _ , err := cat .CreateUserType (t .Name , "E" )
392+ baseOID , err := cat .ResolveType (stmt .TypeName )
393+ if err != nil {
394+ return fmt .Errorf ("domain %q: %w" , name , err )
395+ }
396+ base , err := cat .LookupType (baseOID )
397+ if err != nil {
398+ return err
399+ }
400+ notNull := false
401+ for _ , item := range listItems (stmt .Constraints ) {
402+ if con , ok := item .(* ast.Constraint ); ok && con .Contype == ast .ConstrTypeNotNull {
403+ notNull = true
404+ }
405+ }
406+ _ , err = cat .CreateTypeWithArgs (core.TypeSpec {
407+ Name : name ,
408+ Typtype : "d" ,
409+ Category : base .Category ,
410+ BaseOID : baseOID ,
411+ NotNull : notNull ,
412+ }, nil )
365413 return err
366414}
367415
416+ // applyCompositeType records a composite type as a type whose arguments are
417+ // its fields, labelled by name.
418+ func applyCompositeType (cat * core.Catalog , stmt * ast.CompositeTypeStmt ) error {
419+ if stmt .TypeName == nil {
420+ return fmt .Errorf ("create type with nil name" )
421+ }
422+ name := declaredTypeName (stmt .TypeName )
423+ if name == "" {
424+ return fmt .Errorf ("create type with empty name" )
425+ }
426+ if _ , err := cat .TypeOID (name ); err == nil {
427+ return nil
428+ }
429+ var fields []core.TypeArg
430+ for _ , item := range listItems (stmt .Coldeflist ) {
431+ col , ok := item .(* ast.ColumnDef )
432+ if ! ok || col .TypeName == nil {
433+ continue
434+ }
435+ t := core .ColumnTypeExpr (col )
436+ if t == nil {
437+ continue
438+ }
439+ fields = append (fields , core.TypeArg {Label : col .Colname , Type : t })
440+ }
441+ _ , err := cat .CreateTypeWithArgs (core.TypeSpec {Name : name , Typtype : "c" , Category : "C" }, fields )
442+ return err
443+ }
444+
445+ // applyCreateRange records a range type over its subtype, which is what a
446+ // bound of it has.
447+ func applyCreateRange (cat * core.Catalog , stmt * ast.CreateRangeStmt ) error {
448+ name := strings .ToLower (strings .Join (listStrings (stmt .TypeName ), "." ))
449+ if name == "" {
450+ return fmt .Errorf ("create type with empty name" )
451+ }
452+ if _ , err := cat .TypeOID (name ); err == nil {
453+ return nil
454+ }
455+ spec := core.TypeSpec {Name : name , Typtype : "r" , Category : "R" }
456+ for _ , item := range listItems (stmt .Params ) {
457+ def , ok := item .(* ast.DefElem )
458+ if ! ok || def .Defname == nil || * def .Defname != "subtype" {
459+ continue
460+ }
461+ tn , ok := def .Arg .(* ast.TypeName )
462+ if ! ok {
463+ continue
464+ }
465+ oid , err := cat .ResolveType (tn )
466+ if err != nil {
467+ return fmt .Errorf ("range %q: %w" , name , err )
468+ }
469+ spec .ElementOID = oid
470+ }
471+ _ , err := cat .CreateTypeWithArgs (spec , nil )
472+ return err
473+ }
474+
475+ // declaredTypeName is the name a CREATE TYPE gives, qualified by its schema
476+ // when it names one.
477+ func declaredTypeName (tn * ast.TypeName ) string {
478+ t := core .TypeExprOfTypeName (tn )
479+ if t == nil {
480+ return ""
481+ }
482+ if tn .Schema != "" && ! strings .Contains (t .Name , "." ) {
483+ return strings .ToLower (tn .Schema ) + "." + t .Name
484+ }
485+ return t .Name
486+ }
487+
368488func applyCreateFunction (cat * core.Catalog , stmt * ast.CreateFunctionStmt ) error {
369489 // A procedure returns nothing, so there is no result for a query to
370490 // select and nothing worth recording.
@@ -421,6 +541,13 @@ func resolveOrCreateNamespace(cat *core.Catalog, schema string) (int64, error) {
421541 return cat .CreateNamespace (name )
422542}
423543
544+ // typeNotNull reports whether a column of the type can never be NULL
545+ // because the type itself says so, as a domain declared NOT NULL does.
546+ func typeNotNull (cat * core.Catalog , typeOID int64 ) bool {
547+ info , err := cat .LookupType (typeOID )
548+ return err == nil && info .NotNull
549+ }
550+
424551// columnTypeOID interns a column's type and returns its row.
425552func columnTypeOID (cat * core.Catalog , col * ast.ColumnDef ) (int64 , error ) {
426553 t := core .ColumnTypeExpr (col )
0 commit comments