Skip to content

Add initial support for composite types #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions internal/catalog/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,29 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
}
}

case nodes.CompositeTypeStmt:
fqn, err := ParseRange(n.Typevar)
if err != nil {
return err
}
schema, exists := c.Schemas[fqn.Schema]
if !exists {
return wrap(pg.ErrorSchemaDoesNotExist(fqn.Schema), raw.StmtLocation)
}
// Because tables have associated data types, the type name must also
// be distinct from the name of any existing table in the same
// schema.
// https://www.postgresql.org/docs/current/sql-createtype.html
if _, exists := schema.Tables[fqn.Rel]; exists {
return wrap(pg.ErrorRelationAlreadyExists(fqn.Rel), raw.StmtLocation)
}
if _, exists := schema.Types[fqn.Rel]; exists {
return wrap(pg.ErrorRelationAlreadyExists(fqn.Rel), raw.StmtLocation)
}
schema.Types[fqn.Rel] = pg.CompositeType{
Name: fqn.Rel,
}

case nodes.CreateStmt:
fqn, err := ParseRange(n.Relation)
if err != nil {
Expand Down Expand Up @@ -264,10 +287,17 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
if !exists {
return wrap(pg.ErrorSchemaDoesNotExist(fqn.Schema), raw.StmtLocation)
}
if _, exists := schema.Enums[fqn.Rel]; exists {
// Because tables have associated data types, the type name must also
// be distinct from the name of any existing table in the same
// schema.
// https://www.postgresql.org/docs/current/sql-createtype.html
if _, exists := schema.Tables[fqn.Rel]; exists {
return wrap(pg.ErrorRelationAlreadyExists(fqn.Rel), raw.StmtLocation)
}
if _, exists := schema.Types[fqn.Rel]; exists {
return wrap(pg.ErrorTypeAlreadyExists(fqn.Rel), raw.StmtLocation)
}
schema.Enums[fqn.Rel] = pg.Enum{
schema.Types[fqn.Rel] = pg.Enum{
Name: fqn.Rel,
Vals: stringSlice(n.Vals),
}
Expand Down Expand Up @@ -311,8 +341,8 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
}

case nodes.OBJECT_TYPE:
if _, exists := schema.Enums[fqn.Rel]; exists {
delete(schema.Enums, fqn.Rel)
if _, exists := schema.Types[fqn.Rel]; exists {
delete(schema.Types, fqn.Rel)
} else if !n.MissingOk {
return wrap(pg.ErrorTypeDoesNotExist(fqn.Rel), raw.StmtLocation)
}
Expand Down Expand Up @@ -507,16 +537,19 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
if !exists {
return wrap(pg.ErrorSchemaDoesNotExist(fqn.Schema), raw.StmtLocation)
}
enum, exists := schema.Enums[fqn.Rel]
typ, exists := schema.Types[fqn.Rel]
if !exists {
return wrap(pg.ErrorRelationDoesNotExist(fqn.Rel), raw.StmtLocation)
}
if n.Comment != nil {
enum.Comment = *n.Comment
} else {
enum.Comment = ""
switch t := typ.(type) {
case pg.Enum:
if n.Comment != nil {
t.Comment = *n.Comment
} else {
t.Comment = ""
}
schema.Types[fqn.Rel] = t
}
schema.Enums[fqn.Rel] = enum

}

Expand Down
12 changes: 6 additions & 6 deletions internal/catalog/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUpdate(t *testing.T) {
pg.Catalog{
Schemas: map[string]pg.Schema{
"public": {
Enums: map[string]pg.Enum{
Types: map[string]pg.Type{
"status": pg.Enum{
Name: "status",
Vals: []string{"open", "closed"},
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestUpdate(t *testing.T) {
pg.Catalog{
Schemas: map[string]pg.Schema{
"public": {
Enums: map[string]pg.Enum{
Types: map[string]pg.Type{
"status": pg.Enum{
Name: "status",
Vals: []string{"open", "closed"},
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestUpdate(t *testing.T) {
pg.Catalog{
Schemas: map[string]pg.Schema{
"public": {
Enums: map[string]pg.Enum{},
Types: map[string]pg.Type{},
Tables: map[string]pg.Table{
"arenas": pg.Table{
Name: "arenas",
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestUpdate(t *testing.T) {
pg.Catalog{
Schemas: map[string]pg.Schema{
"public": {
Enums: map[string]pg.Enum{},
Types: map[string]pg.Type{},
Tables: map[string]pg.Table{
"venues": pg.Table{
Name: "venues",
Expand Down Expand Up @@ -530,7 +530,7 @@ func TestUpdate(t *testing.T) {
},
},
},
Enums: map[string]pg.Enum{"bat": {Comment: "Enum comment", Name: "bat", Vals: []string{"bat"}}},
Types: map[string]pg.Type{"bat": pg.Enum{Comment: "Enum comment", Name: "bat", Vals: []string{"bat"}}},
Funcs: map[string][]pg.Function{},
},
},
Expand Down Expand Up @@ -561,7 +561,7 @@ func TestUpdate(t *testing.T) {
},
},
},
Enums: map[string]pg.Enum{"bat": {Comment: "Enum comment", Name: "bat", Vals: []string{"bat"}}},
Types: map[string]pg.Type{"bat": pg.Enum{Comment: "Enum comment", Name: "bat", Vals: []string{"bat"}}},
Funcs: map[string][]pg.Function{},
},
},
Expand Down
20 changes: 14 additions & 6 deletions internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (r Result) Enums(settings CombinedSettings) []GoEnum {
if name == "pg_catalog" {
continue
}
for _, enum := range schema.Enums {
for _, enum := range schema.Enums() {
var enumName string
if name == "public" {
enumName = enum.Name
Expand Down Expand Up @@ -711,12 +711,20 @@ func (r Result) goInnerType(col core.Column, settings CombinedSettings) string {
if name == "pg_catalog" {
continue
}
for _, enum := range schema.Enums {
if fqn.Rel == enum.Name && fqn.Schema == name {
if name == "public" {
return StructName(enum.Name, settings)
for _, typ := range schema.Types {
switch t := typ.(type) {
case core.Enum:
if fqn.Rel == t.Name && fqn.Schema == name {
if name == "public" {
return StructName(t.Name, settings)
}
return StructName(name+"_"+t.Name, settings)
}
case core.CompositeType:
if notNull {
return "string"
}
return StructName(name+"_"+enum.Name, settings)
return "sql.NullString"
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/composite_type/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/composite_type/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions internal/endtoend/testdata/composite_type/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/composite_type/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE SCHEMA foo;

CREATE TYPE point_type AS (
x integer,
y integer
);

CREATE TYPE foo.point_type AS (
x integer,
y integer
);

CREATE TABLE foo.paths (
point_one point_type,
point_two foo.point_type
);

-- name: ListPaths :many
SELECT * FROM foo.paths;
9 changes: 9 additions & 0 deletions internal/endtoend/testdata/composite_type/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1",
"packages": [{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}]
}
28 changes: 26 additions & 2 deletions internal/pg/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewCatalog() Catalog {
func NewSchema() Schema {
return Schema{
Tables: map[string]Table{},
Enums: map[string]Enum{},
Types: map[string]Type{},
Funcs: map[string][]Function{},
}
}
Expand Down Expand Up @@ -93,11 +93,21 @@ func (c Catalog) LookupFunctionN(fqn FQN, argn int) (Function, error) {
type Schema struct {
Name string
Tables map[string]Table
Enums map[string]Enum
Types map[string]Type
Funcs map[string][]Function
Comment string
}

func (s Schema) Enums() []Enum {
var enums []Enum
for _, typ := range s.Types {
if enum, ok := typ.(Enum); ok {
enums = append(enums, enum)
}
}
return enums
}

type Table struct {
ID FQN
Name string
Expand All @@ -117,12 +127,26 @@ type Column struct {
Table FQN
}

type Type interface {
isType()
}

type Enum struct {
Name string
Vals []string
Comment string
}

func (e Enum) isType() {
}

type CompositeType struct {
Name string
}

func (e CompositeType) isType() {
}

type Function struct {
Name string
ArgN int
Expand Down