diff --git a/internal/service/alarms/internal/db/repo/alarms_repository.go b/internal/service/alarms/internal/db/repo/alarms_repository.go index 49be1c4b..e7e39706 100644 --- a/internal/service/alarms/internal/db/repo/alarms_repository.go +++ b/internal/service/alarms/internal/db/repo/alarms_repository.go @@ -21,7 +21,7 @@ type AlarmsRepository struct { // GetAlarmEventRecordWithUuid grabs a row of alarm_event_record using uuid func (ar *AlarmsRepository) GetAlarmEventRecordWithUuid(ctx context.Context, uuid uuid.UUID) (*models.AlarmEventRecord, error) { - return utils.Find[models.AlarmEventRecord](ctx, ar.Db, uuid, nil) + return utils.Find[models.AlarmEventRecord](ctx, ar.Db, uuid) } // DeleteAlarmDictionariesNotIn deletes all alarm dictionaries that are not in the list of resource type IDs diff --git a/internal/service/common/utils/repository.go b/internal/service/common/utils/repository.go index de626388..9565a632 100644 --- a/internal/service/common/utils/repository.go +++ b/internal/service/common/utils/repository.go @@ -25,16 +25,16 @@ var ErrNotFound = errors.New("record not found") // Find retrieves a specific tuple from the database table specified. If no record is found ErrNotFound is returned // as an error; otherwise a pointer to the stored record is returned or a generic error on failure. -func Find[T db.Model](ctx context.Context, db *pgxpool.Pool, uuid uuid.UUID, columns []any) (*T, error) { +func Find[T db.Model](ctx context.Context, db *pgxpool.Pool, uuid uuid.UUID, fields ...string) (*T, error) { // Build sql query var record T - if columns == nil { - tags := GetAllDBTagsFromStruct(record) - columns = tags.Columns() + tags := GetAllDBTagsFromStruct(record) + if len(fields) > 0 { + tags = GetDBTagsFromStructFields(record, fields...) } sql, args, err := psql.Select( - sm.Columns(columns...), + sm.Columns(tags.Columns()...), sm.From(record.TableName()), sm.Where(psql.Quote(record.PrimaryKey()).EQ(psql.Arg(uuid))), ).Build() @@ -61,8 +61,8 @@ func Find[T db.Model](ctx context.Context, db *pgxpool.Pool, uuid uuid.UUID, col // FindAll retrieves all tuples from the database table specified. If no records are found then an empty array is // returned. -func FindAll[T db.Model](ctx context.Context, db *pgxpool.Pool, columns []any) ([]T, error) { - return Search[T](ctx, db, nil, columns) +func FindAll[T db.Model](ctx context.Context, db *pgxpool.Pool, fields ...string) ([]T, error) { + return Search[T](ctx, db, nil, fields...) } // Delete deletes a specific tuple from the database table specified given an expression for Where clause @@ -89,16 +89,16 @@ func Delete[T db.Model](ctx context.Context, db *pgxpool.Pool, expr psql.Express // Search retrieves a tuple from the database using arbitrary column values. If no record is found an empty array // is returned. -func Search[T db.Model](ctx context.Context, db *pgxpool.Pool, expression bob.Expression, columns []any) ([]T, error) { +func Search[T db.Model](ctx context.Context, db *pgxpool.Pool, expression bob.Expression, fields ...string) ([]T, error) { // Build sql query var record T - if columns == nil { - tags := GetAllDBTagsFromStruct(record) - columns = tags.Columns() + tags := GetAllDBTagsFromStruct(record) + if len(fields) > 0 { + tags = GetDBTagsFromStructFields(record, fields...) } params := []bob.Mod[*dialect.SelectQuery]{ - sm.Columns(columns...), + sm.Columns(tags.Columns()...), sm.From(record.TableName()), } @@ -186,7 +186,10 @@ func Create[T db.Model](ctx context.Context, db *pgxpool.Pool, record T) (*T, er // will contain all columns. func Update[T db.Model](ctx context.Context, db *pgxpool.Pool, record T, uuid uuid.UUID, fields ...string) (*T, error) { all := GetAllDBTagsFromStruct(record) - tags := GetDBTagsFromStructFields(record, fields...) + tags := all + if len(fields) > 0 { + tags = GetDBTagsFromStructFields(record, fields...) + } // Set up the arguments to the call to psql.Update(...) by using an array because there's no obvious way to add // multiple Set(..) operation without having to add them one at a time separately. diff --git a/internal/service/resources/db/repo/repository.go b/internal/service/resources/db/repo/repository.go index 524a803b..ba0ea503 100644 --- a/internal/service/resources/db/repo/repository.go +++ b/internal/service/resources/db/repo/repository.go @@ -18,22 +18,22 @@ type ResourcesRepository struct { // GetDeploymentManagers retrieves all DeploymentManager tuples or returns an empty array if no tuples are found func (r *ResourcesRepository) GetDeploymentManagers(ctx context.Context) ([]models.DeploymentManager, error) { - return utils.FindAll[models.DeploymentManager](ctx, r.Db, nil) + return utils.FindAll[models.DeploymentManager](ctx, r.Db) } // GetDeploymentManager retrieves a specific DeploymentManager tuple or returns nil if not found func (r *ResourcesRepository) GetDeploymentManager(ctx context.Context, id uuid.UUID) (*models.DeploymentManager, error) { - return utils.Find[models.DeploymentManager](ctx, r.Db, id, nil) + return utils.Find[models.DeploymentManager](ctx, r.Db, id) } // GetSubscriptions retrieves all Subscription tuples or returns an empty array if no tuples are found func (r *ResourcesRepository) GetSubscriptions(ctx context.Context) ([]models.Subscription, error) { - return utils.FindAll[models.Subscription](ctx, r.Db, nil) + return utils.FindAll[models.Subscription](ctx, r.Db) } // GetSubscription retrieves a specific Subscription tuple or returns nil if not found func (r *ResourcesRepository) GetSubscription(ctx context.Context, id uuid.UUID) (*models.Subscription, error) { - return utils.Find[models.Subscription](ctx, r.Db, id, nil) + return utils.Find[models.Subscription](ctx, r.Db, id) } // DeleteSubscription deletes a Subscription tuple. The caller should ensure that it exists prior to calling this. @@ -49,22 +49,22 @@ func (r *ResourcesRepository) CreateSubscription(ctx context.Context, subscripti // GetResourceTypes retrieves all ResourceType tuples or returns an empty array if no tuples are found func (r *ResourcesRepository) GetResourceTypes(ctx context.Context) ([]models.ResourceType, error) { - return utils.FindAll[models.ResourceType](ctx, r.Db, nil) + return utils.FindAll[models.ResourceType](ctx, r.Db) } // GetResourceType retrieves a specific ResourceType tuple or returns nil if not found func (r *ResourcesRepository) GetResourceType(ctx context.Context, id uuid.UUID) (*models.ResourceType, error) { - return utils.Find[models.ResourceType](ctx, r.Db, id, nil) + return utils.Find[models.ResourceType](ctx, r.Db, id) } // GetResourcePools retrieves all ResourcePool tuples or returns an empty array if no tuples are found func (r *ResourcesRepository) GetResourcePools(ctx context.Context) ([]models.ResourcePool, error) { - return utils.FindAll[models.ResourcePool](ctx, r.Db, nil) + return utils.FindAll[models.ResourcePool](ctx, r.Db) } // GetResourcePool retrieves a specific ResourcePool tuple or returns nil if not found func (r *ResourcesRepository) GetResourcePool(ctx context.Context, id uuid.UUID) (*models.ResourcePool, error) { - return utils.Find[models.ResourcePool](ctx, r.Db, id, nil) + return utils.Find[models.ResourcePool](ctx, r.Db, id) } // ResourcePoolExists determines whether a ResourcePool exists or not @@ -75,10 +75,10 @@ func (r *ResourcesRepository) ResourcePoolExists(ctx context.Context, id uuid.UU // GetResourcePoolResources retrieves all Resource tuples for a specific ResourcePool returns an empty array if not found func (r *ResourcesRepository) GetResourcePoolResources(ctx context.Context, id uuid.UUID) ([]models.Resource, error) { e := psql.Quote("resource_pool_id").EQ(psql.Arg(id)) - return utils.Search[models.Resource](ctx, r.Db, e, nil) + return utils.Search[models.Resource](ctx, r.Db, e) } // GetResource retrieves a specific ResourceType tuple or returns nil if not found func (r *ResourcesRepository) GetResource(ctx context.Context, id uuid.UUID) (*models.Resource, error) { - return utils.Find[models.Resource](ctx, r.Db, id, nil) + return utils.Find[models.Resource](ctx, r.Db, id) }