Skip to content
6 changes: 3 additions & 3 deletions internal/db/attribute_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func createAttributeValueSql(
Suffix("RETURNING id").
ToSql()
}
func (c Client) CreateAttributeValue(ctx context.Context, v *attributes.ValueCreate) (*attributes.Value, error) {
func (c Client) CreateAttributeValue(ctx context.Context, attributeId string, v *attributes.ValueCreateUpdate) (*attributes.Value, error) {
metadataJson, metadata, err := marshalCreateMetadata(v.Metadata)
if err != nil {
return nil, err
}

sql, args, err := createAttributeValueSql(
v.AttributeId,
attributeId,
v.Value,
v.Members,
metadataJson,
Expand Down Expand Up @@ -176,7 +176,7 @@ func updateAttributeValueSql(
Where(sq.Eq{"id": id}).
ToSql()
}
func (c Client) UpdateAttributeValue(ctx context.Context, id string, v *attributes.ValueUpdate) (*attributes.Value, error) {
func (c Client) UpdateAttributeValue(ctx context.Context, id string, v *attributes.ValueCreateUpdate) (*attributes.Value, error) {
prev, err := c.GetAttributeValue(ctx, id)
if err != nil {
return nil, err
Expand Down
11 changes: 9 additions & 2 deletions internal/db/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

var (
AttributeTable = tableName(TableAttributes)
NamespacesTable = tableName(TableNamespaces)
AttributeRuleTypeEnumPrefix = "ATTRIBUTE_RULE_TYPE_ENUM_"
)

Expand Down Expand Up @@ -64,8 +65,8 @@ func attributesSelect() sq.SelectBuilder {
")"+
") AS values",
).
LeftJoin(AttributeValueTable + " ON " + AttributeValueTable + ".id = " + AttributeTable + ".id").
LeftJoin(NamespacesTable + " ON " + NamespacesTable + ".id = " + AttributeTable + ".namespace_id").
LeftJoin(AttributeValueTable+" ON "+AttributeValueTable+".id = "+AttributeTable+".id").
LeftJoin(NamespacesTable+" ON "+NamespacesTable+".id = "+AttributeTable+".namespace_id").
GroupBy(tableField(AttributeTable, "id"), tableField(NamespacesTable, "name"))
}

Expand Down Expand Up @@ -171,6 +172,7 @@ func listAllAttributesSql() (string, []interface{}, error) {
From(AttributeTable).
ToSql()
}

func (c Client) ListAllAttributes(ctx context.Context) ([]*attributes.Attribute, error) {
sql, args, err := listAllAttributesSql()
rows, err := c.query(ctx, sql, args, err)
Expand All @@ -195,6 +197,7 @@ func getAttributeSql(id string) (string, []interface{}, error) {
From(AttributeTable).
ToSql()
}

func (c Client) GetAttribute(ctx context.Context, id string) (*attributes.Attribute, error) {
sql, args, err := getAttributeSql(id)
row, err := c.queryRow(ctx, sql, args, err)
Expand All @@ -218,6 +221,7 @@ func getAttributesByNamespaceSql(namespaceId string) (string, []interface{}, err
From(AttributeTable).
ToSql()
}

func (c Client) GetAttributesByNamespace(ctx context.Context, namespaceId string) ([]*attributes.Attribute, error) {
sql, args, err := getAttributesByNamespaceSql(namespaceId)

Expand All @@ -244,6 +248,7 @@ func createAttributeSql(namespaceId string, name string, rule string, metadata [
Suffix("RETURNING \"id\"").
ToSql()
}

func (c Client) CreateAttribute(ctx context.Context, attr *attributes.AttributeCreateUpdate) (*attributes.Attribute, error) {
metadataJson, metadata, err := marshalCreateMetadata(attr.Metadata)
if err != nil {
Expand Down Expand Up @@ -289,6 +294,7 @@ func updateAttributeSql(id string, name string, rule string, metadata []byte) (s
Where(sq.Eq{tableField(AttributeTable, "id"): id}).
ToSql()
}

func (c Client) UpdateAttribute(ctx context.Context, id string, attr *attributes.AttributeCreateUpdate) (*attributes.Attribute, error) {
// get attribute before updating
a, err := c.GetAttribute(ctx, id)
Expand Down Expand Up @@ -325,6 +331,7 @@ func deleteAttributeSql(id string) (string, []interface{}, error) {
Where(sq.Eq{tableField(AttributeTable, "id"): id}).
ToSql()
}

func (c Client) DeleteAttribute(ctx context.Context, id string) (*attributes.Attribute, error) {
// get attribute before deleting
a, err := c.GetAttribute(ctx, id)
Expand Down
23 changes: 13 additions & 10 deletions internal/db/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"github.com/opentdf/opentdf-v2-poc/services"
)

var NamespacesTable = tableName(TableNamespaces)

func getNamespaceSql(id string) (string, []interface{}, error) {
t := Tables.Namespaces
return newStatementBuilder().
Select("*").
From(NamespacesTable).
Where(sq.Eq{tableField(NamespacesTable, "id"): id}).
From(t.Name()).
Where(sq.Eq{t.Field("id"): id}).
ToSql()
}

Expand Down Expand Up @@ -48,9 +47,10 @@ func (c Client) GetNamespace(ctx context.Context, id string) (*namespaces.Namesp
}

func listNamespacesSql() (string, []interface{}, error) {
t := Tables.Namespaces
return newStatementBuilder().
Select("*").
From(NamespacesTable).
From(t.Name()).
ToSql()
}

Expand Down Expand Up @@ -82,8 +82,9 @@ func (c Client) ListNamespaces(ctx context.Context) ([]*namespaces.Namespace, er
}

func createNamespaceSql(name string) (string, []interface{}, error) {
t := Tables.Namespaces
return newStatementBuilder().
Insert(NamespacesTable).
Insert(t.Name()).
Columns("name").
Values(name).
Suffix("RETURNING \"id\"").
Expand Down Expand Up @@ -111,10 +112,11 @@ func (c Client) CreateNamespace(ctx context.Context, name string) (string, error
}

func updateNamespaceSql(id string, name string) (string, []interface{}, error) {
t := Tables.Namespaces
return newStatementBuilder().
Update(NamespacesTable).
Update(t.Name()).
Set("name", name).
Where(sq.Eq{tableField(NamespacesTable, "id"): id}).
Where(sq.Eq{t.Field("id"): id}).
ToSql()
}

Expand Down Expand Up @@ -143,10 +145,11 @@ func (c Client) UpdateNamespace(ctx context.Context, id string, name string) (*n
}

func deleteNamespaceSql(id string) (string, []interface{}, error) {
t := Tables.Namespaces
// TODO: handle delete cascade, dangerous deletion via special rpc, or "soft-delete" status change
return newStatementBuilder().
Delete(NamespacesTable).
Where(sq.Eq{"id": id}).
Delete(t.Name()).
Where(sq.Eq{t.Field("id"): id}).
Suffix("RETURNING \"id\"").
ToSql()
}
Expand Down
1 change: 1 addition & 0 deletions migrations/20240131000000_diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ erDiagram
}

AttributeValue {
uuid id PK
uuid namespace_id FK
uuid attribute_definition_id FK
varchar value
Expand Down
24 changes: 7 additions & 17 deletions proto/attributes/attributes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ message AttributeCreateUpdate {
];

// optional
repeated ValueCreate values = 5;
repeated ValueCreateUpdate values = 5;
}

message Value {
Expand All @@ -70,18 +70,7 @@ message Value {
repeated string members = 5;
}

message ValueCreate {
common.MetadataMutable metadata = 1;

string attribute_id = 2 [(buf.validate.field).required = true];

string value = 3;

// list of attribute values that this value is related to (attribute group)
repeated string members = 4;
}

message ValueUpdate {
message ValueCreateUpdate {
common.MetadataMutable metadata = 1;

string value = 2;
Expand Down Expand Up @@ -161,15 +150,16 @@ message ListAttributeValuesResponse {

message CreateAttributeValueRequest {
string attribute_id = 1 [(buf.validate.field).required = true];
ValueCreate value = 2 [(buf.validate.field).required = true];
ValueCreateUpdate value = 2 [(buf.validate.field).required = true];
}
message CreateAttributeValueResponse {
Value value = 1;
}

message UpdateAttributeValueRequest {
string id = 1 [(buf.validate.field).required = true];
ValueUpdate value = 2 [(buf.validate.field).required = true];
string attribute_id = 1 [(buf.validate.field).required = true];
string id = 2 [(buf.validate.field).required = true];
ValueCreateUpdate value = 3 [(buf.validate.field).required = true];
}
message UpdateAttributeValueResponse {
Value value = 1;
Expand Down Expand Up @@ -242,7 +232,7 @@ service AttributesService {

rpc UpdateAttributeValue(UpdateAttributeValueRequest) returns (UpdateAttributeValueResponse) {
option (google.api.http) = {
post: "/attributes/_/values/{id}"
post: "/attributes/{attribute_id}/values/{id}"
body: "value"
};
}
Expand Down
Loading