canonical / canonical/sqlair

Support struct access in slices in where clauses

Open
#186 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
19
Forks
10
PR merge metrics
No merged PRs in 30d

Description

Consider:

```go
type machine struct {
UUID string `db:"uuid"`
}
query, err := sqlair.Prepare(`
SELECT uuid AS &machine.uuid FROM machine;
`)
...
var machines []machine
db.Query(ctx, query).Get(&machines)
```

Now later on, we want to use the same information to update a set of columns. We now need to transform the slice of `[]machine` to `[]string` to be useful.

```go
s := make(sqlair.S, len(machines))
for i, v := range machines {
s[i] = v.uuid
}
update, err := query, err := sqlair.Prepare(`
UPDATE machine WHERE uuid IN ($S[:])
`, sqlair.S{})
...
db.Query(ctx, update, s)
```

Instead, it would be preferable to do the following:

```go
update, err := query, err := sqlair.Prepare(`
UPDATE machine WHERE uuid IN ($machines[:].uuid)
`, machines)
...
db.Query(ctx, update, machines)
```

Contributor guide

Open the contributing guide

Research direction

Start by tracing the existing handling of `$S[:]` and struct field mapping in the SQLair implementation. Verify how a `[]machine` is bound for `WHERE uuid IN (...)`, then add coverage showing that `$machines[:].uuid` accepts the example slice and produces the intended update query.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
backend, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.