forked from tangled.org/core
this repo has no description
at master 2.4 kB view raw
1package db 2 3import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/go-git/go-git/v5/plumbing" 9 "github.com/ipfs/go-cid" 10 "tangled.org/core/appview/models" 11) 12 13func AddArtifact(e Execer, artifact models.Artifact) error { 14 _, err := e.Exec( 15 `insert or ignore into artifacts ( 16 did, 17 rkey, 18 repo_at, 19 tag, 20 created, 21 blob_cid, 22 name, 23 size, 24 mimetype 25 ) 26 values (?, ?, ?, ?, ?, ?, ?, ?, ?)`, 27 artifact.Did, 28 artifact.Rkey, 29 artifact.RepoAt, 30 artifact.Tag[:], 31 artifact.CreatedAt.Format(time.RFC3339), 32 artifact.BlobCid.String(), 33 artifact.Name, 34 artifact.Size, 35 artifact.MimeType, 36 ) 37 return err 38} 39 40func GetArtifact(e Execer, filters ...filter) ([]models.Artifact, error) { 41 var artifacts []models.Artifact 42 43 var conditions []string 44 var args []any 45 for _, filter := range filters { 46 conditions = append(conditions, filter.Condition()) 47 args = append(args, filter.Arg()...) 48 } 49 50 whereClause := "" 51 if conditions != nil { 52 whereClause = " where " + strings.Join(conditions, " and ") 53 } 54 55 query := fmt.Sprintf(`select 56 did, 57 rkey, 58 repo_at, 59 tag, 60 created, 61 blob_cid, 62 name, 63 size, 64 mimetype 65 from artifacts %s`, 66 whereClause, 67 ) 68 69 rows, err := e.Query(query, args...) 70 71 if err != nil { 72 return nil, err 73 } 74 defer rows.Close() 75 76 for rows.Next() { 77 var artifact models.Artifact 78 var createdAt string 79 var tag []byte 80 var blobCid string 81 82 if err := rows.Scan( 83 &artifact.Did, 84 &artifact.Rkey, 85 &artifact.RepoAt, 86 &tag, 87 &createdAt, 88 &blobCid, 89 &artifact.Name, 90 &artifact.Size, 91 &artifact.MimeType, 92 ); err != nil { 93 return nil, err 94 } 95 96 artifact.CreatedAt, err = time.Parse(time.RFC3339, createdAt) 97 if err != nil { 98 artifact.CreatedAt = time.Now() 99 } 100 artifact.Tag = plumbing.Hash(tag) 101 artifact.BlobCid = cid.MustParse(blobCid) 102 103 artifacts = append(artifacts, artifact) 104 } 105 106 if err := rows.Err(); err != nil { 107 return nil, err 108 } 109 110 return artifacts, nil 111} 112 113func DeleteArtifact(e Execer, filters ...filter) error { 114 var conditions []string 115 var args []any 116 for _, filter := range filters { 117 conditions = append(conditions, filter.Condition()) 118 args = append(args, filter.Arg()...) 119 } 120 121 whereClause := "" 122 if conditions != nil { 123 whereClause = " where " + strings.Join(conditions, " and ") 124 } 125 126 query := fmt.Sprintf(`delete from artifacts %s`, whereClause) 127 128 _, err := e.Exec(query, args...) 129 return err 130}