2023-04-13 10:16:48 +02:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
2023-09-13 06:03:25 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
2023-04-13 10:16:48 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
type ShareStore struct {
|
|
|
|
getDB GetDBFunc
|
2023-04-13 10:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAttributes implements share.Repository
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) DeleteAttributes(ctx context.Context, origin app.ID, resourceID share.ResourceID, names ...string) error {
|
|
|
|
err := s.withTx(ctx, func(tx *sql.Tx) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
DELETE FROM resources
|
|
|
|
WHERE origin = $1 AND resource_id = $2
|
|
|
|
`
|
|
|
|
args := []any{origin, resourceID}
|
|
|
|
criteria := ""
|
|
|
|
|
|
|
|
for idx, name := range names {
|
|
|
|
if idx == 0 {
|
|
|
|
criteria += " AND ("
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx != 0 {
|
|
|
|
criteria += " OR "
|
|
|
|
}
|
|
|
|
|
|
|
|
criteria += fmt.Sprintf(" name = $%d", len(args)+1)
|
|
|
|
args = append(args, name)
|
|
|
|
|
|
|
|
if idx == len(names)-1 {
|
|
|
|
criteria += " )"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query += criteria
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "executing query",
|
|
|
|
logger.F("query", query),
|
|
|
|
logger.F("args", args),
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := tx.ExecContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if affected == 0 {
|
|
|
|
return errors.WithStack(share.ErrNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteResource implements share.Repository
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) DeleteResource(ctx context.Context, origin app.ID, resourceID share.ResourceID) error {
|
|
|
|
err := s.withTx(ctx, func(tx *sql.Tx) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
DELETE FROM resources
|
|
|
|
WHERE origin = $1 AND resource_id = $2
|
|
|
|
`
|
|
|
|
|
|
|
|
args := []any{origin, resourceID}
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "executing query",
|
|
|
|
logger.F("query", query),
|
|
|
|
logger.F("args", args),
|
|
|
|
)
|
|
|
|
|
|
|
|
res, err := tx.ExecContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if affected == 0 {
|
|
|
|
return errors.WithStack(share.ErrNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindResources implements share.Repository
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) FindResources(ctx context.Context, funcs ...share.FindResourcesOptionFunc) ([]share.Resource, error) {
|
2023-04-13 10:16:48 +02:00
|
|
|
opts := share.FillFindResourcesOptions(funcs...)
|
|
|
|
|
|
|
|
var resources []share.Resource
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
err := s.withTx(ctx, func(tx *sql.Tx) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
main.origin, main.resource_id,
|
|
|
|
main.name, main.type, main.value,
|
|
|
|
main.created_at, main.updated_at
|
|
|
|
FROM resources AS main
|
|
|
|
JOIN resources AS sub ON
|
|
|
|
main.resource_id = sub.resource_id
|
|
|
|
AND main.origin = sub.origin
|
|
|
|
`
|
|
|
|
|
|
|
|
criteria := " WHERE 1 = 1"
|
|
|
|
preparedArgIndex := 1
|
|
|
|
args := make([]any, 0)
|
|
|
|
|
|
|
|
if opts.Name != nil {
|
|
|
|
criteria += fmt.Sprintf(" AND sub.name = $%d", preparedArgIndex)
|
|
|
|
args = append(args, *opts.Name)
|
|
|
|
preparedArgIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.ValueType != nil {
|
|
|
|
criteria += fmt.Sprintf(" AND sub.type = $%d", preparedArgIndex)
|
|
|
|
args = append(args, *opts.ValueType)
|
|
|
|
preparedArgIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
query += criteria
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "executing query",
|
|
|
|
logger.F("query", query),
|
|
|
|
logger.F("args", args),
|
|
|
|
)
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
logger.Error(ctx, "could not close rows", logger.E(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
indexedResources := make(map[string]*share.BaseResource)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
origin string
|
|
|
|
resourceID string
|
|
|
|
name string
|
|
|
|
valueType string
|
|
|
|
value any
|
|
|
|
updatedAt time.Time
|
|
|
|
createdAt time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := rows.Scan(&origin, &resourceID, &name, &valueType, &value, &createdAt, &updatedAt); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceKey := origin + resourceID
|
|
|
|
resource, exists := indexedResources[resourceKey]
|
|
|
|
if !exists {
|
|
|
|
resource = share.NewBaseResource(app.ID(origin), share.ResourceID(resourceID))
|
|
|
|
indexedResources[resourceKey] = resource
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := share.NewBaseAttribute(
|
|
|
|
name,
|
|
|
|
share.ValueType(valueType),
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
|
|
|
|
attr.SetCreatedAt(createdAt)
|
|
|
|
attr.SetUpdatedAt(updatedAt)
|
|
|
|
|
|
|
|
resource.SetAttribute(attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources = make([]share.Resource, 0, len(indexedResources))
|
|
|
|
for _, res := range indexedResources {
|
|
|
|
resources = append(resources, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResource implements share.Repository
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) GetResource(ctx context.Context, origin app.ID, resourceID share.ResourceID) (share.Resource, error) {
|
2023-04-13 10:16:48 +02:00
|
|
|
var (
|
|
|
|
resource *share.BaseResource
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
err = s.withTx(ctx, func(tx *sql.Tx) error {
|
|
|
|
resource, err = s.getResourceWithinTx(ctx, tx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAttributes implements share.Repository
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) UpdateAttributes(ctx context.Context, origin app.ID, resourceID share.ResourceID, attributes ...share.Attribute) (share.Resource, error) {
|
2023-04-13 10:16:48 +02:00
|
|
|
if len(attributes) == 0 {
|
|
|
|
return nil, errors.WithStack(share.ErrAttributeRequired)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resource *share.BaseResource
|
2023-09-13 06:03:25 +02:00
|
|
|
err := s.withTx(ctx, func(tx *sql.Tx) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
INSERT INTO resources (origin, resource_id, name, type, value, created_at, updated_at)
|
|
|
|
VALUES($1, $2, $3, $4, $5, $6, $6)
|
|
|
|
ON CONFLICT (origin, resource_id, name) DO UPDATE SET
|
|
|
|
type = $4, value = $5, updated_at = $6
|
|
|
|
`
|
|
|
|
|
|
|
|
stmt, err := tx.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
logger.Error(ctx, "could not close statement", logger.E(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
for _, attr := range attributes {
|
|
|
|
args := []any{
|
|
|
|
string(origin), string(resourceID),
|
|
|
|
attr.Name(), string(attr.Type()), attr.Value(),
|
|
|
|
now, now,
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "executing query",
|
|
|
|
logger.F("query", query),
|
|
|
|
logger.F("args", args),
|
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := stmt.ExecContext(ctx, args...); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
resource, err = s.getResourceWithinTx(ctx, tx, origin, resourceID)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) getResourceWithinTx(ctx context.Context, tx *sql.Tx, origin app.ID, resourceID share.ResourceID) (*share.BaseResource, error) {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
SELECT name, type, value, created_at, updated_at
|
|
|
|
FROM resources
|
|
|
|
WHERE origin = $1 AND resource_id = $2
|
|
|
|
`
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query, origin, resourceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
logger.Error(ctx, "could not close rows", logger.E(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
attributes := make([]share.Attribute, 0)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
name string
|
|
|
|
valueType string
|
|
|
|
value any
|
|
|
|
updatedAt time.Time
|
|
|
|
createdAt time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := rows.Scan(&name, &valueType, &value, &createdAt, &updatedAt); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := share.NewBaseAttribute(
|
|
|
|
name,
|
|
|
|
share.ValueType(valueType),
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
|
|
|
|
attr.SetCreatedAt(createdAt)
|
|
|
|
attr.SetUpdatedAt(updatedAt)
|
|
|
|
|
|
|
|
attributes = append(attributes, attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(attributes) == 0 {
|
|
|
|
return nil, errors.WithStack(share.ErrNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := share.NewBaseResource(origin, resourceID, attributes...)
|
|
|
|
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func (s *ShareStore) withTx(ctx context.Context, fn func(tx *sql.Tx) error) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
var db *sql.DB
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
db, err := s.getDB(ctx)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := WithTx(ctx, db, fn); err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func ensureShareTables(ctx context.Context, db *sql.DB) error {
|
|
|
|
err := WithTx(ctx, db, func(tx *sql.Tx) error {
|
2023-04-13 10:16:48 +02:00
|
|
|
query := `
|
|
|
|
CREATE TABLE IF NOT EXISTS resources (
|
|
|
|
resource_id TEXT NOT NULL,
|
|
|
|
origin TEXT NOT NULL,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
type TEXT NOT NULL,
|
|
|
|
value TEXT,
|
|
|
|
created_at TIMESTAMP NOT NULL,
|
|
|
|
updated_at TIMESTAMP NOT NULL,
|
|
|
|
UNIQUE(origin, resource_id, name) ON CONFLICT REPLACE
|
|
|
|
);
|
|
|
|
`
|
|
|
|
if _, err := tx.ExecContext(ctx, query); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
query = `
|
|
|
|
CREATE INDEX IF NOT EXISTS resource_idx ON resources (origin, resource_id, name);
|
|
|
|
`
|
|
|
|
if _, err := tx.ExecContext(ctx, query); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func NewShareStore(path string) *ShareStore {
|
|
|
|
getDB := NewGetDBFunc(path, ensureShareTables)
|
2023-04-13 10:16:48 +02:00
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
return &ShareStore{
|
2023-04-13 10:16:48 +02:00
|
|
|
getDB: getDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func NewShareStoreWithDB(db *sql.DB) *ShareStore {
|
|
|
|
getDB := NewGetDBFuncFromDB(db, ensureShareTables)
|
2023-04-13 10:16:48 +02:00
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
return &ShareStore{
|
2023-04-13 10:16:48 +02:00
|
|
|
getDB: getDB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
var _ share.Store = &ShareStore{}
|