Skip to content

Commit e932c91

Browse files
author
Nathan Gabrielson
committed
Support text array parameters
1 parent 39f1f51 commit e932c91

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

server/doltgres_handler.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"regexp"
2727
"runtime/trace"
2828
"strconv"
29+
"strings"
2930
"sync"
3031
"time"
3132

@@ -413,6 +414,22 @@ func (h *DoltgresHandler) convertBindParameterToString(typ uint32, value []byte,
413414
s := u.String()
414415
bindVarString = &s
415416
}
417+
case typ == pgtype.TextArrayOID && isBinaryFormat:
418+
if value != nil {
419+
m := pgtype.NewMap()
420+
var textArray []string
421+
scanPlan := m.PlanScan(pgtype.TextArrayOID, pgtype.BinaryFormatCode, &textArray)
422+
err = scanPlan.Scan(value, &textArray)
423+
if err != nil {
424+
return nil, err
425+
}
426+
quotedArray := make([]string, len(textArray))
427+
for i, v := range textArray {
428+
quotedArray[i] = `"` + strings.ReplaceAll(v, `"`, `\"`) + `"`
429+
}
430+
formattedArray := "{" + strings.Join(quotedArray, ",") + "}"
431+
bindVarString = &formattedArray
432+
}
416433
default:
417434
// For text format or types that can handle binary-to-string conversion
418435
if err := h.pgTypeMap.Scan(typ, formatCode, value, &bindVarString); err != nil {

testing/go/binding_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package _go
1616

1717
import (
18+
"github.com/jackc/pgx/v5/pgtype"
1819
"strconv"
1920
"testing"
2021

@@ -46,3 +47,25 @@ func TestBindingWithOidZero(t *testing.T) {
4647
result := resultReader.Read()
4748
require.NoError(t, result.Err)
4849
}
50+
51+
func TestBindingWithTextArray(t *testing.T) {
52+
ctx, connection, controller := CreateServer(t, "postgres")
53+
defer controller.Stop()
54+
conn := connection.Default
55+
56+
m := pgtype.NewMap()
57+
textArray := []string{"foo", "bar"}
58+
59+
plan := m.PlanEncode(pgtype.TextArrayOID, pgtype.BinaryFormatCode, textArray)
60+
encodedArr, err := plan.Encode(textArray, nil)
61+
require.NoError(t, err)
62+
63+
args := [][]byte{encodedArr}
64+
paramOIDs := []uint32{1009}
65+
paramFormats := []int16{1}
66+
sql := "SELECT $1::text[]"
67+
68+
resultReader := conn.PgConn().ExecParams(ctx, sql, args, paramOIDs, paramFormats, nil)
69+
result := resultReader.Read()
70+
require.NoError(t, result.Err)
71+
}

0 commit comments

Comments
 (0)