zitadel/internal/query/milestone.go

154 lines
4.3 KiB
Go
Raw Normal View History

2023-06-28 08:19:34 +02:00
package query
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
sq "github.com/Masterminds/squirrel"
2023-06-30 13:43:26 +02:00
"github.com/zitadel/zitadel/internal/api/authz"
2023-06-28 08:19:34 +02:00
"github.com/zitadel/zitadel/internal/api/call"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/query/projection"
2023-06-30 13:43:26 +02:00
"github.com/zitadel/zitadel/internal/repository/milestone"
2023-06-28 08:19:34 +02:00
"github.com/zitadel/zitadel/internal/telemetry/tracing"
)
type Milestones struct {
SearchResponse
Milestones []*Milestone
}
type Milestone struct {
InstanceID string
2023-06-28 11:35:22 +02:00
Type milestone.Type
2023-06-28 08:19:34 +02:00
ReachedDate time.Time
PushedDate time.Time
PrimaryDomain string
}
type MilestonesSearchQueries struct {
SearchRequest
Queries []SearchQuery
}
func (q *MilestonesSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
query = q.SearchRequest.toQuery(query)
for _, q := range q.Queries {
query = q.toQuery(query)
}
return query
}
var (
milestonesTable = table{
name: projection.MilestonesProjectionTable,
instanceIDCol: projection.MilestoneColumnInstanceID,
}
MilestoneInstanceIDColID = Column{
name: projection.MilestoneColumnInstanceID,
table: milestonesTable,
}
MilestoneTypeColID = Column{
2023-06-28 11:35:22 +02:00
name: projection.MilestoneColumnType,
2023-06-28 08:19:34 +02:00
table: milestonesTable,
}
MilestonePrimaryDomainColID = Column{
name: projection.MilestoneColumnPrimaryDomain,
table: milestonesTable,
}
MilestoneReachedDateColID = Column{
name: projection.MilestoneColumnReachedDate,
table: milestonesTable,
}
MilestonePushedDateColID = Column{
name: projection.MilestoneColumnPushedDate,
table: milestonesTable,
}
)
// SearchMilestones tries to defer the instanceID from the passed context if no instanceIDs are passed
func (q *Queries) SearchMilestones(ctx context.Context, instanceIDs []string, queries *MilestonesSearchQueries) (_ *Milestones, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
query, scan := prepareMilestonesQuery(ctx, q.client)
if len(instanceIDs) == 0 {
instanceIDs = []string{authz.GetInstance(ctx).InstanceID()}
}
2023-06-28 11:35:22 +02:00
instanceIDParams := make([]string, len(instanceIDs))
instanceIDArgs := make([]interface{}, len(instanceIDs))
for idx := range instanceIDs {
instanceIDParams[idx] = fmt.Sprintf("$%d", idx+1)
instanceIDArgs[idx] = instanceIDs[idx]
}
expr := fmt.Sprintf("%s IN (%s)", MilestoneInstanceIDColID.name, strings.Join(instanceIDParams, ","))
stmt, args, err := queries.toQuery(query).Where(sq.Expr(expr, instanceIDArgs...)).ToSql()
2023-06-28 08:19:34 +02:00
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-A9i5k", "Errors.Query.SQLStatement")
}
rows, err := q.client.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, err
}
milestones, err := scan(rows)
if err != nil {
return nil, err
}
milestones.LatestSequence, err = q.latestSequence(ctx, milestonesTable)
return milestones, err
}
func prepareMilestonesQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Milestones, error)) {
return sq.Select(
2023-06-28 11:35:22 +02:00
MilestoneInstanceIDColID.identifier(),
2023-06-28 08:19:34 +02:00
MilestonePrimaryDomainColID.identifier(),
MilestoneReachedDateColID.identifier(),
MilestonePushedDateColID.identifier(),
MilestoneTypeColID.identifier(),
countColumn.identifier(),
).
2023-06-28 11:35:22 +02:00
From(milestonesTable.identifier() + db.Timetravel(call.Took(ctx))).
2023-06-28 08:19:34 +02:00
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Milestones, error) {
milestones := make([]*Milestone, 0)
var count uint64
for rows.Next() {
m := new(Milestone)
2023-06-28 11:35:22 +02:00
reachedDate := sql.NullTime{}
pushedDate := sql.NullTime{}
primaryDomain := sql.NullString{}
2023-06-28 08:19:34 +02:00
err := rows.Scan(
2023-06-28 11:35:22 +02:00
&m.InstanceID,
&primaryDomain,
&reachedDate,
&pushedDate,
&m.Type,
2023-06-28 08:19:34 +02:00
&count,
)
if err != nil {
return nil, err
}
2023-06-28 11:35:22 +02:00
m.PrimaryDomain = primaryDomain.String
m.ReachedDate = reachedDate.Time
2023-06-30 17:20:08 +02:00
m.PushedDate = pushedDate.Time
2023-06-28 08:19:34 +02:00
milestones = append(milestones, m)
}
if err := rows.Close(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-CK9mI", "Errors.Query.CloseRows")
}
2023-06-30 16:28:03 +02:00
if err := rows.Err(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-asLsI", "Errors.Internal")
}
2023-06-28 08:19:34 +02:00
return &Milestones{
Milestones: milestones,
SearchResponse: SearchResponse{
Count: count,
},
}, nil
}
}