Skip to content

Commit 9da14fd

Browse files
Merge pull request #2415 from dolthub/nathan/textArray
Support text array parameters
2 parents 06696a1 + 2378ca4 commit 9da14fd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"strconv"
1919
"testing"
2020

21+
"github.com/jackc/pgx/v5/pgtype"
22+
2123
"github.com/stretchr/testify/require"
2224
)
2325

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

0 commit comments

Comments
 (0)