Skip to content
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

feat(sdk): Add the plugin SDK package #1463

Merged
merged 1 commit into from Mar 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions internal/codegen/golang/compat.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/codegen/golang/gen.go
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"text/template"

"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)
Expand Down Expand Up @@ -58,9 +58,9 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
}

funcMap := template.FuncMap{
"lowerTitle": codegen.LowerTitle,
"comment": codegen.DoubleSlashComment,
"escape": codegen.EscapeBacktick,
"lowerTitle": sdk.LowerTitle,
"comment": sdk.DoubleSlashComment,
"escape": sdk.EscapeBacktick,
"imports": i.Imports,
"hasPrefix": strings.HasPrefix,
}
Expand Down
50 changes: 4 additions & 46 deletions internal/codegen/golang/go_type.go
@@ -1,60 +1,18 @@
package golang

import (
"github.com/kyleconroy/sqlc/internal/pattern"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/plugin"
)

// XXX: These are copied from python codegen.
func matchString(pat, target string) bool {
matcher, err := pattern.MatchCompile(pat)
if err != nil {
panic(err)
}
return matcher.MatchString(target)
}

func matches(o *plugin.Override, n *plugin.Identifier, defaultSchema string) bool {
if n == nil {
return false
}

schema := n.Schema
if n.Schema == "" {
schema = defaultSchema
}

if o.Table.Catalog != "" && !matchString(o.Table.Catalog, n.Catalog) {
return false
}

if o.Table.Schema == "" && schema != "" {
return false
}

if o.Table.Schema != "" && !matchString(o.Table.Schema, schema) {
return false
}

if o.Table.Name == "" && n.Name != "" {
return false
}

if o.Table.Name != "" && !matchString(o.Table.Name, n.Name) {
return false
}

return true
}

func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
// Check if the column's type has been overridden
for _, oride := range req.Settings.Overrides {
if oride.GoType.TypeName == "" {
continue
}
sameTable := matches(oride, col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && matchString(oride.ColumnName, col.Name) && sameTable {
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
return oride.GoType.TypeName
}
}
Expand All @@ -66,7 +24,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
}

func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := dataType(col.Type)
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray

