Files
zitadel/backend/v3/storage/database/statement.go
T
Tim MöhlmannandGitHub a45908b364 feat(rt): project repository (#10789)
# Which Problems Are Solved

Add projects to the relational tables

# How the Problems Are Solved

- Define table migrations
- Define and implement Project and Project Role repositories.
- Provide projection handlers to populate the relational tables.

# Additional Changes

- Statement Builder now has a constructor which allows setting of a base
query with arguments.
- Certain operations, like Get, Update and Delete require the Primary
Key to be set as conditions. However, this requires knowledge of the
implementation and table definition. This PR proposes an additional
condition for repositories: `PrimaryKeyCondition`. This gives clarity on
the required IDs for these operations.
- Added couple of helpers to the repository package, to allow more DRY
code.
- getOne / getMany: generic functions for query execution and scanning.
- checkRestrictingColumns, checkPkCondition: simplify condition
checking, instead of using ladders of conditionals.
- Added a couple of helpers to the repository test package:
  - Transaction, savepoint and rollback helpers.
- Create instance and organization helpers for objects that depend on
them (like projects).

# Additional Context

- after https://github.com/zitadel/zitadel/pull/10809
- closes #10765
2025-10-01 09:47:04 +00:00

75 lines
1.7 KiB
Go

package database
import (
"strconv"
"strings"
)
type Instruction string
const (
DefaultInstruction Instruction = "DEFAULT"
NowInstruction Instruction = "NOW()"
NullInstruction Instruction = "NULL"
)
// StatementBuilder is a helper to build SQL statement.
type StatementBuilder struct {
strings.Builder
args []any
}
type argWriter interface {
WriteArg(builder *StatementBuilder)
}
func NewStatementBuilder(baseQuery string, baseArgs ...any) *StatementBuilder {
b := &StatementBuilder{}
b.WriteString(baseQuery)
b.AppendArgs(baseArgs...)
return b
}
// WriteArgs adds the argument to the statement and writes the placeholder to the query.
func (b *StatementBuilder) WriteArg(arg any) {
if writer, ok := arg.(argWriter); ok {
writer.WriteArg(b)
return
}
b.WriteString(b.AppendArg(arg))
}
// WriteArgs adds the arguments to the statement and writes the placeholders to the query.
// The placeholders are comma separated.
func (b *StatementBuilder) WriteArgs(args ...any) {
for i, arg := range args {
if i > 0 {
b.WriteString(", ")
}
b.WriteArg(arg)
}
}
// AppendArg adds the argument to the statement and returns the placeholder.
func (b *StatementBuilder) AppendArg(arg any) (placeholder string) {
if instruction, ok := arg.(Instruction); ok {
return string(instruction)
}
b.args = append(b.args, arg)
placeholder = "$" + strconv.Itoa(len(b.args))
return placeholder
}
// AppendArgs adds the arguments to the statement and doesn't return the placeholders.
func (b *StatementBuilder) AppendArgs(args ...any) {
for _, arg := range args {
b.AppendArg(arg)
}
}
// Args returns the arguments added to the statement.
func (b *StatementBuilder) Args() []any {
return b.args
}