1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-18 08:20:36 +00:00

vai: do not discard closing statement errors (#462)

This commit is contained in:
Silvio Moioli
2025-02-07 08:31:08 +01:00
committed by GitHub
parent 772dc7577e
commit 9139e492e0
2 changed files with 23 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ package informer
import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
@@ -147,7 +148,7 @@ func (i *Indexer) AfterUpsert(key string, obj any, tx transaction.Client) error
/* Satisfy cache.Indexer */
// Index returns a list of items that match the given object on the index function
func (i *Indexer) Index(indexName string, obj any) ([]any, error) {
func (i *Indexer) Index(indexName string, obj any) (result []any, err error) {
i.indexersLock.RLock()
defer i.indexersLock.RUnlock()
indexFunc := i.indexers[indexName]
@@ -173,7 +174,13 @@ func (i *Indexer) Index(indexName string, obj any) ([]any, error) {
// HACK: sql.Statement.Query does not allow to pass slices in as of go 1.19 - create an ad-hoc statement
query := fmt.Sprintf(selectQueryFmt, db.Sanitize(i.GetName()), strings.Repeat(", ?", len(values)-1))
stmt := i.Prepare(query)
defer i.CloseStmt(stmt)
defer func() {
cerr := i.CloseStmt(stmt)
if cerr != nil {
err = errors.Join(err, &db.QueryError{QueryString: query, Err: cerr})
}
}()
// HACK: Query will accept []any but not []string
params := []any{indexName}
for _, value := range values {

View File

@@ -454,13 +454,17 @@ func (l *ListOptionIndexer) constructQuery(lo ListOptions, partitions []partitio
return queryInfo, nil
}
func (l *ListOptionIndexer) executeQuery(ctx context.Context, queryInfo *QueryInfo) (*unstructured.UnstructuredList, int, string, error) {
func (l *ListOptionIndexer) executeQuery(ctx context.Context, queryInfo *QueryInfo) (result *unstructured.UnstructuredList, total int, token string, err error) {
stmt := l.Prepare(queryInfo.query)
defer l.CloseStmt(stmt)
defer func() {
cerr := l.CloseStmt(stmt)
if cerr != nil {
err = errors.Join(err, &db.QueryError{QueryString: queryInfo.query, Err: cerr})
}
}()
var items []any
var total int
err := l.WithTransaction(ctx, false, func(tx transaction.Client) error {
err = l.WithTransaction(ctx, false, func(tx transaction.Client) error {
txStmt := tx.Stmt(stmt)
rows, err := txStmt.QueryContext(ctx, queryInfo.params...)
if err != nil {
@@ -474,7 +478,12 @@ func (l *ListOptionIndexer) executeQuery(ctx context.Context, queryInfo *QueryIn
total = len(items)
if queryInfo.countQuery != "" {
countStmt := l.Prepare(queryInfo.countQuery)
defer l.CloseStmt(countStmt)
defer func() {
cerr := l.CloseStmt(countStmt)
if cerr != nil {
err = errors.Join(err, &db.QueryError{QueryString: queryInfo.countQuery, Err: cerr})
}
}()
txStmt := tx.Stmt(countStmt)
rows, err := txStmt.QueryContext(ctx, queryInfo.countParams...)
if err != nil {