-
Notifications
You must be signed in to change notification settings - Fork 4
/
volume_db.go
48 lines (41 loc) · 1.2 KB
/
volume_db.go
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
package adapter
import (
"context"
"database/sql"
"github.com/juju/errors"
"github.com/vertex-center/uuid"
"github.com/vertex-center/vertex/server/apps/containers/core/port"
"github.com/vertex-center/vertex/server/apps/containers/core/types"
"github.com/vertex-center/vertex/server/common/storage"
)
type volumeDBAdapter struct {
db storage.DB
}
func NewVolumeDBAdapter(db storage.DB) port.VolumeAdapter {
return &volumeDBAdapter{db}
}
func (a *volumeDBAdapter) GetContainerVolumes(ctx context.Context, id uuid.UUID) (types.Volumes, error) {
var volumes types.Volumes
err := a.db.Select(&volumes, `
SELECT * FROM volumes
WHERE container_id = $1
`, id)
if errors.Is(err, sql.ErrNoRows) {
return volumes, nil
}
return volumes, err
}
func (a *volumeDBAdapter) CreateVolume(ctx context.Context, vol types.Volume) error {
_, err := a.db.NamedExec(`
INSERT INTO volumes (id, container_id, type, internal_path, external_path)
VALUES (:id, :container_id, :type, :internal_path, :external_path)
`, vol)
return err
}
func (a *volumeDBAdapter) DeleteContainerVolumes(ctx context.Context, id uuid.UUID) error {
_, err := a.db.Exec(`
DELETE FROM volumes
WHERE container_id = $1
`, id)
return err
}