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

UPDATED: sqlc.narg() - allow user-specified nullability #1536

Merged
merged 12 commits into from
Apr 30, 2022
1 change: 1 addition & 0 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
if err != nil {
return nil, err
}

params, err := c.resolveCatalogRefs(qc, rvs, refs, namedParams)
if err != nil {
return nil, err
Expand Down
92 changes: 54 additions & 38 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/astutils"
"github.com/kyleconroy/sqlc/internal/sql/catalog"
"github.com/kyleconroy/sqlc/internal/sql/named"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
)

Expand All @@ -18,26 +19,14 @@ func dataType(n *ast.TypeName) string {
}
}

func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar, args []paramRef, names map[int]string) ([]Parameter, error) {
func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar, args []paramRef, params *named.ParamSet) ([]Parameter, error) {
c := comp.catalog

aliasMap := map[string]*ast.TableName{}
// TODO: Deprecate defaultTable
var defaultTable *ast.TableName
var tables []*ast.TableName

parameterName := func(n int, defaultName string) string {
if n, ok := names[n]; ok {
return n
}
return defaultName
}

isNamedParam := func(n int) bool {
_, ok := names[n]
return ok
}

typeMap := map[string]map[string]map[string]*catalog.Column{}
indexTable := func(table catalog.Table) error {
tables = append(tables, table.Rel)
Expand Down Expand Up @@ -92,24 +81,28 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
switch n := ref.parent.(type) {

case *limitOffset:
defaultP := named.NewInferredParam("offset", true)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, "offset"),
Name: p.Name(),
DataType: "integer",
NotNull: true,
IsNamedParam: isNamedParam(ref.ref.Number),
NotNull: p.NotNull(),
IsNamedParam: isNamed,
},
})

case *limitCount:
defaultP := named.NewInferredParam("limit", true)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, "limit"),
Name: p.Name(),
DataType: "integer",
NotNull: true,
IsNamedParam: isNamedParam(ref.ref.Number),
NotNull: p.NotNull(),
IsNamedParam: isNamed,
},
})

Expand All @@ -127,12 +120,16 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if astutils.Join(n.Name, ".") == "||" {
dataType = "string"
}

defaultP := named.NewParam("")
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, ""),
Name: p.Name(),
DataType: dataType,
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
NotNull: p.NotNull(),
},
})
continue
Expand Down Expand Up @@ -185,16 +182,19 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if ref.name != "" {
key = ref.name
}

defaultP := named.NewInferredParam(key, c.IsNotNull)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, key),
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
NotNull: p.NotNull(),
IsArray: c.IsArray,
Length: c.Length,
Table: table,
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
},
})
}
Expand Down Expand Up @@ -242,15 +242,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
}

if c, ok := typeMap[schema][table.Name][key]; ok {
defaultP := named.NewInferredParam(key, c.IsNotNull)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: number,
Column: &Column{
Name: parameterName(ref.ref.Number, key),
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
NotNull: p.NotNull(),
IsArray: c.IsArray,
Table: table,
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
},
})
}
Expand Down Expand Up @@ -309,12 +311,16 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if argName != "" {
defaultName = argName
}

defaultP := named.NewInferredParam(defaultName, false)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, defaultName),
Name: p.Name(),
DataType: "any",
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
NotNull: p.NotNull(),
},
})
continue
Expand All @@ -340,13 +346,15 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
paramName = funcName
}

defaultP := named.NewInferredParam(paramName, true)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, paramName),
Name: p.Name(),
DataType: dataType(paramType),
NotNull: true,
IsNamedParam: isNamedParam(ref.ref.Number),
NotNull: p.NotNull(),
IsNamedParam: isNamed,
},
})
}
Expand Down Expand Up @@ -399,16 +407,18 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
}

if c, ok := tableMap[key]; ok {
defaultP := named.NewInferredParam(key, c.IsNotNull)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: parameterName(ref.ref.Number, key),
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
NotNull: p.NotNull(),
IsArray: c.IsArray,
Table: &ast.TableName{Schema: schema, Name: rel},
Length: c.Length,
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
},
})
} else {
Expand All @@ -424,7 +434,11 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
return nil, fmt.Errorf("*ast.TypeCast has nil type name")
}
col := toColumn(n.TypeName)
col.Name = parameterName(ref.ref.Number, col.Name)
defaultP := named.NewInferredParam(col.Name, col.NotNull)
p, _ := params.FetchMerge(ref.ref.Number, defaultP)

col.Name = p.Name()
col.NotNull = p.NotNull()
a = append(a, Parameter{
Number: ref.ref.Number,
Column: col,
Expand Down Expand Up @@ -500,15 +514,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if ref.name != "" {
key = ref.name
}
defaultP := named.NewInferredParam(key, c.IsNotNull)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: number,
Column: &Column{
Name: parameterName(ref.ref.Number, key),
Name: p.Name(),
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
IsArray: c.IsArray,
Table: table,
IsNamedParam: isNamedParam(ref.ref.Number),
IsNamedParam: isNamed,
},
})
}
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/sqlc_narg/mysql/go/db.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/sqlc_narg/mysql/go/models.go

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

Loading