11package dinosql
22
33import (
4- "bufio"
54 "errors"
65 "fmt"
76 "io/ioutil"
@@ -311,7 +310,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, src string, rewriteParameters b
311310 }
312311 edits = append (edits , expandEdits ... )
313312
314- expanded , err := editQuery (rawSQL , edits )
313+ expanded , err := source . Mutate (rawSQL , edits )
315314 if err != nil {
316315 return nil , err
317316 }
@@ -323,7 +322,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, src string, rewriteParameters b
323322 }
324323 }
325324
326- trimmed , comments , err := stripComments ( strings . TrimSpace (expanded ) )
325+ trimmed , comments , err := source . StripComments (expanded )
327326 if err != nil {
328327 return nil , err
329328 }
@@ -350,22 +349,6 @@ func rewriteNumberedParameters(refs []paramRef, raw nodes.RawStmt, sql string) (
350349 return edits , nil
351350}
352351
353- func stripComments (sql string ) (string , []string , error ) {
354- s := bufio .NewScanner (strings .NewReader (sql ))
355- var lines , comments []string
356- for s .Scan () {
357- if strings .HasPrefix (s .Text (), "-- name:" ) {
358- continue
359- }
360- if strings .HasPrefix (s .Text (), "--" ) {
361- comments = append (comments , strings .TrimPrefix (s .Text (), "--" ))
362- continue
363- }
364- lines = append (lines , s .Text ())
365- }
366- return strings .Join (lines , "\n " ), comments , s .Err ()
367- }
368-
369352func expand (qc * QueryCatalog , raw nodes.RawStmt ) ([]source.Edit , error ) {
370353 list := ast .Search (raw , func (node nodes.Node ) bool {
371354 switch node .(type ) {
@@ -486,33 +469,6 @@ func expandStmt(qc *QueryCatalog, raw nodes.RawStmt, node nodes.Node) ([]source.
486469 return edits , nil
487470}
488471
489- func editQuery (raw string , a []source.Edit ) (string , error ) {
490- if len (a ) == 0 {
491- return raw , nil
492- }
493- sort .Slice (a , func (i , j int ) bool { return a [i ].Location > a [j ].Location })
494- s := raw
495- for _ , edit := range a {
496- start := edit .Location
497- if start > len (s ) {
498- return "" , fmt .Errorf ("edit start location is out of bounds" )
499- }
500- if len (edit .New ) <= 0 {
501- return "" , fmt .Errorf ("empty edit contents" )
502- }
503- if len (edit .Old ) <= 0 {
504- return "" , fmt .Errorf ("empty edit contents" )
505- }
506- stop := edit .Location + len (edit .Old ) - 1 // Assumes edit.New is non-empty
507- if stop < len (s ) {
508- s = s [:start ] + edit .New + s [stop + 1 :]
509- } else {
510- s = s [:start ] + edit .New
511- }
512- }
513- return s , nil
514- }
515-
516472type QueryCatalog struct {
517473 catalog core.Catalog
518474 ctes map [string ]core.Table
0 commit comments