meta.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package metadata
  2. import (
  3. "bufio"
  4. "fmt"
  5. "strings"
  6. "unicode"
  7. "github.com/sqlc-dev/sqlc/internal/source"
  8. )
  9. type CommentSyntax source.CommentSyntax
  10. type Metadata struct {
  11. Name string
  12. Cmd string
  13. Comments []string
  14. Params map[string]string
  15. Flags map[string]bool
  16. Filename string
  17. }
  18. const (
  19. CmdExec = ":exec"
  20. CmdExecResult = ":execresult"
  21. CmdExecRows = ":execrows"
  22. CmdExecLastId = ":execlastid"
  23. CmdMany = ":many"
  24. CmdOne = ":one"
  25. CmdCopyFrom = ":copyfrom"
  26. CmdBatchExec = ":batchexec"
  27. CmdBatchMany = ":batchmany"
  28. CmdBatchOne = ":batchone"
  29. )
  30. // A query name must be a valid Go identifier
  31. //
  32. // https://golang.org/ref/spec#Identifiers
  33. func validateQueryName(name string) error {
  34. if len(name) == 0 {
  35. return fmt.Errorf("invalid query name: %q", name)
  36. }
  37. for i, c := range name {
  38. isLetter := unicode.IsLetter(c) || c == '_'
  39. isDigit := unicode.IsDigit(c)
  40. if i == 0 && !isLetter {
  41. return fmt.Errorf("invalid query name %q", name)
  42. } else if !(isLetter || isDigit) {
  43. return fmt.Errorf("invalid query name %q", name)
  44. }
  45. }
  46. return nil
  47. }
  48. func ParseQueryNameAndType(t string, commentStyle CommentSyntax) (string, string, error) {
  49. for _, line := range strings.Split(t, "\n") {
  50. var prefix string
  51. if strings.HasPrefix(line, "--") {
  52. if !commentStyle.Dash {
  53. continue
  54. }
  55. prefix = "--"
  56. }
  57. if strings.HasPrefix(line, "/*") {
  58. if !commentStyle.SlashStar {
  59. continue
  60. }
  61. prefix = "/*"
  62. }
  63. if strings.HasPrefix(line, "#") {
  64. if !commentStyle.Hash {
  65. continue
  66. }
  67. prefix = "#"
  68. }
  69. if prefix == "" {
  70. continue
  71. }
  72. rest := line[len(prefix):]
  73. if !strings.HasPrefix(strings.TrimSpace(rest), "name") {
  74. continue
  75. }
  76. if !strings.Contains(rest, ":") {
  77. continue
  78. }
  79. if !strings.HasPrefix(rest, " name: ") {
  80. return "", "", fmt.Errorf("invalid metadata: %s", line)
  81. }
  82. part := strings.Split(strings.TrimSpace(line), " ")
  83. if prefix == "/*" {
  84. part = part[:len(part)-1] // removes the trailing "*/" element
  85. }
  86. if len(part) == 3 {
  87. return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
  88. }
  89. if len(part) != 4 {
  90. return "", "", fmt.Errorf("invalid query comment: %s", line)
  91. }
  92. queryName := part[2]
  93. queryType := strings.TrimSpace(part[3])
  94. switch queryType {
  95. case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
  96. default:
  97. return "", "", fmt.Errorf("invalid query type: %s", queryType)
  98. }
  99. if err := validateQueryName(queryName); err != nil {
  100. return "", "", err
  101. }
  102. return queryName, queryType, nil
  103. }
  104. return "", "", nil
  105. }
  106. func ParseParamsAndFlags(comments []string) (map[string]string, map[string]bool, error) {
  107. params := make(map[string]string)
  108. flags := make(map[string]bool)
  109. for _, line := range comments {
  110. s := bufio.NewScanner(strings.NewReader(line))
  111. s.Split(bufio.ScanWords)
  112. s.Scan()
  113. token := s.Text()
  114. if !strings.HasPrefix(token, "@") {
  115. continue
  116. }
  117. switch token {
  118. case "@param":
  119. s.Scan()
  120. name := s.Text()
  121. var rest []string
  122. for s.Scan() {
  123. paramToken := s.Text()
  124. rest = append(rest, paramToken)
  125. }
  126. params[name] = strings.Join(rest, " ")
  127. default:
  128. flags[token] = true
  129. }
  130. if s.Err() != nil {
  131. return params, flags, s.Err()
  132. }
  133. }
  134. return params, flags, nil
  135. }