Skip to content

Commit cc849aa

Browse files
committed
Python tests are passing
1 parent cf86d86 commit cc849aa

File tree

6 files changed

+546
-230
lines changed

6 files changed

+546
-230
lines changed

internal/cmd/shim.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings {
1111
var over []*plugin.Override
1212
for _, o := range cs.Overrides {
1313
over = append(over, &plugin.Override{
14-
CodeType: "", // FIXME
15-
DbType: o.DBType,
16-
Nullable: o.Nullable,
17-
Column: o.Column,
14+
CodeType: "", // FIXME
15+
DbType: o.DBType,
16+
Nullable: o.Nullable,
17+
Column: o.Column,
18+
PythonType: pluginPythonType(o.PythonType),
1819
})
1920
}
2021
return &plugin.Settings{
@@ -24,6 +25,24 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings {
2425
Queries: []string(cs.Package.Queries),
2526
Overrides: over,
2627
Rename: cs.Rename,
28+
Python: pluginPythonCode(cs.Python),
29+
}
30+
}
31+
32+
func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
33+
return &plugin.PythonCode{
34+
Out: s.Out,
35+
Package: s.Package,
36+
EmitExactTableNames: s.EmitExactTableNames,
37+
EmitSyncQuerier: s.EmitSyncQuerier,
38+
EmitAsyncQuerier: s.EmitAsyncQuerier,
39+
}
40+
}
41+
42+
func pluginPythonType(pt config.PythonType) *plugin.PythonType {
43+
return &plugin.PythonType{
44+
Module: pt.Module,
45+
Name: pt.Name,
2746
}
2847
}
2948

@@ -61,6 +80,11 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
6180
NotNull: c.IsNotNull,
6281
IsArray: c.IsArray,
6382
Length: int32(l),
83+
Table: &plugin.Identifier{
84+
Catalog: t.Rel.Catalog,
85+
Schema: t.Rel.Schema,
86+
Name: t.Rel.Name,
87+
},
6488
})
6589
}
6690
tables = append(tables, &plugin.Table{
@@ -106,15 +130,46 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
106130
Comments: q.Comments,
107131
Columns: columns,
108132
Params: params,
133+
Filename: q.Filename,
109134
})
110135
}
111136
return out
112137
}
113138

