-
Notifications
You must be signed in to change notification settings - Fork 799
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
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.
- Loading branch information
1 parent
20fda73
commit cba2aac
Showing
27 changed files
with
871 additions
and
192 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
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.