Skip to content

Commit

Permalink
CNF-15570: Change variadic arguments on db utils
Browse files Browse the repository at this point in the history
Use a variadic argument for field names rather than an array to avoid
needing to pass in `nil` when not required.

Signed-off-by: Allain Legacy <[email protected]>
  • Loading branch information
alegacy committed Dec 6, 2024
1 parent 873408c commit 8a0272b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions internal/service/common/utils/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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()),
}

Expand Down Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions internal/service/resources/db/repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 8a0272b

Please sign in to comment.