1
0

parse.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package compiler
  2. import (
  3. "errors"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "github.com/davecgh/go-spew/spew"
  8. "github.com/kyleconroy/sqlc/internal/metadata"
  9. "github.com/kyleconroy/sqlc/internal/source"
  10. "github.com/kyleconroy/sqlc/internal/sql/ast"
  11. "github.com/kyleconroy/sqlc/internal/sql/ast/pg"
  12. "github.com/kyleconroy/sqlc/internal/sql/astutils"
  13. "github.com/kyleconroy/sqlc/internal/sql/catalog"
  14. "github.com/kyleconroy/sqlc/internal/sql/rewrite"
  15. "github.com/kyleconroy/sqlc/internal/sql/validate"
  16. )
  17. var ErrUnsupportedStatementType = errors.New("parseQuery: unsupported statement type")
  18. func rewriteNumberedParameters(refs []paramRef, raw *ast.RawStmt, sql string) ([]source.Edit, error) {
  19. edits := make([]source.Edit, len(refs))
  20. for i, ref := range refs {
  21. edits[i] = source.Edit{
  22. Location: ref.ref.Location - raw.StmtLocation,
  23. Old: fmt.Sprintf("$%d", ref.ref.Number),
  24. New: "?",
  25. }
  26. }
  27. return edits, nil
  28. }
  29. func parseQuery(p Parser, c *catalog.Catalog, stmt ast.Node, src string, rewriteParameters bool) (*Query, error) {
  30. if err := validate.ParamStyle(stmt); err != nil {
  31. return nil, err
  32. }
  33. if err := validate.ParamRef(stmt); err != nil {
  34. return nil, err
  35. }
  36. raw, ok := stmt.(*ast.RawStmt)
  37. if !ok {
  38. return nil, errors.New("node is not a statement")
  39. }
  40. switch n := raw.Stmt.(type) {
  41. case *pg.SelectStmt:
  42. case *pg.DeleteStmt:
  43. case *pg.InsertStmt:
  44. if err := validate.InsertStmt(n); err != nil {
  45. return nil, err
  46. }
  47. case *pg.TruncateStmt:
  48. case *pg.UpdateStmt:
  49. default:
  50. return nil, ErrUnsupportedStatementType
  51. }
  52. spew.Dump(raw)
  53. rawSQL, err := source.Pluck(src, raw.StmtLocation, raw.StmtLen)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if rawSQL == "" {
  58. return nil, errors.New("missing semicolon at end of file")
  59. }
  60. if err := validate.FuncCall(c, raw); err != nil {
  61. return nil, err
  62. }
  63. name, cmd, err := metadata.Parse(strings.TrimSpace(rawSQL), p.CommentSyntax())
  64. if err != nil {
  65. return nil, err
  66. }
  67. if err := validate.Cmd(raw.Stmt, name, cmd); err != nil {
  68. return nil, err
  69. }
  70. raw, namedParams, edits := rewrite.NamedParameters(raw)
  71. rvs := rangeVars(raw.Stmt)
  72. refs := findParameters(raw.Stmt)
  73. if rewriteParameters {
  74. edits, err = rewriteNumberedParameters(refs, raw, rawSQL)
  75. if err != nil {
  76. return nil, err
  77. }
  78. } else {
  79. refs = uniqueParamRefs(refs)
  80. sort.Slice(refs, func(i, j int) bool { return refs[i].ref.Number < refs[j].ref.Number })
  81. }
  82. params, err := resolveCatalogRefs(c, rvs, refs, namedParams)
  83. if err != nil {
  84. return nil, err
  85. }
  86. qc, err := buildQueryCatalog(c, raw.Stmt)
  87. if err != nil {
  88. return nil, err
  89. }
  90. cols, err := outputColumns(qc, raw.Stmt)
  91. if err != nil {
  92. return nil, err
  93. }
  94. expandEdits, err := expand(qc, raw)
  95. if err != nil {
  96. return nil, err
  97. }
  98. edits = append(edits, expandEdits...)
  99. expanded, err := source.Mutate(rawSQL, edits)
  100. if err != nil {
  101. return nil, err
  102. }
  103. // If the query string was edited, make sure the syntax is valid
  104. if expanded != rawSQL {
  105. fmt.Println("expanded", expanded)
  106. if _, err := p.Parse(strings.NewReader(expanded)); err != nil {
  107. return nil, fmt.Errorf("edited query syntax is invalid: %w", err)
  108. }
  109. }
  110. trimmed, comments, err := source.StripComments(expanded)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &Query{
  115. Cmd: cmd,
  116. Comments: comments,
  117. Name: name,
  118. Params: params,
  119. Columns: cols,
  120. SQL: trimmed,
  121. }, nil
  122. }
  123. func rangeVars(root ast.Node) []*pg.RangeVar {
  124. var vars []*pg.RangeVar
  125. find := astutils.VisitorFunc(func(node ast.Node) {
  126. switch n := node.(type) {
  127. case *pg.RangeVar:
  128. vars = append(vars, n)
  129. }
  130. })
  131. astutils.Walk(find, root)
  132. return vars
  133. }
  134. func uniqueParamRefs(in []paramRef) []paramRef {
  135. m := make(map[int]struct{}, len(in))
  136. o := make([]paramRef, 0, len(in))
  137. for _, v := range in {
  138. if _, ok := m[v.ref.Number]; !ok {
  139. m[v.ref.Number] = struct{}{}
  140. o = append(o, v)
  141. }
  142. }
  143. return o
  144. }