Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
sapk committed Aug 31, 2023
commit 80a339895b44432eaba87de722806b26c1ffcc84
2 changes: 0 additions & 2 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +45,6 @@ func (c *Compiler) parseCatalog(schemas []string) error {
}

for i := range stmts {
log.Printf("stmts[%d]: %#v", i, stmts[i].Raw.Stmt)
if err := c.catalog.Update(stmts[i], c); err != nil {
merr.Add(filename, contents, stmts[i].Pos(), err)
continue
Expand Down
22 changes: 6 additions & 16 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
case pcast.AlterTableAddColumns:
for _, def := range spec.NewColumns {
name := def.Name.String()
columnDef := convertColumnDef(def)
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_AddColumn,
Def: &columnDef,
Def: convertColumnDef(def),
})
}

Expand All @@ -68,31 +67,25 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {

for _, def := range spec.NewColumns {
name := def.Name.String()
columnDef := convertColumnDef(def)
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_AddColumn,
Def: &columnDef,
Def: convertColumnDef(def),
})

log.Printf("CHANGE COLUMN: %#v\n%#v\n%#v", columnDef, columnDef.TypeName, columnDef.Vals)
}

case pcast.AlterTableModifyColumn:
for _, def := range spec.NewColumns {
name := def.Name.String()
columnDef := convertColumnDef(def)
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_DropColumn,
})
alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_AddColumn,
Def: &columnDef,
Def: convertColumnDef(def),
})

log.Printf("MODIFY COLUMN: %#v\n%#v\n%#v", columnDef, columnDef.TypeName, columnDef.Vals)
}

case pcast.AlterTableAlterColumn:
Expand Down Expand Up @@ -226,10 +219,7 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
create.ReferTable = parseTableName(n.ReferTable)
}
for _, def := range n.Cols {
columnDef := convertColumnDef(def)

log.Printf("CREATE COLUMN: %#v\n%#v\n%#v", columnDef, columnDef.TypeName, columnDef.Vals)
create.Cols = append(create.Cols, &columnDef)
create.Cols = append(create.Cols, convertColumnDef(def))
}
for _, opt := range n.Options {
switch opt.Tp {
Expand All @@ -240,7 +230,7 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
return create
}

func convertColumnDef(def *pcast.ColumnDef) ast.ColumnDef {
func convertColumnDef(def *pcast.ColumnDef) *ast.ColumnDef {
var vals *ast.List
if len(def.Tp.GetElems()) > 0 {
vals = &ast.List{}
Expand Down Expand Up @@ -272,7 +262,7 @@ func convertColumnDef(def *pcast.ColumnDef) ast.ColumnDef {
columnDef.Length = &length
}

return columnDef
return &columnDef
}

func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
Expand Down
2 changes: 1 addition & 1 deletion internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
err = c.createCompositeType(n)

case *ast.CreateEnumStmt:
err = c.createEnum(n)
err = c.createEnum(n, false)

case *ast.CreateExtensionStmt:
err = c.createExtension(n)
Expand Down
5 changes: 1 addition & 4 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package catalog
import (
"errors"
"fmt"
"log"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
Expand Down Expand Up @@ -56,7 +55,6 @@ func (table *Table) addColumn(c *Catalog, cmd *ast.AlterTableCmd) error {
if err != nil {
return err
}
log.Printf("addColumn COLUMN: %#v\n%#v\n%#v", tc, tc.Type, cmd.Def.Vals)

table.Columns = append(table.Columns, tc)
return nil
Expand Down Expand Up @@ -308,7 +306,6 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
if err != nil {
return err
}
log.Printf("createTable COLUMN: %#v\n%#v\n%#v", tc, tc.Type, col.Vals)
tbl.Columns = append(tbl.Columns, tc)
}
}
Expand Down Expand Up @@ -340,7 +337,7 @@ func (c *Catalog) defToColumn(table *ast.TableName, col *ast.ColumnDef) (*Column
Name: fmt.Sprintf("%s_%s", table.Name, col.Colname),
}
s := &ast.CreateEnumStmt{TypeName: &typeName, Vals: col.Vals}
if err := c.createOrSetEnum(s); err != nil {
if err := c.createEnum(s, true); err != nil {
return nil, err
}
tc.Type = typeName
Expand Down
35 changes: 5 additions & 30 deletions internal/sql/catalog/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,7 @@ func sameType(a, b *ast.TypeName) bool {
return true
}

func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
ns := stmt.TypeName.Schema
if ns == "" {
ns = c.DefaultSchema
}
schema, err := c.getSchema(ns)
if err != nil {
return err
}
// 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
tbl := &ast.TableName{
Name: stmt.TypeName.Name,
}
if _, _, err := schema.getTable(tbl); err == nil {
return sqlerr.RelationExists(tbl.Name)
}
if _, _, err := schema.getType(stmt.TypeName); err == nil {
return sqlerr.TypeExists(tbl.Name)
}
schema.Types = append(schema.Types, &Enum{
Name: stmt.TypeName.Name,
Vals: stringSlice(stmt.Vals),
})
return nil
}

func (c *Catalog) createOrSetEnum(stmt *ast.CreateEnumStmt) error {
func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt, overwrite bool) error {
ns := stmt.TypeName.Schema
if ns == "" {
ns = c.DefaultSchema
Expand All @@ -111,6 +82,10 @@ func (c *Catalog) createOrSetEnum(stmt *ast.CreateEnumStmt) error {
return sqlerr.RelationExists(tbl.Name)
}
if typ, _, err := schema.getType(stmt.TypeName); err == nil {
if !overwrite {
return sqlerr.TypeExists(tbl.Name)
}

enum, ok := typ.(*Enum)
if !ok {
return fmt.Errorf("type is not an enum: %s", stmt.TypeName.Name)
Expand Down