-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyzer): Analyze queries using a running PostgreSQL database (#…
…2805) * feat(analyzer): Analyze queries using a running PostgreSQL database 94 of the open issues on sqlc are related to the analyzer. There are many cases where the current analyzer produces false positives or false negatives. sqlx and some other projects have proven that it's possible to extract query metadata from a running database. This approach is a bit different, in that the database analysis is layered on top of the existing query analyzer. We use the new analysis to provide better type information and support a wider set of cases when the existing analyzer fails. * test(analyzer): Update endtoend tests for new analyzer Add a `contexts` key to exec.json to opt certain tests into or out of database-backed analysis. Fix many incorrect test cases that didn't run against an actual database.
- Loading branch information
1 parent
4b7fddd
commit bb84596
Showing
478 changed files
with
4,422 additions
and
996 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
"database": { | ||
"managed": true | ||
}, | ||
"analyzer": { | ||
"database": false | ||
}, | ||
"rules": [ | ||
"sqlc/db-prepare" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
"database": { | ||
"managed": true | ||
}, | ||
"analyzer": { | ||
"database": false | ||
}, | ||
"rules": [ | ||
"sqlc/db-prepare" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package analyzer | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sqlc-dev/sqlc/internal/sql/ast" | ||
"github.com/sqlc-dev/sqlc/internal/sql/named" | ||
) | ||
|
||
type Column struct { | ||
Name string | ||
OriginalName string | ||
DataType string | ||
NotNull bool | ||
Unsigned bool | ||
IsArray bool | ||
ArrayDims int | ||
Comment string | ||
Length *int | ||
IsNamedParam bool | ||
IsFuncCall bool | ||
|
||
// XXX: Figure out what PostgreSQL calls `foo.id` | ||
Scope string | ||
Table *ast.TableName | ||
TableAlias string | ||
Type *ast.TypeName | ||
EmbedTable *ast.TableName | ||
|
||
IsSqlcSlice bool // is this sqlc.slice() | ||
} | ||
|
||
type Parameter struct { | ||
Number int | ||
Column *Column | ||
} | ||
|
||
type Analysis struct { | ||
Columns []Column | ||
Params []Parameter | ||
} | ||
|
||
type Analyzer interface { | ||
Analyze(context.Context, ast.Node, string, []string, *named.ParamSet) (*Analysis, error) | ||
Close(context.Context) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/sqlc-dev/sqlc/internal/config" | ||
) | ||
|
||
type Options struct { | ||
Env Env | ||
Stderr io.Writer | ||
MutateConfig func(*config.Config) | ||
} | ||
|
||
func (o *Options) ReadConfig(dir, filename string) (string, *config.Config, error) { | ||
path, conf, err := readConfig(o.Stderr, dir, filename) | ||
if err != nil { | ||
return path, conf, err | ||
} | ||
if o.MutateConfig != nil { | ||
o.MutateConfig(conf) | ||
} | ||
return path, conf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.