@@ -53,6 +53,8 @@ type Field struct {
5353 Name string
5454 Type pyType
5555 Comment string
56+ // EmbedFields contains the embedded fields that require scanning.
57+ EmbedFields []Field
5658}
5759
5860type Struct struct {
@@ -105,14 +107,41 @@ func (v QueryValue) RowNode(rowVar string) *pyast.Node {
105107 call := & pyast.Call {
106108 Func : v .Annotation (),
107109 }
108- for i , f := range v .Struct .Fields {
109- call .Keywords = append (call .Keywords , & pyast.Keyword {
110- Arg : f .Name ,
111- Value : subscriptNode (
110+ rowIndex := 0 // We need to keep track of the index in the row variable.
111+ for _ , f := range v .Struct .Fields {
112+ var valueNode * pyast.Node
113+ // Check if we are using sqlc.embed, if so we need to create a new object.
114+ if len (f .EmbedFields ) > 0 {
115+ // We keep this separate so we can easily add all arguments.
116+ embed_call := & pyast.Call {Func : f .Type .Annotation ()}
117+
118+ // Now add all field Initializers for the embedded model that index into the original row.
119+ for i , embedField := range f .EmbedFields {
120+ embed_call .Keywords = append (embed_call .Keywords , & pyast.Keyword {
121+ Arg : embedField .Name ,
122+ Value : subscriptNode (
123+ rowVar ,
124+ constantInt (rowIndex + i ),
125+ ),
126+ })
127+ }
128+
129+ valueNode = & pyast.Node {
130+ Node : & pyast.Node_Call {
131+ Call : embed_call ,
132+ },
133+ }
134+
135+ rowIndex += len (f .EmbedFields )
136+ } else {
137+ valueNode = subscriptNode (
112138 rowVar ,
113- constantInt (i ),
114- ),
115- })
139+ constantInt (rowIndex ),
140+ )
141+ rowIndex ++
142+ }
143+
144+ call .Keywords = append (call .Keywords , & pyast.Keyword {Arg : f .Name , Value : valueNode })
116145 }
117146 return & pyast.Node {
118147 Node : & pyast.Node_Call {
@@ -319,6 +348,47 @@ func paramName(p *plugin.Parameter) string {
319348type pyColumn struct {
320349 id int32
321350 * plugin.Column
351+ embed * pyEmbed
352+ }
353+
354+ type pyEmbed struct {
355+ modelType string
356+ modelName string
357+ fields []Field
358+ }
359+
360+ // Taken from https://github.com/sqlc-dev/sqlc/blob/8c59fbb9938a0bad3d9971fc2c10ea1f83cc1d0b/internal/codegen/golang/result.go#L123-L126
361+ // look through all the structs and attempt to find a matching one to embed
362+ // We need the name of the struct and its field names.
363+ func newGoEmbed (embed * plugin.Identifier , structs []Struct , defaultSchema string ) * pyEmbed {
364+ if embed == nil {
365+ return nil
366+ }
367+
368+ for _ , s := range structs {
369+ embedSchema := defaultSchema
370+ if embed .Schema != "" {
371+ embedSchema = embed .Schema
372+ }
373+
374+ // compare the other attributes
375+ if embed .Catalog != s .Table .Catalog || embed .Name != s .Table .Name || embedSchema != s .Table .Schema {
376+ continue
377+ }
378+
379+ fields := make ([]Field , len (s .Fields ))
380+ for i , f := range s .Fields {
381+ fields [i ] = f
382+ }
383+
384+ return & pyEmbed {
385+ modelType : s .Name ,
386+ modelName : s .Name ,
387+ fields : fields ,
388+ }
389+ }
390+
391+ return nil
322392}
323393
324394func columnsToStruct (req * plugin.GenerateRequest , name string , columns []pyColumn ) * Struct {
@@ -342,10 +412,21 @@ func columnsToStruct(req *plugin.GenerateRequest, name string, columns []pyColum
342412 if suffix > 0 {
343413 fieldName = fmt .Sprintf ("%s_%d" , fieldName , suffix )
344414 }
345- gs . Fields = append ( gs . Fields , Field {
415+ f := Field {
346416 Name : fieldName ,
347417 Type : makePyType (req , c .Column ),
348- })
418+ }
419+
420+ if c .embed != nil {
421+ f .Type = pyType {
422+ InnerType : "models." + modelName (c .embed .modelType , req .Settings ),
423+ IsArray : false ,
424+ IsNull : false ,
425+ }
426+ f .EmbedFields = c .embed .fields
427+ }
428+
429+ gs .Fields = append (gs .Fields , f )
349430 seen [colName ]++
350431 }
351432 return & gs
@@ -401,6 +482,7 @@ func buildQueries(conf Config, req *plugin.GenerateRequest, structs []Struct) ([
401482 cols = append (cols , pyColumn {
402483 id : p .Number ,
403484 Column : p .Column ,
485+ embed : newGoEmbed (p .Column .EmbedTable , structs , req .Catalog .DefaultSchema ),
404486 })
405487 }
406488 gq .Args = []QueryValue {{
@@ -459,6 +541,7 @@ func buildQueries(conf Config, req *plugin.GenerateRequest, structs []Struct) ([
459541 columns = append (columns , pyColumn {
460542 id : int32 (i ),
461543 Column : c ,
544+ embed : newGoEmbed (c .EmbedTable , structs , req .Catalog .DefaultSchema ),
462545 })
463546 }
464547 gs = columnsToStruct (req , query .Name + "Row" , columns )
0 commit comments