extended_query_builder.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package pgx
  2. import (
  3. "fmt"
  4. "github.com/jackc/pgx/v5/pgconn"
  5. "github.com/jackc/pgx/v5/pgtype"
  6. )
  7. // ExtendedQueryBuilder is used to choose the parameter formats, to format the parameters and to choose the result
  8. // formats for an extended query.
  9. type ExtendedQueryBuilder struct {
  10. ParamValues [][]byte
  11. paramValueBytes []byte
  12. ParamFormats []int16
  13. ResultFormats []int16
  14. }
  15. // Build sets ParamValues, ParamFormats, and ResultFormats for use with *PgConn.ExecParams or *PgConn.ExecPrepared. If
  16. // sd is nil then QueryExecModeExec behavior will be used.
  17. func (eqb *ExtendedQueryBuilder) Build(m *pgtype.Map, sd *pgconn.StatementDescription, args []any) error {
  18. eqb.reset()
  19. if sd == nil {
  20. for i := range args {
  21. err := eqb.appendParam(m, 0, pgtype.TextFormatCode, args[i])
  22. if err != nil {
  23. err = fmt.Errorf("failed to encode args[%d]: %w", i, err)
  24. return err
  25. }
  26. }
  27. return nil
  28. }
  29. if len(sd.ParamOIDs) != len(args) {
  30. return fmt.Errorf("mismatched param and argument count")
  31. }
  32. for i := range args {
  33. err := eqb.appendParam(m, sd.ParamOIDs[i], -1, args[i])
  34. if err != nil {
  35. err = fmt.Errorf("failed to encode args[%d]: %w", i, err)
  36. return err
  37. }
  38. }
  39. for i := range sd.Fields {
  40. eqb.appendResultFormat(m.FormatCodeForOID(sd.Fields[i].DataTypeOID))
  41. }
  42. return nil
  43. }
  44. // appendParam appends a parameter to the query. format may be -1 to automatically choose the format. If arg is nil it
  45. // must be an untyped nil.
  46. func (eqb *ExtendedQueryBuilder) appendParam(m *pgtype.Map, oid uint32, format int16, arg any) error {
  47. if format == -1 {
  48. preferredFormat := eqb.chooseParameterFormatCode(m, oid, arg)
  49. preferredErr := eqb.appendParam(m, oid, preferredFormat, arg)
  50. if preferredErr == nil {
  51. return nil
  52. }
  53. var otherFormat int16
  54. if preferredFormat == TextFormatCode {
  55. otherFormat = BinaryFormatCode
  56. } else {
  57. otherFormat = TextFormatCode
  58. }
  59. otherErr := eqb.appendParam(m, oid, otherFormat, arg)
  60. if otherErr == nil {
  61. return nil
  62. }
  63. return preferredErr // return the error from the preferred format
  64. }
  65. v, err := eqb.encodeExtendedParamValue(m, oid, format, arg)
  66. if err != nil {
  67. return err
  68. }
  69. eqb.ParamFormats = append(eqb.ParamFormats, format)
  70. eqb.ParamValues = append(eqb.ParamValues, v)
  71. return nil
  72. }
  73. // appendResultFormat appends a result format to the query.
  74. func (eqb *ExtendedQueryBuilder) appendResultFormat(format int16) {
  75. eqb.ResultFormats = append(eqb.ResultFormats, format)
  76. }
  77. // reset readies eqb to build another query.
  78. func (eqb *ExtendedQueryBuilder) reset() {
  79. eqb.ParamValues = eqb.ParamValues[0:0]
  80. eqb.paramValueBytes = eqb.paramValueBytes[0:0]
  81. eqb.ParamFormats = eqb.ParamFormats[0:0]
  82. eqb.ResultFormats = eqb.ResultFormats[0:0]
  83. if cap(eqb.ParamValues) > 64 {
  84. eqb.ParamValues = make([][]byte, 0, 64)
  85. }
  86. if cap(eqb.paramValueBytes) > 256 {
  87. eqb.paramValueBytes = make([]byte, 0, 256)
  88. }
  89. if cap(eqb.ParamFormats) > 64 {
  90. eqb.ParamFormats = make([]int16, 0, 64)
  91. }
  92. if cap(eqb.ResultFormats) > 64 {
  93. eqb.ResultFormats = make([]int16, 0, 64)
  94. }
  95. }
  96. func (eqb *ExtendedQueryBuilder) encodeExtendedParamValue(m *pgtype.Map, oid uint32, formatCode int16, arg any) ([]byte, error) {
  97. if eqb.paramValueBytes == nil {
  98. eqb.paramValueBytes = make([]byte, 0, 128)
  99. }
  100. pos := len(eqb.paramValueBytes)
  101. buf, err := m.Encode(oid, formatCode, arg, eqb.paramValueBytes)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if buf == nil {
  106. return nil, nil
  107. }
  108. eqb.paramValueBytes = buf
  109. return eqb.paramValueBytes[pos:], nil
  110. }
  111. // chooseParameterFormatCode determines the correct format code for an
  112. // argument to a prepared statement. It defaults to TextFormatCode if no
  113. // determination can be made.
  114. func (eqb *ExtendedQueryBuilder) chooseParameterFormatCode(m *pgtype.Map, oid uint32, arg any) int16 {
  115. switch arg.(type) {
  116. case string, *string:
  117. return TextFormatCode
  118. }
  119. return m.FormatCodeForOID(oid)
  120. }