go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/spanutil/util.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package spanutil
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"reflect"
    21  	"text/template"
    22  
    23  	"cloud.google.com/go/spanner"
    24  	"github.com/golang/protobuf/proto"
    25  	"google.golang.org/api/iterator"
    26  
    27  	"go.chromium.org/luci/server/span"
    28  )
    29  
    30  // ErrNoResults is an error returned when a query unexpectedly has no results.
    31  var ErrNoResults = iterator.Done
    32  
    33  // This file implements utility functions that make spanner API slightly easier
    34  // to use.
    35  
    36  func slices(m map[string]any) (keys []string, values []any) {
    37  	keys = make([]string, 0, len(m))
    38  	values = make([]any, 0, len(m))
    39  	for k, v := range m {
    40  		keys = append(keys, k)
    41  		values = append(values, v)
    42  	}
    43  	return
    44  }
    45  
    46  // ReadRow reads a single row from the database and reads its values.
    47  // ptrMap must map from column names to pointers where the values will be
    48  // written.
    49  func ReadRow(ctx context.Context, table string, key spanner.Key, ptrMap map[string]any) error {
    50  	columns, ptrs := slices(ptrMap)
    51  	row, err := span.ReadRow(ctx, table, key, columns)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	return FromSpanner(row, ptrs...)
    57  }
    58  
    59  // QueryFirstRow executes a query, reads the first row into ptrs and stops the
    60  // iterator. Returns ErrNoResults if the query does not return at least one row.
    61  func QueryFirstRow(ctx context.Context, st spanner.Statement, ptrs ...any) error {
    62  	st.Params = ToSpannerMap(st.Params)
    63  	it := span.Query(ctx, st)
    64  	defer it.Stop()
    65  	row, err := it.Next()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	return FromSpanner(row, ptrs...)
    70  }
    71  
    72  // Query executes a query.
    73  // Ensures st.Params are Spanner-compatible by modifying st.Params in place.
    74  func Query(ctx context.Context, st spanner.Statement, fn func(row *spanner.Row) error) error {
    75  	st.Params = ToSpannerMap(st.Params)
    76  	return span.Query(ctx, st).Do(fn)
    77  }
    78  
    79  func isMessageNil(m proto.Message) bool {
    80  	return reflect.ValueOf(m).IsNil()
    81  }
    82  
    83  // GenerateStatement generates a spanner statement from a text template.
    84  func GenerateStatement(tmpl *template.Template, input any) (spanner.Statement, error) {
    85  	sql := &bytes.Buffer{}
    86  	err := tmpl.Execute(sql, input)
    87  	if err != nil {
    88  		return spanner.Statement{}, err
    89  	}
    90  	return spanner.NewStatement(sql.String()), nil
    91  }