@@ -11,18 +11,20 @@ import (
1111 "strings"
1212 "unicode"
1313
14+ "github.com/davecgh/go-spew/spew"
15+ pg "github.com/lfittl/pg_query_go"
16+ nodes "github.com/lfittl/pg_query_go/nodes"
17+
1418 "github.com/kyleconroy/sqlc/internal/catalog"
1519 "github.com/kyleconroy/sqlc/internal/migrations"
1620 "github.com/kyleconroy/sqlc/internal/multierr"
1721 core "github.com/kyleconroy/sqlc/internal/pg"
1822 "github.com/kyleconroy/sqlc/internal/postgres"
1923 "github.com/kyleconroy/sqlc/internal/postgresql/ast"
24+ "github.com/kyleconroy/sqlc/internal/postgresql/rewrite"
2025 "github.com/kyleconroy/sqlc/internal/postgresql/validate"
26+ "github.com/kyleconroy/sqlc/internal/source"
2127 "github.com/kyleconroy/sqlc/internal/sql/sqlpath"
22-
23- "github.com/davecgh/go-spew/spew"
24- pg "github.com/lfittl/pg_query_go"
25- nodes "github.com/lfittl/pg_query_go/nodes"
2628)
2729
2830func keepSpew () {
@@ -344,7 +346,7 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string, rewriteParameter
344346 }
345347
346348 // Re-write query AST
347- raw , namedParams , edits := rewriteNamedParameters (raw )
349+ raw , namedParams , edits := rewrite . NamedParameters (raw )
348350 rvs := rangeVars (raw .Stmt )
349351 refs := findParameters (raw .Stmt )
350352 if rewriteParameters {
@@ -403,10 +405,10 @@ func parseQuery(c core.Catalog, stmt nodes.Node, source string, rewriteParameter
403405 }, nil
404406}
405407
406- func rewriteNumberedParameters (refs []paramRef , raw nodes.RawStmt , sql string ) ([]edit , error ) {
407- edits := make ([]edit , len (refs ))
408+ func rewriteNumberedParameters (refs []paramRef , raw nodes.RawStmt , sql string ) ([]source. Edit , error ) {
409+ edits := make ([]source. Edit , len (refs ))
408410 for i , ref := range refs {
409- edits [i ] = edit {
411+ edits [i ] = source. Edit {
410412 Location : ref .ref .Location - raw .StmtLocation ,
411413 Old : fmt .Sprintf ("$%d" , ref .ref .Number ),
412414 New : "?" ,
@@ -431,13 +433,7 @@ func stripComments(sql string) (string, []string, error) {
431433 return strings .Join (lines , "\n " ), comments , s .Err ()
432434}
433435
434- type edit struct {
435- Location int
436- Old string
437- New string
438- }
439-
440- func expand (qc * QueryCatalog , raw nodes.RawStmt ) ([]edit , error ) {
436+ func expand (qc * QueryCatalog , raw nodes.RawStmt ) ([]source.Edit , error ) {
441437 list := ast .Search (raw , func (node nodes.Node ) bool {
442438 switch node .(type ) {
443439 case nodes.DeleteStmt :
@@ -452,7 +448,7 @@ func expand(qc *QueryCatalog, raw nodes.RawStmt) ([]edit, error) {
452448 if len (list .Items ) == 0 {
453449 return nil , nil
454450 }
455- var edits []edit
451+ var edits []source. Edit
456452 for _ , item := range list .Items {
457453 edit , err := expandStmt (qc , raw , item )
458454 if err != nil {
@@ -470,7 +466,7 @@ func quoteIdent(ident string) string {
470466 return ident
471467}
472468
473- func expandStmt (qc * QueryCatalog , raw nodes.RawStmt , node nodes.Node ) ([]edit , error ) {
469+ func expandStmt (qc * QueryCatalog , raw nodes.RawStmt , node nodes.Node ) ([]source. Edit , error ) {
474470 tables , err := sourceTables (qc , node )
475471 if err != nil {
476472 return nil , err
@@ -490,7 +486,7 @@ func expandStmt(qc *QueryCatalog, raw nodes.RawStmt, node nodes.Node) ([]edit, e
490486 return nil , fmt .Errorf ("outputColumns: unsupported node type: %T" , n )
491487 }
492488
493- var edits []edit
489+ var edits []source. Edit
494490 for _ , target := range targets .Items {
495491 res , ok := target .(nodes.ResTarget )
496492 if ! ok {
@@ -548,7 +544,7 @@ func expandStmt(qc *QueryCatalog, raw nodes.RawStmt, node nodes.Node) ([]edit, e
548544 for _ , p := range parts {
549545 old = append (old , quoteIdent (p ))
550546 }
551- edits = append (edits , edit {
547+ edits = append (edits , source. Edit {
552548 Location : res .Location - raw .StmtLocation ,
553549 Old : strings .Join (old , "." ),
554550 New : strings .Join (cols , ", " ),
@@ -557,7 +553,7 @@ func expandStmt(qc *QueryCatalog, raw nodes.RawStmt, node nodes.Node) ([]edit, e
557553 return edits , nil
558554}
559555
560- func editQuery (raw string , a []edit ) (string , error ) {
556+ func editQuery (raw string , a []source. Edit ) (string , error ) {
561557 if len (a ) == 0 {
562558 return raw , nil
563559 }
0 commit comments