Skip to content

Override globs #862

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/antlr/antlr4 v0.0.0-20200209180723-1177c0b58d07
github.com/davecgh/go-spew v1.1.1
github.com/go-sql-driver/mysql v1.6.0
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.5.5
github.com/jackc/pgx/v4 v4.11.0
github.com/jinzhu/inflection v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func sameTableName(n *ast.TableName, f core.FQN, defaultSchema string) bool {
if n.Schema == "" {
schema = defaultSchema
}
return n.Catalog == f.Catalog && schema == f.Schema && n.Name == f.Rel
return n.Catalog == f.Catalog && ((f.Schema != nil && f.Schema.Match(schema)) || schema == "") && ((f.Rel != nil && f.Rel.Match(n.Name)) || n.Name == "")
}
2 changes: 1 addition & 1 deletion internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func goType(r *compiler.Result, col *compiler.Column, settings config.CombinedSe
continue
}
sameTable := sameTableName(col.Table, oride.Table, r.Catalog.DefaultSchema)
if oride.Column != "" && oride.ColumnName == col.Name && sameTable {
if oride.Column != "" && oride.ColumnName.Match(col.Name) && sameTable {
return oride.GoTypeName
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sort"
"strings"

"github.com/gobwas/glob"

"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
Expand Down Expand Up @@ -74,7 +76,7 @@ func buildStructs(r *compiler.Result, settings config.CombinedSettings) []Struct
structName = inflection.Singular(structName)
}
s := Struct{
Table: core.FQN{Schema: schema.Name, Rel: table.Rel.Name},
Table: core.FQN{Schema: glob.MustCompile(schema.Name), Rel: glob.MustCompile(table.Rel.Name)},
Name: StructName(structName, settings),
Comment: table.Comment,
}
Expand Down
6 changes: 4 additions & 2 deletions internal/codegen/kotlin/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"text/template"

"github.com/gobwas/glob"

"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
Expand All @@ -26,7 +28,7 @@ func sameTableName(n *ast.TableName, f core.FQN) bool {
if n.Schema == "" {
schema = "public"
}
return n.Catalog == n.Catalog && schema == f.Schema && n.Name == f.Rel
return n.Catalog == f.Catalog && ((f.Schema != nil && f.Schema.Match(schema)) || schema == "") && ((f.Rel != nil && f.Rel.Match(n.Name)) || n.Name == "")
}

var ktIdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")
Expand Down Expand Up @@ -286,7 +288,7 @@ func buildDataClasses(r *compiler.Result, settings config.CombinedSettings) []St
structName = inflection.Singular(structName)
}
s := Struct{
Table: core.FQN{Schema: schema.Name, Rel: table.Rel.Name},
Table: core.FQN{Schema: glob.MustCompile(schema.Name), Rel: glob.MustCompile(table.Rel.Name)},
Name: structName,
Comment: table.Comment,
}
Expand Down
7 changes: 4 additions & 3 deletions internal/codegen/python/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/gobwas/glob"
"github.com/kyleconroy/sqlc/internal/codegen"
"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
Expand Down Expand Up @@ -182,7 +183,7 @@ func pyInnerType(r *compiler.Result, col *compiler.Column, settings config.Combi
continue
}
sameTable := sameTableName(col.Table, oride.Table, r.Catalog.DefaultSchema)
if oride.Column != "" && oride.ColumnName == col.Name && sameTable {
if oride.Column != "" && oride.ColumnName.Match(col.Name) && sameTable {
return oride.PythonType.TypeString()
}
if oride.DBType != "" && oride.DBType == col.DataType && oride.Nullable != (col.NotNull || col.IsArray) {
Expand Down Expand Up @@ -284,7 +285,7 @@ func buildModels(r *compiler.Result, settings config.CombinedSettings) []Struct
structName = inflection.Singular(structName)
}
s := Struct{
Table: core.FQN{Schema: schema.Name, Rel: table.Rel.Name},
Table: core.FQN{Schema: glob.MustCompile(schema.Name), Rel: glob.MustCompile(table.Rel.Name)},
Name: ModelName(structName, settings),
Comment: table.Comment,
}
Expand Down Expand Up @@ -363,7 +364,7 @@ func sameTableName(n *ast.TableName, f core.FQN, defaultSchema string) bool {
if n.Schema == "" {
schema = defaultSchema
}
return n.Catalog == f.Catalog && schema == f.Schema && n.Name == f.Rel
return n.Catalog == f.Catalog && ((f.Schema != nil && f.Schema.Match(schema)) || schema == "") && ((f.Rel != nil && f.Rel.Match(n.Name)) || n.Name == "")
}

var postgresPlaceholderRegexp = regexp.MustCompile(`\B\$(\d+)\b`)
Expand Down
18 changes: 10 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

yaml "gopkg.in/yaml.v3"

"github.com/gobwas/glob"

"github.com/kyleconroy/sqlc/internal/core"
)

Expand Down Expand Up @@ -160,7 +162,7 @@ type Override struct {
// fully qualified name of the column, e.g. `accounts.id`
Column string `json:"column" yaml:"column"`

ColumnName string
ColumnName glob.Glob
Table core.FQN
GoImportPath string
GoPackage string
Expand Down Expand Up @@ -198,16 +200,16 @@ func (o *Override) Parse() error {
colParts := strings.Split(o.Column, ".")
switch len(colParts) {
case 2:
o.ColumnName = colParts[1]
o.Table = core.FQN{Schema: "public", Rel: colParts[0]}
o.ColumnName = glob.MustCompile(colParts[1])
o.Table = core.FQN{Schema: glob.MustCompile("public"), Rel: glob.MustCompile(colParts[0])}
case 3:
o.ColumnName = colParts[2]
o.Table = core.FQN{Schema: colParts[0], Rel: colParts[1]}
o.ColumnName = glob.MustCompile(colParts[2])
o.Table = core.FQN{Schema: glob.MustCompile(colParts[0]), Rel: glob.MustCompile(colParts[1])}
case 4:
o.ColumnName = colParts[3]
o.Table = core.FQN{Catalog: colParts[0], Schema: colParts[1], Rel: colParts[2]}
o.ColumnName = glob.MustCompile(colParts[3])
o.Table = core.FQN{Catalog: colParts[0], Schema: glob.MustCompile(colParts[1]), Rel: glob.MustCompile(colParts[2])}
default:
return fmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]colname.tablename'", o.Column)
return fmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]tablename.colname'", o.Column)
}
}

Expand Down
17 changes: 4 additions & 13 deletions internal/core/fqn.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package core

import "github.com/gobwas/glob"

// TODO: This is the last struct left over from the old architecture. Figure
// out how to remove it at some point
type FQN struct {
Catalog string
Schema string
Rel string
}

func (f FQN) String() string {
s := f.Rel
if f.Schema != "" {
s = f.Schema + "." + s
}
if f.Catalog != "" {
s = f.Catalog + "." + s
}
return s
Schema glob.Glob
Rel glob.Glob
}