@@ -38,12 +38,7 @@ func (c *Compiler) expand(qc *QueryCatalog, raw *ast.RawStmt) ([]source.Edit, er
3838
3939func (c * Compiler ) quoteIdent (ident string ) string {
4040 if c .parser .IsReservedKeyword (ident ) {
41- switch c .conf .Engine {
42- case config .EngineMySQL :
43- return "`" + ident + "`"
44- default :
45- return "\" " + ident + "\" "
46- }
41+ return c .quote (ident )
4742 }
4843 if c .conf .Engine == config .EnginePostgreSQL {
4944 // camelCase means the column is also camelCase
@@ -54,6 +49,15 @@ func (c *Compiler) quoteIdent(ident string) string {
5449 return ident
5550}
5651
52+ func (c * Compiler ) quote (x string ) string {
53+ switch c .conf .Engine {
54+ case config .EngineMySQL :
55+ return "`" + x + "`"
56+ default :
57+ return "\" " + x + "\" "
58+ }
59+ }
60+
5761func (c * Compiler ) expandStmt (qc * QueryCatalog , raw * ast.RawStmt , node ast.Node ) ([]source.Edit , error ) {
5862 tables , err := c .sourceTables (qc , node )
5963 if err != nil {
@@ -132,16 +136,36 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
132136 for _ , p := range parts {
133137 old = append (old , c .quoteIdent (p ))
134138 }
135- oldString := strings .Join (old , "." )
139+
140+ var oldString string
141+ var oldFunc func (string ) int
136142
137143 // use the sqlc.embed string instead
138144 if embed , ok := qc .embeds .Find (ref ); ok {
139145 oldString = embed .Orig ()
146+ } else {
147+ oldFunc = func (s string ) int {
148+ length := 0
149+ for i , o := range old {
150+ if hasSeparator := i > 0 ; hasSeparator {
151+ length ++
152+ }
153+ if strings .HasPrefix (s [length :], o ) {
154+ length += len (o )
155+ } else if quoted := c .quote (o ); strings .HasPrefix (s [length :], quoted ) {
156+ length += len (quoted )
157+ } else {
158+ length += len (o )
159+ }
160+ }
161+ return length
162+ }
140163 }
141164
142165 edits = append (edits , source.Edit {
143166 Location : res .Location - raw .StmtLocation ,
144167 Old : oldString ,
168+ OldFunc : oldFunc ,
145169 New : strings .Join (cols , ", " ),
146170 })
147171 }
0 commit comments