1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
package repositories
import (
"database/sql"
"strings"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"repodiff/constants"
e "repodiff/entities"
"repodiff/interactors"
"repodiff/mappers"
repoSQL "repodiff/persistence/sql"
"repodiff/utils"
)
type NullCommit struct {
originalErr error
}
func (n NullCommit) InsertCommitRows(commitRows []e.AnalyzedCommitRow) error {
return n.originalErr
}
func (n NullCommit) GetFirstSeenTimestamp(commitHashes []string, nullTimestamp e.RepoTimestamp) (map[string]e.RepoTimestamp, error) {
return nil, n.originalErr
}
func (n NullCommit) GetMostRecentCommits() ([]e.AnalyzedCommitRow, error) {
return nil, n.originalErr
}
type Commit struct {
db *sql.DB
target e.MappedDiffTarget
timestampGenerator func() e.RepoTimestamp
}
func (c Commit) WithTimestampGenerator(t func() e.RepoTimestamp) Commit {
return Commit{
db: c.db,
target: c.target,
timestampGenerator: t,
}
}
func (c Commit) InsertCommitRows(commitRows []e.AnalyzedCommitRow) error {
return errors.Wrap(
repoSQL.SingleTransactionInsert(
c.db,
`INSERT INTO project_commit (
upstream_target_id,
downstream_target_id,
timestamp,
uuid,
row_index,
commit_,
downstream_project,
author,
subject,
project_type
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
mappers.PrependMappedDiffTarget(
c.target,
mappers.CommitRowsToPersistCols(commitRows, c.timestampGenerator()),
),
),
"Error inserting rows into project_commit",
)
}
func (c Commit) GetMostRecentOuterKey() (int64, uuid.UUID, error) {
var timestamp int64
var uuidBytes []byte
err := c.db.QueryRow(
`SELECT timestamp, uuid FROM project_commit WHERE upstream_target_id = ? AND downstream_target_id = ? AND timestamp=(
SELECT MAX(timestamp) FROM project_commit WHERE upstream_target_id = ? AND downstream_target_id = ?
) LIMIT 1`,
c.target.UpstreamTarget,
c.target.DownstreamTarget,
c.target.UpstreamTarget,
c.target.DownstreamTarget,
).Scan(
×tamp,
&uuidBytes,
)
if err != nil {
return 0, constants.NullUUID(), err
}
u, err := uuid.FromBytes(uuidBytes)
if err != nil {
return 0, constants.NullUUID(), errors.Wrap(err, "Error casting string to UUID")
}
return timestamp, u, nil
}
func (c Commit) GetMostRecentCommits() ([]e.AnalyzedCommitRow, error) {
timestamp, uid, err := c.GetMostRecentOuterKey()
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
var errMapping error
var commitRows []e.AnalyzedCommitRow
var commitCursor e.AnalyzedCommitRow
errSelect := repoSQL.Select(
c.db,
func(row *sql.Rows) {
if err := interactors.ExistingErrorOr(
errMapping,
func() error {
commitCursor, err = mappers.SQLRowToCommitRow(row)
return err
},
); err != nil {
errMapping = err
return
}
commitRows = append(
commitRows,
commitCursor,
)
},
`SELECT
timestamp,
uuid,
row_index,
commit_,
downstream_project,
author,
subject,
project_type
FROM project_commit
WHERE
upstream_target_id = ?
AND downstream_target_id = ?
AND timestamp = ?
AND uuid = ?`,
c.target.UpstreamTarget,
c.target.DownstreamTarget,
timestamp,
string(uid.Bytes()),
)
if err := interactors.AnyError(errSelect, errMapping); err != nil {
return nil, err
}
return commitRows, nil
}
func (c Commit) GetFirstSeenTimestamp(commitHashes []string, nullTimestamp e.RepoTimestamp) (map[string]e.RepoTimestamp, error) {
if len(commitHashes) == 0 {
return map[string]e.RepoTimestamp{}, nil
}
commitToTimestamp := make(map[string]e.RepoTimestamp, len(commitHashes))
var commitCursor string
var timestampCursor e.RepoTimestamp
var errMapping error
errSelect := repoSQL.Select(
c.db,
func(row *sql.Rows) {
if err := interactors.ExistingErrorOr(
errMapping,
func() error {
return row.Scan(
&commitCursor,
×tampCursor,
)
},
); err != nil {
errMapping = err
return
}
commitToTimestamp[commitCursor] = timestampCursor
},
`SELECT commit_, MIN(timestamp)
FROM project_commit
WHERE upstream_target_id = ?
AND downstream_target_id = ?
AND commit_ IN(?`+strings.Repeat(",?", len(commitHashes)-1)+`)
GROUP BY commit_
`,
append(
[]interface{}{
c.target.UpstreamTarget,
c.target.DownstreamTarget,
},
asInterfaceSlice(commitHashes)...,
)...,
)
if err := interactors.AnyError(errSelect, errMapping); err != nil {
return nil, err
}
mutateEmptyValues(commitToTimestamp, commitHashes, nullTimestamp)
return commitToTimestamp, nil
}
func NewCommitRepository(target e.MappedDiffTarget) (Commit, error) {
db, err := repoSQL.GetDBConnectionPool()
return Commit{
db: db,
target: target,
timestampGenerator: utils.TimestampSeconds,
}, errors.Wrap(err, "Could not establish a database connection")
}
func NewNullObject(originalErr error) NullCommit {
return NullCommit{
originalErr: originalErr,
}
}
func mutateEmptyValues(existing map[string]e.RepoTimestamp, shouldExist []string, defaultValue e.RepoTimestamp) {
for _, key := range shouldExist {
if _, ok := existing[key]; !ok {
existing[key] = defaultValue
}
}
}
func asInterfaceSlice(strings []string) []interface{} {
casted := make([]interface{}, len(strings))
for i, s := range strings {
casted[i] = s
}
return casted
}
|