Files
zitadel/backend/v3/storage/database/query.go

88 lines
2.2 KiB
Go
Raw Normal View History

2025-05-06 07:18:11 +02:00
package database
type QueryOption func(opts *QueryOpts)
2025-07-17 15:32:50 +02:00
// WithCondition sets the condition for the query.
2025-05-06 07:18:11 +02:00
func WithCondition(condition Condition) QueryOption {
return func(opts *QueryOpts) {
opts.Condition = condition
}
}
2025-07-17 15:32:50 +02:00
// WithOrderBy sets the columns to order the results by.
2025-05-06 07:18:11 +02:00
func WithOrderBy(orderBy ...Column) QueryOption {
return func(opts *QueryOpts) {
opts.OrderBy = orderBy
}
}
2025-07-17 15:32:50 +02:00
// WithLimit sets the maximum number of results to return.
2025-05-06 07:18:11 +02:00
func WithLimit(limit uint32) QueryOption {
return func(opts *QueryOpts) {
opts.Limit = limit
}
}
2025-07-17 15:32:50 +02:00
// WithOffset sets the number of results to skip before returning the results.
2025-05-06 07:18:11 +02:00
func WithOffset(offset uint32) QueryOption {
return func(opts *QueryOpts) {
opts.Offset = offset
}
}
2025-07-17 15:32:50 +02:00
// QueryOpts holds the options for a query.
// It is used to build the SQL SELECT statement.
2025-05-06 07:18:11 +02:00
type QueryOpts struct {
2025-07-17 15:32:50 +02:00
// Condition is the condition to filter the results.
// It is used to build the WHERE clause of the SQL statement.
2025-05-06 07:18:11 +02:00
Condition Condition
2025-07-17 15:32:50 +02:00
// OrderBy is the columns to order the results by.
// It is used to build the ORDER BY clause of the SQL statement.
2025-05-06 07:18:11 +02:00
OrderBy Columns
2025-07-17 15:32:50 +02:00
// Limit is the maximum number of results to return.
// It is used to build the LIMIT clause of the SQL statement.
2025-05-06 07:18:11 +02:00
Limit uint32
2025-07-17 15:32:50 +02:00
// Offset is the number of results to skip before returning the results.
// It is used to build the OFFSET clause of the SQL statement.
2025-05-06 07:18:11 +02:00
Offset uint32
}
2025-07-17 15:32:50 +02:00
func (opts *QueryOpts) Write(builder *StatementBuilder) {
opts.WriteCondition(builder)
opts.WriteOrderBy(builder)
opts.WriteLimit(builder)
opts.WriteOffset(builder)
}
2025-05-06 07:18:11 +02:00
func (opts *QueryOpts) WriteCondition(builder *StatementBuilder) {
if opts.Condition == nil {
return
}
builder.WriteString(" WHERE ")
opts.Condition.Write(builder)
}
func (opts *QueryOpts) WriteOrderBy(builder *StatementBuilder) {
if len(opts.OrderBy) == 0 {
return
}
builder.WriteString(" ORDER BY ")
opts.OrderBy.Write(builder)
2025-05-06 07:18:11 +02:00
}
func (opts *QueryOpts) WriteLimit(builder *StatementBuilder) {
if opts.Limit == 0 {
return
}
builder.WriteString(" LIMIT ")
builder.WriteArg(opts.Limit)
}
func (opts *QueryOpts) WriteOffset(builder *StatementBuilder) {
if opts.Offset == 0 {
return
}
builder.WriteString(" OFFSET ")
builder.WriteArg(opts.Offset)
}