2022-04-05 07:58:09 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 22:36:43 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2022-12-01 09:18:53 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-04-05 07:58:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceDomain struct {
|
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
Domain string
|
|
|
|
InstanceID string
|
|
|
|
IsGenerated bool
|
2022-04-14 14:19:18 +02:00
|
|
|
IsPrimary bool
|
2022-04-05 07:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceDomains struct {
|
|
|
|
SearchResponse
|
|
|
|
Domains []*InstanceDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceDomainSearchQueries struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *InstanceDomainSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
|
|
|
query = q.SearchRequest.toQuery(query)
|
|
|
|
for _, q := range q.Queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstanceDomainDomainSearchQuery(method TextComparison, value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(InstanceDomainDomainCol, value, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstanceDomainInstanceIDSearchQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(InstanceDomainInstanceIDCol, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:19:18 +02:00
|
|
|
func NewInstanceDomainGeneratedSearchQuery(generated bool) (SearchQuery, error) {
|
|
|
|
return NewBoolQuery(InstanceDomainIsGeneratedCol, generated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstanceDomainPrimarySearchQuery(primary bool) (SearchQuery, error) {
|
|
|
|
return NewBoolQuery(InstanceDomainIsPrimaryCol, primary)
|
2022-04-05 07:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) SearchInstanceDomains(ctx context.Context, queries *InstanceDomainSearchQueries) (domains *InstanceDomains, err error) {
|
2022-12-01 09:18:53 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 22:36:43 +01:00
|
|
|
query, scan := prepareInstanceDomainsQuery(ctx, q.client)
|
2022-04-05 07:58:09 +02:00
|
|
|
stmt, args, err := queries.toQuery(query).
|
|
|
|
Where(sq.Eq{
|
|
|
|
InstanceDomainInstanceIDCol.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-inlsF", "Errors.Query.SQLStatement")
|
2022-04-05 07:58:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 15:45:19 +02:00
|
|
|
return q.queryInstanceDomains(ctx, stmt, scan, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) SearchInstanceDomainsGlobal(ctx context.Context, queries *InstanceDomainSearchQueries) (domains *InstanceDomains, err error) {
|
2022-12-01 09:18:53 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 22:36:43 +01:00
|
|
|
query, scan := prepareInstanceDomainsQuery(ctx, q.client)
|
2022-06-14 15:45:19 +02:00
|
|
|
stmt, args, err := queries.toQuery(query).ToSql()
|
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-IHhLR", "Errors.Query.SQLStatement")
|
2022-06-14 15:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return q.queryInstanceDomains(ctx, stmt, scan, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) queryInstanceDomains(ctx context.Context, stmt string, scan func(*sql.Rows) (*InstanceDomains, error), args ...interface{}) (domains *InstanceDomains, err error) {
|
2023-08-22 14:49:02 +02:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
domains, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
2022-04-05 07:58:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
domains.State, err = q.latestState(ctx, instanceDomainsTable)
|
2022-04-05 07:58:09 +02:00
|
|
|
return domains, err
|
|
|
|
}
|
|
|
|
|
2023-02-27 22:36:43 +01:00
|
|
|
func prepareInstanceDomainsQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*InstanceDomains, error)) {
|
2022-04-05 07:58:09 +02:00
|
|
|
return sq.Select(
|
|
|
|
InstanceDomainCreationDateCol.identifier(),
|
|
|
|
InstanceDomainChangeDateCol.identifier(),
|
|
|
|
InstanceDomainSequenceCol.identifier(),
|
|
|
|
InstanceDomainDomainCol.identifier(),
|
|
|
|
InstanceDomainInstanceIDCol.identifier(),
|
|
|
|
InstanceDomainIsGeneratedCol.identifier(),
|
2022-04-14 14:19:18 +02:00
|
|
|
InstanceDomainIsPrimaryCol.identifier(),
|
2022-04-05 07:58:09 +02:00
|
|
|
countColumn.identifier(),
|
2023-02-27 22:36:43 +01:00
|
|
|
).From(instanceDomainsTable.identifier() + db.Timetravel(call.Took(ctx))).
|
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-04-05 07:58:09 +02:00
|
|
|
func(rows *sql.Rows) (*InstanceDomains, error) {
|
|
|
|
domains := make([]*InstanceDomain, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
domain := new(InstanceDomain)
|
|
|
|
err := rows.Scan(
|
|
|
|
&domain.CreationDate,
|
|
|
|
&domain.ChangeDate,
|
|
|
|
&domain.Sequence,
|
|
|
|
&domain.Domain,
|
|
|
|
&domain.InstanceID,
|
|
|
|
&domain.IsGenerated,
|
2022-04-14 14:19:18 +02:00
|
|
|
&domain.IsPrimary,
|
2022-04-05 07:58:09 +02:00
|
|
|
&count,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-8nlWW", "Errors.Query.CloseRows")
|
2022-04-05 07:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &InstanceDomains{
|
|
|
|
Domains: domains,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
instanceDomainsTable = table{
|
2022-10-27 08:08:36 +02:00
|
|
|
name: projection.InstanceDomainTable,
|
|
|
|
instanceIDCol: projection.InstanceDomainInstanceIDCol,
|
2022-04-05 07:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InstanceDomainCreationDateCol = Column{
|
|
|
|
name: projection.InstanceDomainCreationDateCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
|
|
|
InstanceDomainChangeDateCol = Column{
|
|
|
|
name: projection.InstanceDomainChangeDateCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
|
|
|
InstanceDomainSequenceCol = Column{
|
|
|
|
name: projection.InstanceDomainSequenceCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
|
|
|
InstanceDomainDomainCol = Column{
|
|
|
|
name: projection.InstanceDomainDomainCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
|
|
|
InstanceDomainInstanceIDCol = Column{
|
|
|
|
name: projection.InstanceDomainInstanceIDCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
|
|
|
InstanceDomainIsGeneratedCol = Column{
|
|
|
|
name: projection.InstanceDomainIsGeneratedCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
2022-04-14 14:19:18 +02:00
|
|
|
InstanceDomainIsPrimaryCol = Column{
|
|
|
|
name: projection.InstanceDomainIsPrimaryCol,
|
|
|
|
table: instanceDomainsTable,
|
|
|
|
}
|
2022-04-05 07:58:09 +02:00
|
|
|
)
|