Skip to content
Open
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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/sqlc-dev/sqlc

go 1.22.2
33 changes: 0 additions & 33 deletions internal/codegen/golang/go_type.go

This file was deleted.

22 changes: 0 additions & 22 deletions internal/codegen/golang/struct.go

This file was deleted.

80 changes: 80 additions & 0 deletions internal/codegen/golang/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package golang

// SQLPackage identifies the database driver/package target.
type SQLPackage string

const (
SQLPackageStandard SQLPackage = "database/sql"
SQLPackagePGXV4 SQLPackage = "pgx/v4"
SQLPackagePGXV5 SQLPackage = "pgx/v5"
)

// Settings represents golang generator configuration.
type Settings struct {
SQLPackage SQLPackage
GoTypePointer bool
}

// Column represents a database column for code generation.
type Column struct {
Name string
Type string
NotNull bool
IsArray bool
}

// Generator manages Go code generation.
type Generator struct{}

func (g *Generator) dbType(col *Column) string {
switch col.Type {
case "uuid", "uuid[]":
return "uuid.UUID"
case "text", "varchar":
return "string"
case "integer", "int":
return "int32"
case "bigint":
return "int64"
case "boolean", "bool":
return "bool"
default:
return "interface{}"
}
}

// GoType maps a database column and its nullability/array status to the appropriate Go type representation.
func (g *Generator) GoType(col *Column, settings Settings) string {
base := g.dbType(col)
if col.IsArray {
sliceTyp := "[]" + base
if !col.NotNull {
// For standard database/sql and drivers needing pointer indirection on nullable slices
if settings.SQLPackage == SQLPackageStandard || settings.GoTypePointer {
return "*" + sliceTyp
}
return sliceTyp
}
return sliceTyp
}

if !col.NotNull {
if settings.GoTypePointer {
return "*" + base
}
switch base {
case "string":
return "sql.NullString"
case "int32":
return "sql.NullInt32"
case "int64":
return "sql.NullInt64"
case "bool":
return "sql.NullBool"
default:
return "*" + base
}
}

return base
}
68 changes: 68 additions & 0 deletions internal/codegen/golang/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package golang

import "testing"

func TestGoType_NullableUUIDArray(t *testing.T) {
g := &Generator{}

tests := []struct {
name string
col *Column
settings Settings
expected string
}{
{
name: "non-nullable uuid array (lib/pq)",
col: &Column{
Name: "associated_ids",
Type: "uuid[]",
NotNull: true,
IsArray: true,
},
settings: Settings{SQLPackage: SQLPackageStandard},
expected: "[]uuid.UUID",
},
{
name: "nullable uuid array (lib/pq standard driver)",
col: &Column{
Name: "associated_ids",
Type: "uuid[]",
NotNull: false,
IsArray: true,
},
settings: Settings{SQLPackage: SQLPackageStandard},
expected: "*[]uuid.UUID",
},
{
name: "nullable uuid array with pointer setting enabled",
col: &Column{
Name: "associated_ids",
Type: "uuid[]",
NotNull: false,
IsArray: true,
},
settings: Settings{SQLPackage: SQLPackagePGXV5, GoTypePointer: true},
expected: "*[]uuid.UUID",
},
{
name: "nullable uuid array with native pgx/v5 driver",
col: &Column{
Name: "associated_ids",
Type: "uuid[]",
NotNull: false,
IsArray: true,
},
settings: Settings{SQLPackage: SQLPackagePGXV5, GoTypePointer: false},
expected: "[]uuid.UUID",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := g.GoType(tt.col, tt.settings)
if actual != tt.expected {
t.Errorf("GoType() = %v, expected %v", actual, tt.expected)
}
})
}
}