implementation done

This commit is contained in:
adlerhurst
2025-07-29 17:59:02 +02:00
parent 432295ba76
commit ef74f5ee40
13 changed files with 820 additions and 169 deletions

View File

@@ -10,9 +10,10 @@ func WithCondition(condition Condition) QueryOption {
}
// WithOrderBy sets the columns to order the results by.
func WithOrderBy(orderBy ...Column) QueryOption {
func WithOrderBy(descending bool, orderBy ...Column) QueryOption {
return func(opts *QueryOpts) {
opts.OrderBy = orderBy
opts.OrderByDescending = descending
}
}
@@ -69,6 +70,9 @@ type QueryOpts struct {
// OrderBy is the columns to order the results by.
// It is used to build the ORDER BY clause of the SQL statement.
OrderBy Columns
// OrderByDescending indicates if the results should be ordered in descending order.
// default is ascending order.
OrderByDescending bool
// Limit is the maximum number of results to return.
// It is used to build the LIMIT clause of the SQL statement.
Limit uint32
@@ -105,7 +109,15 @@ func (opts *QueryOpts) WriteOrderBy(builder *StatementBuilder) {
return
}
builder.WriteString(" ORDER BY ")
opts.OrderBy.Write(builder)
for i, col := range opts.OrderBy {
if i > 0 {
builder.WriteString(", ")
}
col.Write(builder)
if opts.OrderByDescending {
builder.WriteString(" DESC")
}
}
}
func (opts *QueryOpts) WriteLimit(builder *StatementBuilder) {

View File

@@ -538,7 +538,7 @@ func TestListInstance(t *testing.T) {
// check instance values
returnedInstances, err := instanceRepo.List(ctx,
database.WithCondition(condition),
database.WithOrderBy(instanceRepo.CreatedAtColumn(true)),
database.WithOrderBy(false, instanceRepo.CreatedAtColumn(true)),
)
require.NoError(t, err)
if tt.noInstanceReturned {

View File

@@ -785,7 +785,7 @@ func TestListOrganization(t *testing.T) {
// check organization values
returnedOrgs, err := organizationRepo.List(ctx,
database.WithCondition(condition),
database.WithOrderBy(organizationRepo.CreatedAtColumn(true)),
database.WithOrderBy(false, organizationRepo.CreatedAtColumn(true)),
)
require.NoError(t, err)
if tt.noOrganizationReturned {