114139
func pluginQueryColumn(c *compiler.Column) *plugin.Column {
115-
return &plugin.Column{
116-
Name: c.Name,
140+
l := -1
141+
if c.Length != nil {
142+
l = *c.Length
117143
}
144+
out := &plugin.Column{
145+
Name: c.Name,
146+
Comment: c.Comment,
147+
NotNull: c.NotNull,
148+
IsArray: c.IsArray,
149+
Length: int32(l),
150+
}
151+
152+
if c.Type != nil {
153+
out.Type = &plugin.Identifier{
154+
Catalog: c.Type.Catalog,
155+
Schema: c.Type.Schema,
156+
Name: c.Type.Name,
157+
}
158+
} else {
159+
out.Type = &plugin.Identifier{
160+
Name: c.DataType,
161+
}
162+
}
163+
164+
if c.Table != nil {
165+
out.Table = &plugin.Identifier{
166+
Catalog: c.Table.Catalog,
167+
Schema: c.Table.Schema,
168+
Name: c.Table.Name,
169+
}
170+
}
171+
172+
return out
118173
}
119174

120175
func pluginQueryParam(p compiler.Parameter) *plugin.Parameter {

internal/codegen/python2/gen.go

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import (
1111
"github.com/kyleconroy/sqlc/internal/codegen"
1212
"github.com/kyleconroy/sqlc/internal/config"
1313
"github.com/kyleconroy/sqlc/internal/core"
14+
"github.com/kyleconroy/sqlc/internal/debug"
1415
"github.com/kyleconroy/sqlc/internal/inflection"
1516
"github.com/kyleconroy/sqlc/internal/metadata"
1617
"github.com/kyleconroy/sqlc/internal/plugin"
1718
pyast "github.com/kyleconroy/sqlc/internal/python/ast"
1819
"github.com/kyleconroy/sqlc/internal/python/poet"
19-
// pyprint "github.com/kyleconroy/sqlc/internal/python/printer"
20-
// "github.com/kyleconroy/sqlc/internal/sql/ast"
21-
// "github.com/kyleconroy/sqlc/internal/sql/catalog"
20+
pyprint "github.com/kyleconroy/sqlc/internal/python/printer"
2221
)
2322

2423
type Constant struct {
@@ -192,15 +191,16 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
192191

193192
func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
194193
for _, oride := range req.Settings.Overrides {
195-
if oride.CodeType == "" {
194+
if !pyTypeIsSet(oride.PythonType) {
196195
continue
197196
}
197+
// TODO: What do we do about regexs?
198198
// sameTable := oride.Matches(col.Table, req.Catalog.DefaultSchema)
199199
// if oride.Column != "" && oride.ColumnName.MatchString(col.Name) && sameTable {
200-
// return oride.CodeType // TODO: Used to call .TypeString()
200+
// return pyTypeString(oride.PythonType)
201201
// }
202202
if oride.DbType != "" && oride.DbType == col.DataType && oride.Nullable != (col.NotNull || col.IsArray) {
203-
return oride.CodeType // TODO: Used to call .TypeString()
203+
return pyTypeString(oride.PythonType)
204204
}
205205
}
206206

@@ -290,8 +290,7 @@ func buildModels(req *plugin.CodeGenRequest) []Struct {
290290
tableName = schema.Name + "_" + table.Rel.Name
291291
}
292292
structName := tableName
293-
// FIXME How do we deal with plugin specific settings?
294-
if false { // !req.Settings.Python.EmitExactTableNames {
293+
if !req.Settings.Python.EmitExactTableNames {
295294
structName = inflection.Singular(structName)
296295
}
297296
s := Struct{
@@ -414,6 +413,12 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
414413
SourceName: query.Filename,
415414
}
416415

416+
dump := methodName == "get_venue"
417+
if dump {
418+
debug.Dump(query)
419+
debug.Dump(gq)
420+
}
421+
417422
if len(query.Params) > 4 {
418423
var cols []pyColumn
419424
for _, p := range query.Params {
@@ -453,6 +458,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
453458
continue
454459
}
455460
same := true
461+
456462
for i, f := range s.Fields {
457463
c := query.Columns[i]
458464
// HACK: models do not have "models." on their types, so trim that so we can find matches
@@ -461,6 +467,10 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
461467
sameName := f.Name == columnName(c, i)
462468
sameType := f.Type == trimmedPyType
463469
sameTable := sameTableName(c.Table, s.Table, req.Catalog.DefaultSchema)
470+
if dump {
471+
debug.Dump(c.Table, s.Table, req.Catalog.DefaultSchema)
472+
debug.Dump(sameName, sameType, sameTable)
473+
}
464474
if !sameName || !sameType || !sameTable {
465475
same = false
466476
}
@@ -1066,33 +1076,43 @@ func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
10661076
Enums: enums,
10671077
}
10681078

1069-
// tctx := pyTmplCtx{
1070-
// Models: models,
1071-
// Queries: queries,
1072-
// Enums: enums,
1073-
// EmitSync: settings.Python.EmitSyncQuerier,
1074-
// EmitAsync: settings.Python.EmitAsyncQuerier,
1075-
// }
1076-
1077-
// output := map[string]string{}
1078-
// result := pyprint.Print(buildModelsTree(&tctx, i), pyprint.Options{})
1079-
// tctx.SourceName = "models.py"
1080-
// output["models.py"] = string(result.Python)
1081-
1082-
// files := map[string]struct{}{}
1083-
// for _, q := range queries {
1084-
// files[q.SourceName] = struct{}{}
1085-
// }
1086-
1087-
// for source := range files {
1088-
// tctx.SourceName = source
1089-
// result := pyprint.Print(buildQueryTree(&tctx, i, source), pyprint.Options{})
1090-
// name := source
1091-
// if !strings.HasSuffix(name, ".py") {
1092-
// name = strings.TrimSuffix(name, ".sql")
1093-
// name += ".py"
1094-
// }
1095-
// output[name] = string(result.Python)
1096-
// }
1097-
return &plugin.CodeGenResponse{}, nil
1079+
tctx := pyTmplCtx{
1080+
Models: models,
1081+
Queries: queries,
1082+
Enums: enums,
1083+
EmitSync: req.Settings.Python.EmitSyncQuerier,
1084+
EmitAsync: req.Settings.Python.EmitAsyncQuerier,
1085+
}
1086+
1087+
output := map[string]string{}
1088+
result := pyprint.Print(buildModelsTree(&tctx, i), pyprint.Options{})
1089+
tctx.SourceName = "models.py"
1090+
output["models.py"] = string(result.Python)
1091+
1092+
files := map[string]struct{}{}
1093+
for _, q := range queries {
1094+
files[q.SourceName] = struct{}{}
1095+
}
1096+
1097+
for source := range files {
1098+
tctx.SourceName = source
1099+
result := pyprint.Print(buildQueryTree(&tctx, i, source), pyprint.Options{})
1100+
name := source
1101+
if !strings.HasSuffix(name, ".py") {
1102+
name = strings.TrimSuffix(name, ".sql")
1103+
name += ".py"
1104+
}
1105+
output[name] = string(result.Python)
1106+
}
1107+
1108+
resp := plugin.CodeGenResponse{}
1109+
1110+
for filename, code := range output {
1111+
resp.Files = append(resp.Files, &plugin.File{
1112+
Name: filename,
1113+
Contents: []byte(code),
1114+
})
1115+
}
1116+
1117+
return &resp, nil
10981118
}

internal/codegen/python2/imports.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sort"
66
"strings"
77

8-
"github.com/kyleconroy/sqlc/internal/config"
8+
"github.com/kyleconroy/sqlc/internal/plugin"
99
)
1010

1111
type importSpec struct {
@@ -14,6 +14,17 @@ type importSpec struct {
1414
Alias string
1515
}
1616

17+
func pyTypeIsSet(t *plugin.PythonType) bool {
18+
return t.Module != "" || t.Name != ""
19+
}
20+
21+
func pyTypeString(t *plugin.PythonType) string {
22+
if t.Name != "" && t.Module == "" {
23+
return t.Name
24+
}
25+
return t.Module + "." + t.Name
26+
}
27+
1728
func (i importSpec) String() string {
1829
if i.Alias != "" {
1930
if i.Name == "" {
@@ -96,8 +107,8 @@ func (i *importer) modelImportSpecs() (map[string]importSpec, map[string]importS
96107
pkg := make(map[string]importSpec)
97108

98109
for _, o := range i.Settings.Overrides {
99-
if o.PythonType.IsSet() && o.PythonType.Module != "" {
100-
if modelUses(o.PythonType.TypeString()) {
110+
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
111+
if modelUses(pyTypeString(o.PythonType)) {
101112
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
102113
}
103114
}
@@ -142,8 +153,8 @@ func (i *importer) queryImportSpecs(fileName string) (map[string]importSpec, map
142153
}
143154

144155
for _, o := range i.Settings.Overrides {
145-
if o.PythonType.IsSet() && o.PythonType.Module != "" {
146-
if queryUses(o.PythonType.TypeString()) {
156+
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
157+
if queryUses(pyTypeString(o.PythonType)) {
147158
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
148159
}
149160
}

internal/codegen/python2/postgresql_type.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import (
66
"github.com/kyleconroy/sqlc/internal/plugin"
77
)
88

9+
func dataType(n *plugin.Identifier) string {
10+
if n.Schema != "" {
11+
return n.Schema + "." + n.Name
12+
} else {
13+
return n.Name
14+
}
15+
}
16+
917
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
10-
columnType := col.DataType
18+
columnType := dataType(col.Type)
1119

1220
switch columnType {
1321
case "serial", "serial4", "pg_catalog.serial4", "bigserial", "serial8", "pg_catalog.serial8", "smallserial", "serial2", "pg_catalog.serial2", "integer", "int", "int4", "pg_catalog.int4", "bigint", "int8", "pg_catalog.int8", "smallint", "int2", "pg_catalog.int2":

0 commit comments

Comments
 (0)