2
0

result.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package executor
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // Result represents the result of executing a SQL statement.
  7. type Result struct {
  8. Columns []string // Column names
  9. ColumnTypes []string // Column types (for type inference)
  10. Rows [][]interface{} // Row data
  11. RowCount int // Number of affected/returned rows
  12. RowsAffected int64 // Number of rows affected (for INSERT/UPDATE/DELETE)
  13. LastInsertID int64 // Last insert ID (for INSERT with AUTOINCREMENT)
  14. CommandTag string // Command type (SELECT, INSERT, UPDATE, DELETE, etc.)
  15. }
  16. // NewResult creates a new empty result.
  17. func NewResult(tag string) *Result {
  18. return &Result{
  19. CommandTag: tag,
  20. Rows: make([][]interface{}, 0),
  21. }
  22. }
  23. // AddColumn adds a column to the result.
  24. func (r *Result) AddColumn(name string) {
  25. r.Columns = append(r.Columns, name)
  26. }
  27. // AddRow adds a row to the result.
  28. func (r *Result) AddRow(values ...interface{}) {
  29. r.Rows = append(r.Rows, values)
  30. r.RowCount = len(r.Rows)
  31. }
  32. // SetRowCount sets the row count (for non-SELECT queries).
  33. func (r *Result) SetRowCount(count int) {
  34. r.RowCount = count
  35. r.RowsAffected = int64(count)
  36. }
  37. // SetLastInsertID sets the last insert ID.
  38. func (r *Result) SetLastInsertID(id int64) {
  39. r.LastInsertID = id
  40. }
  41. // AddColumnWithType adds a column with its type to the result.
  42. func (r *Result) AddColumnWithType(name, colType string) {
  43. r.Columns = append(r.Columns, name)
  44. r.ColumnTypes = append(r.ColumnTypes, colType)
  45. }
  46. // GetColumnType returns the type for a column index.
  47. func (r *Result) GetColumnType(idx int) string {
  48. if idx < len(r.ColumnTypes) {
  49. return r.ColumnTypes[idx]
  50. }
  51. return "ANY"
  52. }
  53. // String returns a string representation of the result.
  54. func (r *Result) String() string {
  55. var sb strings.Builder
  56. if len(r.Columns) > 0 {
  57. // Calculate column widths
  58. widths := make([]int, len(r.Columns))
  59. for i, col := range r.Columns {
  60. widths[i] = len(col)
  61. }
  62. for _, row := range r.Rows {
  63. for i, val := range row {
  64. if i < len(widths) {
  65. w := len(fmt.Sprintf("%v", val))
  66. if w > widths[i] {
  67. widths[i] = w
  68. }
  69. }
  70. }
  71. }
  72. // Print header
  73. for i, col := range r.Columns {
  74. if i > 0 {
  75. sb.WriteString(" | ")
  76. }
  77. sb.WriteString(padRight(col, widths[i]))
  78. }
  79. sb.WriteString("\n")
  80. // Print separator
  81. for i, w := range widths {
  82. if i > 0 {
  83. sb.WriteString("-+-")
  84. }
  85. sb.WriteString(strings.Repeat("-", w))
  86. }
  87. sb.WriteString("\n")
  88. // Print rows
  89. for _, row := range r.Rows {
  90. for i, val := range row {
  91. if i > 0 {
  92. sb.WriteString(" | ")
  93. }
  94. if i < len(widths) {
  95. sb.WriteString(padRight(fmt.Sprintf("%v", val), widths[i]))
  96. }
  97. }
  98. sb.WriteString("\n")
  99. }
  100. }
  101. // Print row count
  102. sb.WriteString(fmt.Sprintf("(%d row", r.RowCount))
  103. if r.RowCount != 1 {
  104. sb.WriteString("s")
  105. }
  106. sb.WriteString(")\n")
  107. return sb.String()
  108. }
  109. func padRight(s string, width int) string {
  110. if len(s) >= width {
  111. return s
  112. }
  113. return s + strings.Repeat(" ", width-len(s))
  114. }
  115. // ToMaps converts the result to a slice of maps.
  116. func (r *Result) ToMaps() []map[string]interface{} {
  117. result := make([]map[string]interface{}, len(r.Rows))
  118. for i, row := range r.Rows {
  119. m := make(map[string]interface{})
  120. for j, col := range r.Columns {
  121. if j < len(row) {
  122. m[col] = row[j]
  123. }
  124. }
  125. result[i] = m
  126. }
  127. return result
  128. }