summaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/v5/rows.go
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2023-06-19 09:56:15 +0200
committerLibravatar GitHub <noreply@github.com>2023-06-19 09:56:15 +0200
commit70357a770f7e84eed1d3d6a0dfc2ce9f30e5c5be (patch)
tree7462b5e97a94a874b488404e2029fd182e9ae030 /vendor/github.com/jackc/pgx/v5/rows.go
parent[chore]: Bump golang.org/x/net from 0.10.0 to 0.11.0 (#1909) (diff)
downloadgotosocial-70357a770f7e84eed1d3d6a0dfc2ce9f30e5c5be.tar.xz
[chore]: Bump github.com/jackc/pgx/v5 from 5.3.1 to 5.4.1 (#1907)
Bumps [github.com/jackc/pgx/v5](https://github.com/jackc/pgx) from 5.3.1 to 5.4.1. - [Changelog](https://github.com/jackc/pgx/blob/master/CHANGELOG.md) - [Commits](https://github.com/jackc/pgx/compare/v5.3.1...v5.4.1) --- updated-dependencies: - dependency-name: github.com/jackc/pgx/v5 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/jackc/pgx/v5/rows.go')
-rw-r--r--vendor/github.com/jackc/pgx/v5/rows.go47
1 files changed, 37 insertions, 10 deletions
diff --git a/vendor/github.com/jackc/pgx/v5/rows.go b/vendor/github.com/jackc/pgx/v5/rows.go
index ffe739b02..cdd72a25f 100644
--- a/vendor/github.com/jackc/pgx/v5/rows.go
+++ b/vendor/github.com/jackc/pgx/v5/rows.go
@@ -28,12 +28,16 @@ type Rows interface {
// to call Close after rows is already closed.
Close()
- // Err returns any error that occurred while reading.
+ // Err returns any error that occurred while reading. Err must only be called after the Rows is closed (either by
+ // calling Close or by Next returning false). If it is called early it may return nil even if there was an error
+ // executing the query.
Err() error
// CommandTag returns the command tag from this query. It is only available after Rows is closed.
CommandTag() pgconn.CommandTag
+ // FieldDescriptions returns the field descriptions of the columns. It may return nil. In particular this can occur
+ // when there was an error executing the query.
FieldDescriptions() []pgconn.FieldDescription
// Next prepares the next row for reading. It returns true if there is another
@@ -533,13 +537,11 @@ func (rs *positionalStructRowScanner) appendScanTargets(dstElemValue reflect.Val
for i := 0; i < dstElemType.NumField(); i++ {
sf := dstElemType.Field(i)
- if sf.PkgPath == "" {
- // Handle anonymous struct embedding, but do not try to handle embedded pointers.
- if sf.Anonymous && sf.Type.Kind() == reflect.Struct {
- scanTargets = rs.appendScanTargets(dstElemValue.Field(i), scanTargets)
- } else {
- scanTargets = append(scanTargets, dstElemValue.Field(i).Addr().Interface())
- }
+ // Handle anonymous struct embedding, but do not try to handle embedded pointers.
+ if sf.Anonymous && sf.Type.Kind() == reflect.Struct {
+ scanTargets = rs.appendScanTargets(dstElemValue.Field(i), scanTargets)
+ } else if sf.PkgPath == "" {
+ scanTargets = append(scanTargets, dstElemValue.Field(i).Addr().Interface())
}
}
@@ -565,8 +567,28 @@ func RowToAddrOfStructByName[T any](row CollectableRow) (*T, error) {
return &value, err
}
+// RowToStructByNameLax returns a T scanned from row. T must be a struct. T must have greater than or equal number of named public
+// fields as row has fields. The row and T fields will by matched by name. The match is case-insensitive. The database
+// column name can be overridden with a "db" struct tag. If the "db" struct tag is "-" then the field will be ignored.
+func RowToStructByNameLax[T any](row CollectableRow) (T, error) {
+ var value T
+ err := row.Scan(&namedStructRowScanner{ptrToStruct: &value, lax: true})
+ return value, err
+}
+
+// RowToAddrOfStructByNameLax returns the address of a T scanned from row. T must be a struct. T must have greater than or
+// equal number of named public fields as row has fields. The row and T fields will by matched by name. The match is
+// case-insensitive. The database column name can be overridden with a "db" struct tag. If the "db" struct tag is "-"
+// then the field will be ignored.
+func RowToAddrOfStructByNameLax[T any](row CollectableRow) (*T, error) {
+ var value T
+ err := row.Scan(&namedStructRowScanner{ptrToStruct: &value, lax: true})
+ return &value, err
+}
+
type namedStructRowScanner struct {
ptrToStruct any
+ lax bool
}
func (rs *namedStructRowScanner) ScanRow(rows Rows) error {
@@ -578,7 +600,6 @@ func (rs *namedStructRowScanner) ScanRow(rows Rows) error {
dstElemValue := dstValue.Elem()
scanTargets, err := rs.appendScanTargets(dstElemValue, nil, rows.FieldDescriptions())
-
if err != nil {
return err
}
@@ -638,7 +659,13 @@ func (rs *namedStructRowScanner) appendScanTargets(dstElemValue reflect.Value, s
colName = sf.Name
}
fpos := fieldPosByName(fldDescs, colName)
- if fpos == -1 || fpos >= len(scanTargets) {
+ if fpos == -1 {
+ if rs.lax {
+ continue
+ }
+ return nil, fmt.Errorf("cannot find field %s in returned row", colName)
+ }
+ if fpos >= len(scanTargets) && !rs.lax {
return nil, fmt.Errorf("cannot find field %s in returned row", colName)
}
scanTargets[fpos] = dstElemValue.Field(i).Addr().Interface()