Skip to content

Commit 1c0bf21

Browse files
authored
fix: Enable strict function checking (#1405)
* fix: Enable strict function checking
1 parent 495be9d commit 1c0bf21

File tree

8 files changed

+39
-13
lines changed

8 files changed

+39
-13
lines changed

internal/compiler/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
7272
if rawSQL == "" {
7373
return nil, errors.New("missing semicolon at end of file")
7474
}
75-
if err := validate.FuncCall(c.catalog, raw); err != nil {
75+
if err := validate.FuncCall(c.catalog, c.combo, raw); err != nil {
7676
return nil, err
7777
}
7878
name, cmd, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
@@ -106,7 +106,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
106106
if err != nil {
107107
return nil, err
108108
}
109-
params, err := resolveCatalogRefs(c.catalog, qc, rvs, refs, namedParams)
109+
params, err := c.resolveCatalogRefs(qc, rvs, refs, namedParams)
110110
if err != nil {
111111
return nil, err
112112
}

internal/compiler/resolve.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func dataType(n *ast.TypeName) string {
1818
}
1919
}
2020

21-
func resolveCatalogRefs(c *catalog.Catalog, qc *QueryCatalog, rvs []*ast.RangeVar, args []paramRef, names map[int]string) ([]Parameter, error) {
21+
func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar, args []paramRef, names map[int]string) ([]Parameter, error) {
22+
c := comp.catalog
23+
2224
aliasMap := map[string]*ast.TableName{}
2325
// TODO: Deprecate defaultTable
2426
var defaultTable *ast.TableName

internal/config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ type GenKotlin struct {
9797
}
9898

9999
type SQL struct {
100-
Engine Engine `json:"engine,omitempty" yaml:"engine"`
101-
Schema Paths `json:"schema" yaml:"schema"`
102-
Queries Paths `json:"queries" yaml:"queries"`
103-
Gen SQLGen `json:"gen" yaml:"gen"`
100+
Engine Engine `json:"engine,omitempty" yaml:"engine"`
101+
Schema Paths `json:"schema" yaml:"schema"`
102+
Queries Paths `json:"queries" yaml:"queries"`
103+
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
104+
Gen SQLGen `json:"gen" yaml:"gen"`
104105
}
105106

106107
type SQLGen struct {

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type v1PackageSettings struct {
3838
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
3939
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
4040
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
41+
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
4142
}
4243

4344
func v1ParseConfig(rd io.Reader) (Config, error) {
@@ -134,6 +135,7 @@ func (c *V1GenerateSettings) Translate() Config {
134135
OutputFilesSuffix: pkg.OutputFilesSuffix,
135136
},
136137
},
138+
StrictFunctionChecks: pkg.StrictFunctionChecks,
137139
})
138140
}
139141

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: F :exec
2+
SELECT doesntexist();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"name": "querytest",
6+
"path": "go",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"engine": "postgresql",
10+
"strict_function_checks": true
11+
}
12+
]
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package querytest
2+
query.sql:1:1: function "doesntexist" does not exist

internal/sql/validate/func_call.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/kyleconroy/sqlc/internal/config"
78
"github.com/kyleconroy/sqlc/internal/sql/ast"
89
"github.com/kyleconroy/sqlc/internal/sql/astutils"
910
"github.com/kyleconroy/sqlc/internal/sql/catalog"
1011
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1112
)
1213

1314
type funcCallVisitor struct {
14-
catalog *catalog.Catalog
15-
err error
15+
catalog *catalog.Catalog
16+
settings config.CombinedSettings
17+
err error
1618
}
1719

1820
func (v *funcCallVisitor) Visit(node ast.Node) astutils.Visitor {
@@ -59,16 +61,18 @@ func (v *funcCallVisitor) Visit(node ast.Node) astutils.Visitor {
5961
}
6062

6163
fun, err := v.catalog.ResolveFuncCall(call)
62-
if fun != nil || errors.Is(err, sqlerr.NotFound) {
64+
if fun != nil {
65+
return v
66+
}
67+
if errors.Is(err, sqlerr.NotFound) && !v.settings.Package.StrictFunctionChecks {
6368
return v
6469
}
65-
6670
v.err = err
6771
return nil
6872
}
6973

70-
func FuncCall(c *catalog.Catalog, n ast.Node) error {
71-
visitor := funcCallVisitor{catalog: c}
74+
func FuncCall(c *catalog.Catalog, cs config.CombinedSettings, n ast.Node) error {
75+
visitor := funcCallVisitor{catalog: c, settings: cs}
7276
astutils.Walk(&visitor, n)
7377
return visitor.err
7478
}

0 commit comments

Comments
 (0)