// package overrides have a higher precedence
Expand Down
11 changes: 2 additions & 9 deletions internal/codegen/golang/mysql_type.go
Expand Up @@ -3,20 +3,13 @@ package golang
import (
"log"

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func dataType(n *plugin.Identifier) string {
if n.Schema != "" {
return n.Schema + "." + n.Name
} else {
return n.Name
}
}

func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := dataType(col.Type)
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray

switch columnType {
Expand Down
31 changes: 28 additions & 3 deletions internal/codegen/golang/postgresql_type.go
@@ -1,15 +1,40 @@
package golang

import (
"fmt"
"log"
"strings"

"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
Comment on lines -6 to +8
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the main point of the change. The Go codegen no longer depends on the compiler or config packages.

"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func parseIdentifierString(name string) (*plugin.Identifier, error) {
parts := strings.Split(name, ".")
switch len(parts) {
case 1:
return &plugin.Identifier{
Name: parts[0],
}, nil
case 2:
return &plugin.Identifier{
Schema: parts[0],
Name: parts[1],
}, nil
case 3:
return &plugin.Identifier{
Catalog: parts[0],
Schema: parts[1],
Name: parts[2],
}, nil
default:
return nil, fmt.Errorf("invalid name: %s", name)
}
}

func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := dataType(col.Type)
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
driver := parseDriver(req.Settings)

Expand Down Expand Up @@ -239,7 +264,7 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
return "interface{}"

default:
rel, err := compiler.ParseRelationString(columnType)
rel, err := parseIdentifierString(columnType)
if err != nil {
// TODO: Should this actually return an error here?
return "interface{}"
Expand Down
10 changes: 5 additions & 5 deletions internal/codegen/golang/result.go
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"strings"

"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/inflection"
"github.com/kyleconroy/sqlc/internal/plugin"
)
Expand Down Expand Up @@ -140,15 +140,15 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)

var constantName string
if req.Settings.Go.EmitExportedQueries {
constantName = codegen.Title(query.Name)
constantName = sdk.Title(query.Name)
} else {
constantName = codegen.LowerTitle(query.Name)
constantName = sdk.LowerTitle(query.Name)
}

gq := Query{
Cmd: query.Cmd,
ConstantName: constantName,
FieldName: codegen.LowerTitle(query.Name) + "Stmt",
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
MethodName: query.Name,
SourceName: query.Filename,
SQL: query.Text,
Expand Down Expand Up @@ -209,7 +209,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
c := query.Columns[i]
sameName := f.Name == StructName(columnName(c, i), req.Settings)
sameType := f.Type == goType(req, c)
sameTable := sameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
if !sameName || !sameType || !sameTable {
same = false
}
Expand Down
3 changes: 2 additions & 1 deletion internal/codegen/golang/sqlite_type.go
Expand Up @@ -4,11 +4,12 @@ import (
"log"
"strings"

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func sqliteType(req *plugin.CodeGenRequest, col *plugin.Column) string {
dt := strings.ToLower(dataType(col.Type))
dt := strings.ToLower(sdk.DataType(col.Type))
notNull := col.NotNull || col.IsArray

switch dt {
Expand Down
29 changes: 9 additions & 20 deletions internal/codegen/kotlin/gen.go
Expand Up @@ -10,23 +10,12 @@ import (
"strings"
"text/template"

"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/inflection"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func sameTableName(n, f *plugin.Identifier) bool {
if n == nil {
return false
}
schema := n.Schema
if n.Schema == "" {
schema = "public"
}
return n.Catalog == n.Catalog && schema == f.Schema && n.Name == f.Name
}

var ktIdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")

type Constant struct {
Expand Down Expand Up @@ -269,7 +258,7 @@ func dataClassName(name string, settings *plugin.Settings) string {
}

func memberName(name string, settings *plugin.Settings) string {
return codegen.LowerTitle(dataClassName(name, settings))
return sdk.LowerTitle(dataClassName(name, settings))
}

func buildDataClasses(req *plugin.CodeGenRequest) []Struct {
Expand Down Expand Up @@ -365,7 +354,7 @@ func makeType(req *plugin.CodeGenRequest, col *plugin.Column) ktType {
IsEnum: isEnum,
IsArray: col.IsArray,
IsNull: !col.NotNull,
DataType: dataType(col.Type),
DataType: sdk.DataType(col.Type),
Engine: req.Settings.Engine,
}
}
Expand Down Expand Up @@ -468,9 +457,9 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
gq := Query{
Cmd: query.Cmd,
ClassName: strings.Title(query.Name),
ConstantName: codegen.LowerTitle(query.Name),
FieldName: codegen.LowerTitle(query.Name) + "Stmt",
MethodName: codegen.LowerTitle(query.Name),
ConstantName: sdk.LowerTitle(query.Name),
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
MethodName: sdk.LowerTitle(query.Name),
SourceName: query.Filename,
SQL: jdbcSQL(query.Text, req.Settings.Engine),
Comments: query.Comments,
Expand Down Expand Up @@ -507,7 +496,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
c := query.Columns[i]
sameName := f.Name == memberName(ktColumnName(c, i), req.Settings)
sameType := f.Type == makeType(req, c)
sameTable := sameTableName(c.Table, &s.Table)
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)

if !sameName || !sameType || !sameTable {
same = false
Expand Down Expand Up @@ -779,8 +768,8 @@ func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
}

funcMap := template.FuncMap{
"lowerTitle": codegen.LowerTitle,
"comment": codegen.DoubleSlashComment,
"lowerTitle": sdk.LowerTitle,
"comment": sdk.DoubleSlashComment,
"imports": i.Imports,
"offset": Offset,
}
Expand Down
11 changes: 2 additions & 9 deletions internal/codegen/kotlin/mysql_type.go
Expand Up @@ -3,20 +3,13 @@ package kotlin
import (
"log"

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func dataType(n *plugin.Identifier) string {
if n.Schema != "" {
return n.Schema + "." + n.Name
} else {
return n.Name
}
}

func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) (string, bool) {
columnType := dataType(col.Type)
columnType := sdk.DataType(col.Type)

switch columnType {

Expand Down
3 changes: 2 additions & 1 deletion internal/codegen/kotlin/postgresql_type.go
Expand Up @@ -3,11 +3,12 @@ package kotlin
import (
"log"

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) (string, bool) {
columnType := dataType(col.Type)
columnType := sdk.DataType(col.Type)

switch columnType {
case "serial", "pg_catalog.serial4":
Expand Down