<commit_before>package db

import (
	"context"
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"time"

	sq "github.com/Masterminds/squirrel"
	"github.com/lib/pq"

	"github.com/concourse/concourse/atc"
	"github.com/concourse/concourse/atc/db/lock"
)

var ErrPinnedThroughConfig = errors.New("resource is pinned through config")

const CheckBuildName = "check"

type CausalityDirection string

const (
	CausalityDownstream CausalityDirection = "downstream"
	CausalityUpstream   CausalityDirection = "upstream"

	downStreamCausalityQuery = `
WITH RECURSIVE build_ids AS (
		SELECT DISTINCT i.build_id
		FROM build_resource_config_version_inputs i
		WHERE i.resource_id=$1 AND i.version_md5=$2
	UNION
		SELECT bp.to_build_id AS build_id
		FROM build_ids bi
		INNER JOIN build_pipes bp ON bi.build_id = bp.from_build_id
		INNER JOIN build_resource_config_version_inputs i ON i.build_id = bi.build_id
		WHERE i.resource_id!=$1
)
`

	upStreamCausalityQuery = `
WITH RECURSIVE build_ids AS (
		SELECT DISTINCT o.build_id
		FROM build_resource_config_version_outputs o
		WHERE o.resource_id=$1 AND o.version_md5=$2
	UNION
		SELECT bp.from_build_id AS build_id
		FROM build_ids bi
		INNER JOIN build_pipes bp ON bi.build_id = bp.to_build_id
		INNER JOIN build_resource_config_version_inputs i ON i.build_id = bi.build_id
		WHERE i.resource_id!=$1
)
`
)

//counterfeiter:generate . Resource
type Resource interface {
	PipelineRef

	ID() int
	Name() string
	Public() bool
	TeamID() int
	TeamName() string
	Type() string
	Source() atc.Source
	CheckEvery() *atc.CheckEvery
	CheckTimeout() string
	LastCheckStartTime() time.Time
	LastCheckEndTime() time.Time
	Tags() atc.Tags
	WebhookToken() string
	Config() atc.ResourceConfig
	ConfigPinnedVersion() atc.Version
	APIPinnedVersion() atc.Version
	PinComment() string
	SetPinComment(string) error<commit_msg>B: switch to version string base causality<commit_after>package db

import (
	"context"
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"time"

	sq "github.com/Masterminds/squirrel"
	"github.com/lib/pq"

	"github.com/concourse/concourse/atc"
	"github.com/concourse/concourse/atc/db/lock"
)

var ErrPinnedThroughConfig = errors.New("resource is pinned through config")

const CheckBuildName = "check"

type CausalityDirection string

const (
	CausalityDownstream CausalityDirection = "downstream"
	CausalityUpstream   CausalityDirection = "upstream"

	downStreamCausalityQuery = `
WITH RECURSIVE build_ids AS (
		SELECT DISTINCT i.build_id
		FROM build_resource_config_version_inputs i
		WHERE i.resource_id=$1 AND i.version_md5=$2
	UNION
		SELECT i.build_id
		FROM build_ids bi
		INNER JOIN build_resource_config_version_outputs o ON o.build_id = bi.build_id
		INNER JOIN build_resource_config_version_inputs i ON i.resource_id = o.resource_id AND i.version_md5 = o.version_md5
		WHERE i.resource_id!=$1
)
`

	upStreamCausalityQuery = `
WITH RECURSIVE build_ids AS (
		SELECT DISTINCT o.build_id
		FROM build_resource_config_version_outputs o
		WHERE o.resource_id=$1 AND o.version_md5=$2
	UNION
		SELECT o.build_id
		FROM build_ids bi
		INNER JOIN build_resource_config_version_inputs i ON i.build_id = bi.build_id
		INNER JOIN build_resource_config_version_outputs o ON o.resource_id = i.resource_id AND o.version_md5 = i.version_md5
		WHERE i.resource_id!=$1
)
`
)

//counterfeiter:generate . Resource
type Resource interface {
	PipelineRef

	ID() int
	Name() string
	Public() bool
	TeamID() int
	TeamName() string
	Type() string
	Source() atc.Source
	CheckEvery() *atc.CheckEvery
	CheckTimeout() string
	LastCheckStartTime() time.Time
	LastCheckEndTime() time.Time
	Tags() atc.Tags
	WebhookToken() string
	Config() atc.ResourceConfig
	ConfigPinnedVersion() atc.Version
	APIPinnedVersion() atc.Version
	PinComment() string
	SetPinComment(string) error
{'fraction_non_alphanumeric': 0.08448862150143865, 'fraction_numerical': 0.0052314935914203504, 'mean_word_length': 5.079491255961845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2711754', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1353, 'n_words': 317}