Skip to content

Commit

Permalink
feat: add helpers for ColArrOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Apr 6, 2022
1 parent 70fa856 commit e004f5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions proto/col_arr_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
11 changes: 3 additions & 8 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit e004f5f

Please sign in to comment.