2
0

oid_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package pgserver
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "testing"
  6. "github.com/danfragoso/pizzasql-next/pkg/executor"
  7. "github.com/danfragoso/pizzasql-next/pkg/parser"
  8. )
  9. func TestGetOIDForTypeDatetime(t *testing.T) {
  10. c := &Connection{}
  11. cases := map[string]int32{
  12. "DATETIME": 1184,
  13. "TIMESTAMP": 1184,
  14. "BIGINT": 20,
  15. "INTEGER": 23,
  16. "INT": 23,
  17. "TEXT": 25,
  18. "UUID": 25,
  19. "BOOLEAN": 16,
  20. }
  21. for name, want := range cases {
  22. if got := c.getOIDForType(name); got != want {
  23. t.Errorf("getOIDForType(%q) = %d, want %d", name, got, want)
  24. }
  25. }
  26. }
  27. func TestSendRowDescriptionTimestampOID(t *testing.T) {
  28. c, cc := newCountingTestConnection(t)
  29. if err := c.sendRowDescription([]string{"created_at", "id"}, []string{"TIMESTAMP", "BIGINT"}); err != nil {
  30. t.Fatal(err)
  31. }
  32. if err := c.sendReadyForQuery(); err != nil {
  33. t.Fatal(err)
  34. }
  35. msgs := readAllMessages(t, cc.bytes())
  36. rd := findRowDescription(t, msgs)
  37. oids := rowDescriptionOIDs(t, rd.Data)
  38. if len(oids) != 2 {
  39. t.Fatalf("got %d columns, want 2", len(oids))
  40. }
  41. if oids[0] != 1184 {
  42. t.Errorf("timestamp OID = %d, want 1184", oids[0])
  43. }
  44. if oids[1] != 20 {
  45. t.Errorf("bigint OID = %d, want 20", oids[1])
  46. }
  47. }
  48. func TestSendResultEmptyProjectionSendsRowDescription(t *testing.T) {
  49. c, cc := newCountingTestConnection(t)
  50. result := executor.NewResult("SELECT")
  51. result.AddColumnWithType("id", "BIGINT")
  52. result.AddColumnWithType("created_at", "DATETIME")
  53. if err := c.sendResult(result, &parser.SelectStmt{}); err != nil {
  54. t.Fatal(err)
  55. }
  56. if err := c.sendReadyForQuery(); err != nil {
  57. t.Fatal(err)
  58. }
  59. msgs := readAllMessages(t, cc.bytes())
  60. rd := findRowDescription(t, msgs)
  61. oids := rowDescriptionOIDs(t, rd.Data)
  62. if len(oids) != 2 || oids[0] != 20 || oids[1] != 1184 {
  63. t.Fatalf("oids = %v, want [20 1184]", oids)
  64. }
  65. }
  66. func findRowDescription(t *testing.T, msgs []*Message) *Message {
  67. t.Helper()
  68. for _, m := range msgs {
  69. if m.Type == MsgRowDescription {
  70. return m
  71. }
  72. }
  73. t.Fatal("no RowDescription message found")
  74. return nil
  75. }
  76. func rowDescriptionOIDs(t *testing.T, data []byte) []int32 {
  77. t.Helper()
  78. if len(data) < 2 {
  79. t.Fatalf("row description too short")
  80. }
  81. count := int(binary.BigEndian.Uint16(data[:2]))
  82. pos := 2
  83. oids := make([]int32, 0, count)
  84. for i := 0; i < count; i++ {
  85. end := bytes.IndexByte(data[pos:], 0)
  86. if end < 0 {
  87. t.Fatalf("unterminated column name")
  88. }
  89. pos += end + 1 // name
  90. pos += 4 // table OID
  91. pos += 2 // column attribute number
  92. if pos+4 > len(data) {
  93. t.Fatalf("truncated row description")
  94. }
  95. oids = append(oids, int32(binary.BigEndian.Uint32(data[pos:pos+4])))
  96. pos += 4 // type OID
  97. pos += 2 // type size
  98. pos += 4 // type modifier
  99. pos += 2 // format code
  100. }
  101. return oids
  102. }