zitadel/internal/query/org_domain.go
Silvan 6d94975a85
fix(queries): actions prepare funcs (#2689)
* chore(queries): test suite for prepare stmt funcs

* test(queries): prepare project funcs

* refactor: add comments

* test: simlify expected sql, added possibility to add args to expected queries

* test(queries): prepare funcs in org

* chore(backend): correct modules

* test(queries): org domain prepare funcs

* test: correct name

* refactor: file name

* refactor: add table to login policy columns

* chore(prepare_test): only add row to result if columns

* test(queries): login policy prepare funcs

* chore: add comments for configs

* test(queries): prepare idp funcs

* fix(queries): add table to password complexity policy cols

* test(queries): password complexity policy prepare funcs

* fix(queries): add table to password age policy cols

* test(queries): password age policy prepare func

* fix(queries): set cols on lockout policy

* test(queries): lockout policy prepare funs

* fix(queries): set table on privacy policy cols

* test(queries): privacy policy prepare funcs

* fix(queries): set table on org iam policy cols

* fix(queries): correct table in org iam policy cols

* test(queries): org iam policy prepare funcs

* test(queries): prepare project grant funcs

* refactor(queries): prepareProjectRoleQuery as func

* test(queries): prepare project role funcs

* test(queries): project grant check for nulls in joins

* fix(queries): allow null values in project grant

* refactor(queries): make toQuery private

* test(queries): action prepare funcs

* refactor: rename prepareFlowQuery to prepareFlowsQuery

* test: generic count only if count in cols

* refactor: remove param in prepareFlowQuery

* fix(queries): correct left joins in action flows

* test(queries): action flow prepare funcs
2021-11-16 14:04:22 +01:00

155 lines
3.8 KiB
Go

package query
import (
"context"
"database/sql"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query/projection"
)
type Domain struct {
CreationDate time.Time
ChangeDate time.Time
Sequence uint64
Domain string
OrgID string
IsVerified bool
IsPrimary bool
ValidationType domain.OrgDomainValidationType
}
type Domains struct {
SearchResponse
Domains []*Domain
}
type OrgDomainSearchQueries struct {
SearchRequest
Queries []SearchQuery
}
func (q *OrgDomainSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
query = q.SearchRequest.toQuery(query)
for _, q := range q.Queries {
query = q.toQuery(query)
}
return query
}
func NewOrgDomainDomainSearchQuery(method TextComparison, value string) (SearchQuery, error) {
return NewTextQuery(OrgDomainDomainCol, value, method)
}
func NewOrgDomainOrgIDSearchQuery(value string) (SearchQuery, error) {
return NewTextQuery(OrgDomainOrgIDCol, value, TextEquals)
}
func (q *Queries) SearchOrgDomains(ctx context.Context, queries *OrgDomainSearchQueries) (domains *Domains, err error) {
query, scan := prepareDomainsQuery()
stmt, args, err := queries.toQuery(query).ToSql()
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "QUERY-ZRfj1", "Errors.Query.SQLStatement")
}
rows, err := q.client.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-M6mYN", "Errors.Internal")
}
domains, err = scan(rows)
if err != nil {
return nil, err
}
domains.LatestSequence, err = q.latestSequence(ctx, orgDomainsTable)
return domains, err
}
func prepareDomainsQuery() (sq.SelectBuilder, func(*sql.Rows) (*Domains, error)) {
return sq.Select(
OrgDomainCreationDateCol.identifier(),
OrgDomainChangeDateCol.identifier(),
OrgDomainSequenceCol.identifier(),
OrgDomainDomainCol.identifier(),
OrgDomainOrgIDCol.identifier(),
OrgDomainIsVerifiedCol.identifier(),
OrgDomainIsPrimaryCol.identifier(),
OrgDomainValidationTypeCol.identifier(),
countColumn.identifier(),
).From(orgDomainsTable.identifier()).PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Domains, error) {
domains := make([]*Domain, 0)
var count uint64
for rows.Next() {
domain := new(Domain)
err := rows.Scan(
&domain.CreationDate,
&domain.ChangeDate,
&domain.Sequence,
&domain.Domain,
&domain.OrgID,
&domain.IsVerified,
&domain.IsPrimary,
&domain.ValidationType,
&count,
)
if err != nil {
return nil, err
}
domains = append(domains, domain)
}
if err := rows.Close(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-rKd6k", "Errors.Query.CloseRows")
}
return &Domains{
Domains: domains,
SearchResponse: SearchResponse{
Count: count,
},
}, nil
}
}
var (
orgDomainsTable = table{
name: projection.OrgDomainTable,
}
OrgDomainCreationDateCol = Column{
name: projection.OrgDomainCreationDateCol,
table: orgDomainsTable,
}
OrgDomainChangeDateCol = Column{
name: projection.OrgDomainChangeDateCol,
table: orgDomainsTable,
}
OrgDomainSequenceCol = Column{
name: projection.OrgDomainSequenceCol,
table: orgDomainsTable,
}
OrgDomainDomainCol = Column{
name: projection.OrgDomainDomainCol,
table: orgDomainsTable,
}
OrgDomainOrgIDCol = Column{
name: projection.OrgDomainOrgIDCol,
table: orgDomainsTable,
}
OrgDomainIsVerifiedCol = Column{
name: projection.OrgDomainIsVerifiedCol,
table: orgDomainsTable,
}
OrgDomainIsPrimaryCol = Column{
name: projection.OrgDomainIsPrimaryCol,
table: orgDomainsTable,
}
OrgDomainValidationTypeCol = Column{
name: projection.OrgDomainValidationTypeCol,
table: orgDomainsTable,
}
)