github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/pkg/db/build_repo.go (about)

     1  // Copyright 2024 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package db
     5  
     6  import (
     7  	"context"
     8  
     9  	"cloud.google.com/go/spanner"
    10  	"github.com/google/uuid"
    11  )
    12  
    13  type BuildRepository struct {
    14  	client *spanner.Client
    15  	*genericEntityOps[Build, string]
    16  }
    17  
    18  func NewBuildRepository(client *spanner.Client) *BuildRepository {
    19  	return &BuildRepository{
    20  		client: client,
    21  		genericEntityOps: &genericEntityOps[Build, string]{
    22  			client:   client,
    23  			keyField: "ID",
    24  			table:    "Builds",
    25  		},
    26  	}
    27  }
    28  
    29  func (repo *BuildRepository) Insert(ctx context.Context, build *Build) error {
    30  	if build.ID == "" {
    31  		build.ID = uuid.NewString()
    32  	}
    33  	return repo.genericEntityOps.Insert(ctx, build)
    34  }
    35  
    36  type LastBuildParams struct {
    37  	Arch       string
    38  	TreeName   string
    39  	ConfigName string
    40  	Status     string
    41  	Commit     string
    42  }
    43  
    44  func (repo *BuildRepository) LastBuiltTree(ctx context.Context, params *LastBuildParams) (*Build, error) {
    45  	stmt := spanner.Statement{
    46  		SQL:    "SELECT * FROM `Builds` WHERE 1=1",
    47  		Params: map[string]interface{}{},
    48  	}
    49  	if params.Arch != "" {
    50  		stmt.SQL += " AND `Arch` = @arch"
    51  		stmt.Params["arch"] = params.Arch
    52  	}
    53  	if params.TreeName != "" {
    54  		stmt.SQL += " AND `TreeName` = @tree"
    55  		stmt.Params["tree"] = params.TreeName
    56  	}
    57  	if params.ConfigName != "" {
    58  		stmt.SQL += " AND `ConfigName` = @config"
    59  		stmt.Params["config"] = params.ConfigName
    60  	}
    61  	if params.Status != "" {
    62  		stmt.SQL += " AND `Status` = @status"
    63  		stmt.Params["status"] = params.Status
    64  	}
    65  	if params.Commit != "" {
    66  		stmt.SQL += " AND `CommitHash` = @commit"
    67  		stmt.Params["commit"] = params.Commit
    68  	}
    69  	stmt.SQL += " ORDER BY `CommitDate` DESC LIMIT 1"
    70  	return readEntity[Build](ctx, repo.client.Single(), stmt)
    71  }