returning.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package executor
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/danfragoso/pizzasql-next/pkg/parser"
  6. "github.com/danfragoso/pizzasql-next/pkg/storage"
  7. )
  8. // returningProjection expands a RETURNING column list into output column names
  9. // and types. A wildcard expands the table's schema columns; an aliased
  10. // expression uses its alias; a bare column reference uses the column name; any
  11. // other expression falls back to a synthesized name.
  12. func returningProjection(cols []parser.SelectColumn, schema *storage.Schema) ([]string, []string) {
  13. var names, types []string
  14. for i, col := range cols {
  15. switch {
  16. case col.Star:
  17. for _, c := range schema.Columns {
  18. names = append(names, c.Name)
  19. types = append(types, c.Type)
  20. }
  21. case col.TableStar != "":
  22. for _, c := range schema.Columns {
  23. names = append(names, c.Name)
  24. types = append(types, c.Type)
  25. }
  26. case col.Alias != "":
  27. names = append(names, col.Alias)
  28. types = append(types, projectionType(col.Expr, schema))
  29. default:
  30. if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  31. names = append(names, ref.Column)
  32. } else {
  33. name := parser.FormatExpr(col.Expr)
  34. if name == "" {
  35. name = fmt.Sprintf("column%d", i+1)
  36. }
  37. names = append(names, name)
  38. }
  39. types = append(types, projectionType(col.Expr, schema))
  40. }
  41. }
  42. return names, types
  43. }
  44. // projectionType resolves the declared type of a direct column reference.
  45. func projectionType(expr parser.Expr, schema *storage.Schema) string {
  46. ref, ok := expr.(*parser.ColumnRef)
  47. if !ok {
  48. return "TEXT"
  49. }
  50. for _, c := range schema.Columns {
  51. if strings.EqualFold(c.Name, ref.Column) {
  52. return c.Type
  53. }
  54. }
  55. return "TEXT"
  56. }
  57. // returningResult evaluates a RETURNING projection over the affected rows and
  58. // builds the result set. For INSERT/UPDATE the rows are the post-change rows;
  59. // for DELETE the caller passes the removed rows.
  60. func (e *Executor) returningResult(cols []parser.SelectColumn, schema *storage.Schema, rows []storage.Row) (*Result, error) {
  61. names, types := returningProjection(cols, schema)
  62. result := NewResult("SELECT")
  63. for i := range names {
  64. result.AddColumnWithType(names[i], types[i])
  65. }
  66. for _, row := range rows {
  67. values := make([]interface{}, 0, len(cols))
  68. for _, col := range cols {
  69. switch {
  70. case col.Star:
  71. for _, c := range schema.Columns {
  72. values = append(values, e.lookupRowColumn(row, c.Name))
  73. }
  74. case col.TableStar != "":
  75. for _, c := range schema.Columns {
  76. values = append(values, e.lookupRowColumn(row, c.Name))
  77. }
  78. default:
  79. val, err := e.evalExpr(col.Expr, row)
  80. if err != nil {
  81. return nil, err
  82. }
  83. values = append(values, val)
  84. }
  85. }
  86. result.AddRow(values...)
  87. }
  88. return result, nil
  89. }
  90. // lookupRowColumn resolves a column from a row case-insensitively.
  91. func (e *Executor) lookupRowColumn(row storage.Row, name string) interface{} {
  92. if v, ok := row[name]; ok {
  93. return v
  94. }
  95. for k, v := range row {
  96. if strings.EqualFold(k, name) {
  97. return v
  98. }
  99. }
  100. return nil
  101. }