returning.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 RETURNING expression. Direct
  45. // column references use the schema type and expressions use the executor's type
  46. // inference, so RETURNING metadata matches the equivalent SELECT projection.
  47. func projectionType(expr parser.Expr, schema *storage.Schema) string {
  48. return inferExprType(expr, schemaTypeResolver(schema))
  49. }
  50. // returningResult evaluates a RETURNING projection over the affected rows and
  51. // builds the result set. For INSERT/UPDATE the rows are the post-change rows;
  52. // for DELETE the caller passes the removed rows.
  53. func (e *Executor) returningResult(cols []parser.SelectColumn, schema *storage.Schema, rows []storage.Row) (*Result, error) {
  54. names, types := returningProjection(cols, schema)
  55. result := NewResult("SELECT")
  56. for i := range names {
  57. result.AddColumnWithType(names[i], types[i])
  58. }
  59. for _, row := range rows {
  60. values := make([]interface{}, 0, len(cols))
  61. for _, col := range cols {
  62. switch {
  63. case col.Star:
  64. for _, c := range schema.Columns {
  65. values = append(values, e.lookupRowColumn(row, c.Name))
  66. }
  67. case col.TableStar != "":
  68. for _, c := range schema.Columns {
  69. values = append(values, e.lookupRowColumn(row, c.Name))
  70. }
  71. default:
  72. val, err := e.evalExpr(col.Expr, row)
  73. if err != nil {
  74. return nil, err
  75. }
  76. values = append(values, val)
  77. }
  78. }
  79. result.AddRow(values...)
  80. }
  81. return result, nil
  82. }
  83. // lookupRowColumn resolves a column from a row case-insensitively.
  84. func (e *Executor) lookupRowColumn(row storage.Row, name string) interface{} {
  85. if v, ok := row[name]; ok {
  86. return v
  87. }
  88. for k, v := range row {
  89. if strings.EqualFold(k, name) {
  90. return v
  91. }
  92. }
  93. return nil
  94. }