2
0

oid_test.go 2.7 KB

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