github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/pipeline_ref.go (about)

     1  package db
     2  
     3  import (
     4  	"database/sql"
     5  	sq "github.com/Masterminds/squirrel"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/atc/db/lock"
     9  )
    10  
    11  // A lot of struct refer to a pipeline. This is a helper interface that should
    12  // embedded in those interfaces that need to refer to a pipeline. Accordingly,
    13  // implementations of those interfaces should embed "pipelineRef".
    14  type PipelineRef interface {
    15  	PipelineID() int
    16  	PipelineName() string
    17  	PipelineInstanceVars() atc.InstanceVars
    18  	PipelineRef() atc.PipelineRef
    19  	Pipeline() (Pipeline, bool, error)
    20  }
    21  
    22  type pipelineRef struct {
    23  	pipelineID           int
    24  	pipelineName         string
    25  	pipelineInstanceVars atc.InstanceVars
    26  
    27  	conn        Conn
    28  	lockFactory lock.LockFactory
    29  }
    30  
    31  func NewPipelineRef(id int, name string, instanceVars atc.InstanceVars, conn Conn, lockFactory lock.LockFactory) PipelineRef {
    32  	return pipelineRef{
    33  		pipelineID:           id,
    34  		pipelineName:         name,
    35  		pipelineInstanceVars: instanceVars,
    36  		conn:                 conn,
    37  		lockFactory:          lockFactory,
    38  	}
    39  }
    40  
    41  func (r pipelineRef) PipelineID() int {
    42  	return r.pipelineID
    43  }
    44  
    45  func (r pipelineRef) PipelineName() string {
    46  	return r.pipelineName
    47  }
    48  
    49  func (r pipelineRef) PipelineInstanceVars() atc.InstanceVars {
    50  	return r.pipelineInstanceVars
    51  }
    52  
    53  func (r pipelineRef) PipelineRef() atc.PipelineRef {
    54  	return atc.PipelineRef{
    55  		Name:         r.pipelineName,
    56  		InstanceVars: r.pipelineInstanceVars,
    57  	}
    58  }
    59  
    60  func (r pipelineRef) Pipeline() (Pipeline, bool, error) {
    61  	if r.PipelineID() == 0 {
    62  		return nil, false, nil
    63  	}
    64  
    65  	row := pipelinesQuery.
    66  		Where(sq.Eq{"p.id": r.PipelineID()}).
    67  		RunWith(r.conn).
    68  		QueryRow()
    69  
    70  	pipeline := newPipeline(r.conn, r.lockFactory)
    71  	err := scanPipeline(pipeline, row)
    72  	if err != nil {
    73  		if err == sql.ErrNoRows {
    74  			return nil, false, nil
    75  		}
    76  		return nil, false, err
    77  	}
    78  
    79  	return pipeline, true, nil
    80  }