diff --git a/README.md b/README.md index 84cf1b2d..c520514e 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,25 @@ q := ch.Query{ * Tuple(T1, T2, ..., Tn) * Nullable(T) +## Generics + +### ArrayOf + +Generic for `Array(T)` + +```go +// Array(String) +arr := proto.ArrayOf[string](new(proto.ColStr)) +// Or +arr := new(proto.ColStr).Array() +q := ch.Query{ + Body: "SELECT ['foo', 'bar', 'baz']::Array(String) as v", + Result: arr.Results("v"), +} +// Do ... +arr.Row(0) // ["foo", "bar", "baz"] +``` + ## TODO - [ ] Connection pools - [ ] Improved i/o timeout handling diff --git a/proto/col_arr_of.go b/proto/col_arr_of.go index ba432130..92ed1d10 100644 --- a/proto/col_arr_of.go +++ b/proto/col_arr_of.go @@ -97,3 +97,13 @@ func (c *ColArrOf[T]) AppendArr(v [][]T) { c.Append(e) } } + +// Result for current column. +func (c *ColArrOf[T]) Result(column string) ResultColumn { + return ResultColumn{Name: column, Data: c} +} + +// Results return Results containing single column. +func (c *ColArrOf[T]) Results(column string) Results { + return Results{c.Result(column)} +} diff --git a/query_test.go b/query_test.go index bce5d0e6..9d6dee6a 100644 --- a/query_test.go +++ b/query_test.go @@ -651,15 +651,10 @@ func TestClient_Query(t *testing.T) { }) t.Run("SelectArrayOf", func(t *testing.T) { t.Parallel() - arr := proto.ArrayOf[string](new(proto.ColStr)) + arr := new(proto.ColStr).Array() selectArr := Query{ - Body: "SELECT ['foo', 'bar', 'baz']::Array(String) as v", - Result: proto.Results{ - { - Name: "v", - Data: arr, - }, - }, + Body: "SELECT ['foo', 'bar', 'baz']::Array(String) as v", + Result: arr.Results("v"), } require.NoError(t, Conn(t).Do(ctx, selectArr)) require.Equal(t, 1, arr.Rows())