@@ -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
2423type Constant struct {
@@ -192,15 +191,16 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
192191
193192func 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}
0 commit comments